feisty meow concerns codebase 2.140
checksums.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : checksums group *
4* Authors: Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 1992-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
15#include "checksums.h"
16
17#include <basis/definitions.h>
18
19using namespace basis;
20
21namespace structures {
22
23const int HIGHEST_MOD_VALUE = 32014;
24
25unsigned int checksums::bizarre_checksum(const abyte *data, int length)
26{
27 int sum = 0;
28 for (int i = 0; i < length; i++) sum += data[i] % 23 + i % 9;
29 sum = (sum % (HIGHEST_MOD_VALUE - 1)) + 1;
30 return (unsigned int)sum;
31}
32
33// fletcher checksum is from Dr. Dobbs Journal May 1992, pp. 32-38.
34
36{
37 int sum1 = 0;
38 basis::un_int sum2 = 0;
39 const abyte *buffer = data;
40
41 while (length--) {
42 sum1 += int(*buffer++);
43 // check for overflow into high byte.
44 if (sum1 > 255) sum1++;
45 sum1 &= 0xFF; // remove any bits in high byte for next sum.
46 sum2 += basis::un_int(sum1);
47 }
48 if (sum1 == 255) sum1 = 0;
49 unsigned int fletch = basis::un_int(sum2 & 0xFF);
50 fletch <<= 8;
51 fletch |= basis::un_int(sum1 & 0xFF);
52
53 return basis::un_short(fletch);
54}
55
57 int len)
58{ return previous ^ fletcher_checksum(data, len); }
59
60abyte checksums::byte_checksum(const abyte *data, int length)
61{
62 abyte to_return = 0;
63 for (int i = 0; i < length; i++) to_return += abyte(data[i]);
64 return to_return;
65}
66
68{
69 basis::un_int to_return = 0;
70 for (int i = 0; i < length; i++) to_return += data[i];
71 return to_return;
72}
73
74basis::un_int checksums::hash_bytes(const void *key_data, int key_length)
75{
76 if (!key_data) return 0; // error!
77 if (!key_length) return 0; // ditto!
78
79 abyte *our_key = (abyte *)key_data;
80 abyte hashed[4] = { 0, 0, 0, 0 };
81
82 int fill_posn = 0;
83 for (int i = 0; i < key_length; i++) {
84 // add to the primary area.
85 hashed[fill_posn] = hashed[fill_posn] + our_key[i];
86 fill_posn++;
87 if (fill_posn >= 4) fill_posn = 0;
88 // add to the secondary area (the next in rotation after primary).
89 hashed[fill_posn] = hashed[fill_posn] + (our_key[i] / 4);
90 }
91
92 basis::un_int to_return = 0;
93 for (int j = 0; j < 4; j++) to_return = (to_return << 8) + hashed[j];
94 return to_return;
95}
96
97} //namespace.
98
static basis::un_int short_checksum(const basis::abyte *data, int length)
simple shorty checksum based on additive roll-over.
Definition checksums.cpp:67
static basis::un_short rolling_fletcher_checksum(basis::un_short previous, const basis::abyte *data, int len)
Fletcher checksums applied to streaming data.
Definition checksums.cpp:56
static basis::abyte byte_checksum(const basis::abyte *data, int length)
simple byte-sized checksum based on additive roll-over.
Definition checksums.cpp:60
static unsigned int bizarre_checksum(const basis::abyte *data, int length)
A different type of checksum with somewhat unknown properties.
Definition checksums.cpp:25
static basis::un_int hash_bytes(const void *key_data, int key_length)
returns a value that can be used for indexing into a hash table.
Definition checksums.cpp:74
static basis::un_short fletcher_checksum(const basis::abyte *data, int length)
A positionally computed error detection value.
Definition checksums.cpp:35
Constants and objects used throughout HOOPLE.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition definitions.h:51
unsigned int un_int
Abbreviated name for unsigned integers.
Definition definitions.h:62
unsigned short un_short
Abbreviated name for unsigned short integers.
Definition definitions.h:64
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
const int HIGHEST_MOD_VALUE
Definition checksums.cpp:23