summaryrefslogtreecommitdiff
path: root/sys-apps/the_silver_searcher/files/the_silver_searcher-2.1.0-lzma.patch
blob: 21eb7118b2047054f16ec4248d87c664038364b0 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
From 24995eb239799e52ae09d47b7520d50a3b8b606c Mon Sep 17 00:00:00 2001
From: Allen Wild <allenwild93@gmail.com>
Date: Tue, 5 Sep 2017 19:48:49 -0400
Subject: [PATCH] zfile: fix build when zlib and/or lzma are excluded

Currently, zfile won't build if either zlib.h or lzma.h are missing

  * Add several inline wrapper functions which check cookie->ctype and
    call an inline zlib or lzma implementation function.
    - This cleans up zfile_read at the expense of growing the file and
      adding 2 layers of function calls. Everything is static and inline
      so the compiler should be able to optimize sufficiently.
  * Stub functions are #define'd for the unsupported compression method
    (if applicable)
  * Use an Automake conditional to only build zfile.c if either
    zlib.h or lzma.h are available, define the symbol USE_FOPENCOOKIE if
    this is the case and check it instead of HAVE_FOPENCOOKIE
  * Replace tabs in zfile.c with spaces for consistency with the rest of
    the codebase

Fixes: #1147
---
 Makefile.am      |   6 +-
 configure.ac     |   5 +
 src/decompress.h |   2 +-
 src/search.c     |   2 +-
 src/zfile.c      | 301 ++++++++++++++++++++++++++++++++++++++++---------------
 5 files changed, 232 insertions(+), 84 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 3931c3a7..4d85f54f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,9 +1,13 @@
 ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
 
 bin_PROGRAMS = ag
-ag_SOURCES = src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c src/zfile.c
+ag_SOURCES = src/ignore.c src/ignore.h src/log.c src/log.h src/options.c src/options.h src/print.c src/print_w32.c src/print.h src/scandir.c src/scandir.h src/search.c src/search.h src/lang.c src/lang.h src/util.c src/util.h src/decompress.c src/decompress.h src/uthash.h src/main.c
 ag_LDADD = ${PCRE_LIBS} ${LZMA_LIBS} ${ZLIB_LIBS} $(PTHREAD_LIBS)
 
+if USE_FOPENCOOKIE
+ag_SOURCES += src/zfile.c src/zfile.h
+endif
+
 dist_man_MANS = doc/ag.1
 
 bashcompdir = $(pkgdatadir)/completions
