コード例 #1
0
ファイル: db_blocklist.c プロジェクト: inevity/ebgn
uint8_t
gdbGetFreeBlockList(GDatabase *db, GdbFreeBlock **blocks, uint32_t *count)
{
    GdbFreeBlock *blockList;
    uint32_t listSize;
    uint8_t *buffer;
    size_t   s;
    uint32_t i, counter = 0;
    offset_t __offset;

    if (blocks == NULL || count == NULL)
    {
        return 0;
    }
    *blocks = NULL;

    /* Seek to the start of the block list. */
    rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET, SEEK_SET);
    __offset = DB_FREE_BLOCK_LIST_OFFSET;
    if (rawFileRead(db->idxRawFile, __offset, &db->freeBlockCount, sizeof(uint32_t), 1, LOC_DB_0001) != 1)
    {
        db->freeBlockCount = 0;
    }
    else
    {
        __offset += sizeof(uint32_t);
    }
    db->freeBlockCount = gdb_ntoh_uint32(db->freeBlockCount);

    *count = db->freeBlockCount;

    if (db->freeBlockCount == 0)
    {
        return 0;
    }
    /* Get the total size of the free blocks list. */
    listSize = db->freeBlockCount * (sizeof(uint16_t) + sizeof(offset_t));

    /* Allocate the buffer. */
    MEM_CHECK(buffer = (uint8_t *)SAFE_MALLOC(listSize, LOC_DB_0002));

    /* Read in the list. */
    //rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET + sizeof(uint32_t), SEEK_SET);
    if ((s = rawFileRead(db->idxRawFile, __offset, buffer, 1, listSize, LOC_DB_0003)) != listSize)
    {
        dbg_log(SEC_0131_DB, 0)(LOGSTDOUT,"error:gdbGetFreeBlockList: Truncated block list.\n"
                          "Expected %d bytes, got %d bytes. Block list offset = %d\n"
                          "Free block count = %d. Filename = %s\n",
                          listSize, s, DB_FREE_BLOCK_LIST_OFFSET, db->freeBlockCount,
                          db->filename);
        abort();
    }

    MEM_CHECK(blockList = (GdbFreeBlock *)SAFE_MALLOC(db->freeBlockCount * sizeof(GdbFreeBlock), LOC_DB_0004));

    for (i = 0; i < db->freeBlockCount; i++)
    {
        blockList[i].size   = gdbGet16(buffer, &counter);
        blockList[i].offset = gdbGetOffset(buffer, &counter);
    }

    *blocks = blockList;

    SAFE_FREE(buffer, LOC_DB_0005);

    return 1;
}
コード例 #2
0
ファイル: btree_node.c プロジェクト: petercloud/RFS
/*comment: read BTree Node out of buffer*/
void *
btreeReadNodeBlock(GdbBlock *block, const uint8_t *buffer, void *extra)
{
    BTreeNode *node;
    uint8_t   i;
    uint32_t  counter = 0;

    node = (BTreeNode *)btreeCreateNodeBlock(block, extra);

    //comment: tree and block of the node were set in btreeCreateNodeBlock
    //node->tree  = (BTree *)extra;
    //node->block = block;

    node->keyCount = gdbGet8(buffer, &counter);

    for (i = 0; i < node->tree->order; i++)
    {
        node->children[i] = gdbGetOffset(buffer, &counter);
    }
    for (i = 0; i < node->tree->order - 1; i++)
    {
        node->keySizes[i] = gdbGet16(buffer, &counter);
    }
    for (i = 0; i < node->tree->order - 1; i++)
    {
        if (node->keySizes[i] > 0)
        {
            MEM_CHECK(node->keys[i] = keyNew(node->keySizes[i], LOC_BTREE_0158));
            memcpy(node->keys[i], buffer + counter, node->keySizes[i]);

            counter += node->keySizes[i];
        }
    }

#if (SWITCH_ON == COMPRESS_MODE)
    if (node->keyCount >= 2)
    {
        uint8_t **newKeys;
        uint16_t *newKeySizes;

        __uncompressNode(node, &newKeys, &newKeySizes);

        /* Free up the compressed keys. */
        for (i = 0; i < node->keyCount; i++)
        {
            if (node->keys[i] != NULL)
            {
                keyFree(node->keys[i], LOC_BTREE_0159);
            }
        }

        SAFE_FREE(node->keys, LOC_BTREE_0160);
        SAFE_FREE(node->keySizes, LOC_BTREE_0161);

        /* Move over the new arrays. */
        node->keys     = newKeys;
        node->keySizes = newKeySizes;
    }
#endif/*(SWITCH_ON == COMPRESS_MODE)*/
    return node;
}