<commit_before>/*
 * Copyright (c) 2008 <NAME>, <NAME>, <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <Module.h>
#include <Log.h>
#include <processor/Processor.h>
#include "sqlite3.h"
#include <utilities/utility.h>
#include <BootstrapInfo.h>
#include <panic.h>

#include <processor/PhysicalMemoryManager.h>
#include <processor/VirtualAddressSpace.h>
#include <processor/MemoryRegion.h>

#include <linker/KernelElf.h>

#include "Config.h"

extern BootstrapStruct_t *g_pBootstrapInfo;

uint8_t *g_pFile = 0;
size_t g_FileSz = 0;

extern "C" void log_(unsigned long a)
{
    NOTICE("Int: " << a);
}

extern "C" int atoi(const char *str)
{
    return strtoul(str, 0, 10);
}

#ifndef STATIC_DRIVERS // Defined in the kernel.
extern "C" int memcmp(const void *s1, const void *s2, size_t n)
{
    const unsigned char *c1 = reinterpret_cast<const unsigned char*>(s1);
    const unsigned char *c2 = reinterpret_cast<const unsigned char*>(s2);
    for (size_t i = 0; i < n; i++)
        if (c1[i] != c2[i])
            return (c1[i]>c2[i]) ? 1 : -1;
    return 0;
}
#endif

int xClose(sqlite3_file *file)
{
    return 0;
}

int xRead(sqlite3_file *file, void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    int ret = 0;
    if (iOfst+static_cast<unsigned int>(iAmt) >= g_FileSz)
    {
        memset(ptr, 0, iAmt);
        iAmt = g_FileSz - iOfst;
        ret = SQLITE_IOERR_SHORT_READ;
    }
    memcpy(ptr, &g_pFile[iOfst], iAmt);
    return ret;
}

int xReadFail(sqlite3_file *file, void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    memset(ptr, 0, iAmt);
    return SQLITE_IOERR_SHORT_READ;
}

int xWrite(sqlite3_file *file, const void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    if (iOfst+static_cast<unsigned int>(iAmt) >= g_FileSz)
    {
        uint8_t *tmp = new uint8_t[g_FileSz*2];
        memset(tmp, 0, g_FileSz*2);
        memcpy(tmp, g_pFile, g_FileSz);
        delete [] g_pFile;
        g_pFile = tmp;
        g_FileSz *= 2;
    }

    memcpy(&g_pFile[iOfst], ptr, iAmt);
    return 0;
}

int xWriteFail(sqlite3_file *file, const void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    return 0;
}

int xTruncate(sqlite3_file *file, sqlite3_int64 size)
{
    return 0;
}

int xSync(sqlite3_file *file, int flags)
{
    return 0;
}

int xFileSize(sqlite3_file *file, sqlite3_int64 *pSize)
{
    *pSize = g_FileSz;
    return 0;
}

int xLock(sqlite3_file *file, int a)
{
    return 0;<commit_msg>In the config module, fix xWrite to resize the file according to the given amount rather than just assuming doubling the file size is enough.<commit_after>/*
 * Copyright (c) 2008 <NAME>, <NAME>, <NAME>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <Module.h>
#include <Log.h>
#include <processor/Processor.h>
#include "sqlite3.h"
#include <utilities/utility.h>
#include <BootstrapInfo.h>
#include <panic.h>

#include <processor/PhysicalMemoryManager.h>
#include <processor/VirtualAddressSpace.h>
#include <processor/MemoryRegion.h>

#include <linker/KernelElf.h>

#include "Config.h"

extern BootstrapStruct_t *g_pBootstrapInfo;

uint8_t *g_pFile = 0;
size_t g_FileSz = 0;

extern "C" void log_(unsigned long a)
{
    NOTICE("Int: " << a);
}

extern "C" int atoi(const char *str)
{
    return strtoul(str, 0, 10);
}

#ifndef STATIC_DRIVERS // Defined in the kernel.
extern "C" int memcmp(const void *s1, const void *s2, size_t n)
{
    const unsigned char *c1 = reinterpret_cast<const unsigned char*>(s1);
    const unsigned char *c2 = reinterpret_cast<const unsigned char*>(s2);
    for (size_t i = 0; i < n; i++)
        if (c1[i] != c2[i])
            return (c1[i]>c2[i]) ? 1 : -1;
    return 0;
}
#endif

int xClose(sqlite3_file *file)
{
    return 0;
}

int xRead(sqlite3_file *file, void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    int ret = 0;
    if (iOfst+static_cast<unsigned int>(iAmt) >= g_FileSz)
    {
        memset(ptr, 0, iAmt);
        iAmt = g_FileSz - iOfst;
        ret = SQLITE_IOERR_SHORT_READ;
    }
    memcpy(ptr, &g_pFile[iOfst], iAmt);
    return ret;
}

int xReadFail(sqlite3_file *file, void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    memset(ptr, 0, iAmt);
    return SQLITE_IOERR_SHORT_READ;
}

int xWrite(sqlite3_file *file, const void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    // Write past the end of the file?
    if((iOfst + static_cast<unsigned int>(iAmt)) >= g_FileSz)
    {
        // How many extra bytes do we need?
        size_t nNewSize = iOfst + iAmt + 1; // We know the read crosses the EOF, so this is correct

        // Allocate it, zero, and copy
        uint8_t *tmp = new uint8_t[nNewSize];
        memset(tmp, 0, nNewSize);
        memcpy(tmp, g_pFile, g_FileSz);
        
        delete [] g_pFile;
        
        g_pFile = tmp;
        g_FileSz = nNewSize;
    }

    memcpy(&g_pFile[iOfst], ptr, iAmt);
    return 0;
}

int xWriteFail(sqlite3_file *file, const void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    return 0;
}

int xTruncate(sqlite3_file *file, sqlite3_int64 size)
{
    return 0;
}

int xSync(sqlite3_file *file, int flags)
{
    return 0;
}

int xFileSize(sqlite3_file *file, sqlite3_int64 *pSize)
{
    *pSize = g_FileSz;
    return 0;
}

int xLock(sqlite3_file *file, int a)
{
    return 0;
{'fraction_non_alphanumeric': 0.10931297709923664, 'fraction_numerical': 0.020458015267175573, 'mean_word_length': 3.524171270718232, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '2249944', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2359, 'n_words': 742}