]> arthur.barton.de Git - bup.git/blob - hsplit.c
Extremely cheesy initial implementation of rolling-sum-based splitting.
[bup.git] / hsplit.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <assert.h>
4 #include <memory.h>
5
6 #define BLOBBITS (14)
7 #define BLOBSIZE (1<<(BLOBBITS-1))
8 #define WINDOWBITS (7)
9 #define WINDOWSIZE (1<<(WINDOWBITS-1))
10
11
12 static uint32_t rol(uint32_t v, unsigned bits)
13 {
14     bits = bits % 32;
15     return (v << bits) | (v >> (32-bits));
16 }
17
18
19 static uint32_t stupidsum_add(uint32_t old, uint8_t drop, uint8_t add)
20 {
21     return ((old<<1) | (old>>31)) ^ drop ^ add;
22 }
23
24
25 int main()
26 {
27     printf("hello world\n");
28     assert(rol(1,0) == 1);
29     assert(rol(1,1) == 2);
30     assert(rol(1,32) == 1);
31     assert(rol(1,33) == 2);
32     assert(rol(0x12345678, 16) == 0x56781234);
33     assert(rol(0x12345678, 34) == 0x48d159e0);
34     assert(rol(0x92345678, 34) == 0x48d159e2);
35     assert(WINDOWSIZE >= 32);
36     assert(BLOBSIZE >= 32);
37     
38     uint8_t buf[WINDOWSIZE];
39     uint32_t sum = 0;
40     int i = 0, count = 0, c;
41     
42     memset(buf, 0, sizeof(buf));
43     
44     while ((c = fgetc(stdin)) != EOF)
45     {
46         sum = stupidsum_add(sum, buf[i], c);
47         buf[i] = c;
48         
49         int cprint = (c >= 32 && c <= 126) ? c : '.';
50         printf("[%05X] %02X '%c' %08X\n", i, c, cprint, sum);
51         
52         i = (i + 1) % WINDOWSIZE;
53         count++;
54         
55         if ((sum & (BLOBSIZE-1)) == 0)
56         {
57             printf("\nSPLIT @ %d (%d)\n\n", count, BLOBSIZE);
58             count = i = 0;
59             memset(buf, 0, sizeof(buf));
60         }
61     }
62     
63     return 0;
64 }