home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / myisam / mi_checksum.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  2KB  |  68 lines

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16.  
  17. /* Calculate a checksum for a row */
  18.  
  19. #include "myisamdef.h"
  20.  
  21. ha_checksum mi_checksum(MI_INFO *info, const byte *buf)
  22. {
  23.   uint i;
  24.   ha_checksum crc=0;
  25.   MI_COLUMNDEF *rec=info->s->rec;
  26.  
  27.   for (i=info->s->base.fields ; i-- ; buf+=(rec++)->length)
  28.   {
  29.     const byte *pos;
  30.     const byte *end;
  31.     switch (rec->type) {
  32.     case FIELD_BLOB:
  33.     {
  34.       ulong length=_mi_calc_blob_length(rec->length-
  35.                     mi_portable_sizeof_char_ptr,
  36.                     buf);
  37.       memcpy((char*) &pos, buf+rec->length- mi_portable_sizeof_char_ptr,
  38.          sizeof(char*));
  39.       end=pos+length;
  40.       break;
  41.     }
  42.     case FIELD_VARCHAR:
  43.     {
  44.       uint length;
  45.       length=uint2korr(buf);
  46.       pos=buf+2; end=pos+length;
  47.       break;
  48.     }
  49.     default:
  50.       pos=buf; end=buf+rec->length;
  51.       break;
  52.     }
  53.     for ( ; pos != end ; pos++)
  54.       crc=((crc << 8) + *((uchar*) pos)) + (crc >> (8*sizeof(ha_checksum)-8));
  55.   }
  56.   return crc;
  57. }
  58.  
  59.  
  60. ha_checksum mi_static_checksum(MI_INFO *info, const byte *pos)
  61. {
  62.   ha_checksum crc;
  63.   const byte *end=pos+info->s->base.reclength;
  64.   for (crc=0; pos != end; pos++)
  65.       crc=((crc << 8) + *((uchar*) pos)) + (crc >> (8*sizeof(ha_checksum)-8));
  66.   return crc;
  67. }
  68.