MYSQL中字节序操作宏

 

字节序操作宏

 

       无论是MySQL的通信协议,还是MyISAM的数据存储文件,都会涉及到将整数转换到字符串,以及将“整数字符串”转到整数。由于MySQL运行在不同的平台上,这些平台有可能使用大端法,也有可能使用小端法,为了存储数据格式的统一,必须将字节序做一个统一的存储。MySQL选择了大端法来存储整数到磁盘上。做这些转换的函数定义在/include/myisampach.h中,实际上他们是宏。在这个头文件的开头注释处,留意如下的一段:

  Storing of values in high byte first order.

  integer keys and file pointers are stored with high byte first to get better compression

从上面的注释可以看出,它将整数的高字节存储在字符串的开始处。

      

       我们先看将字符串转换到整形的操作:

//转成8位有符号整数

#define mi_sint1korr(A)       ((int8)(*A))

//转成8位无符号整数

#define mi_uint1korr(A)       ((uint8)(*A))

//转成16位有符号整数

#define mi_sint2korr(A)       ((int16) (((int16) (((uchar*) (A))[1])) +\

                               ((int16) ((int16) ((char*) (A))[0]) << 8)))

#define mi_sint3korr(A)       ((int32) (((((uchar*) (A))[0]) & 128) ? \

                                 (((uint32) 255L << 24) | \

                                 (((uint32) ((uchar*) (A))[0]) << 16) |\

                                 (((uint32) ((uchar*) (A))[1]) << 8) | \

                                  ((uint32) ((uchar*) (A))[2])) : \

                                  (((uint32) ((uchar*) (A))[0]) << 16) |\

                                  (((uint32) ((uchar*) (A))[1]) << 8) | \

                                  ((uint32) ((uchar*) (A))[2])))

#define mi_sint4korr(A) ((int32) (((int32) (((uchar*) (A))[3])) +\

                                  ((int32) (((uchar*) (A))[2]) << 8) +\

                                  ((int32) (((uchar*) (A))[1]) << 16) +\

                                  ((int32) ((int16) ((char*) (A))[0]) << 24)))

#define mi_sint8korr(A) ((longlong) mi_uint8korr(A))

#define mi_uint2korr(A) ((uint16)(((uint16)(((uchar*)(A))[1])) + ((uint16) (((uchar*)(A))[0]) << 8)))

#define mi_uint3korr(A) ((uint32) (((uint32) (((uchar*) (A))[2])) +\

                                   (((uint32) (((uchar*) (A))[1])) << 8) +\

                                   (((uint32) (((uchar*) (A))[0])) << 16)))

 

//转成32位无符号整数

#define mi_uint4korr(A)       ((uint32) (((uint32) (((uchar*) (A))[3])) +\

                                (((uint32) (((uchar*) (A))[2])) << 8) +\

                                (((uint32) (((uchar*) (A))[1])) << 16) +\

                                (((uint32) (((uchar*) (A))[0])) << 24)))

#define mi_uint5korr(A)       ((ulonglong)(((uint32) (((uchar*) (A))[4])) +\

                                  (((uint32) (((uchar*) (A))[3])) << 8) +\

                                  (((uint32) (((uchar*) (A))[2])) << 16) +\

                                  (((uint32) (((uchar*) (A))[1])) << 24)) +\

                                    (((ulonglong) (((uchar*) (A))[0])) << 32))

#define mi_uint6korr(A)       ((ulonglong)(((uint32) (((uchar*) (A))[5])) +\

                                  (((uint32) (((uchar*) (A))[4])) << 8) +\

                                  (((uint32) (((uchar*) (A))[3])) << 16) +\

                                  (((uint32) (((uchar*) (A))[2])) << 24)) +\

                                  (((ulonglong) (((uint32) (((uchar*) (A))[1])) +\

                                  (((uint32) (((uchar*) (A))[0]) << 8)))) <<\

                                     32))

#define mi_uint7korr(A) ((ulonglong)(((uint32) (((uchar*) (A))[6])) +\

                                    (((uint32) (((uchar*) (A))[5])) << 8) +\

                                    (((uint32) (((uchar*) (A))[4])) << 16) +\

                                    (((uint32) (((uchar*) (A))[3])) << 24)) +\

                        (((ulonglong) (((uint32) (((uchar*) (A))[2])) +\

                                    (((uint32) (((uchar*) (A))[1])) << 8) +\

                                    (((uint32) (((uchar*) (A))[0])) << 16))) <<\

                                     32))

