<commit_before>#include <memory.h>
#include <string.h>
#include <tar.h>
#include <vfs.h>
#include <ramdisk.h>

static struct ramdisk_image ramdiskImage;
static struct ramdisk_filesystem ramdiskFilesystem;

static unsigned int validate(struct tar_header *header)
{

    unsigned int i;
    unsigned char *address = (unsigned char *)header;
    unsigned int checksum = string_read_num(header->checksum, 8);

    for (i = 0; i < 148; i++)
        checksum -= address[i];

    for (i = 148; i < 156; i++)
        checksum -= 32;

    for (i = 156; i < TAR_BLOCK_SIZE; i++)
        checksum -= address[i];

    return !checksum;

}

static unsigned int parse(struct ramdisk_image *self, void *address)
{

    char *current;

    for (current = address; *current; current += TAR_BLOCK_SIZE)
    {

        struct tar_header *header = (struct tar_header *)current;
        unsigned int size = string_read_num(header->size, 8);

        if (!validate(header))
            return 0;

        self->headers[self->count] = header;
        self->count++;

        current += ((size / TAR_BLOCK_SIZE) + ((size % TAR_BLOCK_SIZE) ? 1 : 0)) * TAR_BLOCK_SIZE;

    }

    return self->count;

}

static unsigned int read_file(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int size = string_read_num(header->size, 8);
    unsigned int c = (size > offset) ? size - offset : 0;
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    if (c > count)
        c = count;

    memory_copy(buffer, (void *)(data + offset), c);

    return c;

}

static unsigned int read(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);

    if (header->name[length - 1] == '/')
    {

        unsigned int i;
        unsigned int c = 0;
        char *out = buffer;

        if (offset > 0)
            return 0;

        memory_copy(out + c, "../\n", 4);
        c += 4;

        for (i = 0; i < filesystem->image->count; i++)
        {

            struct tar_header *current = filesystem->image->headers[i];
            char *start;
            char *slash;
            unsigned int size;

            if (current == header)
                continue;

            if (!memory_find(current->name, header->name, string_length(current->name), string_length(header->name)))
                continue;

            start = current->name + string_length(header->name);
            size = string_length(start);

            if (!size)
                continue;

            slash = memory_find(start, "/", size, 1);

            if (slash && slash < start + size - 1)
                continue;

            memory_copy(out + c, start, size);
            memory_copy(out + c + size, "\n", 1);
            c += size + 1;

        }

        return c;

    }

    return read_file(self, id, offset, count, buffer);

}

static unsigned int write_file(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int size = string_read_num(header->size, 8);
    unsigned int c = (size > offset) ? size - offset : 0;
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    if (c > count)
        c = count;

    memory_copy((void *)(data + offset), buffer, c);

    return c;

}

static unsigned int write(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);

    if (header->name[length - 1] == '/')
        return 0;

    return write_file(self, id, offset, count, buffer);

}

static unsigned int parent(struct vfs_interface *self, unsigned int id)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);
    unsigned int i;

    while (--length)
    {

        if (header->name[length - 1] == '/')
            break;

    }

    for (i = 0; i < id - 1; i++)
    {

        if (!memory_match(filesystem->image->headers[i]->name, header->name, length))
            continue;

        return i + 1;

    }

    return 1;

}

static unsigned int walk(struct vfs_interface *self, unsigned int id, unsigned int count, char *path)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int offset = string_length(header->name);
    unsigned int i;

    if (!count)
        return id;

    if (memory_match(path, "../", 3))
        return walk(self, parent(self, id), count - 3, path + 3);

    for (i = id; i < filesystem->image->count; i++)
    {

        unsigned int length = string_length(filesystem->image->headers[i]->name);

        if (length < offset)
            break;

        length -= offset;

        if (!memory_match(filesystem->image->headers[i]->name + offset, path, length))
            continue;

        return walk(self, i + 1, count - length, path + length);

    }

    return 0;

}

static unsigned int get_physical(struct vfs_interface *self, unsigned int id)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    return data;

}

void ramdisk_init_image(struct ramdisk_image *image)
{

    memory_clear(image, sizeof (struct ramdisk_image));

    image->parse = parse;

}

void ramdisk_init_filesystem(struct ramdisk_filesystem *filesystem, struct ramdisk_image *image)
{

    memory_clear(filesystem, sizeof (struct ramdisk_filesystem));

    vfs_init_interface(&filesystem->interface, 1, "ramdisk", 0, 0, read, write, parent, walk, get_physical);

    filesystem->image = image;

}

struct vfs_interface *ramdisk_setup(int ramdiskc, void **ramdiskv)
{

    unsigned int i;

    ramdisk_init_image(&ramdiskImage);

    for (i = 0; i < ramdiskc; i++)
        ramdiskImage.parse(&ramdiskImage, *(ramdiskv + i));

    ramdisk_init_filesystem(&ramdiskFilesystem, &ramdiskImage);

    return &ramdiskFilesystem.interface;

}

<commit_msg>more cleanups<commit_after>#include <memory.h>
#include <string.h>
#include <tar.h>
#include <vfs.h>
#include <ramdisk.h>

static struct ramdisk_image ramdiskImage;
static struct ramdisk_filesystem ramdiskFilesystem;

static unsigned int validate(struct tar_header *header)
{

    unsigned int i;
    unsigned char *address = (unsigned char *)header;
    unsigned int checksum = string_read_num(header->checksum, 8);

    for (i = 0; i < 148; i++)
        checksum -= address[i];

    for (i = 148; i < 156; i++)
        checksum -= 32;

    for (i = 156; i < TAR_BLOCK_SIZE; i++)
        checksum -= address[i];

    return !checksum;

}

