~rdoering/ubuntu/intrepid/erlang/fix-535090

« back to all changes in this revision

Viewing changes to erts/emulator/zlib/example.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2007-05-01 16:57:10 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070501165710-2sapk0hp2gf3o0ip
Tags: 1:11.b.4-2ubuntu1
* Merge with Debian Unstable. Remaining changes:
  - Add -fno-stack-protector to fix broken crypto_drv.
* DebianMaintainerField update.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* example.c -- usage example of the zlib compression library
2
 
 * Copyright (C) 1995-1998 Jean-loup Gailly.
3
 
 * For conditions of distribution and use, see copyright notice in zlib.h 
 
2
 * Copyright (C) 1995-2004 Jean-loup Gailly.
 
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
4
 */
5
5
 
6
6
/* @(#) $Id$ */
14
14
#ifdef STDC
15
15
#  include <string.h>
16
16
#  include <stdlib.h>
17
 
#else
18
 
   extern void exit  OF((int));
19
17
#endif
20
18
 
21
19
#if defined(VMS) || defined(RISCOS)
40
38
uLong dictId; /* Adler32 value of the dictionary */
41
39
 
42
40
void test_compress      OF((Byte *compr, uLong comprLen,
43
 
                            Byte *uncompr, uLong uncomprLen));
44
 
void test_gzio          OF((const char *out, const char *in, 
45
 
                            Byte *uncompr, int uncomprLen));
 
41
                            Byte *uncompr, uLong uncomprLen));
 
42
void test_gzio          OF((const char *fname,
 
43
                            Byte *uncompr, uLong uncomprLen));
46
44
void test_deflate       OF((Byte *compr, uLong comprLen));
47
45
void test_inflate       OF((Byte *compr, uLong comprLen,
48
 
                            Byte *uncompr, uLong uncomprLen));
 
46
                            Byte *uncompr, uLong uncomprLen));
49
47
void test_large_deflate OF((Byte *compr, uLong comprLen,
50
 
                            Byte *uncompr, uLong uncomprLen));
 
48
                            Byte *uncompr, uLong uncomprLen));
51
49
void test_large_inflate OF((Byte *compr, uLong comprLen,
52
 
                            Byte *uncompr, uLong uncomprLen));
 
50
                            Byte *uncompr, uLong uncomprLen));
53
51
void test_flush         OF((Byte *compr, uLong *comprLen));
54
52
void test_sync          OF((Byte *compr, uLong comprLen,
55
 
                            Byte *uncompr, uLong uncomprLen));
 
53
                            Byte *uncompr, uLong uncomprLen));
56
54
void test_dict_deflate  OF((Byte *compr, uLong comprLen));
57
55
void test_dict_inflate  OF((Byte *compr, uLong comprLen,
58
 
                            Byte *uncompr, uLong uncomprLen));
 
56
                            Byte *uncompr, uLong uncomprLen));
59
57
int  main               OF((int argc, char *argv[]));
60
58
 
61
59
/* ===========================================================================
66
64
    uLong comprLen, uncomprLen;
67
65
{
68
66
    int err;
69
 
    uLong len = strlen(hello)+1;
 
67
    uLong len = (uLong)strlen(hello)+1;
70
68
 
71
69
    err = compress(compr, &comprLen, (const Bytef*)hello, len);
72
70
    CHECK_ERR(err, "compress");
78
76
 
79
77
    if (strcmp((char*)uncompr, hello)) {
80
78
        fprintf(stderr, "bad uncompress\n");
81
 
        exit(1);
 
79
        exit(1);
82
80
    } else {
83
81
        printf("uncompress(): %s\n", (char *)uncompr);
84
82
    }
87
85
/* ===========================================================================
88
86
 * Test read/write of .gz files
89
87
 */
90
 
void test_gzio(out, in, uncompr, uncomprLen)
91
 
    const char *out; /* compressed output file */
92
 
    const char *in;  /* compressed input file */
 
88
void test_gzio(fname, uncompr, uncomprLen)
 
89
    const char *fname; /* compressed file name */
93
90
    Byte *uncompr;
94
 
    int  uncomprLen;
 
91
    uLong uncomprLen;
