]> git.notmuchmail.org Git - notmuch/blob - util/zlib-extra.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / util / zlib-extra.c
1 /* zlib-extra.c -  Extra or enhanced routines for compressed I/O.
2  *
3  * Copyright (c) 2014 David Bremner
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20
21 #include "zlib-extra.h"
22 #include <talloc.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 /* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */
27 util_status_t
28 gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)
29 {
30     char *buf = *bufptr;
31     unsigned int len;
32     size_t offset = 0;
33
34     if (buf) {
35         len = talloc_array_length (buf);
36     } else {
37         /* same as getdelim from gnulib */
38         len = 120;
39         buf = talloc_array (talloc_ctx, char, len);
40         if (buf == NULL)
41             return UTIL_OUT_OF_MEMORY;
42     }
43
44     while (1) {
45         if (! gzgets (stream, buf + offset, len - offset)) {
46             /* Null indicates EOF or error */
47             int zlib_status = 0;
48             (void) gzerror (stream, &zlib_status);
49             switch (zlib_status) {
50             case Z_STREAM_END:
51             case Z_OK:
52                 /* no data read before EOF */
53                 if (offset == 0)
54                     return UTIL_EOF;
55                 else
56                     goto SUCCESS;
57             case Z_ERRNO:
58                 return UTIL_ERRNO;
59             default:
60                 return UTIL_GZERROR;
61             }
62         }
63
64         offset += strlen (buf + offset);
65
66         if (buf[offset - 1] == '\n')
67             goto SUCCESS;
68
69         len *= 2;
70         buf = talloc_realloc (talloc_ctx, buf, char, len);
71         if (buf == NULL)
72             return UTIL_OUT_OF_MEMORY;
73     }
74   SUCCESS:
75     *bufptr = buf;
76     *bytes_read = offset;
77     return UTIL_SUCCESS;
78 }
79
80 const char *
81 gz_error_string (util_status_t status, gzFile file)
82 {
83     if (status == UTIL_GZERROR)
84         return gzerror_str (file);
85     else
86         return util_error_string (status);
87 }
88
89 const char *
90 gzerror_str(gzFile file)
91 {
92     int dummy;
93     return gzerror (file, &dummy);
94 }