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

Simplified implementation of zlib functions. More...

Files

file  zlib.c
 Simplified implementation of zlib functions.
 

Data Structures

struct  zobj
 Zlib buffer used for compression and decompression. More...
 

Functions

struct zobjzcompress (uint8_t *buff, uint16_t len, uint8_t level)
 Allocate a buffer and return it with compressed data. More...
 
void zuncompress (struct zobj *buff, uint8_t *obuff)
 Uncompress zobj buffer to buffer. More...
 
int is_gzip (uint8_t *buf, int buf_size)
 check a buffer if it contains gzip magic More...
 
uint8_t * gzinflatebuf (uint8_t *buf_in, int buf_size, uint32_t *len)
 Ungzip a buffer. More...
 

Detailed Description

Simplified implementation of zlib functions.

Function Documentation

uint8_t* gzinflatebuf ( uint8_t *  buf_in,
int  buf_size,
uint32_t *  len 
)

Ungzip a buffer.

Parameters
buf_inBuffer to inflate.
buf_sizeSize of buf_in buffer.
lenPointer that will contain the uncompressed data length.
Returns
Uncompressed data in a buffer or NULL on error.

Definition at line 101 of file zlib.c.

Referenced by curl_ungzip().

101  {
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 }
int is_gzip ( uint8_t *  buf,
int  buf_size 
)

check a buffer if it contains gzip magic

Parameters
bufbuffer to check.
buf_sizebuffer len it must be more than 4.
Returns
non zero value if the buffer contains gzip data

Definition at line 85 of file zlib.c.

Referenced by curl_ungzip().

85  {
86  if (buf_size < 4) {
87  return 0;
88  }
89  if (memcmp(buf, gzipMagicBytes, 4)) {
90  return 0;
91  }
92  return 1;
93 }
struct zobj* zcompress ( uint8_t *  buff,
uint16_t  len,
uint8_t  level 
)

Allocate a buffer and return it with compressed data.

Parameters
buffBuffer to compress.
lenLength of the buffer.
levelCompression level.
Returns
reference to zobj data structure containing compressed data or NULL on error.

Definition at line 47 of file zlib.c.

References zobj::buff, objalloc(), zobj::olen, and zobj::zlen.

47  {
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 }
uint16_t zlen
Compressed size of data.
Definition: dtsapp.h:170
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
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
void zuncompress ( struct zobj buff,
uint8_t *  obuff 
)

Uncompress zobj buffer to buffer.

Parameters
buffCompressed buffer to uncompress.
obuffBuffer to uncompress too.
Warning
obuff needs to be large enough to contain the data.
Todo:
Implement this without needing original buff len using inflate

Definition at line 71 of file zlib.c.

References zobj::buff, zobj::olen, and zobj::zlen.

71  {
72  uLongf olen = buff->olen;
73 
74  if (!obuff) {
75  return;
76  }
77 
78  uncompress(obuff, &olen, buff->buff, buff->zlen);
79 }
uint16_t zlen
Compressed size of data.
Definition: dtsapp.h:170
uint8_t * buff
Buffer with compressed/uncompressed data.
Definition: dtsapp.h:166
uint16_t olen
Original size of data.
Definition: dtsapp.h:168