95
92
{
 
93
#ifdef NO_GZCOMPRESS
 
94
    fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
 
95
#else
96
96
    int err;
97
 
    int len = strlen(hello)+1;
 
97
    int len = (int)strlen(hello)+1;
98
98
    gzFile file;
99
99
    z_off_t pos;
100
100
 
101
 
    file = gzopen(out, "wb");
 
101
    file = gzopen(fname, "wb");
102
102
    if (file == NULL) {
103
103
        fprintf(stderr, "gzopen error\n");
104
104
        exit(1);
106
106
    gzputc(file, 'h');
107
107
    if (gzputs(file, "ello") != 4) {
108
108
        fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
109
 
        exit(1);
 
109
        exit(1);
110
110
    }
111
111
    if (gzprintf(file, ", %s!", "hello") != 8) {
112
112
        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
113
 
        exit(1);
 
113
        exit(1);
114
114
    }
115
115
    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
116
116
    gzclose(file);
117
117
 
118
 
    file = gzopen(in, "rb");
 
118
    file = gzopen(fname, "rb");
119
119
    if (file == NULL) {
120
120
        fprintf(stderr, "gzopen error\n");
 
121
        exit(1);
121
122
    }
122
123
    strcpy((char*)uncompr, "garbage");
123
124
 
124
 
    uncomprLen = gzread(file, uncompr, (unsigned)uncomprLen);
125
 
    if (uncomprLen != len) {
 
125
    if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
126
126
        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
127
 
        exit(1);
 
127
        exit(1);
128
128
    }
129
129
    if (strcmp((char*)uncompr, hello)) {
130
130
        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
131
 
        exit(1);
 
131
        exit(1);
132
132
    } else {
133
 
        printf("gzread(): %s\n", (char *)uncompr);
 
133
        printf("gzread(): %s\n", (char*)uncompr);
134
134
    }
135
135
 
136
136
    pos = gzseek(file, -8L, SEEK_CUR);
137
137
    if (pos != 6 || gztell(file) != pos) {
138
 
        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
139
 
                (long)pos, (long)gztell(file));
140
 
        exit(1);
 
138
        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
 
139
                (long)pos, (long)gztell(file));
 
140
        exit(1);
141
141
    }
142
142
 
143
143
    if (gzgetc(file) != ' ') {
144
 
        fprintf(stderr, "gzgetc error\n");
145
 
        exit(1);
146
 
    }
147
 
 
148
 
    gzgets(file, (char*)uncompr, uncomprLen);
149
 
    uncomprLen = strlen((char*)uncompr);
150
 
    if (uncomprLen != 6) { /* "hello!" */
 
144
        fprintf(stderr, "gzgetc error\n");
 
145
        exit(1);
 
146
    }
 
147
 
 
148
    if (gzungetc(' ', file) != ' ') {
 
149
        fprintf(stderr, "gzungetc error\n");
 
150
        exit(1);
 
151
    }
 
152
 
 
153
    gzgets(file, (char*)uncompr, (int)uncomprLen);
 
154
    if (strlen((char*)uncompr) != 7) { /* " hello!" */
151
155
        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
152
 
        exit(1);
 
156
        exit(1);
153
157
    }
154
 
    if (strcmp((char*)uncompr, hello+7)) {
 
158
    if (strcmp((char*)uncompr, hello + 6)) {
155
159
        fprintf(stderr, "bad gzgets after gzseek\n");
156
 
        exit(1);
 
160
        exit(1);
157
161
    } else {
158
 
        printf("gzgets() after gzseek: %s\n", (char *)uncompr);
 
162
        printf("gzgets() after gzseek: %s\n", (char*)uncompr);
159
163
    }
160
164
 
161
165
    gzclose(file);
 
166
#endif
162
167
}
163
168
 
164
169
/* ===========================================================================
170
175
{
171
176
    z_stream c_stream; /* compression stream */
172
177
    int err;
173
 
    int len = strlen(hello)+1;
 
178
    uLong len = (uLong)strlen(hello)+1;
174
179
 
175
180
    c_stream.zalloc = (alloc_func)0;
176
181
    c_stream.zfree = (free_func)0;
182
187
    c_stream.next_in  = (Bytef*)hello;
183
188
    c_stream.next_out = compr;
184
189
 
185
 
    while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
 
