+static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
+{
+ z_stream zstrm;
+ int r;
+ zstrm.zalloc = Z_NULL;
+ zstrm.zfree = Z_NULL;
+ zstrm.opaque = Z_NULL;
+ if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
+ return -1;
+ *out = malloc(len * 2 + 36);
+ if (!out) {
+ deflateEnd(&zstrm);
+ return -1;
+ }
+ zstrm.next_in = buffer;
+ zstrm.avail_in = len;
+ zstrm.next_out = *out + 6;
+ zstrm.avail_out = len * 2 + 30;
+ while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
+ zstrm.avail_out > 0);
+ if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
+ deflateEnd(&zstrm);
+ free(*out);
+ return -1;
+ }
+ *olen = zstrm.avail_out + 6;
+ (*out)[0] = 0x1;
+ (*out)[1] = (zstrm.avail_out >> 8);
+ (*out)[2] = (zstrm.avail_out & 0xff);
+ (*out)[3] = (len >> 8);
+ (*out)[4] = (len & 0xff);
+ (*out)[5] = 0x1;
+ deflateEnd(&zstrm);
+ return 0;
+}
+