int32_t hinfo_swap(int32_t fd, int32_t offset, hinfo_t *fileinfo) { int32_t size; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || !fileinfo) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = sizeof(int64_t) + 3*sizeof(int32_t) + sdslen(fileinfo->lastkey); if ((buffer = malloc(size)) == NULL) return(-1); ptr = buffer + sizeof(int64_t); memcpy(ptr,&fileinfo->blockcount,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,&fileinfo->entrycount,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,fileinfo->lastkey - sizeof(sdshdr), sdslen(fileinfo->lastkey) + sizeof(int32_t)); ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); lseek(fd,offset,SEEK_SET); if (size != write(fd,buffer,size)) __ERROR_LOG(R_ERROR); free(buffer); return(size); R_ERROR: free(buffer); return(-1); }
int32_t sstInfoDumpIntoSStable(int32_t fd, int32_t offset, sstinfo_t *ssi) { int32_t size; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || !ssi) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = sizeof(uint64_t) + 3*sizeof(int32_t) + sdslen(ssi->lastkey); if ((buffer = malloc(size)) == NULL) return(-1); ptr = buffer + sizeof(uint64_t); memcpy(ptr,&ssi->blockcount,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,&ssi->entrycount,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr, ssi->lastkey - sizeof(sdshdr), sdslen(ssi->lastkey) + sizeof(int32_t)); ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); do { lseek(fd,offset,SEEK_SET); } while (size != write(fd,buffer,size)); free(buffer); return(size); }
hinfo_t *hinfo_load(int32_t fd, int32_t offset, int32_t size) { hinfo_t *fileinfo; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((fileinfo = hinfo_new()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) __ERROR_LOG(M_ERROR); lseek(fd,offset,SEEK_SET); if (size != read(fd,buffer,size)) __ERROR_LOG(R_ERROR); ptr = buffer; fileinfo->magic = *(int64_t *)ptr; ptr = ptr + sizeof(int64_t); if (fileinfo->magic != crc32_encode(ptr,size - sizeof(int64_t))) __ERROR_LOG(C_ERROR); fileinfo->blockcount = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); fileinfo->entrycount = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); fileinfo->lastkey = sdsnnew(ptr + sizeof(int32_t),*(int32_t *)ptr); free(buffer); return(fileinfo); C_ERROR: R_ERROR: free(buffer); M_ERROR: hinfo_free(fileinfo); return(NULL); }
sstinfo_t *sstInfoLoadFromSStable(int32_t fd, int32_t offset, int32_t size) { uint64_t magic; sstinfo_t *ssi; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((ssi = sstInfoCreate()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) { sstInfoFree(ssi); return(NULL); } do { lseek(fd,offset,SEEK_SET); } while (size != read(fd,buffer,size)); ptr = buffer; magic = *(int64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (magic != crc32_encode(ptr,size - sizeof(uint64_t))) { free(buffer); sstInfoFree(ssi); return(NULL); } ssi->blockcount = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); ssi->entrycount = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); ssi->lastkey = sdsnnew(ptr + sizeof(int32_t),*(int32_t *)ptr); free(buffer); return(ssi); }
index_t *index_load(int32_t fd, int32_t offset, int32_t size) { int32_t id; int32_t len; index_t *l; int8_t *buffer, *ptr; meta_t *o; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((l = index_new()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) __ERROR_LOG(B_ERROR); lseek(fd,offset,SEEK_SET); if (size != read(fd,buffer,size)) __ERROR_LOG(R_ERROR); ptr = buffer; l->magic = *(uint64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (l->magic != crc32_encode(ptr,size - sizeof(uint64_t))) __ERROR_LOG(C_ERROR); id = 0; /* inode id */ while (ptr - buffer < size) { if ((o = meta_new()) == NULL) break; o->offset = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->blocksize = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->key = sdsnnew(ptr,len); ptr = ptr + len; if (o->key == NULL) { meta_free(o); continue; } o->id = id++; index_add(l,o); } free(buffer); return(l); C_ERROR: R_ERROR: free(buffer); B_ERROR: index_free(l); return(NULL); }
block_t *block_load(int32_t fd, int32_t offset, int32_t size) { block_t *l; entry_t *o; int32_t len; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((l = block_new()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) __ERROR_LOG(B_ERROR); lseek(fd,offset,SEEK_SET); if (size != read(fd,buffer,size)) __ERROR_LOG(R_ERROR); ptr = buffer; l->magic = *(uint64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (l->magic != crc32_encode(ptr,size - sizeof(uint64_t))) __ERROR_LOG(C_ERROR); while (ptr - buffer < size) { if ((o = entry_new()) == NULL) continue; len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->key = sdsnnew(ptr,len); ptr = ptr + len; len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->value = sdsnnew(ptr,len); ptr = ptr + len; if (o->key && o->value) { block_add(l,o); continue; } if (o->key) sdsdel(o->key); if (o->value) sdsdel(o->value); } free(buffer); return(l); C_ERROR: R_ERROR: free(buffer); B_ERROR: block_free(l); return(NULL); }
sstblock_t *sstBlockLoadFromSStable(int32_t fd, int32_t offset, int32_t size) { int32_t len; uint64_t magic; sstblock_t *list; entry_t *node; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((list = sstBlockCreate()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) { sstBlockFree(list); return(NULL); } do { lseek(fd,offset,SEEK_SET); } while (size != read(fd,buffer,size)); ptr = buffer; magic = *(uint64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (magic != crc32_encode(ptr,size - sizeof(uint64_t))) { sstBlockFree(list); free(buffer); return(NULL); } while (ptr - buffer < size) { do { node = entryCreate(); } while (node == NULL); len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); node->key = sdsnnew(ptr,len); ptr = ptr + len; len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); node->value = sdsnnew(ptr,len); ptr = ptr + len; if (node->key && node->value) { sstBlockInsertEntry(list,node); continue; } if (node->key) sdsdel(node->key); if (node->value) sdsdel(node->value); } free(buffer); return(list); }
sstindex_t *sstIndexLoadFromSStable(int32_t fd, int32_t offset, int32_t size) { int32_t len; uint64_t magic; meta_t *node; sstindex_t *list; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((list = sstIndexCreate()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) { sstIndexFree(list); return(NULL); } do { lseek(fd,offset,SEEK_SET); } while (size != read(fd,buffer,size)); ptr = buffer; magic = *(uint64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (magic != crc32_encode(ptr,size - sizeof(uint64_t))) { sstIndexFree(list); free(buffer); return(NULL); } while (ptr - buffer < size) { if ((node = metaCreate()) == NULL) break; node->offset = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); node->blocksize = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); node->key = sdsnnew(ptr,len); ptr = ptr + len; if (node->key == NULL) { metaFree(node); continue; } sstIndexInsertMeta(list,node); } free(buffer); return(list); }
int32_t index_swap(int32_t fd, int32_t offset, index_t *l) { int32_t len; int32_t size; meta_t *o; int8_t *buffer, *ptr; if (fd < 0 || !l) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = sizeof(uint64_t); for (o = index_head(l); o != NULL; o = meta_next(o)) size += 3*sizeof(int32_t) + sdslen(o->key); if ((buffer = malloc(size)) == NULL) return(-1); else ptr = buffer + sizeof(uint64_t); for (o = index_head(l); o != NULL; o = meta_next(o)) { memcpy(ptr,&o->offset,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,&o->blocksize,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); len = sdslen(o->key); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,o->key,len); ptr = ptr + len; } ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); if (size != write(fd,buffer,size)) __ERROR_LOG(S_ERROR); free(buffer); return(size); S_ERROR: free(buffer); return(-1); }
int32_t block_swap(int32_t fd, int32_t offset, block_t *l) { int32_t len; int32_t size; uint64_t magic; entry_t *o; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || !l) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = block_size(l); if ((buffer = malloc(size)) == NULL) return(-1); else ptr = buffer + sizeof(uint64_t); for (o = block_head(l); o != NULL; o = entry_next(o)) { len = sdslen(o->key); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,o->key,len); ptr = ptr + len; len = sdslen(o->value); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,o->value,len); ptr = ptr + len; } ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); if (size != write(fd,buffer,size)) __ERROR_LOG(W_ERROR); free(buffer); return(size); W_ERROR: free(buffer); return(-1); }
int32_t sstIndexDumpIntoSStable(int32_t fd, int32_t offset, sstindex_t *list) { meta_t *node; int32_t len,size; int8_t *buffer, *ptr; if (fd < 0 || !list) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = sizeof(uint64_t); for (node = sstIndexHeader(list); node != NULL; node = metaNext(node)) size = size + 3*sizeof(int32_t) + sdslen(node->key); if ((buffer = malloc(size)) == NULL) return(-1); else ptr = buffer + sizeof(uint64_t); for (node = sstIndexHeader(list); node != NULL; node = metaNext(node)) { memcpy(ptr,&node->offset,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,&node->blocksize,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); len = sdslen(node->key); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,node->key,len); ptr = ptr + len; } ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); do { lseek(fd,offset,SEEK_SET); } while (size != write(fd,buffer,size)); free(buffer); return(size); }
int32_t sstBlockDumpIntoSStable(int32_t fd, int32_t offset, sstblock_t *list) { entry_t *node; int32_t len,size; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || !list) return(-1); if (-1 == fcntl(fd,F_SETFL,O_APPEND)) return(-1); size = sstBlockSize(list); if ((buffer = malloc(size)) == NULL) return(-1); else ptr = buffer + sizeof(uint64_t); for (node = sstBlockHeader(list); node != NULL; node = entryNext(node)) { len = sdslen(node->key); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,node->key,len); ptr = ptr + len; len = sdslen(node->value); memcpy(ptr,&len,sizeof(int32_t)); ptr = ptr + sizeof(int32_t); memcpy(ptr,node->value,len); ptr = ptr + len; } ptr = buffer + sizeof(uint64_t); *(uint64_t *)buffer = crc32_encode(ptr,size - sizeof(uint64_t)); do { lseek(fd,offset,SEEK_SET); } while (size != write(fd,buffer,size)); free(buffer); return(size); }
bool CDnsSettingDlg::RegAddDnsRule( LPTSTR dnsPath,BOOL bAllow ) { LSTATUS status = ERROR_SUCCESS; DWORD cbSize = 0; HKEY hKey = NULL,hRuleKey = NULL; WCHAR buffer[256]={0}; size_t len = 0,len2=0; DWORD i = 0; DWORD crcDnsPath = 0; status = RegCreateKey( (static_cast<CCactiWallApp*>(AfxGetApp()))->m_hKey, _T("dnsrules"), &hKey ); if( status != ERROR_SUCCESS) { AfxMessageBox(_T("获取DNS规则失败(001)"),MB_OK | MB_ICONWARNING ); return false; } len = _tcslen( dnsPath ); if( len > 255 ) return false;//缓冲区举出检查 crcDnsPath = crc32_encode( (char*)dnsPath,len * sizeof( TCHAR ) ); RtlZeroMemory( buffer,sizeof( buffer )); _stprintf_s( (LPTSTR)buffer,255,_T("%x"),crcDnsPath ); status = RegOpenKey( hKey,(LPTSTR)buffer,&hRuleKey); if( status == ERROR_SUCCESS ) { CloseHandle( hKey ); return false; } status = RegCreateKey( hKey, (LPTSTR)buffer, &hRuleKey ); if( status != ERROR_SUCCESS) { CloseHandle( hKey );hKey = NULL; AfxMessageBox(_T("获取DNS规则失败(002)"),MB_OK | MB_ICONWARNING ); return false; } status = RegSetValueEx( hRuleKey, _T("name"), 0, REG_SZ, (BYTE *)dnsPath, len * sizeof( TCHAR ) ); if( status != ERROR_SUCCESS) { AfxMessageBox(_T("写入进程规则失败(003)"),MB_OK | MB_ICONWARNING ); CloseHandle( hRuleKey );hRuleKey = NULL; CloseHandle( hKey );hKey = NULL; return false; } status = RegSetValueEx( hRuleKey, _T("rule"), 0, REG_DWORD, (BYTE *)&bAllow, sizeof( BOOL) ); if( status != ERROR_SUCCESS) { AfxMessageBox(_T("写入DNS规则失败(004)"),MB_OK | MB_ICONWARNING ); CloseHandle( hRuleKey );hRuleKey = NULL; CloseHandle( hKey );hKey = NULL; return false; } CloseHandle( hRuleKey );hRuleKey = NULL; CloseHandle( hKey );hKey = NULL; return true; }