整理SQL SERVER資料頁checksum校驗演算法
在SQL SERVER2005以上版本中,資料頁預設開啟checksum,標識為m_flagBits & 0x200 == True,其值m_tornBits位於頁頭0x3C,4位元組。
其演算法概述如下:
讀8KB 進BUF 將BUF頭部 CHECKSUM的4位元組值清0 uint32 checksum = 0 //初始checksumfor i in range(0,15): //每扇區的初始checksum overall = 0; for ii in range(0,127): //對當前扇區的每個4位元組做累加異或 overall = overall ^ BUF[i][ii]; //對每扇區的checksum進行移位,方法為向左移位15-i位, //左邊移出的15-i位補到最低位。 checksum = checksum ^ rol(overall, 15- i); return checksum; //Gets checksum
c原始碼如下:
//***CODE***//#include <stdio.h>#include <stdlib.h> #define seed 15 //Initial seed(for first sector)#define CHAR_BIT 8 //***PROTOTYPES***//unsigned int page_checksum(int page_id, unsigned int *ondisk);unsigned int rol(unsigned int value, unsigned int rotation); int main(int argc, char *argv[]) { unsigned int computed_checksum; //Var to retrieve calculated checksum unsigned int ondisk_checksum; //Var to retrieve checksum on disk computed_checksum = page_checksum(152, &ondisk_checksum); //page_checksum call to retrieve stored and calculated checksum for page 152 //***PRINTS***// printf("Calculated checksum: 0x%08x\n", computed_checksum); printf("On disk checksum: 0x%08x\n", ondisk_checksum); } unsigned int page_checksum(int page_id, unsigned int *ondisk){ FILE *fileptr; unsigned int i; unsigned int j; unsigned int checksum; unsigned int overall; unsigned int *pagebuf[16][128]; //A pointer to describe 2d array [sector][element] fileptr = fopen("C:\\Users\\andre\\Desktop\\teste.mdf", "r+b"); //Open dummy data file for binary read fseek(fileptr, page_id * 8192, SEEK_SET); //Calculate page address on data file and points to it fread(pagebuf, 4, 2048, fileptr); //Read page buffer fclose(fileptr); checksum = 0; overall = 0; *ondisk = pagebuf[0][15]; //This means that torn bits is stored on first sector in 15th element, Internals researches understand this pagebuf[0][15] = 0x00000000; //Fill checksum field with zeroes (this field will be discarded in algorithm) for (i = 0; i < 16; i++) //Loop through sectors { overall = 0; //Reset overall sum for sectors for (j = 0; j < 128; j++) //Loop through elements in sector i { overall = overall ^ (unsigned int)pagebuf[i][j]; //XOR operation between sector i elements } checksum = checksum ^ rol(overall, seed - i); //Current checksum is overall for sector i circular shifted by seed (15 - i) } return checksum; //Gets checksum } unsigned int rol(unsigned int value, unsigned int rotation){ return (value) << (rotation) | (value) >> (sizeof(int) * CHAR_BIT - rotation) & ( (1 << rotation) -1); }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31380569/viewspace-2652929/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 好用的資料校驗&修復工具gt-checksum開源啦
- 計算校驗和工具:Checksum Thing MacMac
- SQL Server 資料頁損壞修復SQLServer
- Sqlserver關於校驗和_備份還原的CHECKSUMSQLServer
- 資料校驗
- MySQL手動資料校驗+雲資料庫資料校驗MySql資料庫
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- SQL Server常用函式整理SQLServer函式
- [資料校驗/資料質量] 資料校驗框架(Java):hibernate-validation框架Java
- easypoi資料校驗
- SQL server 修改表資料SQLServer
- 資料庫映象 (SQL Server)資料庫SQLServer
- SQL Server資料庫安全SQLServer資料庫
- SQL Server資料庫中分頁編號的另一種方式SQLServer資料庫
- 行式填報 資料校驗 --- 小計校驗
- Binding(四):資料校驗
- ORACLE資料校驗文件Oracle
- 【資料庫資料恢復】透過資料頁恢復Sql Server資料庫資料的過程資料庫資料恢復SQLServer
- Blazor使用sql server 資料庫BlazorSQLServer資料庫
- SQL Server資料庫巡檢SQLServer資料庫
- sql server 2005資料庫快照SQLServer資料庫
- SQL Server收縮資料庫SQLServer資料庫
- sql server 資料庫收縮SQLServer資料庫
- SQL Server資料庫遷移SQLServer資料庫
- SQL Server-資料型別SQLServer資料型別
- 管理SQL Server資料庫安全SQLServer資料庫
- 基於Sql server資料庫的四種分頁方式總結SQLServer資料庫
- SQL Server資料庫怎麼找出一個表包含的頁資訊(Page)SQLServer資料庫
- struts2資料校驗
- JSR303 資料校驗JS
- Hibernate資料校驗簡介
- SQL Server 2014 匯出資料字典SQLServer
- SQL Server 跨資料庫查詢SQLServer資料庫
- Logstash : 從 SQL Server 讀取資料SQLServer
- 資料庫映象 (SQL Server)操作模式資料庫SQLServer模式
- SQL Server 資料訪問策略:CLRMESQLServer
- 前端資料校驗後,後端介面是否需要再次校驗?前端後端
- antd頁面多表單校驗