# Path: cle/errors.py
# class CLEError(Exception):
#     """
#     Base class for errors raised by CLE.
#     """
#     pass
# 
# class CLEFileNotFoundError(CLEError):
#     """
#     Error raised when a file does not exist.
#     """
#     pass


import os
import contextlib
from .errors import CLEError, CLEFileNotFoundError
}
int main(int argc, char* argv[])
{
    make_test_file();

    int fd = open("./test.data", O_RDONLY);
    unsigned char* mapping = mmap(NULL, 0x123, PROT_READ, MAP_PRIVATE, fd, 4096);

    for (int i=0; i < 0x1000; i++)
    {
        printf("%02x ", mapping[i]);
        if (i % sizeof(void*) == (sizeof(void*) - 1))
            printf("| ");
        if (i % 16 == 15)
            printf("\n");
    }
}"""
def get_mmaped_data(stream, offset, length, page_size):
    if offset % page_size != 0:
        raise CLEError("libc helper for mmap: Invalid page offset, should be multiple of page size! Stream {}, offset {}, length: {}".format(stream, offset, length))

    read_length = ALIGN_UP(length, page_size)
    stream.seek(offset)
    data = stream.read(read_length)
    return data.ljust(read_length, b'\0')

@contextlib.contextmanager
def stream_or_path(obj, perms='rb'):
    if hasattr(obj, 'read') and hasattr(obj, 'seek'):
        obj.seek(0)

# Path: cle/errors.py
# class CLEError(Exception):
#     """
#     Base class for errors raised by CLE.
#     """
#     pass
# 
# class CLEFileNotFoundError(CLEError):
#     """
#     Error raised when a file does not exist.
#     """
#     pass


# Path: cle/utils.py
import os
import contextlib
from .errors import CLEError, CLEFileNotFoundError

}
int main(int argc, char* argv[])
{
    make_test_file();

    int fd = open("./test.data", O_RDONLY);
    unsigned char* mapping = mmap(NULL, 0x123, PROT_READ, MAP_PRIVATE, fd, 4096);

    for (int i=0; i < 0x1000; i++)
    {
        printf("%02x ", mapping[i]);
        if (i % sizeof(void*) == (sizeof(void*) - 1))
            printf("| ");
        if (i % 16 == 15)
            printf("\n");
    }
}"""
def get_mmaped_data(stream, offset, length, page_size):
    if offset % page_size != 0:
        raise CLEError("libc helper for mmap: Invalid page offset, should be multiple of page size! Stream {}, offset {}, length: {}".format(stream, offset, length))

    read_length = ALIGN_UP(length, page_size)
    stream.seek(offset)
    data = stream.read(read_length)
    return data.ljust(read_length, b'\0')

@contextlib.contextmanager
def stream_or_path(obj, perms='rb'):
    if hasattr(obj, 'read') and hasattr(obj, 'seek'):
        obj.seek(0)

        yield obj