Hash :
32cb896c
Author :
Date :
2018-03-11T14:10:56
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
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sha1.h>
#include <zlib.h>
#include "got_error.h"
#include "got_object.h"
#include "got_path_lib.h"
#include "got_zbuf_lib.h"
const struct got_error *
got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
{
const struct got_error *err = NULL;
memset(zb, 0, sizeof(*zb));
zb->z.zalloc = Z_NULL;
zb->z.zfree = Z_NULL;
if (inflateInit(&zb->z) != Z_OK) {
err = got_error(GOT_ERR_IO);
goto done;
}
zb->inlen = zb->outlen = bufsize;
zb->inbuf = calloc(1, zb->inlen);
if (zb->inbuf == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
zb->outbuf = calloc(1, zb->outlen);
if (zb->outbuf == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
done:
if (err)
got_inflate_end(zb);
return err;
}
const struct got_error *
got_inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
{
size_t last_total_out = zb->z.total_out;
z_stream *z = &zb->z;
int ret;
z->next_out = zb->outbuf;
z->avail_out = zb->outlen;
*outlenp = 0;
do {
if (z->avail_in == 0) {
size_t n = fread(zb->inbuf, 1, zb->inlen, f);
if (n == 0) {
if (ferror(f))
return got_ferror(f, GOT_ERR_IO);
break; /* EOF */
}
z->next_in = zb->inbuf;
z->avail_in = n;
}
ret = inflate(z, Z_SYNC_FLUSH);
} while (ret == Z_OK && z->avail_out > 0);
if (ret == Z_OK) {
zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
} else {
if (ret != Z_STREAM_END)
return got_error(GOT_ERR_DECOMPRESSION);
zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
}
*outlenp = z->total_out - last_total_out;
return NULL;
}
void
got_inflate_end(struct got_zstream_buf *zb)
{
free(zb->inbuf);
free(zb->outbuf);
inflateEnd(&zb->z);
}
const struct got_error *
got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
{
const struct got_error *err;
size_t avail;
struct got_zstream_buf zb;
void *newbuf;
err = got_inflate_init(&zb, 8192);
if (err)
return err;
*outbuf = NULL;
*outlen = 0;
do {
err = got_inflate_read(&zb, f, &avail);
if (err)
return err;
if (avail > 0) {
newbuf = reallocarray(*outbuf, 1, *outlen + avail);
if (newbuf == NULL) {
free(*outbuf);
*outbuf = NULL;
*outlen = 0;
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
memcpy(newbuf + *outlen, zb.outbuf, avail);
*outbuf = newbuf;
*outlen += avail;
}
} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
done:
got_inflate_end(&zb);
return err;
}
const struct got_error *
got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
{
const struct got_error *err;
size_t avail;
struct got_zstream_buf zb;
void *newbuf;
err = got_inflate_init(&zb, 8192);
if (err)
goto done;
*outlen = 0;
do {
err = got_inflate_read(&zb, infile, &avail);
if (err)
return err;
if (avail > 0) {
size_t n;
n = fwrite(zb.outbuf, avail, 1, outfile);
if (n != 1) {
err = got_ferror(outfile, GOT_ERR_IO);
goto done;
}
*outlen += avail;
}
} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
done:
if (err == NULL)
rewind(outfile);
got_inflate_end(&zb);
return err;
}