2014-03-29 18:53:17 +01:00
|
|
|
/* zlib-extra.c - Extra or enhanced routines for compressed I/O.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 David Bremner
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2014-03-29 18:53:17 +01:00
|
|
|
*
|
|
|
|
* Author: David Bremner <david@tethera.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zlib-extra.h"
|
|
|
|
#include <talloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */
|
|
|
|
util_status_t
|
|
|
|
gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)
|
|
|
|
{
|
|
|
|
char *buf = *bufptr;
|
|
|
|
unsigned int len;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
len = talloc_array_length (buf);
|
|
|
|
} else {
|
|
|
|
/* same as getdelim from gnulib */
|
|
|
|
len = 120;
|
|
|
|
buf = talloc_array (talloc_ctx, char, len);
|
|
|
|
if (buf == NULL)
|
|
|
|
return UTIL_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (! gzgets (stream, buf + offset, len - offset)) {
|
|
|
|
/* Null indicates EOF or error */
|
|
|
|
int zlib_status = 0;
|
|
|
|
(void) gzerror (stream, &zlib_status);
|
|
|
|
switch (zlib_status) {
|
2020-04-14 19:38:40 +02:00
|
|
|
case Z_STREAM_END:
|
2014-03-29 18:53:17 +01:00
|
|
|
case Z_OK:
|
|
|
|
/* no data read before EOF */
|
|
|
|
if (offset == 0)
|
|
|
|
return UTIL_EOF;
|
|
|
|
else
|
|
|
|
goto SUCCESS;
|
|
|
|
case Z_ERRNO:
|
|
|
|
return UTIL_ERRNO;
|
|
|
|
default:
|
|
|
|
return UTIL_GZERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += strlen (buf + offset);
|
|
|
|
|
|
|
|
if (buf[offset - 1] == '\n')
|
|
|
|
goto SUCCESS;
|
|
|
|
|
|
|
|
len *= 2;
|
|
|
|
buf = talloc_realloc (talloc_ctx, buf, char, len);
|
|
|
|
if (buf == NULL)
|
|
|
|
return UTIL_OUT_OF_MEMORY;
|
|
|
|
}
|
2019-06-13 12:33:13 +02:00
|
|
|
SUCCESS:
|
2014-03-29 18:53:17 +01:00
|
|
|
*bufptr = buf;
|
|
|
|
*bytes_read = offset;
|
|
|
|
return UTIL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:33:13 +02:00
|
|
|
const char *
|
|
|
|
gz_error_string (util_status_t status, gzFile file)
|
2014-03-29 18:53:17 +01:00
|
|
|
{
|
|
|
|
if (status == UTIL_GZERROR)
|
2020-04-13 01:00:31 +02:00
|
|
|
return gzerror_str (file);
|
2014-03-29 18:53:17 +01:00
|
|
|
else
|
|
|
|
return util_error_string (status);
|
|
|
|
}
|
2020-04-27 14:24:22 +02:00
|
|
|
|
|
|
|
const char *
|
2021-03-13 13:45:34 +01:00
|
|
|
gzerror_str (gzFile file)
|
2020-04-27 14:24:22 +02:00
|
|
|
{
|
|
|
|
int dummy;
|
2021-03-13 13:45:34 +01:00
|
|
|
|
2020-04-27 14:24:22 +02:00
|
|
|
return gzerror (file, &dummy);
|
|
|
|
}
|