190
    while (c_stream.total_in != len && c_stream.total_out < comprLen) {
186
191
        c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
187
192
        err = deflate(&c_stream, Z_NO_FLUSH);
188
193
        CHECK_ERR(err, "deflate");
234
239
 
235
240
    if (strcmp((char*)uncompr, hello)) {
236
241
        fprintf(stderr, "bad inflate\n");
237
 
        exit(1);
 
242
        exit(1);
238
243
    } else {
239
244
        printf("inflate(): %s\n", (char *)uncompr);
240
245
    }
269
274
    CHECK_ERR(err, "deflate");
270
275
    if (c_stream.avail_in != 0) {
271
276
        fprintf(stderr, "deflate not greedy\n");
272
 
        exit(1);
 
277
        exit(1);
273
278
    }
274
279
 
275
280
    /* Feed in already compressed data and switch to no compression: */
289
294
    err = deflate(&c_stream, Z_FINISH);
290
295
    if (err != Z_STREAM_END) {
291
296
        fprintf(stderr, "deflate should report Z_STREAM_END\n");
292
 
        exit(1);
 
297
        exit(1);
293
298
    }
294
299
    err = deflateEnd(&c_stream);
295
300
    CHECK_ERR(err, "deflateEnd");
319
324
 
320
325
    for (;;) {
321
326
        d_stream.next_out = uncompr;            /* discard the output */
322
 
        d_stream.avail_out = (uInt)uncomprLen;
 
327
        d_stream.avail_out = (uInt)uncomprLen;
323
328
        err = inflate(&d_stream, Z_NO_FLUSH);
324
329
        if (err == Z_STREAM_END) break;
325
330
        CHECK_ERR(err, "large inflate");
330
335
 
331
336
    if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
332
337
        fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
333
 
        exit(1);
 
338
        exit(1);
334
339
    } else {
335
340
        printf("large_inflate(): OK\n");
336
341
    }
345
350
{
346
351
    z_stream c_stream; /* compression stream */
347
352
    int err;
348
 
    int len = strlen(hello)+1;
 
353
    uInt len = (uInt)strlen(hello)+1;
349
354
 
350
355
    c_stream.zalloc = (alloc_func)0;
351
356
    c_stream.zfree = (free_func)0;
410
415
    if (err != Z_DATA_ERROR) {
411
416
        fprintf(stderr, "inflate should report DATA_ERROR\n");
412
417
        /* Because of incorrect adler32 */
413
 
        exit(1);
 
418
        exit(1);
414
419
    }
415
420
    err = inflateEnd(&d_stream);
416
421
    CHECK_ERR(err, "inflateEnd");
436
441
    CHECK_ERR(err, "deflateInit");
437
442
 
438
443
    err = deflateSetDictionary(&c_stream,
439
 
                               (const Bytef*)dictionary, sizeof(dictionary));
 
444
                               (const Bytef*)dictionary, sizeof(dictionary));
440
445
    CHECK_ERR(err, "deflateSetDictionary");
441
446
 
442
447
    dictId = c_stream.adler;
449
454
    err = deflate(&c_stream, Z_FINISH);
450
455
    if (err != Z_STREAM_END) {
451
456
        fprintf(stderr, "deflate should report Z_STREAM_END\n");
452
 
        exit(1);
 
457
        exit(1);
453
458
    }
454
459
    err = deflateEnd(&c_stream);
455
460
    CHECK_ERR(err, "deflateEnd");
483
488
    for (;;) {
484
489
        err = inflate(&d_stream, Z_NO_FLUSH);
485
490
        if (err == Z_STREAM_END) break;
486
 
        if (err == Z_NEED_DICT) {
487
 
            if (d_stream.adler != dictId) {
488
 
                fprintf(stderr, "unexpected dictionary");
489
 
                exit(1);
490
 
            }
491
 
            err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
492
 
                                       sizeof(dictionary));
493
 
        }
 
491
        if (err == Z_NEED_DICT) {
 
492
            if (d_stream.adler != dictId) {
 
493
                fprintf(stderr, "unexpected dictionary");
 
494
                exit(1);
 
495
            }
 
496
            err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
 
497
                                       sizeof(dictionary));
 
498
        }
494
499
        CHECK_ERR(err, "inflate with dict");
495
500
    }
496
501
 
499
504
 
500
505
    if (strcmp((char*)uncompr, hello)) {
501
506
        fprintf(stderr, "bad inflate with dict\n");
502
 
        exit(1);
 
507
        exit(1);
503
508
    } else {
504
509
        printf("inflate with dictionary: %s\n", (char *)uncompr);
505
510
    }
526
531
        fprintf(stderr, "warning: different zlib version\n");
527
532
    }
528
533
 
 
534
    printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
 
535
            ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
 
536
 
529
537
    compr    = (Byte*)calloc((uInt)comprLen, 1);
530
538
    uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
531
539
    /* compr and uncompr are cleared to avoid reading uninitialized
533
541
     */
534
542
    if (compr == Z_NULL || uncompr == Z_NULL) {
535
543
        printf("out of memory\n");
536
 
        exit(1);
 
544
        exit(1);
537
545
    }
538
546
    test_compress(compr, comprLen, uncompr, uncomprLen);
539
547
 
540
548
    test_gzio((argc > 1 ? argv[1] : TESTFILE),
541
 
              (argc > 2 ? argv[2] : TESTFILE),
542
 
              uncompr, (int)uncomprLen);
 
549
              uncompr, uncomprLen);
543
550
 
544
551
    test_deflate(compr, comprLen);
545
552
    test_inflate(compr, comprLen, uncompr, uncomprLen);
554
561
    test_dict_deflate(compr, comprLen);
555
562
    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
556
563
 
557
 
    exit(0);
558
 
    return 0; /* to avoid warning */
 
564
    free(compr);
 
565
    free(uncompr);
 
566
 
 
567
    return 0;
559
568
}