# 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

void make_test_file()
{
    void* data = (void*)0xdead0000;
    int fd = open("./test.data", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    for (int i = 0; i < 0x1800; i += sizeof(void*)) // Only write 1 1/2 pages worth
    {
        write(fd, &data, sizeof(void*));
        data += sizeof(void*);
    }
    close(fd);
}
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:

# 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


void make_test_file()
{
    void* data = (void*)0xdead0000;
    int fd = open("./test.data", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    for (int i = 0; i < 0x1800; i += sizeof(void*)) // Only write 1 1/2 pages worth
    {
        write(fd, &data, sizeof(void*));
        data += sizeof(void*);
    }
    close(fd);
}
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))