#include #include #define RSYNC_WIN 4096 struct rsync_state { uint64_t n; /* number of elements in the window */ uint64_t sum; /* current sum */ unsigned char win[RSYNC_WIN]; /* window elements */ }; static inline bool rsync_next(struct rsync_state *s, unsigned char c) { if (s->n < RSYNC_WIN) { /* not enough elements */ s->sum += c; /* update the sum */ s->win[s->n++] = c; /* remember the element */ return false; /* no match */ } int i = s->n++ % RSYNC_WIN; /* wrap up */ s->sum -= s->win[i]; /* move the window on */ s->sum += c; s->win[i] = c; if (s->sum % RSYNC_WIN == 0) { /* match */ s->n = 0; /* reset */ s->sum = 0; return true; } return false; } #include int main() { struct rsync_state rs = { 0, 0 }; int c; while ((c = getc(stdin)) != EOF) if (rsync_next(&rs, c)) printf("sync at %ld\n", ftell(stdin)); return 0; }