Blob


1 /*
2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <zlib.h>
25 #include <time.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_path.h"
31 #include "got_lib_deflate.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 const struct got_error *
38 got_deflate_init(struct got_deflate_buf *zb, uint8_t *outbuf, size_t bufsize)
39 {
40 const struct got_error *err = NULL;
41 int zerr;
43 memset(&zb->z, 0, sizeof(zb->z));
45 zb->z.zalloc = Z_NULL;
46 zb->z.zfree = Z_NULL;
47 zerr = deflateInit(&zb->z, Z_DEFAULT_COMPRESSION);
48 if (zerr != Z_OK) {
49 if (zerr == Z_ERRNO)
50 return got_error_from_errno("deflateInit");
51 if (zerr == Z_MEM_ERROR) {
52 errno = ENOMEM;
53 return got_error_from_errno("deflateInit");
54 }
55 return got_error(GOT_ERR_COMPRESSION);
56 }
58 zb->inlen = zb->outlen = bufsize;
60 zb->inbuf = calloc(1, zb->inlen);
61 if (zb->inbuf == NULL) {
62 err = got_error_from_errno("calloc");
63 goto done;
64 }
66 zb->flags = 0;
67 if (outbuf == NULL) {
68 zb->outbuf = calloc(1, zb->outlen);
69 if (zb->outbuf == NULL) {
70 err = got_error_from_errno("calloc");
71 goto done;
72 }
73 zb->flags |= GOT_DEFLATE_F_OWN_OUTBUF;
74 } else
75 zb->outbuf = outbuf;
77 done:
78 if (err)
79 got_deflate_end(zb);
80 return err;
81 }
83 const struct got_error *
84 got_deflate_read(struct got_deflate_buf *zb, FILE *f, size_t *outlenp)
85 {
86 size_t last_total_out = zb->z.total_out;
87 z_stream *z = &zb->z;
88 int ret = Z_ERRNO;
90 z->next_out = zb->outbuf;
91 z->avail_out = zb->outlen;
93 *outlenp = 0;
94 do {
95 if (z->avail_in == 0) {
96 size_t n = fread(zb->inbuf, 1, zb->inlen, f);
97 if (n == 0) {
98 if (ferror(f))
99 return got_ferror(f, GOT_ERR_IO);
100 /* EOF */
101 ret = deflate(z, Z_FINISH);
102 break;
104 z->next_in = zb->inbuf;
105 z->avail_in = n;
107 ret = deflate(z, Z_NO_FLUSH);
108 } while (ret == Z_OK && z->avail_out > 0);
110 if (ret == Z_OK) {
111 zb->flags |= GOT_DEFLATE_F_HAVE_MORE;
112 } else {
113 if (ret != Z_STREAM_END)
114 return got_error(GOT_ERR_COMPRESSION);
115 zb->flags &= ~GOT_DEFLATE_F_HAVE_MORE;
118 *outlenp = z->total_out - last_total_out;
119 return NULL;
122 void
123 got_deflate_end(struct got_deflate_buf *zb)
125 free(zb->inbuf);
126 if (zb->flags & GOT_DEFLATE_F_OWN_OUTBUF)
127 free(zb->outbuf);
128 deflateEnd(&zb->z);
131 const struct got_error *
132 got_deflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
134 const struct got_error *err;
135 size_t avail;
136 struct got_deflate_buf zb;
138 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
139 if (err)
140 goto done;
142 *outlen = 0;
144 do {
145 err = got_deflate_read(&zb, infile, &avail);
146 if (err)
147 goto done;
148 if (avail > 0) {
149 size_t n;
150 n = fwrite(zb.outbuf, avail, 1, outfile);
151 if (n != 1) {
152 err = got_ferror(outfile, GOT_ERR_IO);
153 goto done;
155 *outlen += avail;
157 } while (zb.flags & GOT_DEFLATE_F_HAVE_MORE);
159 done:
160 if (err == NULL)
161 rewind(outfile);
162 got_deflate_end(&zb);
163 return err;