]> mj.ucw.cz Git - libucw.git/blob - lib/lizard-test.c
22fc96e49a60cf69a4f7976c3d2bf5b9f8e7742c
[libucw.git] / lib / lizard-test.c
1 #include "lib/lib.h"
2 #include "lib/conf2.h"
3 #include "lib/fastbuf.h"
4 #include "lib/lizard.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <getopt.h>
10 #include <sys/user.h>
11
12 static char *options = CF_SHORT_OPTS "cdtx";
13 static char *help = "\
14 Usage: lizard-test <options> input-file [output-file]\n\
15 \n\
16 Options:\n"
17 CF_USAGE
18 "-c\t\tCompress\n\
19 -d\t\tDecompress\n\
20 -t\t\tCompress, decompress, and compare (in memory only, default)\n\
21 -x\t\tLet the test crash by shrinking the output buffer\n\
22 ";
23
24 static void NONRET
25 usage(void)
26 {
27   fputs(help, stderr);
28   exit(1);
29 }
30
31 int
32 main(int argc, char **argv)
33 {
34   int opt;
35   uns action = 't';
36   uns crash = 0;
37   log_init(argv[0]);
38   while ((opt = cf_get_opt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0)
39     switch (opt)
40     {
41       case 'c':
42       case 'd':
43       case 't':
44         action = opt;
45         break;
46       case 'x':
47         crash++;
48         break;
49       default:
50         usage();
51     }
52   if (action == 't' && argc != optind+1
53   || action != 't' && argc != optind+2)
54     usage();
55
56   void *mi, *mo;
57   int li, lo;
58   uns adler = 0;
59
60   struct stat st;
61   stat(argv[optind], &st);
62   li = st.st_size;
63   struct fastbuf *fi = bopen(argv[optind], O_RDONLY, 1<<16);
64   if (action != 'd')
65   {
66     lo = li * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
67     li += LIZARD_NEEDS_CHARS;
68   }
69   else
70   {
71     lo = bgetl(fi);
72     adler = bgetl(fi);
73     li -= 8;
74   }
75   mi = xmalloc(li);
76   mo = xmalloc(lo);
77   li = bread(fi, mi, li);
78   bclose(fi);
79
80   printf("%d ", li);
81   if (action == 'd')
82     printf("->expected %d (%08x) ", lo, adler);
83   fflush(stdout);
84   if (action != 'd')
85     lo = lizard_compress(mi, li, mo);
86   else
87   {
88     lo = lizard_decompress(mi, mo);
89     if (adler32(mo, lo) != adler)
90       printf("wrong Adler32 ");
91   }
92   printf("-> %d ", lo);
93   fflush(stdout);
94
95   if (action != 't')
96   {
97     struct fastbuf *fo = bopen(argv[optind+1], O_CREAT | O_TRUNC | O_WRONLY, 1<<16);
98     if (action == 'c')
99     {
100       bputl(fo, li);
101       bputl(fo, adler32(mi, li));
102     }
103     bwrite(fo, mo, lo);
104     bclose(fo);
105   }
106   else
107   {
108     int smaller_li;
109     if (li >= (int) PAGE_SIZE)
110       smaller_li = li - PAGE_SIZE;
111     else
112       smaller_li = 0;
113     struct lizard_buffer *buf = lizard_alloc();
114     byte *ptr = lizard_decompress_safe(mo, buf, crash ? smaller_li : li);
115     if (!ptr)
116       printf("err: %m");
117     else if (memcmp(mi, ptr, li))
118       printf("WRONG");
119     else
120       printf("OK");
121     lizard_free(buf);
122   }
123   printf("\n");
124 }