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