1 /* zlib-extra.c - Extra or enhanced routines for compressed I/O.
3 * Copyright (c) 2014 David Bremner
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.
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.
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/ .
18 * Author: David Bremner <david@tethera.net>
21 #include "zlib-extra.h"
26 /* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */
28 gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)
35 len = talloc_array_length (buf);
37 /* same as getdelim from gnulib */
39 buf = talloc_array (talloc_ctx, char, len);
41 return UTIL_OUT_OF_MEMORY;
45 if (! gzgets (stream, buf + offset, len - offset)) {
46 /* Null indicates EOF or error */
48 (void) gzerror (stream, &zlib_status);
49 switch (zlib_status) {
51 /* no data read before EOF */
63 offset += strlen (buf + offset);
65 if (buf[offset - 1] == '\n')
69 buf = talloc_realloc (talloc_ctx, buf, char, len);
71 return UTIL_OUT_OF_MEMORY;
79 const char *gz_error_string (util_status_t status, gzFile file)
81 if (status == UTIL_GZERROR)
82 return gzerror (file, NULL);
84 return util_error_string (status);