DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
zlib.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2012 Gregory Nietsky <gregory@distrotetch.co.za>
3  http://www.distrotech.co.za
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 <http://www.gnu.org/licenses/>.
17 */
18 
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <zlib.h>
28 
29 #include "include/dtsapp.h"
30 
31 static const unsigned char gzipMagicBytes[] = { 0x1f, 0x8b, 0x08, 0x00 };
32 
33 static void zobj_free(void *data) {
34  struct zobj *zdata = data;
35 
36  if (zdata->buff) {
37  free(zdata->buff);
38  }
39 }
40 
47 extern struct zobj *zcompress(uint8_t *buff, uint16_t len, uint8_t level) {
48  struct zobj *ret;
49 
50  if (!(ret = objalloc(sizeof(*ret), zobj_free))) {
51  return (NULL);
52  }
53 
54  ret->zlen = compressBound(len);
55  ret->olen = len;
56 
57  if (!(ret->buff = malloc(ret->zlen))) {
58  return (NULL);
59  }
60  compress2(ret->buff, (uLongf *)&ret->zlen, buff, len, level);
61 
62  return (ret);
63 }
64 
71 extern void zuncompress(struct zobj *buff, uint8_t *obuff) {
72  uLongf olen = buff->olen;
73 
74  if (!obuff) {
75  return;
76  }
77 
78  uncompress(obuff, &olen, buff->buff, buff->zlen);
79 }
80 
85 extern int is_gzip(uint8_t *buf, int buf_size) {
86  if (buf_size < 4) {
87  return 0;
88  }
89  if (memcmp(buf, gzipMagicBytes, 4)) {
90  return 0;
91  }
92  return 1;
93 }
94 
101 extern uint8_t *gzinflatebuf(uint8_t *buf_in, int buf_size, uint32_t *len) {
102  z_stream zdat;
103  uint8_t *buf = NULL, *tmp;
104  int res;
105 
106  zdat.opaque = NULL;
107  zdat.zalloc = NULL;
108  zdat.zfree = NULL;
109 
110  zdat.next_in = buf_in;
111  zdat.avail_in = buf_size;
112  zdat.next_out = buf;
113  zdat.avail_out = 0;
114  zdat.total_out = 0;
115 
116  if (inflateInit2(&zdat, 31)) {
117  return NULL;
118  }
119 
120  do {
121  if (!(tmp = realloc(buf,zdat.total_out + (zdat.avail_in * 5) + 1))) {
122  res = Z_MEM_ERROR;
123  break;
124  } else {
125  buf = tmp;
126  }
127  buf[zdat.total_out] = '\0';
128  zdat.next_out = &buf[zdat.total_out];
129  zdat.avail_out += zdat.avail_in * 5;
130  } while ((res = inflate(&zdat, Z_NO_FLUSH)) == Z_OK);
131 
132  if (res == Z_STREAM_END) {
133  buf = realloc(buf, zdat.total_out);
134  *len = zdat.total_out;
135  } else {
136  free(buf);
137  *len = 0;
138  buf = NULL;
139  }
140  inflateEnd(&zdat);
141 
142  return buf;
143 }
144 
void zuncompress(struct zobj *buff, uint8_t *obuff)
Uncompress zobj buffer to buffer.
Definition: zlib.c:71
uint16_t zlen
Compressed size of data.
Definition: dtsapp.h:170
uint8_t * gzinflatebuf(uint8_t *buf_in, int buf_size, uint32_t *len)
Ungzip a buffer.
Definition: zlib.c:101
struct zobj * zcompress(uint8_t *buff, uint16_t len, uint8_t level)
Allocate a buffer and return it with compressed data.
Definition: zlib.c:47
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
DTS Application library API Include file.
Zlib buffer used for compression and decompression.
Definition: dtsapp.h:164
uint8_t * buff
Buffer with compressed/uncompressed data.
Definition: dtsapp.h:166
uint16_t olen
Original size of data.
Definition: dtsapp.h:168
int is_gzip(uint8_t *buf, int buf_size)
check a buffer if it contains gzip magic
Definition: zlib.c:85