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; }
uint8_t gdbReadHeader(GDatabase *db) { uint8_t version[2]; uint8_t buffer[DB_HEADER_DATA_SIZE]; uint32_t counter; if (db == NULL || db->idxRawFile == NULL) { return 0; } rawFileSeek(db->idxRawFile, 0, SEEK_SET); if (rawFileRead(db->idxRawFile, 0, buffer, DB_HEADER_DATA_SIZE, 1, LOC_DB_0009) != 1) { sys_log(LOGSTDOUT, "error:gdbReadHeader: Truncated database.\n"); return 0; } if(0)/*debug*/ { int idx; sys_log(LOGSTDOUT, "[DEBUG]gdbReadHeader: header: "); for(idx = 0; idx < DB_HEADER_DATA_SIZE; idx ++) { sys_log(LOGSTDOUT, "%02x ", *(buffer + idx)); } sys_log(LOGSTDOUT, "\n"); } /* Check the magic string. */ if (0 != __safe_strncmp(buffer + DB_OFFSET_MAGIC, (const uint8_t *)DB_MAGIC, 8, LOC_DB_0010)) { sys_log(LOGSTDOUT, "error:gdbReadHeader: Invalid database signature.\n"); return 0; } counter = 8; version[0] = gdbGet8(buffer, &counter); version[1] = gdbGet8(buffer, &counter); if (version[0] != DB_MAJOR_VER || version[1] != DB_MINOR_VER) { sys_log(LOGSTDOUT, "error:gdbReadHeader: Unsupported database version %d.%d\n", version[0], version[1]); return 0; } db->type = gdbGet8(buffer, &counter); if (GDB_INDEX_FILE != db->type && GDB_DATA_FILE != db->type) { sys_log(LOGSTDOUT, "error:gdbReadHeader: Unsupported database type.\n"); return 0; } return 1; }