00001 #ifndef UTILITY_IMPLEMENTATION_FILE
00002 #define UTILITY_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "earth_time.h"
00019 #include "istring.h"
00020 #include "utility.h"
00021
00022 #include <stdlib.h>
00023
00024
00025
00026
00027 const int HIGHEST_MOD_VALUE = 32014;
00028
00029 unsigned int utility::bizarre_checksum(const byte *data, int length)
00030 {
00031 int sum = 0;
00032 for (int i = 0; i < length; i++) sum += data[i] % 23 + i % 9;
00033 sum = (sum % (HIGHEST_MOD_VALUE - 1)) + 1;
00034 return (unsigned int)sum;
00035 }
00036
00037
00038
00039 uint16 utility::fletcher_checksum(const byte *data, int length)
00040 {
00041 int sum1 = 0;
00042 u_int sum2 = 0;
00043 const byte *buffer = data;
00044
00045 while (length--) {
00046 sum1 += int(*buffer++);
00047
00048 if (sum1 > 255) sum1++;
00049 sum1 &= 0xFF;
00050 sum2 += u_int(sum1);
00051 }
00052 if (sum1 == 255) sum1 = 0;
00053 unsigned int fletch = u_int(sum2 & 0xFF);
00054 fletch <<= 8;
00055 fletch |= u_int(sum1 & 0xFF);
00056
00057 return uint16(fletch);
00058 }
00059
00060 uint16 utility::rolling_fletcher_checksum(uint16 previous, const byte *data,
00061 int len)
00062 { return previous ^ fletcher_checksum(data, len); }
00063
00064 byte utility::byte_checksum(const byte *data, int length)
00065 {
00066 byte to_return = 0;
00067 for (int i = 0; i < length; i++) to_return += byte(data[i]);
00068 return to_return;
00069 }
00070
00071 u_int utility::short_checksum(const byte *data, int length)
00072 {
00073 u_int to_return = 0;
00074 for (int i = 0; i < length; i++) to_return += data[i];
00075 return to_return;
00076 }
00077
00078 istring timestamp(bool add_space, bool add_date)
00079 { return utility::timestamp(add_space, add_date); }
00080
00081 istring utility::timestamp(bool add_space, bool add_date)
00082 {
00083 using namespace earth_time;
00084 const time_locus the_time = now();
00085 istring to_return;
00086 if (add_date)
00087 the_time.text_form(to_return,
00088 clock_time::MILITARY | clock_time::MILLISECONDS);
00089 else
00090 the_time.clock_time::text_form(to_return, clock_time::MILITARY
00091 | clock_time::MILLISECONDS);
00092 if (add_space)
00093 to_return += " ";
00094 return to_return;
00095 }
00096
00097 u_int utility::hash_bytes(const void *key_data, int key_length)
00098 {
00099 if (!key_data) return 0;
00100 if (!key_length) return 0;
00101
00102 byte *our_key = (byte *)key_data;
00103 byte hashed[4] = { 0, 0, 0, 0 };
00104
00105 int fill_posn = 0;
00106 for (int i = 0; i < key_length; i++) {
00107
00108 hashed[fill_posn] = hashed[fill_posn] + our_key[i];
00109 fill_posn++;
00110 if (fill_posn >= 4) fill_posn = 0;
00111
00112 hashed[fill_posn] = hashed[fill_posn] + (our_key[i] / 4);
00113 }
00114
00115 u_int to_return = 0;
00116 for (int j = 0; j < 4; j++) to_return = (to_return << 8) + hashed[j];
00117 return to_return;
00118 }
00119
00120
00121 #endif //UTILITY_IMPLEMENTATION_FILE
00122