整理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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- pt-table-checksum進行主從資料校驗
- 好用的資料校驗&修復工具gt-checksum開源啦
- 計算校驗和工具:Checksum Thing MacMac
- SQL SERVER分頁演算法SQLServer演算法
- 資料校驗
- SQL Server 資料頁損壞修復SQLServer
- MySQL手動資料校驗+雲資料庫資料校驗MySql資料庫
- 痞子衡嵌入式:常用的資料差錯控制技術(3)- 和校驗(Checksum)
- 教你解決整理SQL Server輸入的資料SQLServer
- Sqlserver關於校驗和_備份還原的CHECKSUMSQLServer
- easypoi資料校驗
- IP資料包的校驗和演算法_儒雅演算法
- SQL Server資料庫安全管理經驗談SQLServer資料庫
- Veridata校驗SQL Server和Oracle時的注意點SQLServerOracle
- 解析SQL SERVER 資料頁面頭部結構SQLServer
- [資料校驗/資料質量] 資料校驗框架(Java):hibernate-validation框架Java
- 行式填報 資料校驗 --- 小計校驗
- ORACLE資料校驗文件Oracle
- 網路資料包效驗和(checksum)的計算
- SQL Server之旅(6):使用winHex利器加深理解資料頁SQLServer
- Binding(四):資料校驗
- struts2資料校驗
- 使用 voluptuous 校驗資料
- DW中的資料校驗
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- 前端資料校驗後,後端介面是否需要再次校驗?前端後端
- SQL Server常用函式整理SQLServer函式
- Hibernate資料校驗簡介
- 分頁procedure (SQL Server)SQLServer
- SQL資料分頁SQL
- SQL Server資料庫安全SQLServer資料庫
- SQL server 修改表資料SQLServer
- SQL Server 資料庫映象SQLServer資料庫
- SQL Server 資料庫索引SQLServer資料庫索引
- 資料庫映象 (SQL Server)資料庫SQLServer
- SQL Server資料庫中分頁編號的另一種方式SQLServer資料庫
- Sql Server之旅——第六站 使用winHex利器加深理解資料頁SQLServer
- SQL Server 2005資料頁讀取--高階掃描SQLServer