removed some of the debug logging and added author details
[httpd-crcsyncproxy.git] / crccache / crccache.c
blobec19babdaee0b6db2bb692fc5e0165c08764814e
1 /*
2 * crccache.c
4 * Created on: 22/02/2009
5 * Author: Toby Collett
6 */
8 #include <string.h>
9 #include <apr_base64.h>
11 /* fills in the first 5 bytes of target with the base64 bit encoded hash
13 it is endian safe, and adds a null terminator, so target must have space for 6 bytes
15 char * encode_30bithash(unsigned hash, char * target)
17 char temp_result[9];
18 unsigned char source[4];
19 // possibly a faster way, but needs to be endian safe, including bit endianness
20 source[0]=(hash&0x3fc00000)>>22;
21 source[1]=(hash&0x003fc000)>>14;
22 source[2]=(hash&0x00003fc0)>>6;
23 source[3]=(hash&0x0000003f)<<2;
25 apr_base64_encode (temp_result, (void *)source, 4);
26 memcpy(target,temp_result,5);
27 target[5]='\0';
28 return target;
32 /* decodes a 5 bytes base 64bit string to the lower 30 bits of an int
34 it is endian safe, and assumes an input string of 5 bytes
36 unsigned decode_30bithash(const char * source)
38 char temp_source[6];
39 unsigned char target[7];
40 memcpy(temp_source,source,5);
41 temp_source[5]='0';
42 temp_source[6]='0';
43 temp_source[7]='0';
44 temp_source[8]='\0';
46 apr_base64_decode ((void*)target, temp_source);
47 unsigned result;
48 result = target[0] << 22;
49 result |= target[1] << 14;
50 result |= target[2] << 6;
51 result |= target[3] >> 2;
53 return result;