#define mi_uint8korr(A) ((ulonglong)(((uint32) (((uchar*) (A))[7])) +\

                                    (((uint32) (((uchar*) (A))[6])) << 8) +\

                                    (((uint32) (((uchar*) (A))[5])) << 16) +\

                                    (((uint32) (((uchar*) (A))[4])) << 24)) +\

                        (((ulonglong) (((uint32) (((uchar*) (A))[3])) +\

                                    (((uint32) (((uchar*) (A))[2])) << 8) +\

                                    (((uint32) (((uchar*) (A))[1])) << 16) +\

                                    (((uint32) (((uchar*) (A))[0])) << 24))) <<\

                                    32))

 

 

将整数转换成字符串:

/* This one is for uniformity */

#define mi_int1store(T,A) *((uchar*)(T))= (uchar) (A)

 

#define mi_int2store(T,A)   { uint def_temp= (uint) (A) ;\

                              ((uchar*)(T))[1]= (uchar)(def_temp);\

                              ((uchar*)(T))[0]= (uchar)(def_temp >> 8); }

#define mi_int3store(T,A)   { /*lint -save -e734 */\

                              ulong def_temp= (ulong) (A);\

                              ((uchar*) (T))[2]= (uchar) (def_temp);\

                              ((uchar*) (T))[1]= (uchar) (def_temp >> 8);\

                              ((uchar*) (T))[0]= (uchar) (def_temp >> 16);\

                              /*lint -restore */}

#define mi_int4store(T,A)   { ulong def_temp= (ulong) (A);\

                              ((uchar*) (T))[3]= (uchar) (def_temp);\

                              ((uchar*) (T))[2]= (uchar) (def_temp >> 8);\

                              ((uchar*) (T))[1]= (uchar) (def_temp >> 16);\

                              ((uchar*) (T))[0]= (uchar) (def_temp >> 24); }

#define mi_int5store(T,A)   { ulong def_temp= (ulong) (A),\

                              def_temp2= (ulong) ((A) >> 32);\

                              ((uchar*) (T))[4]= (uchar) (def_temp);\

                              ((uchar*) (T))[3]= (uchar) (def_temp >> 8);\

                              ((uchar*) (T))[2]= (uchar) (def_temp >> 16);\

                              ((uchar*) (T))[1]= (uchar) (def_temp >> 24);\

                              ((uchar*) (T))[0]= (uchar) (def_temp2); }

#define mi_int6store(T,A)   { ulong def_temp= (ulong) (A),\

                              def_temp2= (ulong) ((A) >> 32);\

                              ((uchar*) (T))[5]= (uchar) (def_temp);\

                              ((uchar*) (T))[4]= (uchar) (def_temp >> 8);\

                              ((uchar*) (T))[3]= (uchar) (def_temp >> 16);\

                              ((uchar*) (T))[2]= (uchar) (def_temp >> 24);\

                              ((uchar*) (T))[1]= (uchar) (def_temp2);\

                              ((uchar*) (T))[0]= (uchar) (def_temp2 >> 8); }

#define mi_int7store(T,A)   { ulong def_temp= (ulong) (A),\

                              def_temp2= (ulong) ((A) >> 32);\

                              ((uchar*) (T))[6]= (uchar) (def_temp);\

                              ((uchar*) (T))[5]= (uchar) (def_temp >> 8);\

                              ((uchar*) (T))[4]= (uchar) (def_temp >> 16);\

                              ((uchar*) (T))[3]= (uchar) (def_temp >> 24);\

                              ((uchar*) (T))[2]= (uchar) (def_temp2);\

                              ((uchar*) (T))[1]= (uchar) (def_temp2 >> 8);\

                              ((uchar*) (T))[0]= (uchar) (def_temp2 >> 16); }

#define mi_int8store(T,A)   { ulong def_temp3= (ulong) (A),\

                              def_temp4= (ulong) ((A) >> 32);\

                              mi_int4store((uchar*) (T) + 0, def_temp4);\

                              mi_int4store((uchar*) (T) + 4, def_temp3); }

 

posted on 2008-06-03 14:51  kevin.Tian  阅读(1891)  评论(0编辑  收藏  举报

导航