]> git.ozlabs.org Git - ccan/blob - ccan/crcsync/_info.c
Slight cleanup for crcsync.c
[ccan] / ccan / crcsync / _info.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * crcsync - routines to use crc for an rsync-like protocol.
7  *
8  * This is a complete library for synchronization using a variant of the
9  * rsync protocol.
10  *
11  * Example:
12  *      // Calculate checksums of file (3-arg mode)
13  *      // Or print differences between file and checksums (4+ arg mode)
14  *      #include <ccan/crcsync/crcsync.h>
15  *      #include <ccan/grab_file/grab_file.h>
16  *      #include <stdio.h>
17  *      #include <stdlib.h>
18  *      #include <err.h>
19  *      
20  *      static void print_result(long result)
21  *      {
22  *              if (result < 0)
23  *                      printf("MATCHED CRC %lu\n", -result - 1);
24  *              else if (result > 0)
25  *                      printf("%lu literal bytes\n", result);
26  *      }
27  *      
28  *      int main(int argc, char *argv[])
29  *      {
30  *              size_t len, used, blocksize;
31  *              char *file;
32  *              struct crc_context *ctx;
33  *              uint32_t *crcs;
34  *              long res, i;
35  *      
36  *              if (argc < 3 || (blocksize = atoi(argv[1])) == 0)
37  *                      errx(1, "Usage: %s <blocksize> <file> <crc>...\n"
38  *                           "OR: %s <blocksize> <file>", argv[0], argv[0]);
39  *      
40  *              file = grab_file(NULL, argv[2], &len);
41  *              if (!file)
42  *                      err(1, "Opening file %s", argv[2]);
43  *      
44  *              if (argc == 3) {
45  *                      // Short form prints CRCs of file for use in long form.
46  *                      used = (len + blocksize - 1) / blocksize;
47  *                      crcs = malloc(used * sizeof(uint32_t));
48  *                      crc_of_blocks(file, len, blocksize, 32, crcs);
49  *                      for (i = 0; i < used; i++)
50  *                              printf("%i ", crcs[i]);
51  *                      printf("\n");
52  *                      return 0;
53  *              }
54  *      
55  *              crcs = malloc((argc - 3) * sizeof(uint32_t));
56  *              for (i = 0; i < argc-3; i++)
57  *                      crcs[i] = atoi(argv[3+i]);
58  *      
59  *              ctx = crc_context_new(blocksize, 32, crcs, argc-3);
60  *              for (used = 0; used < len; ) {
61  *                      used += crc_read_block(ctx, &res, file+used, len-used);
62  *                      print_result(res);
63  *              }
64  *              while ((res = crc_read_flush(ctx)) != 0)
65  *                      print_result(res);
66  *      
67  *              return 0;
68  *      }
69  *
70  * Licence: LGPL (v2 or any later version)
71  */
72 int main(int argc, char *argv[])
73 {
74         if (argc != 2)
75                 return 1;
76
77         if (strcmp(argv[1], "depends") == 0) {
78                 printf("ccan/crc\n");
79                 return 0;
80         }
81
82         return 1;
83 }