示例#1
0
文件: checkfreelist.c 项目: cznic/cc
/*
** An SQL function invoked as follows:
**
**   sqlite_readint32(BLOB)           -- Decode 32-bit integer from start of blob
*/
static void readint_function(
  sqlite3_context *pCtx,
  int nArg,
  sqlite3_value **apArg
){
  const u8 *zBlob;
  int nBlob;
  int iOff = 0;
  u32 iRet = 0;

  if( nArg!=1 && nArg!=2 ){
    sqlite3_result_error(
        pCtx, "wrong number of arguments to function sqlite_readint32()", -1
    );
    return;
  }
  if( nArg==2 ){
    iOff = sqlite3_value_int(apArg[1]);
  }

  zBlob = sqlite3_value_blob(apArg[0]);
  nBlob = sqlite3_value_bytes(apArg[0]);

  if( nBlob>=(iOff+4) ){
    iRet = get4byte(&zBlob[iOff]);
  }

  sqlite3_result_int64(pCtx, (sqlite3_int64)iRet);
}
示例#2
0
void test_1b_2(void)
{
  int rc;
  chidb *db;
  MemPage *page;
  uint8_t *rawpage;
  
  remove(NEWFILE);
  db = malloc(sizeof(chidb));
  rc = chidb_Btree_open(NEWFILE, db, &db->bt);
  CU_ASSERT(rc == CHIDB_OK);
  
  rc = chidb_Pager_readPage(db->bt->pager, 1, &page);
  CU_ASSERT(rc == CHIDB_OK);
  rawpage = page->data;
  
  if (strcmp((char *) rawpage, "SQLite format 3") || 
      rawpage[18] != 1 || 
      rawpage[19] != 1 || 
      rawpage[20] != 0 || 
      rawpage[21] != 64 || 
      rawpage[22] != 32 || 
      rawpage[23] != 32 ||
      get4byte(&rawpage[32]) != 0 || 
      get4byte(&rawpage[36]) != 0 || 
      get4byte(&rawpage[44]) != 1 || 
      get4byte(&rawpage[52]) != 0 || 
      get4byte(&rawpage[56]) != 1 || 
      get4byte(&rawpage[64]) != 0 ||
      get4byte(&rawpage[48]) != 20000)
    CU_FAIL("File header is not well-formed.");	
  
  if (rawpage[100] != PGTYPE_TABLE_LEAF || 
      get2byte(&rawpage[101]) != 108 || 
      get2byte(&rawpage[103]) != 0 ||
      get2byte(&rawpage[105]) != 1024 || 
      rawpage[107] != 0)
    CU_FAIL("Page 1 header is not well-formed.");	
  
  rc = chidb_Btree_close(db->bt);
  remove(NEWFILE);
  free(db);
}
示例#3
0
文件: checkfreelist.c 项目: cznic/cc
static int checkFreelist(
  sqlite3 *db, 
  const char *zDb,
  char **pzOut
){
  /* This query returns one row for each page on the free list. Each row has
  ** two columns - the page number and page content.  */
  const char *zTrunk = 
    "WITH freelist_trunk(i, d, n) AS ("
      "SELECT 1, NULL, sqlite_readint32(data, 32) "
      "FROM sqlite_dbpage(:1) WHERE pgno=1 "
        "UNION ALL "
      "SELECT n, data, sqlite_readint32(data) "
      "FROM freelist_trunk, sqlite_dbpage(:1) WHERE pgno=n "
    ")"
    "SELECT i, d FROM freelist_trunk WHERE i!=1;";

  int rc, rc2;                    /* Return code */
  sqlite3_stmt *pTrunk = 0;       /* Compilation of zTrunk */
  u32 nPage = 0;                  /* Number of pages in db */
  u32 nExpected = 0;              /* Expected number of free pages */
  u32 nFree = 0;                  /* Number of pages on free list */

  if( zDb==0 ) zDb = "main";

  if( (rc = sqlGetInteger(db, zDb, "PRAGMA %s.page_count", &nPage))
   || (rc = sqlGetInteger(db, zDb, "PRAGMA %s.freelist_count", &nExpected))
  ){
    return rc;
  }

  rc = sqlite3_prepare_v2(db, zTrunk, -1, &pTrunk, 0);
  if( rc!=SQLITE_OK ) return rc;
  sqlite3_bind_text(pTrunk, 1, zDb, -1, SQLITE_STATIC);
  while( rc==SQLITE_OK && sqlite3_step(pTrunk)==SQLITE_ROW ){
    u32 i;
    u32 iTrunk = (u32)sqlite3_column_int(pTrunk, 0);
    const u8 *aData = (const u8*)sqlite3_column_blob(pTrunk, 1);
    int nData = sqlite3_column_bytes(pTrunk, 1);
    u32 iNext = get4byte(&aData[0]);
    u32 nLeaf = get4byte(&aData[4]);

    if( nLeaf>((nData/4)-2-6) ){
      rc = checkFreelistError(pzOut, 
          "leaf count out of range (%d) on trunk page %d", 
          (int)nLeaf, (int)iTrunk
      );
      nLeaf = (nData/4) - 2 - 6;
    }

    nFree += 1+nLeaf;
    if( iNext>nPage ){
      rc = checkFreelistError(pzOut, 
          "trunk page %d is out of range", (int)iNext
      );
    }

    for(i=0; rc==SQLITE_OK && i<nLeaf; i++){
      u32 iLeaf = get4byte(&aData[8 + 4*i]);
      if( iLeaf==0 || iLeaf>nPage ){
        rc = checkFreelistError(pzOut,
            "leaf page %d is out of range (child %d of trunk page %d)", 
            (int)iLeaf, (int)i, (int)iTrunk
        );
      }
    }
  }

  if( rc==SQLITE_OK && nFree!=nExpected ){
    rc = checkFreelistError(pzOut,
        "free-list count mismatch: actual=%d header=%d", 
        (int)nFree, (int)nExpected
    );
  }

  rc2 = sqlite3_finalize(pTrunk);
  if( rc==SQLITE_OK ) rc = rc2;
  return rc;
}