File: zlib-test.c

package info (click to toggle)
extlib 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: ml: 6,942; makefile: 137; sh: 42; ansic: 31
file content (43 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download | duplicates (4)
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
/* compile using gcc -o zlib-test zlib-test.c -lz

   usage: zlib-test <compression-level> <string>

   where compression-level is from 0 to 9
*/
#include <stdio.h>
#include <zlib.h>
#include <string.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
    unsigned char dest[32];
    unsigned long destLen = 31;
    int retval, i;
    int level;
    char *endptr;

    if (argc != 3) {
        fprintf (stderr, "Usage: %s: <compression-level> <string>\n", argv[0]);
        return 1;
    }

    level = strtol (argv[1], &endptr, 10);
    if (endptr == argv[1] || level < 0 || level > 9) {
        fprintf (stderr, "Invalid compression level\n");
        return 1;
    }

    retval = compress2 (dest, &destLen, (unsigned char *)argv[2], strlen (argv[2]), level);
    if (retval != Z_OK) {
        fprintf (stderr, "Error calling zlib compress2 function: %d\n", retval);
        return 1;
    }

    for (i = 0; i < destLen; i++)
        printf ("\\x%02hhx", dest[i]);
    printf ("\n");

    return 0;
}