在SQL SERVER2005以上版本中,数据页默认开启checksum,标识为m_flagBits & 0x200 == True,其值m_tornBits位于页头0x3C,4字节。
其算法概述如下:

读8KB 进BUF
将BUF头部 CHECKSUM的4字节值清0
uint32 checksum = 0 //初始checksum
for i in range(0,15)://每扇区的初始checksumoverall = 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 checksumunsigned int ondisk_checksum; //Var to retrieve checksum on diskcomputed_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 readfseek(fileptr, page_id * 8192, SEEK_SET); //Calculate page address on data file and points to itfread(pagebuf, 4, 2048, fileptr); //Read page bufferfclose(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 thispagebuf[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 sectorsfor (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);
}

转载于:https://blog.51cto.com/zhangyu/2407762

SQL SERVER数据页checksum校验算法相关推荐

  1. Sql Server 数据分页

    Sql Server 数据分页 在列表查询时由于数据量非常多,一次性查出来会非常慢,就算一次查出来了,也不能一次性显示给客户端,所以要把数据进行分批查询出来,每页显示一定量的数据,这就是数据要分页. ...

  2. 实验10 SQL Server 数据备份/恢复

    实验10 SQL Server 数据备份/恢复 一.实验目的 1.了解数据库备份的过程和属性设置: 2.掌握应用企业管理器备份和恢复数据库: 3.掌握应用T-SQL备份和恢复数据库: 4.掌握数据导入 ...

  3. 使用SQL Server数据工具进行SQL单元测试

    This article on SQL Unit Testing is the second part on the series about SSDT and database developmen ...

  4. 检测java是否连接到SQL server数据库 + SQL server数据库内置账户sa无法登录

    检测java是否连接到SQL server数据库 + SQL server数据库内置账户sa无法登录 SQL 检测java是否连接到SQL server数据库 SQL server数据库内置账户SA登 ...

  5. 浅析SQL Server数据修复命令DBCC的使用

    SQL Server数据库提供了修复命令DBCC,当SQL Server数据库遭到质疑或者是有的无法完成读取时可以尝试用此命令来修复.以下是一些常见的DBCC修复命令,希望会给读者带来帮助.    1 ...

  6. 将本地的MS SQL Server数据导入到远程服务器上

    该演示引导客户将本地的MS SQL Server数据导入到远程服务器上,供客户的网站使用. 首先登录到远程数据库服务器: 1.右击您准备导入数据的数据库,选择"所有任务"下的&qu ...

  7. php实现sql server数据导入到mysql数据库_php实现SQL Server数据导入Mysql数据库(示例)...

    php将mssql的数据导入到mysql数据库中,代码: 代码示例: //连接SQL server数据库 $conn=mssql_connect("localhost"," ...

  8. SQL SERVER 数据库主键和外键的思考

    SQL SERVER 数据库主键和外键的思考 什么是主键: 主键是指表中一个列或者列的组合,其值能够唯一的标识表中的每一个行.这样的一列或者多列成为表的主键,通过它可以强制表的实体完整性.当创建或者更 ...

  9. Sql server 数据转到 Mysql 数据库

    Sql server 数据转到 Mysql 数据库http://www.bieryun.com/3355.html 在网上找了一些方案,目前一个可行的较好的方案,虽然不够完美但也十分好用: 用到的用具 ...

最新文章

  1. R语言卡方分布函数Binomial Distribution(dchisq, pchisq, qchisq rchisq)实战
  2. 每日一皮:在调试时,将断点设置在错误的位置...
  3. PyTorch ResNet 测试
  4. VTK:可视化之ColorActorEdges
  5. php截取字符串函数 左右,php截取中文字符串函数的技巧
  6. java ee规范_测试Java EE 8规范
  7. 在非托管对象中使用Spring托管Bean
  8. 前端基础-html-表格的标题和表头单元格标签
  9. Protocol Buffers动态消息解析
  10. 给IT人的15点建议:苦逼程序员的辛酸反省与总结
  11. 光学图像、SAR图像等区别
  12. 小菜叫你玩破解和逆向(1)
  13. 联通应用商店一周年硕果累累
  14. [error]: Found option without preceding group in config file ....\my.ini at line:1
  15. ddr5和ddr6的显卡插口区别 ddr5和ddr6差距有多大
  16. Doxygen一个程序的文件产生工具
  17. Rebound动画框架简单介绍
  18. Windows Modules Installer禁用导致的windows2008R2 安装补丁“此更新不适用于你的计算机”解决办法
  19. 中兴路由器查看时间_中兴路由器简单命令
  20. 计算机著作的写作方法,写作手法概念解析:写作手法大致分为哪几类? -备考资料...

热门文章

  1. scala言语基础学习八
  2. Android实例]------获取sd卡图片显示出来
  3. mongoDB 文档操作_改
  4. 作业要求 20181127-1 附加作业 软件工程原则的应用实例分析
  5. 利用JavaScript的复制
  6. 思科“万物互联”路线下一站:连接分析
  7. python 目录下的__init__.py
  8. 使用链路聚合进行负载分担
  9. EXCEL 函数----用countif()函数计算重复数值个数和表示重复值
  10. 【原创】如何在 Linux 下调整可打开文件/文件描述符数目