static unsigned int parse(struct ramdisk_image *self, void *address)
{

    char *current;

    for (current = address; *current; current += TAR_BLOCK_SIZE)
    {

        struct tar_header *header = (struct tar_header *)current;
        unsigned int size = string_read_num(header->size, 8);

        if (!validate(header))
            return 0;

        self->headers[self->count] = header;
        self->count++;

        current += ((size / TAR_BLOCK_SIZE) + ((size % TAR_BLOCK_SIZE) ? 1 : 0)) * TAR_BLOCK_SIZE;

    }

    return self->count;

}

static unsigned int read_file(struct tar_header *header, unsigned int offset, unsigned int count, void *buffer)
{

    unsigned int size = string_read_num(header->size, 8);
    unsigned int c = (size > offset) ? size - offset : 0;
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    if (c > count)
        c = count;

    memory_copy(buffer, (void *)(data + offset), c);

    return c;

}

static unsigned int read_directory(struct ramdisk_filesystem *filesystem, struct tar_header *header, unsigned int offset, unsigned int count, void *buffer)
{

    unsigned int i;
    unsigned int c = 0;
    char *out = buffer;

    if (offset > 0)
        return 0;

    memory_copy(out + c, "../\n", 4);
    c += 4;

    for (i = 0; i < filesystem->image->count; i++)
    {

        struct tar_header *current = filesystem->image->headers[i];
        char *start;
        char *slash;
        unsigned int size;

        if (current == header)
            continue;

        if (!memory_find(current->name, header->name, string_length(current->name), string_length(header->name)))
            continue;

        start = current->name + string_length(header->name);
        size = string_length(start);

        if (!size)
            continue;

        slash = memory_find(start, "/", size, 1);

        if (slash && slash < start + size - 1)
            continue;

        memory_copy(out + c, start, size);
        memory_copy(out + c + size, "\n", 1);
        c += size + 1;

    }

    return c;

}

static unsigned int read(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);

    if (header->name[length - 1] == '/')
        return read_directory(filesystem, header, offset, count, buffer);

    return read_file(header, offset, count, buffer);

}

static unsigned int write_file(struct tar_header *header, unsigned int offset, unsigned int count, void *buffer)
{

    unsigned int size = string_read_num(header->size, 8);
    unsigned int c = (size > offset) ? size - offset : 0;
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    if (c > count)
        c = count;

    memory_copy((void *)(data + offset), buffer, c);

    return c;

}

static unsigned int write(struct vfs_interface *self, unsigned int id, unsigned int offset, unsigned int count, void *buffer)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);

    if (header->name[length - 1] == '/')
        return 0;

    return write_file(header, offset, count, buffer);

}

static unsigned int parent(struct vfs_interface *self, unsigned int id)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int length = string_length(header->name);
    unsigned int i;

    while (--length)
    {

        if (header->name[length - 1] == '/')
            break;

    }

    for (i = 0; i < id - 1; i++)
    {

        if (!memory_match(filesystem->image->headers[i]->name, header->name, length))
            continue;

        return i + 1;

    }

    return 1;

}

static unsigned int walk(struct vfs_interface *self, unsigned int id, unsigned int count, char *path)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int offset = string_length(header->name);
    unsigned int i;

    if (!count)
        return id;

    if (memory_match(path, "../", 3))
        return walk(self, parent(self, id), count - 3, path + 3);

    for (i = id; i < filesystem->image->count; i++)
    {

        unsigned int length = string_length(filesystem->image->headers[i]->name);

        if (length < offset)
            break;

        length -= offset;

        if (!memory_match(filesystem->image->headers[i]->name + offset, path, length))
            continue;

        return walk(self, i + 1, count - length, path + length);

    }

    return 0;

}

static unsigned int get_physical(struct vfs_interface *self, unsigned int id)
{

    struct ramdisk_filesystem *filesystem = (struct ramdisk_filesystem *)self;
    struct tar_header *header = filesystem->image->headers[id - 1];
    unsigned int data = (unsigned int)header + TAR_BLOCK_SIZE;

    return data;

}

void ramdisk_init_image(struct ramdisk_image *image)
{

    memory_clear(image, sizeof (struct ramdisk_image));

    image->parse = parse;

}

void ramdisk_init_filesystem(struct ramdisk_filesystem *filesystem, struct ramdisk_image *image)
{

    memory_clear(filesystem, sizeof (struct ramdisk_filesystem));

    vfs_init_interface(&filesystem->interface, 1, "ramdisk", 0, 0, read, write, parent, walk, get_physical);

    filesystem->image = image;

}

struct vfs_interface *ramdisk_setup(int ramdiskc, void **ramdiskv)
{

    unsigned int i;

    ramdisk_init_image(&ramdiskImage);

    for (i = 0; i < ramdiskc; i++)
        ramdiskImage.parse(&ramdiskImage, *(ramdiskv + i));

    ramdisk_init_filesystem(&ramdiskFilesystem, &ramdiskImage);

    return &ramdiskFilesystem.interface;

}


{'fraction_non_alphanumeric': 0.12196023357232612, 'fraction_numerical': 0.008426343410451622, 'mean_word_length': 2.8547008547008548, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274488', 'n_tokens_mistral': 4335, 'n_tokens_neox': 4042, 'n_words': 1202}