Ejemplo n.º 1
0
GdbBlock *
gdbReadBlockHeader(GDatabase *db, offset_t offset, blocktype_t blockType)
{
	GdbBlock *block;
	char header[GDB_BLOCK_HEADER_SIZE];
	int counter = 0;
	blocktype_t typeIndex;

	if (db == NULL || !GDB_VALID_OFFSET(offset) ||
		(blockType != GDB_BLOCK_ANY && !GDB_VALID_BLOCK_TYPE(blockType)))
	{
		return NULL;
	}

	/* See if the block is cached. */
	if ((block = gdbCacheGetBlock(db, offset)) != NULL)
	{
		if (blockType == GDB_BLOCK_ANY || blockType == block->type)
			return block;
		else
			return NULL;
	}

	/* Seek to the offset of the block. */
	fseek(db->fp, offset, SEEK_SET);
	
	if (fread(header, GDB_BLOCK_HEADER_SIZE, 1, db->fp) != 1)
	{
		return NULL;
	}

	/* Allocate memory for the block. */
	MEM_CHECK(block = (GdbBlock *)malloc(sizeof(GdbBlock)));
	memset(block, 0, sizeof(GdbBlock));

	block->db = db;

	/* Store the info from the header. */
	block->type = gdbGet8(header, &counter);

	/* Make sure the type is valid. */
	if (!GDB_VALID_BLOCK_TYPE(block->type) ||
		(blockType != GDB_BLOCK_ANY && blockType != block->type))
	{
		free(block);

		return NULL;
	}

	typeIndex = block->type - 1;

	block->offset = offset;
	
	block->multiple = blockTypeInfo[typeIndex].multiple;
	
	block->dataSize = gdbGet32(header, &counter);
	block->flags    = gdbGet16(header, &counter);
	block->next     = gdbGet32(header, &counter);
	block->listNext = gdbGet32(header, &counter);

	GDB_CLEAR_DIRTY(block);

	gdbCacheAddBlock(block->db, block);

	return block;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
/*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;
}