16296d357SDmitri Tikhonov/* Decode Huffman string -- use for benchmarking
26296d357SDmitri Tikhonov *
36296d357SDmitri Tikhonov * Usage: huff-decode $file $count $mode
46296d357SDmitri Tikhonov *
56296d357SDmitri Tikhonov * $mode is either "fast" or "slow"
66296d357SDmitri Tikhonov */
76296d357SDmitri Tikhonov
86296d357SDmitri Tikhonov#include <stdio.h>
96296d357SDmitri Tikhonov#include <stdlib.h>
106296d357SDmitri Tikhonov#include <string.h>
116296d357SDmitri Tikhonov
122219c75eSDmitri Tikhonov#ifndef LS_HPACK_USE_LARGE_TABLES
132219c75eSDmitri Tikhonov#define LS_HPACK_USE_LARGE_TABLES 1
142219c75eSDmitri Tikhonov#endif
152219c75eSDmitri Tikhonov
162219c75eSDmitri Tikhonov#if LS_HPACK_USE_LARGE_TABLES
176296d357SDmitri Tikhonovint
186296d357SDmitri Tikhonovlshpack_dec_huff_decode_full (const unsigned char *src, int src_len,
196296d357SDmitri Tikhonov                                    unsigned char *dst, int dst_len);
202219c75eSDmitri Tikhonov#endif
216296d357SDmitri Tikhonov
226296d357SDmitri Tikhonovint
236296d357SDmitri Tikhonovlshpack_dec_huff_decode (const unsigned char *src, int src_len,
246296d357SDmitri Tikhonov                                    unsigned char *dst, int dst_len);
256296d357SDmitri Tikhonov
266296d357SDmitri Tikhonovint
276296d357SDmitri Tikhonovmain (int argc, char **argv)
286296d357SDmitri Tikhonov{
296296d357SDmitri Tikhonov    size_t in_sz;
306296d357SDmitri Tikhonov    int count, i, rv;
316296d357SDmitri Tikhonov    FILE *in;
326296d357SDmitri Tikhonov    int (*decode)(const unsigned char *, int, unsigned char *, int);
336296d357SDmitri Tikhonov    unsigned char in_buf[0x1000];
346296d357SDmitri Tikhonov    unsigned char out_buf[0x4000];
356296d357SDmitri Tikhonov
366296d357SDmitri Tikhonov    if (argc != 4)
376296d357SDmitri Tikhonov    {
386296d357SDmitri Tikhonov        fprintf(stderr, "Usage: %s $file $count $mode\n", argv[0]);
396296d357SDmitri Tikhonov        exit(EXIT_FAILURE);
406296d357SDmitri Tikhonov    }
416296d357SDmitri Tikhonov
426296d357SDmitri Tikhonov    if (strcasecmp(argv[3], "slow") == 0)
432219c75eSDmitri Tikhonov#if LS_HPACK_USE_LARGE_TABLES
446296d357SDmitri Tikhonov        decode = lshpack_dec_huff_decode_full;
452219c75eSDmitri Tikhonov#else
462219c75eSDmitri Tikhonov        decode = lshpack_dec_huff_decode;
472219c75eSDmitri Tikhonov#endif
486296d357SDmitri Tikhonov    else if (strcasecmp(argv[3], "fast") == 0)
492219c75eSDmitri Tikhonov#if LS_HPACK_USE_LARGE_TABLES
506296d357SDmitri Tikhonov        decode = lshpack_dec_huff_decode;
512219c75eSDmitri Tikhonov#else
522219c75eSDmitri Tikhonov    {
532219c75eSDmitri Tikhonov        fprintf(stderr, "support for fast decoder is compiled out\n");
542219c75eSDmitri Tikhonov        exit(EXIT_FAILURE);
552219c75eSDmitri Tikhonov    }
562219c75eSDmitri Tikhonov#endif
576296d357SDmitri Tikhonov    else
586296d357SDmitri Tikhonov    {
596296d357SDmitri Tikhonov        fprintf(stderr, "Mode `%s' is invalid.  Specify either `slow' or "
606296d357SDmitri Tikhonov                                                        "`fast'.\n", argv[3]);
616296d357SDmitri Tikhonov        exit(EXIT_FAILURE);
626296d357SDmitri Tikhonov    }
636296d357SDmitri Tikhonov
646296d357SDmitri Tikhonov    in = fopen(argv[1], "rb");
656296d357SDmitri Tikhonov    if (!in)
666296d357SDmitri Tikhonov    {
676296d357SDmitri Tikhonov        perror("fopen");
686296d357SDmitri Tikhonov        exit(EXIT_FAILURE);
696296d357SDmitri Tikhonov    }
706296d357SDmitri Tikhonov
716296d357SDmitri Tikhonov    in_sz = fread(in_buf, 1, sizeof(in_buf), in);
726296d357SDmitri Tikhonov    if (in_sz == 0 || in_sz == sizeof(in_buf))
736296d357SDmitri Tikhonov    {
746296d357SDmitri Tikhonov        fprintf(stderr, "input file is either too short or too long\n");
756296d357SDmitri Tikhonov        exit(EXIT_FAILURE);
766296d357SDmitri Tikhonov    }
776296d357SDmitri Tikhonov    (void) fclose(in);
786296d357SDmitri Tikhonov
796296d357SDmitri Tikhonov    count = atoi(argv[2]);
806296d357SDmitri Tikhonov    if (!count)
816296d357SDmitri Tikhonov        count = 1;
826296d357SDmitri Tikhonov
836296d357SDmitri Tikhonov    /* TODO: validate against slow if fast is selected */
846296d357SDmitri Tikhonov
856296d357SDmitri Tikhonov    rv = decode(in_buf, in_sz, out_buf, sizeof(out_buf));
866296d357SDmitri Tikhonov    if (rv < 0)
876296d357SDmitri Tikhonov    {
886296d357SDmitri Tikhonov        fprintf(stderr, "decode-%s returned %d\n", argv[3], rv);
896296d357SDmitri Tikhonov        exit(EXIT_FAILURE);
906296d357SDmitri Tikhonov    }
916296d357SDmitri Tikhonov
926296d357SDmitri Tikhonov    for (i = 0; i < count; ++i)
936296d357SDmitri Tikhonov    {
946296d357SDmitri Tikhonov        rv = decode(in_buf, in_sz, out_buf, sizeof(out_buf));
956296d357SDmitri Tikhonov        (void) rv;
966296d357SDmitri Tikhonov    }
976296d357SDmitri Tikhonov
986296d357SDmitri Tikhonov    exit(EXIT_SUCCESS);
996296d357SDmitri Tikhonov}
100