1/* Decode Huffman string -- use for benchmarking
2 *
3 * Usage: huff-decode $file $count $mode
4 *
5 * $mode is either "fast" or "slow"
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#ifndef LS_HPACK_USE_LARGE_TABLES
13#define LS_HPACK_USE_LARGE_TABLES 1
14#endif
15
16#if LS_HPACK_USE_LARGE_TABLES
17int
18lshpack_dec_huff_decode_full (const unsigned char *src, int src_len,
19                                    unsigned char *dst, int dst_len);
20#endif
21
22int
23lshpack_dec_huff_decode (const unsigned char *src, int src_len,
24                                    unsigned char *dst, int dst_len);
25
26int
27main (int argc, char **argv)
28{
29    size_t in_sz;
30    int count, i, rv;
31    FILE *in;
32    int (*decode)(const unsigned char *, int, unsigned char *, int);
33    unsigned char in_buf[0x1000];
34    unsigned char out_buf[0x4000];
35
36    if (argc != 4)
37    {
38        fprintf(stderr, "Usage: %s $file $count $mode\n", argv[0]);
39        exit(EXIT_FAILURE);
40    }
41
42    if (strcasecmp(argv[3], "slow") == 0)
43#if LS_HPACK_USE_LARGE_TABLES
44        decode = lshpack_dec_huff_decode_full;
45#else
46        decode = lshpack_dec_huff_decode;
47#endif
48    else if (strcasecmp(argv[3], "fast") == 0)
49#if LS_HPACK_USE_LARGE_TABLES
50        decode = lshpack_dec_huff_decode;
51#else
52    {
53        fprintf(stderr, "support for fast decoder is compiled out\n");
54        exit(EXIT_FAILURE);
55    }
56#endif
57    else
58    {
59        fprintf(stderr, "Mode `%s' is invalid.  Specify either `slow' or "
60                                                        "`fast'.\n", argv[3]);
61        exit(EXIT_FAILURE);
62    }
63
64    in = fopen(argv[1], "rb");
65    if (!in)
66    {
67        perror("fopen");
68        exit(EXIT_FAILURE);
69    }
70
71    in_sz = fread(in_buf, 1, sizeof(in_buf), in);
72    if (in_sz == 0 || in_sz == sizeof(in_buf))
73    {
74        fprintf(stderr, "input file is either too short or too long\n");
75        exit(EXIT_FAILURE);
76    }
77    (void) fclose(in);
78
79    count = atoi(argv[2]);
80    if (!count)
81        count = 1;
82
83    /* TODO: validate against slow if fast is selected */
84
85    rv = decode(in_buf, in_sz, out_buf, sizeof(out_buf));
86    if (rv < 0)
87    {
88        fprintf(stderr, "decode-%s returned %d\n", argv[3], rv);
89        exit(EXIT_FAILURE);
90    }
91
92    for (i = 0; i < count; ++i)
93    {
94        rv = decode(in_buf, in_sz, out_buf, sizeof(out_buf));
95        (void) rv;
96    }
97
98    exit(EXIT_SUCCESS);
99}
100