Checksum.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "../GeneralInclude/CompilerSwitches.h"
00011 #include "../GeneralInclude/DebugSupport.h"
00012 #include "../GeneralInclude/Constants.h"
00013 #include "../Utilities/Checksum.h"
00014
00015 unsigned short doChecksum(unsigned char* item, unsigned int n) {
00016 assert( item!=pNULL );
00017 assert( n>0 );
00018
00019 C0 = 0;
00020 C1 = 0;
00021
00022 for (unsigned int i=0; i<n; i++) {
00023 C0 = C0 + (int)item[i];
00024 C1 = C1 + C0;
00025 }
00026
00027 CK1 = (unsigned char)(255-((C0+C1)%255));
00028 CK2 = (unsigned char)(C1%255);
00029
00030 if (CK1==0)
00031 CK1=255;
00032
00033 if (CK2==0)
00034 CK2=255;
00035
00036 return (CK1*256+CK2);
00037 }
00038
00039 bool verifyChecksum(unsigned char* item, unsigned int n, unsigned short checksum) {
00040 assert( item!=pNULL );
00041 assert( n>0 );
00042
00043 CK1 = (unsigned char)(checksum/256);
00044 CK2 = (unsigned char)(checksum%256);
00045
00046 if ( ((CK1==0)&&(CK2!=0)) || ((CK1!=0)&&(CK2==0)) )
00047 return false;
00048
00049 C0 = 0;
00050 C1 = 0;
00051
00052 for (unsigned int i=0; i<n; i++) {
00053 C0 = C0 + (unsigned short)item[i];
00054 C1 = C1 + C0;
00055 }
00056
00057 C0 = C0 + CK1;
00058 C1 = C1 + C0;
00059
00060 C0 = C0 + CK2;
00061 C1 = C1 + C0;
00062
00063 if ( ( (C0%255)==0) && ( (C1%255)==0) )
00064 return true;
00065 else
00066 return false;
00067 }
Copyright 2003 P&P Software GmbH - All Rights Reserved