diff --git a/configure.ac b/configure.ac
index 982cc3b2..5917ecc1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -66,6 +66,11 @@ AC_CHECK_MEMBER([struct dirent.d_namlen], [AC_DEFINE([HAVE_DIRENT_DNAMLEN], [],
 
 AC_CHECK_FUNCS(fgetln fopencookie getline realpath strlcpy strndup vasprintf madvise posix_fadvise pthread_setaffinity_np pledge)
 
+# Only build zfile.c if we need it
+AM_CONDITIONAL([USE_FOPENCOOKIE], [(test "$ac_cv_func_fopencookie" = "yes") &&
+                                   (test "$ac_cv_header_zlib_h" = "yes" || test "$ac_cv_header_lzma_h" = "yes")])
+AM_COND_IF([USE_FOPENCOOKIE], [AC_DEFINE([USE_FOPENCOOKIE], [], [Use fopencookie streaming in zfile.c])])
+
 AC_CONFIG_FILES([Makefile the_silver_searcher.spec])
 AC_CONFIG_HEADERS([src/config.h])
 
diff --git a/src/decompress.h b/src/decompress.h
index 9c5592cf..d5c3d582 100644
--- a/src/decompress.h
+++ b/src/decompress.h
@@ -19,7 +19,7 @@ ag_compression_type is_zipped(const void *buf, const int buf_len);
 
 void *decompress(const ag_compression_type zip_type, const void *buf, const int buf_len, const char *dir_full_path, int *new_buf_len);
 
-#if HAVE_FOPENCOOKIE
+#ifdef USE_FOPENCOOKIE
 FILE *decompress_open(int fd, const char *mode, ag_compression_type ctype);
 #endif
 
diff --git a/src/search.c b/src/search.c
index 14e9d415..84d9423f 100644
--- a/src/search.c
+++ b/src/search.c
@@ -357,7 +357,7 @@ void search_file(const char *file_full_path) {
     if (opts.search_zip_files) {
         ag_compression_type zip_type = is_zipped(buf, f_len);
         if (zip_type != AG_NO_COMPRESSION) {
-#if HAVE_FOPENCOOKIE
+#ifdef USE_FOPENCOOKIE
             log_debug("%s is a compressed file. stream searching", file_full_path);
             fp = decompress_open(fd, "r", zip_type);
             search_stream(fp, file_full_path);
diff --git a/src/zfile.c b/src/zfile.c
index e4b75662..74de37e6 100644
--- a/src/zfile.c
+++ b/src/zfile.c
@@ -33,10 +33,10 @@ typedef _off64_t off64_t;
 
 #if HAVE_FOPENCOOKIE
 
-#define min(a, b) ({				\
-	__typeof (a) _a = (a);			\
-	__typeof (b) _b = (b);			\
-	_a < _b ? _a : _b; })
+#define min(a, b) ({                \
+    __typeof (a) _a = (a);          \
+    __typeof (b) _b = (b);          \
+    _a < _b ? _a : _b; })
 
 static cookie_read_function_t zfile_read;
 static cookie_seek_function_t zfile_seek;
@@ -60,23 +60,217 @@ struct zfile {
     ag_compression_type ctype;
 
     union {
+#ifdef HAVE_ZLIB_H
         z_stream gz;
+#endif
+#ifdef HAVE_LZMA_H
         lzma_stream lzma;
+#endif
     } stream;
 
     uint8_t inbuf[32 * KB];
     uint8_t outbuf[256 * KB];
     bool eof;
+    bool eof_next;
 };
 
-#define CAVAIL_IN(c) ((c)->ctype == AG_GZIP ? (c)->stream.gz.avail_in : (c)->stream.lzma.avail_in)
-#define CNEXT_OUT(c) ((c)->ctype == AG_GZIP ? (c)->stream.gz.next_out : (c)->stream.lzma.next_out)
+#ifdef HAVE_ZLIB_H
+/***************** zlib functions ********************************/
+static int zfile_zlib_cookie_init(struct zfile *cookie) {
+    int rc;
+
+    memset(&cookie->stream.gz, 0, sizeof(cookie->stream.gz));
+    rc = inflateInit2(&cookie->stream.gz, 32 + 15);
+    if (rc != Z_OK) {
+        log_err("Unable to initialize zlib: %s", zError(rc));
+        return EIO;
+    }
+    cookie->stream.gz.next_in = NULL;
+    cookie->stream.gz.avail_in = 0;
+    cookie->stream.gz.next_out = cookie->outbuf;
+    cookie->stream.gz.avail_out = sizeof cookie->outbuf;
+    return 0;
+}
+
+static inline size_t zfile_zlib_cavail_in(struct zfile *cookie) {
+    return (size_t)cookie->stream.gz.avail_in;
+}
+
+static inline uint8_t *zfile_zlib_cnext_out(struct zfile *cookie) {
+    return (uint8_t *)cookie->stream.gz.next_out;
+}
+
+static inline int zfile_zlib_is_stream_end(int ret) {
+    return ret == Z_STREAM_END;
+}
+
+static inline void zfile_zlib_set_next_in(struct zfile *cookie, size_t nb) {
+    cookie->stream.gz.avail_in = nb;
+    cookie->stream.gz.next_in = cookie->inbuf;
+}
+
+static inline void zfile_zlib_set_next_out(struct zfile *cookie) {
+    cookie->stream.gz.next_out = cookie->outbuf;
+    cookie->stream.gz.avail_out = sizeof(cookie->outbuf);
+}
+
+static inline int zfile_zlib_inflate(struct zfile *cookie) {
+    int ret = inflate(&cookie->stream.gz, Z_NO_FLUSH);
+    if (ret == Z_STREAM_END) {
+        cookie->eof_next = true;
+        return 0;
+    }
+    return ret == Z_OK ? 0 : -1;
+}
+
+#else
+/***************** zlib stubs ********************************/
+#define zfile_zlib_cookie_init(c) EINVAL
+#define zfile_zlib_cavail_in(c) 0
+#define zfile_zlib_cnext_out(c) 0
+#define zfile_zlib_is_stream_end(r) 1
+#define zfile_zlib_set_next_in(c, n) (void)0
+#define zfile_zlib_set_next_out(c) (void)0
+#define zfile_zlib_inflate(c) -1
+#endif
 
-static int
-zfile_cookie_init(struct zfile *cookie) {
 #ifdef HAVE_LZMA_H
+/***************** lzma functions ********************************/
+static int zfile_lzma_cookie_init(struct zfile *cookie) {
     lzma_ret lzrc;
+    cookie->stream.lzma = (lzma_stream)LZMA_STREAM_INIT;
+    lzrc = lzma_auto_decoder(&cookie->stream.lzma, -1, 0);
+    if (lzrc != LZMA_OK) {
+        log_err("Unable to initialize lzma_auto_decoder: %d", lzrc);
+        return EIO;
+    }
+    cookie->stream.lzma.next_in = NULL;
+    cookie->stream.lzma.avail_in = 0;
+    cookie->stream.lzma.next_out = cookie->outbuf;
+    cookie->stream.lzma.avail_out = sizeof(cookie->outbuf);
+    return 0;
+}
+
+static inline size_t zfile_lzma_cavail_in(struct zfile *cookie) {
+    return cookie->stream.lzma.avail_in;
+}
+
+static inline uint8_t *zfile_lzma_cnext_out(struct zfile *cookie) {
+    return cookie->stream.lzma.next_out;
+}
+
+static inline int zfile_lzma_is_stream_end(int ret) {
+    return (lzma_ret)ret == LZMA_STREAM_END;
+}
+
+static inline void zfile_lzma_set_next_in(struct zfile *cookie, size_t nb) {
+    cookie->stream.lzma.avail_in = nb;
+    cookie->stream.lzma.next_in = cookie->inbuf;
+}
+
+static inline void zfile_lzma_set_next_out(struct zfile *cookie) {
+    cookie->stream.lzma.next_out = cookie->outbuf;
+    cookie->stream.lzma.avail_out = sizeof(cookie->outbuf);
+}
+
+static inline int zfile_lzma_inflate(struct zfile *cookie) {
+    lzma_ret ret = lzma_code(&cookie->stream.lzma, LZMA_RUN);
+    if (ret == LZMA_STREAM_END) {
+        cookie->eof_next = true;
+        return 0;
+    }
+    return ret == LZMA_OK ? 0 : -1;
+}
+
+#else
+/***************** lzma stubs ********************************/
+#define zfile_lzma_cookie_init(c) EINVAL
+#define zfile_lzma_cavail_in(c) 0
+#define zfile_lzma_cnext_out(c) 0
+#define zfile_lzma_is_stream_end(r) 1
+#define zfile_lzma_set_next_in(c, n) (void)0
+#define zfile_lzma_set_next_out(c) (void)0
+#define zfile_lzma_inflate(c) -1
 #endif
+
+static inline size_t zfile_cavail_in(struct zfile *cookie) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            return zfile_zlib_cavail_in(cookie);
+            break;
+        case AG_XZ:
+            return zfile_lzma_cavail_in(cookie);
+            break;
+        default:
+            return 0;
+    }
+}
+
+static inline uint8_t *zfile_cnext_out(struct zfile *cookie) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            return zfile_zlib_cnext_out(cookie);
+            break;
+        case AG_XZ:
+            return zfile_lzma_cnext_out(cookie);
+            break;
+        default:
+            return NULL;
+    }
+}
+
+static inline int zfile_is_stream_end(struct zfile *cookie, int ret) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            return zfile_zlib_is_stream_end(ret);
+        case AG_XZ:
+            return zfile_lzma_is_stream_end(ret);
+        default:
+            return 1;
+    }
+}
+
+static inline void zfile_set_next_in(struct zfile *cookie, size_t nb) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            zfile_zlib_set_next_in(cookie, nb);
+            break;
+        case AG_XZ:
+            zfile_lzma_set_next_in(cookie, nb);
+            break;
+        default:
+            break;
+    }
+}
+
+static inline void zfile_set_next_out(struct zfile *cookie) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            zfile_zlib_set_next_out(cookie);
+            break;
+        case AG_XZ:
+            zfile_lzma_set_next_out(cookie);
+            break;
+        default:
+            break;
+    }
+}
+
+static inline int zfile_inflate(struct zfile *cookie) {
+    switch (cookie->ctype) {
+        case AG_GZIP:
+            return zfile_zlib_inflate(cookie);
+            break;
+        case AG_XZ:
+            return zfile_lzma_inflate(cookie);
+            break;
+        default:
+            return -1;
+    }
+}
+
+static int
+zfile_cookie_init(struct zfile *cookie) {
     int rc;
 
     assert(cookie->logic_offset == 0);
@@ -85,39 +279,18 @@ zfile_cookie_init(struct zfile *cookie) {
     cookie->actual_len = 0;
 
     switch (cookie->ctype) {
-#ifdef HAVE_ZLIB_H
         case AG_GZIP:
-            memset(&cookie->stream.gz, 0, sizeof cookie->stream.gz);
-            rc = inflateInit2(&cookie->stream.gz, 32 + 15);
-            if (rc != Z_OK) {
-                log_err("Unable to initialize zlib: %s", zError(rc));
-                return EIO;
-            }
-            cookie->stream.gz.next_in = NULL;
-            cookie->stream.gz.avail_in = 0;
-            cookie->stream.gz.next_out = cookie->outbuf;
-            cookie->stream.gz.avail_out = sizeof cookie->outbuf;
+            rc = zfile_zlib_cookie_init(cookie);
             break;
-#endif
-#ifdef HAVE_LZMA_H
         case AG_XZ:
-            cookie->stream.lzma = (lzma_stream)LZMA_STREAM_INIT;
-            lzrc = lzma_auto_decoder(&cookie->stream.lzma, -1, 0);
-            if (lzrc != LZMA_OK) {
-                log_err("Unable to initialize lzma_auto_decoder: %d", lzrc);
-                return EIO;
-            }
-            cookie->stream.lzma.next_in = NULL;
-            cookie->stream.lzma.avail_in = 0;
-            cookie->stream.lzma.next_out = cookie->outbuf;
-            cookie->stream.lzma.avail_out = sizeof cookie->outbuf;
+            rc = zfile_lzma_cookie_init(cookie);
             break;
-#endif
         default:
             log_err("Unsupported compression type: %d", cookie->ctype);
             return EINVAL;
     }
-
+    if (rc)
+        return rc;
 
     cookie->outbuf_start = 0;
     cookie->eof = false;
@@ -165,10 +338,10 @@ decompress_open(int fd, const char *mode, ag_compression_type ctype) {
         goto out;
 
     /*
-	 * No validation of compression type is done -- file is assumed to
-	 * match input.  In Ag, the compression type is already detected, so
-	 * that's ok.
-	 */
+     * No validation of compression type is done -- file is assumed to
+     * match input.  In Ag, the compression type is already detected, so
+     * that's ok.
+     */
     cookie = malloc(sizeof *cookie);
     if (cookie == NULL) {
         errno = ENOMEM;
@@ -207,8 +380,6 @@ zfile_read(void *cookie_, char *buf, size_t size) {
     struct zfile *cookie = cookie_;
     size_t nb, ignorebytes;
     ssize_t total = 0;
-    lzma_ret lzret;
-    int ret;
 
     assert(size <= SSIZE_MAX);
 
@@ -218,9 +389,6 @@ zfile_read(void *cookie_, char *buf, size_t size) {
     if (cookie->eof)
         return 0;
 
-    ret = Z_OK;
-    lzret = LZMA_OK;
-
     ignorebytes = cookie->logic_offset - cookie->decode_offset;
     assert(ignorebytes == 0);
 
@@ -228,9 +396,9 @@ zfile_read(void *cookie_, char *buf, size_t size) {
         size_t inflated;
 
         /* Drain output buffer first */
-        while (CNEXT_OUT(cookie) >
+        while (zfile_cnext_out(cookie) >
                &cookie->outbuf[cookie->outbuf_start]) {
-            size_t left = CNEXT_OUT(cookie) -
+            size_t left = zfile_cnext_out(cookie) -
                           &cookie->outbuf[cookie->outbuf_start];
             size_t ignoreskip = min(ignorebytes, left);
             size_t toread;
@@ -265,21 +433,13 @@ zfile_read(void *cookie_, char *buf, size_t size) {
         if (size == 0)
             break;
 
-        /*
-		 * If we have not satisfied read, the output buffer must be
-		 * empty.
-		 */
-        assert(cookie->stream.gz.next_out ==
-               &cookie->outbuf[cookie->outbuf_start]);
-
-        if ((cookie->ctype == AG_XZ && lzret == LZMA_STREAM_END) ||
-            (cookie->ctype == AG_GZIP && ret == Z_STREAM_END)) {
+        if (cookie->eof_next) {
             cookie->eof = true;
             break;
         }
 
         /* Read more input if empty */
-        if (CAVAIL_IN(cookie) == 0) {
+        if (zfile_cavail_in(cookie) == 0) {
             nb = fread(cookie->inbuf, 1, sizeof cookie->inbuf,
                        cookie->in);
             if (ferror(cookie->in)) {
@@ -290,39 +450,18 @@ zfile_read(void *cookie_, char *buf, size_t size) {
                 warn("truncated file");
                 exit(1);
             }
-            if (cookie->ctype == AG_XZ) {
-                cookie->stream.lzma.avail_in = nb;
-                cookie->stream.lzma.next_in = cookie->inbuf;
-            } else {
-                cookie->stream.gz.avail_in = nb;
-                cookie->stream.gz.next_in = cookie->inbuf;
-            }
+            zfile_set_next_in(cookie, nb);
         }
 
         /* Reset stream state to beginning of output buffer */
-        if (cookie->ctype == AG_XZ) {
-            cookie->stream.lzma.next_out = cookie->outbuf;
-            cookie->stream.lzma.avail_out = sizeof cookie->outbuf;
-        } else {
-            cookie->stream.gz.next_out = cookie->outbuf;
-            cookie->stream.gz.avail_out = sizeof cookie->outbuf;
-        }
+        zfile_set_next_out(cookie);
         cookie->outbuf_start = 0;
 
-        if (cookie->ctype == AG_GZIP) {
-            ret = inflate(&cookie->stream.gz, Z_NO_FLUSH);
-            if (ret != Z_OK && ret != Z_STREAM_END) {
-                log_err("Found mem/data error while decompressing zlib stream: %s", zError(ret));
-                return -1;
-            }
-        } else {
-            lzret = lzma_code(&cookie->stream.lzma, LZMA_RUN);
-            if (lzret != LZMA_OK && lzret != LZMA_STREAM_END) {
-                log_err("Found mem/data error while decompressing xz/lzma stream: %d", lzret);
-                return -1;
-            }
+        if (zfile_inflate(cookie)) {
+            log_err("Found mem/data error while decompressing stream");
+            return -1;
         }
-        inflated = CNEXT_OUT(cookie) - &cookie->outbuf[0];
+        inflated = zfile_cnext_out(cookie) - &cookie->outbuf[0];
         cookie->actual_len += inflated;
     } while (!ferror(cookie->in) && size > 0);