Drizzled Public API Documentation

mi_checksum.cc
00001 /* Copyright (C) 2000-2001, 2003-2004 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* Calculate a checksum for a row */
00017 
00018 #include "myisam_priv.h"
00019 #include <zlib.h>
00020 
00021 using namespace drizzled;
00022 
00023 /*
00024   Calculate a long checksum for a memoryblock.
00025 
00026   SYNOPSIS
00027     my_checksum()
00028       crc       start value for crc
00029       pos       pointer to memory block
00030       length    length of the block
00031 */
00032 
00033 static ha_checksum my_checksum(ha_checksum crc, const unsigned char *pos, size_t length)
00034 {
00035   return ha_checksum(crc32((uint32_t)crc, pos, uInt(length)));
00036 }
00037 
00038 ha_checksum mi_checksum(MI_INFO *info, const unsigned char *buf)
00039 {
00040   uint32_t i;
00041   ha_checksum crc=0;
00042   MI_COLUMNDEF *rec=info->s->rec;
00043 
00044   for (i=info->s->base.fields ; i-- ; buf+=(rec++)->length)
00045   {
00046     const unsigned char *pos;
00047     ulong length;
00048     switch (rec->type) {
00049     case FIELD_BLOB:
00050     {
00051       length=_mi_calc_blob_length(rec->length-
00052           portable_sizeof_char_ptr,
00053           buf);
00054       memcpy(&pos, buf+rec->length - portable_sizeof_char_ptr, sizeof(char*));
00055       break;
00056     }
00057     case FIELD_VARCHAR:
00058     {
00059       uint32_t pack_length= ha_varchar_packlength(rec->length-1);
00060       if (pack_length == 1)
00061         length= (ulong) *(unsigned char*) buf;
00062       else
00063         length= uint2korr(buf);
00064       pos= buf+pack_length;
00065       break;
00066     }
00067     default:
00068       length=rec->length;
00069       pos=buf;
00070       break;
00071     }
00072     crc=my_checksum(crc, pos ? pos : (unsigned char*) "", length);
00073   }
00074   return crc;
00075 }
00076 
00077 
00078 ha_checksum mi_static_checksum(MI_INFO *info, const unsigned char *pos)
00079 {
00080   return my_checksum(0, pos, info->s->base.reclength);
00081 }