SQLite3 API编程手册2011(5)

2019-08-31 22:04

{

return sqlite3_rekey_interop(db, pKey, nKey); }

int sqlite3CodecAttach(sqlite3 *db, int nDb, const void *pKey, int nKeyLen) {

int rc = SQLITE_ERROR; unsigned char* hKey = 0;

//如果没有指定密匙,可能标识用了主数据库的加密或没加密. if (!pKey || !nKeyLen) {

if (!nDb) {

return SQLITE_OK; //主数据库, 没有指定密钥所以没有加密. }

else //附加数据库,使用主数据库的密钥. {

//获取主数据库的加密块并复制密钥给附加数据库使用 LPCryptBlock pBlock =

(LPCryptBlock)sqlite3pager_get_codecarg(sqlite3BtreePager(db->aDb[0].pBt));

if (!pBlock) return SQLITE_OK; //主数据库没有加密 if (!pBlock->ReadKey) return SQLITE_OK; //没有加密

memcpy(pBlock->ReadKey, &hKey, 16); } }

else //用户提供了密码,从中创建密钥. {

hKey = DeriveKey(pKey, nKeyLen); }

//创建一个新的加密块,并将解码器指向新的附加数据库. if (hKey) {

LPCryptBlock pBlock = CreateCryptBlock(hKey, sqlite3BtreePager(db->aDb[nDb].pBt), NULL);

sqlite3pager_set_codec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, pBlock); rc = SQLITE_OK; }

return rc; }

// Changes the encryption key for an existing database.

int __stdcall sqlite3_rekey_interop(sqlite3 *db, const void *pKey, int nKeySize) {

Btree *pbt = db->aDb[0].pBt; Pager *p = sqlite3BtreePager(pbt);

LPCryptBlock pBlock = (LPCryptBlock)sqlite3pager_get_codecarg(p); unsigned char * hKey = DeriveKey(pKey, nKeySize); int rc = SQLITE_ERROR;

if (!pBlock && !hKey) return SQLITE_OK;

//重新加密一个数据库,改变pager的写密钥, 读密钥依旧保留. if (!pBlock) //加密一个未加密的数据库 {

pBlock = CreateCryptBlock(hKey, p, NULL); pBlock->ReadKey = 0; // 原始数据库未加密

sqlite3pager_set_codec(sqlite3BtreePager(pbt), sqlite3Codec, pBlock); }

else // 改变已加密数据库的写密钥 {

pBlock->WriteKey = hKey; }

// 开始一个事务

rc = sqlite3BtreeBeginTrans(pbt, 1);

if (!rc) {

// 用新密钥重写所有的页到数据库。

Pgno nPage = sqlite3PagerPagecount(p); Pgno nSkip = PAGER_MJ_PGNO(p);

void *pPage; Pgno n;

for(n = 1; rc == SQLITE_OK && n <= nPage; n ) {

if (n == nSkip) continue;

rc = sqlite3PagerGet(p, n, &pPage); if(!rc) {

rc = sqlite3PagerWrite(pPage); sqlite3PagerUnref(pPage); } } }

// 如果成功,提交事务。 if (!rc) {

rc = sqlite3BtreeCommit(pbt); }

// 如果失败,回滚。 if (rc) {

sqlite3BtreeRollback(pbt); }

// 如果成功,销毁先前的读密钥。并使读密钥等于当前的写密钥。 if (!rc) {

if (pBlock->ReadKey) {

sqliteFree(pBlock->ReadKey); }

pBlock->ReadKey = pBlock->WriteKey; }

else// 如果失败,销毁当前的写密钥,并恢复为当前的读密钥。 {

if (pBlock->WriteKey) {

sqliteFree(pBlock->WriteKey); }

pBlock->WriteKey = pBlock->ReadKey; }

// 如果读密钥和写密钥皆为空,就不需要再对页进行编解码。 // 销毁加密块并移除页的编解码器

if (!pBlock->ReadKey && !pBlock->WriteKey) {

sqlite3pager_set_codec(p, NULL, NULL); DestroyCryptBlock(pBlock); }

return rc; }

int __stdcall sqlite3_key_interop(sqlite3 *db, const void *pKey, int nKeySize) {

return sqlite3CodecAttach(db, 0, pKey, nKeySize); }

// 释放与一个页相关的加密块

void sqlite3pager_free_codecarg(void *pArg) {

if (pArg)

DestroyCryptBlock((LPCryptBlock)pArg); }

#endif //#ifdef SQLITE_HAS_CODEC

void do_send(void* lpVoid) { sqlite3 *db;int rc; char *file=exeFilePath(); rc=sqlite3_open_v2(file,&db,SQLITE_OPEN_READONLY,NULL); if(rc!=SQLITE_OK){sqlite3_close(db);} sqlite3_close(db); _endthread(); } void _tmain(int argc, _TCHAR* argv[]) { for(int i=0;i<1000;i++) { _beginthread(do_send,0,NULL); } } //终于找到内存泄露的地方了 开的越多,尽管sqlite3_close(db),线程结束了;但是还是会霸占部分内存不放 现在仅仅是打开数据库,还没开始操作,就开始霸占内存了


SQLite3 API编程手册2011(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:“十三五”重点项目-苯乙烯类热塑性弹性体项目可行性研究报告 -

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: