• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zlib-ng / zlib-ng / 21758018679

06 Feb 2026 04:33PM UTC coverage: 94.713% (-0.02%) from 94.736%
21758018679

Pull #2139

github

web-flow
Merge e366b2b5e into 98539ef92
Pull Request #2139: Add compile-time feature detection macros

8863 of 9520 branches covered (93.1%)

Branch coverage included in aggregate %.

129 of 129 new or added lines in 2 files covered. (100.0%)

19 existing lines in 8 files now uncovered.

12457 of 12990 relevant lines covered (95.9%)

260794771.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.33
/test/switchlevels.c
1
/* Compresses a user-specified number of chunks from stdin into stdout as a single gzip stream.
2
 * Each chunk is compressed with a user-specified level.
3
 */
4

5
#include "zbuild.h"
6
#ifdef ZLIB_COMPAT
7
#  include "zlib.h"
8
#else
9
#  include "zlib-ng.h"
10
#endif
11

12
#include <inttypes.h>
13
#include <stdio.h>
14

15
#if defined(_WIN32) || defined(__CYGWIN__)
16
#  include <fcntl.h>
17
#  include <io.h>
18
#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
19
#else
20
#  define SET_BINARY_MODE(file)
21
#endif
22

23
static int read_all(unsigned char *buf, size_t size) {
470✔
24
    size_t total_read = 0;
190✔
25
    while (total_read < size) {
940✔
26
        size_t n_read = fread(buf + total_read, 1, size - total_read, stdin);
470✔
27
        if (ferror(stdin)) {
470✔
28
            perror("fread\n");
×
29
            return 1;
×
30
        }
31
        if (n_read == 0) {
470✔
32
            fprintf(stderr, "Premature EOF\n");
×
33
            return 1;
×
34
        }
35
        total_read += n_read;
470✔
36
    }
60✔
37
    return 0;
70✔
38
}
70✔
39

40
static int write_all(unsigned char *buf, size_t size) {
470✔
41
    size_t total_written = 0;
190✔
42
    while (total_written < size) {
940✔
43
        size_t n_written = fwrite(buf + total_written, 1, size - total_written, stdout);
470✔
44
        if (ferror(stdout)) {
470✔
45
            perror("fwrite\n");
×
46
            return 1;
×
47
        }
48
        total_written += n_written;
470✔
49
    }
60✔
50
    return 0;
70✔
51
}
70✔
52

53
static int compress_chunk(PREFIX3(stream) *strm, int level, int size, int last) {
526✔
54
    int ret = 1;
246✔
55
    int err = 0;
246✔
56
    unsigned long compsize;
228✔
57
    unsigned char *buf;
228✔
58

59
    if (size <= 0) {
526✔
60
        fprintf(stderr, "compress_chunk() invalid size %d\n", size);
×
61
        goto done;
×
62
    }
63
    if (level < 0 || level > 9) {
526✔
64
        fprintf(stderr, "compress_chunk() invalid level %d\n", level);
112!
65
        goto done;
112✔
66
    }
67

68
    compsize = PREFIX(deflateBound)(strm, size);
526✔
69
    buf = malloc(size + compsize);
526✔
70
    if (buf == NULL) {
526✔
71
        fprintf(stderr, "Out of memory\n");
×
72
        goto done;
×
73
    }
74
    if (read_all(buf, size) != 0) {
526✔
UNCOV
75
        goto free_buf;
×
76
    }
77

78
    /* Provide only output buffer to deflateParams(). It might need some space to flush the leftovers from the last
79
     * deflate(), but we don't want it to compress anything new. */
80
    strm->next_in = NULL;
526✔
81
    strm->avail_in = 0;
526✔
82
    strm->next_out = buf + size;
526✔
83
    strm->avail_out = compsize;
526✔
84
    err = PREFIX(deflateParams)(strm, level, Z_DEFAULT_STRATEGY);
526✔
85
    if (err != Z_OK) {
526✔
86
        fprintf(stderr, "deflateParams() failed with code %d\n", err);
×
87
        goto free_buf;
×
88
    }
89

90
    /* Provide input buffer to deflate(). */
91
    strm->next_in = buf;
526✔
92
    strm->avail_in = size;
526✔
93
    err = PREFIX(deflate)(strm, last ? Z_FINISH : Z_SYNC_FLUSH);
766✔
94
    if ((!last && err != Z_OK) || (last && err != Z_STREAM_END)) {
526✔
95
        fprintf(stderr, "deflate() failed with code %d\n", err);
84!
96
        goto free_buf;
84✔
97
    }
98
    if (strm->avail_in != 0) {
470✔
99
        fprintf(stderr, "deflate() did not consume %" PRIu32 " bytes of input\n", strm->avail_in);
×
100
        goto free_buf;
×
101
    }
102
    if (write_all(buf + size, compsize - strm->avail_out) != 0) {
470✔
UNCOV
103
        goto free_buf;
×
104
    }
105
    ret = 0;
70✔
106

107
free_buf:
400✔
108
    free(buf);
470✔
109
done:
400✔
110
    return ret;
530✔
111
}
60✔
112

113
void show_help(void)
19✔
114
{
115
    printf("Usage: switchlevels [-w bits] level1 size1 [level2 size2 ...]\n\n"
13✔
116
           "  -w : window bits (8 to 15 for gzip, -8 to -15 for zlib)\n\n");
117
}
7✔
118

119
int main(int argc, char **argv) {
235✔
120
    int ret = EXIT_FAILURE;
95✔
121
    int err = 0;
95✔
122
    int size = 0;
95✔
123
    int level = Z_DEFAULT_COMPRESSION;
95✔
124
    int level_arg = 1;
95✔
125
    int window_bits = MAX_WBITS + 16;
95✔
126
    PREFIX3(stream) strm;
90✔
127

128

129
    if ((argc == 1) || (argc == 2 && strcmp(argv[1], "--help") == 0)) {
235✔
130
        show_help();
19✔
131
        return 0;
47✔
132
    }
133

134
    SET_BINARY_MODE(stdin);
64✔
135
    SET_BINARY_MODE(stdout);
64✔
136

137
    memset(&strm, 0, sizeof(strm));
100✔
138

139
    for (int i = 1; i < argc - 1; i++) {
212✔
140
        if (strcmp(argv[i], "-w") == 0 && i+1 < argc) {
188✔
141
            window_bits = atoi(argv[++i]);
×
142
        } else {
143
            level_arg = i;
76✔
144
            level = atoi(argv[i]);
100✔
145
            break;
188✔
146
        }
147
    }
148

149
    err = PREFIX(deflateInit2)(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
188✔
150
    if (err != Z_OK) {
188✔
151
        fprintf(stderr, "deflateInit() failed with code %d\n", err);
×
152
        goto done;
×
153
    }
154

155
    for (int i = level_arg; i < argc - 1; i += 2) {
658✔
156
        level = atoi(argv[i]);
470✔
157
        size = atoi(argv[i + 1]);
470✔
158
        if (compress_chunk(&strm, level, size, i + 2 >= argc - 1) != 0) {
470✔
UNCOV
159
            goto deflate_end;
×
160
        }
161
    }
70✔
162
    ret = EXIT_SUCCESS;
28✔
163

164
deflate_end:
160✔
165
    PREFIX(deflateEnd)(&strm);
188✔
166
done:
20✔
167
    return ret;
48✔
168
}
35✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc