Main Page | Class Hierarchy | Class List | File List | Class Members

/Users/baford/proj/netsteria/sst/lib/chk32.h

00001 #ifndef SST_CHK32_H
00002 #define SST_CHK32_H
00003 
00004 #include <stdint.h>
00005 
00006 namespace SST {
00007 
00008 
00009 // Default initialization value for unkeyed use: b = 0, a = 1
00010 //#define CHK32_INIT    ((uint32_t)0x00000001)
00011 
00012 
00013 class Chk32
00014 {
00015 private:
00016         // Counter initialization values
00017         static const uint32_t InitA = 0;
00018         static const uint32_t InitB = 0;
00019 
00020         // Prime modulus for our wraparound checksum arithmetic
00021         static const int Modulus = 65537;
00022 
00023         // Number of words we can process before our 64-bit counters overflow.
00024         // Actually 23726746, but just to be safe...
00025         static const int MaxRun = 23726740;
00026 
00027         uint64_t a;
00028         uint64_t b;
00029         int run;
00030         union {
00031                 uint8_t b[2];
00032                 uint16_t w;
00033         } oddbuf;
00034         bool haveodd;
00035 
00036 public:
00037         inline Chk32() : a(InitA), b(InitB), run(MaxRun), haveodd(false) { }
00038 
00039         // Primitive init/update/final API for 
00040         inline void init16()
00041                 { a = InitA; b = InitB; run = MaxRun; haveodd = false; }
00042         void update16(const uint16_t *buf, int nwords);
00043         uint32_t final16();
00044 
00045         // Standard init/update/final API for byte streams
00046         inline void init() { init16(); }
00047         void update(const void *buf, size_t size);
00048         uint32_t final();
00049 
00050         // Compute a checksum over a single data block
00051         static inline uint32_t sum(const void *buf, size_t size) {
00052                 Chk32 c; c.update(buf, size); return c.final(); }
00053 };
00054 
00055 
00056 } // namespace SST
00057 
00058 #endif  // SST_CHK32_H

Generated on Wed Mar 28 11:48:05 2007 for SST by doxygen 1.3.4