fuzzing: dump test patterns to files for initial corpus
[platform/upstream/libaec.git] / tests / check_aec.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "check_aec.h"
5
6 static void out_lsb(unsigned char *dest, unsigned long long int val, int size)
7 {
8     int i;
9
10     for (i = 0; i < size; i++)
11         dest[i] = (unsigned char)(val >> (8 * i));
12 }
13
14 static void out_msb(unsigned char *dest, unsigned long long int val, int size)
15 {
16     int i;
17
18     for (i = 0; i < size; i++)
19         dest[i] = (unsigned char)(val >> (8 * (size - 1 - i)));
20 }
21
22 int update_state(struct test_state *state)
23 {
24     struct aec_stream *strm = state->strm;
25
26     if (strm->bits_per_sample > 16) {
27         state->id_len = 5;
28
29         if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
30             state->bytes_per_sample = 3;
31         } else {
32             state->bytes_per_sample = 4;
33         }
34     }
35     else if (strm->bits_per_sample > 8) {
36         state->id_len = 4;
37         state->bytes_per_sample = 2;
38     } else {
39         state->id_len = 3;
40         state->bytes_per_sample = 1;
41     }
42
43     if (strm->flags & AEC_DATA_MSB)
44         state->out = out_msb;
45     else
46         state->out = out_lsb;
47
48     if (strm->flags & AEC_DATA_SIGNED) {
49         state->xmin = -(1LL << (strm->bits_per_sample - 1));
50         state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
51     } else {
52         state->xmin = 0;
53         state->xmax = (1ULL << strm->bits_per_sample) - 1;
54     }
55
56     return 0;
57 }
58
59 int encode_decode_small(struct test_state *state)
60 {
61     int status, i;
62     size_t compressed_size;
63     size_t n_in, avail_in, avail_out, total_out;
64     struct aec_stream *strm = state->strm;
65
66     status = aec_encode_init(strm);
67     if (status != AEC_OK) {
68         printf("Init failed.\n");
69         return 99;
70     }
71
72     n_in = 0;
73     avail_in = 1;
74     avail_out = 1;
75     total_out = 0;
76     strm->next_in = state->ubuf;
77     strm->avail_in = state->bytes_per_sample;
78     strm->avail_out = 1;
79     strm->next_out = state->cbuf;
80
81     while ((avail_in || avail_out) && total_out < state->cbuf_len) {
82         if (strm->avail_in == 0 && avail_in) {
83             n_in += state->bytes_per_sample;
84             if (n_in < state->buf_len) {
85                 strm->avail_in = state->bytes_per_sample;
86                 strm->next_in = state->ubuf + n_in;
87             } else {
88                 avail_in = 0;
89             }
90         }
91
92         status = aec_encode(strm, AEC_NO_FLUSH);
93         if (status != AEC_OK) {
94             printf("Decode failed.\n");
95             return 99;
96         }
97
98         if (strm->total_out - total_out > 0
99             && total_out < state->cbuf_len) {
100             total_out = strm->total_out;
101             strm->avail_out = 1;
102             strm->next_out = state->cbuf + total_out;
103             avail_out = 1;
104         } else {
105             avail_out = 0;
106         }
107     }
108
109     status = aec_encode(strm, AEC_FLUSH);
110     if (status != AEC_OK) {
111         printf("Encode failed.\n");
112         return 99;
113     }
114
115     aec_encode_end(strm);
116
117     compressed_size = strm->total_out;
118
119     strm->avail_in = 1;
120     strm->next_in = state->cbuf;
121
122     strm->avail_out = state->bytes_per_sample;
123     strm->next_out = state->obuf;
124
125     status = aec_decode_init(strm);
126     if (status != AEC_OK) {
127         printf("Init failed.\n");
128         return 99;
129     }
130
131     n_in = 0;
132     avail_in = 1;
133     avail_out = 1;
134     total_out = 0;
135     strm->next_in = state->cbuf;
136     strm->avail_in = 1;
137     strm->avail_out = state->bytes_per_sample;
138     strm->next_out = state->obuf;
139
140     while ((avail_in || avail_out) && total_out < state->buf_len) {
141         if (strm->avail_in == 0 && avail_in) {
142             n_in++;
143             if (n_in < compressed_size) {
144                 strm->avail_in = 1;
145                 strm->next_in = state->cbuf + n_in;
146             } else {
147                 avail_in = 0;
148             }
149         }
150
151         status = aec_decode(strm, AEC_NO_FLUSH);
152         if (status != AEC_OK) {
153             printf("Decode failed.\n");
154             return 99;
155         }
156
157         if (strm->total_out - total_out > 0
158             && total_out < state->buf_len) {
159             total_out = strm->total_out;
160             strm->avail_out = state->bytes_per_sample;
161             strm->next_out = state->obuf + total_out;
162             avail_out = 1;
163         } else {
164             avail_out = 0;
165         }
166     }
167
168     status = aec_decode(strm, AEC_FLUSH);
169     if (status != AEC_OK) {
170         printf("Decode failed.\n");
171         return 99;
172     }
173
174     if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
175         printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
176
177         printf("\nuncompressed buf");
178         for (i = 0; i < 80; i++) {
179             if (i % 8 == 0)
180                 printf("\n");
181             printf("%02x ", state->ubuf[i]);
182         }
183         printf("\n\ncompressed buf len %li", compressed_size);
184         for (i = 0; i < 80; i++) {
185             if (i % 8 == 0)
186                 printf("\n");
187             printf("%02x ", state->cbuf[i]);
188         }
189         printf("\n\ndecompressed buf");
190         for (i = 0; i < 80; i++) {
191             if (i % 8 == 0)
192                 printf("\n%04i ", i);
193             printf("%02x ", state->obuf[i]);
194         }
195         printf("\n");
196         return 99;
197     }
198     aec_decode_end(strm);
199     return 0;
200 }
201
202 int encode_decode_large(struct test_state *state)
203 {
204     int status, i;
205     char fbase[1024];
206     char fname[1024];
207     FILE *fp;
208     int bflags;
209     size_t to;
210     struct aec_stream *strm = state->strm;
211
212     strm->avail_in = state->ibuf_len;
213     strm->avail_out = state->cbuf_len;
214     strm->next_in = state->ubuf;
215     strm->next_out = state->cbuf;
216
217     status = aec_encode_init(strm);
218     if (status != AEC_OK) {
219         printf("Init failed.\n");
220         return 99;
221     }
222     if (state->dump) {
223         snprintf(fbase, sizeof(fbase), "BPS%02iID%iBS%02iRSI%04iFLG%04i",
224                  strm->bits_per_sample,
225                  state->id,
226                  strm->block_size,
227                  strm->rsi,
228                  strm->flags);
229         snprintf(fname, sizeof(fname), "%s.dat", fbase);
230         if ((fp = fopen(fname, "wb")) == NULL) {
231             fprintf(stderr, "ERROR: cannot open dump file %s\n", fname);
232             return 99;
233         }
234         fputc(strm->bits_per_sample, fp);
235         bflags = strm->block_size >> 8;
236         if (strm->flags | AEC_DATA_MSB)
237             bflags |= 0x80;
238         if (strm->flags | AEC_DATA_SIGNED)
239             bflags |= 0x40;
240         if (strm->flags | AEC_DATA_3BYTE)
241             bflags |= 0x10;
242         bflags |= 0x20; /* encode */
243         fputc(bflags, fp);
244         fwrite(strm->next_in, strm->avail_in, 1, fp);
245         fclose(fp);
246     }
247
248     status = aec_encode(strm, AEC_FLUSH);
249     if (status != AEC_OK) {
250         printf("Encode failed.\n");
251         return 99;
252     }
253
254     aec_encode_end(strm);
255
256     if (state->dump) {
257         snprintf(fname, sizeof(fname), "%s.rz", fbase);
258         if ((fp = fopen(fname, "wb")) == NULL) {
259             fprintf(stderr, "ERROR: cannot open dump file %s\n", fname);
260             return 99;
261         }
262         fputc(strm->bits_per_sample, fp);
263         bflags &= ~0x20;
264         fputc(bflags, fp);
265         fwrite(state->cbuf, strm->total_out, 1, fp);
266         fclose(fp);
267     }
268
269     strm->avail_in = strm->total_out;
270     strm->avail_out = state->buf_len;
271     strm->next_in = state->cbuf;
272     strm->next_out = state->obuf;
273     to = strm->total_out;
274
275     status = aec_decode_init(strm);
276     if (status != AEC_OK) {
277         printf("Init failed.\n");
278         return 99;
279     }
280
281     status = aec_decode(strm, AEC_FLUSH);
282     if (status != AEC_OK) {
283         printf("Decode failed.\n");
284         return 99;
285     }
286
287     if (memcmp(state->ubuf, state->obuf, state->ibuf_len)) {
288         printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL);
289
290         printf("\nuncompressed buf");
291         for (i = 0; i < 80; i++) {
292             if (i % 8 == 0)
293                 printf("\n");
294             printf("%02x ", state->ubuf[i]);
295         }
296         printf("\n\ncompressed buf len %li", to);
297         for (i = 0; i < 80; i++) {
298             if (i % 8 == 0)
299                 printf("\n");
300             printf("%02x ", state->cbuf[i]);
301         }
302         printf("\n\ndecompressed buf");
303         for (i = 0; i < 80; i++) {
304             if (i % 8 == 0)
305                 printf("\n");
306             printf("%02x ", state->obuf[i]);
307         }
308         printf("\n");
309         return 99;
310     }
311     aec_decode_end(strm);
312     return 0;
313 }