summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles/compress.c
blob: 96f0be5232532b6b2e51798fb2f7c0f870624365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* This program compresses the help content found in help/ to standard
 * output. Do not directly compile or use this program, instead it
 * will be automatically used by genhelp.sh */

#include <ctype.h>
#include <lz4hc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "help.h"

char *compress(const char *in, int inlen, int *rc, int *minsz, int *minlev)
{
    int maxsz = LZ4_compressBound(inlen);
    unsigned char *outbuf = malloc(maxsz);
    *minsz = 9999999;
    *minlev = 0;

    for(int i = LZ4HC_CLEVEL_MIN; i < LZ4HC_CLEVEL_MAX; ++i)
    {
        *rc = LZ4_compress_HC(help_text, outbuf, inlen, maxsz, i);
        if(!*rc)
        {
            fprintf(stderr, "compress failed\n");
            return NULL;
        }
        if(*rc < *minsz)
        {
            *minsz = *rc;
            *minlev = i;
        }
    }
    *rc = LZ4_compress_HC(help_text, outbuf, inlen, maxsz, *minlev);
    return outbuf;
}

void dump_bytes(unsigned char *buf, int len)
{
    int i = 0;
    while(i < len)
    {
        int stop = i + 10;
        for(;i < stop && i < len; ++i)
        {
            printf("0x%02x, ", buf[i]);
        }
        printf("\n");
    }
}

int main()
{
    int inlen = strlen(help_text) + 1, len;
    int minsz, minlev;
    unsigned char *outbuf = compress(help_text, inlen, &len, &minsz, &minlev);

    printf("/* auto-generated on " __DATE__ " by genhelp.sh */\n");
    printf("/* orig %d comp %d ratio %g level %d saved %d */\n", inlen, minsz, (double)minsz / inlen, minlev, inlen - minsz);
    printf("/* DO NOT EDIT! */\n\n");
    printf("const char help_text[] = {\n");

    dump_bytes(outbuf, len);
    free(outbuf);

    printf("};\n");
    printf("const unsigned short help_text_len = %d;\n", inlen);

    return 0;
}