aboutsummaryrefslogtreecommitdiff
path: root/zlib/examples/gzlog.c
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2009-06-28 22:07:26 +0000
committermarha <marha@users.sourceforge.net>2009-06-28 22:07:26 +0000
commit3562e78743202e43aec8727005182a2558117eca (patch)
tree8f9113a77d12470c5c851a2a8e4cb02e89df7d43 /zlib/examples/gzlog.c
downloadvcxsrv-3562e78743202e43aec8727005182a2558117eca.tar.gz
vcxsrv-3562e78743202e43aec8727005182a2558117eca.tar.bz2
vcxsrv-3562e78743202e43aec8727005182a2558117eca.zip
Checked in the following released items:
xkeyboard-config-1.4.tar.gz ttf-bitstream-vera-1.10.tar.gz font-alias-1.0.1.tar.gz font-sun-misc-1.0.0.tar.gz font-sun-misc-1.0.0.tar.gz font-sony-misc-1.0.0.tar.gz font-schumacher-misc-1.0.0.tar.gz font-mutt-misc-1.0.0.tar.gz font-misc-misc-1.0.0.tar.gz font-misc-meltho-1.0.0.tar.gz font-micro-misc-1.0.0.tar.gz font-jis-misc-1.0.0.tar.gz font-isas-misc-1.0.0.tar.gz font-dec-misc-1.0.0.tar.gz font-daewoo-misc-1.0.0.tar.gz font-cursor-misc-1.0.0.tar.gz font-arabic-misc-1.0.0.tar.gz font-winitzki-cyrillic-1.0.0.tar.gz font-misc-cyrillic-1.0.0.tar.gz font-cronyx-cyrillic-1.0.0.tar.gz font-screen-cyrillic-1.0.1.tar.gz font-xfree86-type1-1.0.1.tar.gz font-adobe-utopia-type1-1.0.1.tar.gz font-ibm-type1-1.0.0.tar.gz font-bitstream-type1-1.0.0.tar.gz font-bitstream-speedo-1.0.0.tar.gz font-bh-ttf-1.0.0.tar.gz font-bh-type1-1.0.0.tar.gz font-bitstream-100dpi-1.0.0.tar.gz font-bh-lucidatypewriter-100dpi-1.0.0.tar.gz font-bh-100dpi-1.0.0.tar.gz font-adobe-utopia-100dpi-1.0.1.tar.gz font-adobe-100dpi-1.0.0.tar.gz font-util-1.0.1.tar.gz font-bitstream-75dpi-1.0.0.tar.gz font-bh-lucidatypewriter-75dpi-1.0.0.tar.gz font-adobe-utopia-75dpi-1.0.1.tar.gz font-bh-75dpi-1.0.0.tar.gz bdftopcf-1.0.1.tar.gz font-adobe-75dpi-1.0.0.tar.gz mkfontscale-1.0.6.tar.gz openssl-0.9.8k.tar.gz bigreqsproto-1.0.2.tar.gz xtrans-1.2.2.tar.gz resourceproto-1.0.2.tar.gz inputproto-1.4.4.tar.gz compositeproto-0.4.tar.gz damageproto-1.1.0.tar.gz zlib-1.2.3.tar.gz xkbcomp-1.0.5.tar.gz freetype-2.3.9.tar.gz pthreads-w32-2-8-0-release.tar.gz pixman-0.12.0.tar.gz kbproto-1.0.3.tar.gz evieext-1.0.2.tar.gz fixesproto-4.0.tar.gz recordproto-1.13.2.tar.gz randrproto-1.2.2.tar.gz scrnsaverproto-1.1.0.tar.gz renderproto-0.9.3.tar.gz xcmiscproto-1.1.2.tar.gz fontsproto-2.0.2.tar.gz xextproto-7.0.3.tar.gz xproto-7.0.14.tar.gz libXdmcp-1.0.2.tar.gz libxkbfile-1.0.5.tar.gz libfontenc-1.0.4.tar.gz libXfont-1.3.4.tar.gz libX11-1.1.5.tar.gz libXau-1.0.4.tar.gz libxcb-1.1.tar.gz xorg-server-1.5.3.tar.gz
Diffstat (limited to 'zlib/examples/gzlog.c')
-rw-r--r--zlib/examples/gzlog.c413
1 files changed, 413 insertions, 0 deletions
diff --git a/zlib/examples/gzlog.c b/zlib/examples/gzlog.c
new file mode 100644
index 000000000..f71f817c8
--- /dev/null
+++ b/zlib/examples/gzlog.c
@@ -0,0 +1,413 @@
+/*
+ * gzlog.c
+ * Copyright (C) 2004 Mark Adler
+ * For conditions of distribution and use, see copyright notice in gzlog.h
+ * version 1.0, 26 Nov 2004
+ *
+ */
+
+#include <string.h> /* memcmp() */
+#include <stdlib.h> /* malloc(), free(), NULL */
+#include <sys/types.h> /* size_t, off_t */
+#include <unistd.h> /* read(), close(), sleep(), ftruncate(), */
+ /* lseek() */
+#include <fcntl.h> /* open() */
+#include <sys/file.h> /* flock() */
+#include "zlib.h" /* deflateInit2(), deflate(), deflateEnd() */
+
+#include "gzlog.h" /* interface */
+#define local static
+
+/* log object structure */
+typedef struct {
+ int id; /* object identifier */
+ int fd; /* log file descriptor */
+ off_t extra; /* offset of extra "ap" subfield */
+ off_t mark_off; /* offset of marked data */
+ off_t last_off; /* offset of last block */
+ unsigned long crc; /* uncompressed crc */
+ unsigned long len; /* uncompressed length (modulo 2^32) */
+ unsigned stored; /* length of current stored block */
+} gz_log;
+
+#define GZLOGID 19334 /* gz_log object identifier */
+
+#define LOCK_RETRY 1 /* retry lock once a second */
+#define LOCK_PATIENCE 1200 /* try about twenty minutes before forcing */
+
+/* acquire a lock on a file */
+local int lock(int fd)
+{
+ int patience;
+
+ /* try to lock every LOCK_RETRY seconds for LOCK_PATIENCE seconds */
+ patience = LOCK_PATIENCE;
+ do {
+ if (flock(fd, LOCK_EX + LOCK_NB) == 0)
+ return 0;
+ (void)sleep(LOCK_RETRY);
+ patience -= LOCK_RETRY;
+ } while (patience > 0);
+
+ /* we've run out of patience -- give up */
+ return -1;
+}
+
+/* release lock */
+local void unlock(int fd)
+{
+ (void)flock(fd, LOCK_UN);
+}
+
+/* release a log object */
+local void log_clean(gz_log *log)
+{
+ unlock(log->fd);
+ (void)close(log->fd);
+ free(log);
+}
+
+/* read an unsigned long from a byte buffer little-endian */
+local unsigned long make_ulg(unsigned char *buf)
+{
+ int n;
+ unsigned long val;
+
+ val = (unsigned long)(*buf++);
+ for (n = 8; n < 32; n += 8)
+ val += (unsigned long)(*buf++) << n;
+ return val;
+}
+
+/* read an off_t from a byte buffer little-endian */
+local off_t make_off(unsigned char *buf)
+{
+ int n;
+ off_t val;
+
+ val = (off_t)(*buf++);
+ for (n = 8; n < 64; n += 8)
+ val += (off_t)(*buf++) << n;
+ return val;
+}
+
+/* write an unsigned long little-endian to byte buffer */
+local void dice_ulg(unsigned long val, unsigned char *buf)
+{
+ int n;
+
+ for (n = 0; n < 4; n++) {
+ *buf++ = val & 0xff;
+ val >>= 8;
+ }
+}
+
+/* write an off_t little-endian to byte buffer */
+local void dice_off(off_t val, unsigned char *buf)
+{
+ int n;
+
+ for (n = 0; n < 8; n++) {
+ *buf++ = val & 0xff;
+ val >>= 8;
+ }
+}
+
+/* initial, empty gzip file for appending */
+local char empty_gz[] = {
+ 0x1f, 0x8b, /* magic gzip id */
+ 8, /* compression method is deflate */
+ 4, /* there is an extra field */
+ 0, 0, 0, 0, /* no modification time provided */
+ 0, 0xff, /* no extra flags, no OS */
+ 20, 0, 'a', 'p', 16, 0, /* extra field with "ap" subfield */
+ 32, 0, 0, 0, 0, 0, 0, 0, /* offset of uncompressed data */
+ 32, 0, 0, 0, 0, 0, 0, 0, /* offset of last block */
+ 1, 0, 0, 0xff, 0xff, /* empty stored block (last) */
+ 0, 0, 0, 0, /* crc */
+ 0, 0, 0, 0 /* uncompressed length */
+};
+
+/* initialize a log object with locking */
+void *gzlog_open(char *path)
+{
+ unsigned xlen;
+ unsigned char temp[20];
+ unsigned sub_len;
+ int good;
+ gz_log *log;
+
+ /* allocate log structure */
+ log = malloc(sizeof(gz_log));
+ if (log == NULL)
+ return NULL;
+ log->id = GZLOGID;
+
+ /* open file, creating it if necessary, and locking it */
+ log->fd = open(path, O_RDWR | O_CREAT, 0600);
+ if (log->fd < 0) {
+ free(log);
+ return NULL;
+ }
+ if (lock(log->fd)) {
+ close(log->fd);
+ free(log);
+ return NULL;
+ }
+
+ /* if file is empty, write new gzip stream */
+ if (lseek(log->fd, 0, SEEK_END) == 0) {
+ if (write(log->fd, empty_gz, sizeof(empty_gz)) != sizeof(empty_gz)) {
+ log_clean(log);
+ return NULL;
+ }
+ }
+
+ /* check gzip header */
+ (void)lseek(log->fd, 0, SEEK_SET);
+ if (read(log->fd, temp, 12) != 12 || temp[0] != 0x1f ||
+ temp[1] != 0x8b || temp[2] != 8 || (temp[3] & 4) == 0) {
+ log_clean(log);
+ return NULL;
+ }
+
+ /* process extra field to find "ap" sub-field */
+ xlen = temp[10] + (temp[11] << 8);
+ good = 0;
+ while (xlen) {
+ if (xlen < 4 || read(log->fd, temp, 4) != 4)
+ break;
+ sub_len = temp[2];
+ sub_len += temp[3] << 8;
+ xlen -= 4;
+ if (memcmp(temp, "ap", 2) == 0 && sub_len == 16) {
+ good = 1;
+ break;
+ }
+ if (xlen < sub_len)
+ break;
+ (void)lseek(log->fd, sub_len, SEEK_CUR);
+ xlen -= sub_len;
+ }
+ if (!good) {
+ log_clean(log);
+ return NULL;
+ }
+
+ /* read in "ap" sub-field */
+ log->extra = lseek(log->fd, 0, SEEK_CUR);
+ if (read(log->fd, temp, 16) != 16) {
+ log_clean(log);
+ return NULL;
+ }
+ log->mark_off = make_off(temp);
+ log->last_off = make_off(temp + 8);
+
+ /* get crc, length of gzip file */
+ (void)lseek(log->fd, log->last_off, SEEK_SET);
+ if (read(log->fd, temp, 13) != 13 ||
+ memcmp(temp, "\001\000\000\377\377", 5) != 0) {
+ log_clean(log);
+ return NULL;
+ }
+ log->crc = make_ulg(temp + 5);
+ log->len = make_ulg(temp + 9);
+
+ /* set up to write over empty last block */
+ (void)lseek(log->fd, log->last_off + 5, SEEK_SET);
+ log->stored = 0;
+ return (void *)log;
+}
+
+/* maximum amount to put in a stored block before starting a new one */
+#define MAX_BLOCK 16384
+
+/* write a block to a log object */
+int gzlog_write(void *obj, char *data, size_t len)
+{
+ size_t some;
+ unsigned char temp[5];
+ gz_log *log;
+
+ /* check object */
+ log = (gz_log *)obj;
+ if (log == NULL || log->id != GZLOGID)
+ return 1;
+
+ /* write stored blocks until all of the input is written */
+ do {
+ some = MAX_BLOCK - log->stored;
+ if (some > len)
+ some = len;
+ if (write(log->fd, data, some) != some)
+ return 1;
+ log->crc = crc32(log->crc, data, some);
+ log->len += some;
+ len -= some;
+ data += some;
+ log->stored += some;
+
+ /* if the stored block is full, end it and start another */
+ if (log->stored == MAX_BLOCK) {
+ (void)lseek(log->fd, log->last_off, SEEK_SET);
+ temp[0] = 0;
+ dice_ulg(log->stored + ((unsigned long)(~log->stored) << 16),
+ temp + 1);
+ if (write(log->fd, temp, 5) != 5)
+ return 1;
+ log->last_off = lseek(log->fd, log->stored, SEEK_CUR);
+ (void)lseek(log->fd, 5, SEEK_CUR);
+ log->stored = 0;
+ }
+ } while (len);
+ return 0;
+}
+
+/* recompress the remaining stored deflate data in place */
+local int recomp(gz_log *log)
+{
+ z_stream strm;
+ size_t len, max;
+ unsigned char *in;
+ unsigned char *out;
+ unsigned char temp[16];
+
+ /* allocate space and read it all in (it's around 1 MB) */
+ len = log->last_off - log->mark_off;
+ max = len + (len >> 12) + (len >> 14) + 11;
+ out = malloc(max);
+ if (out == NULL)
+ return 1;
+ in = malloc(len);
+ if (in == NULL) {
+ free(out);
+ return 1;
+ }
+ (void)lseek(log->fd, log->mark_off, SEEK_SET);
+ if (read(log->fd, in, len) != len) {
+ free(in);
+ free(out);
+ return 1;
+ }
+
+ /* recompress in memory, decoding stored data as we go */
+ /* note: this assumes that unsigned is four bytes or more */
+ /* consider not making that assumption */
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ if (deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 8,
+ Z_DEFAULT_STRATEGY) != Z_OK) {
+ free(in);
+ free(out);
+ return 1;
+ }
+ strm.next_in = in;
+ strm.avail_out = max;
+ strm.next_out = out;
+ while (len >= 5) {
+ if (strm.next_in[0] != 0)
+ break;
+ strm.avail_in = strm.next_in[1] + (strm.next_in[2] << 8);
+ strm.next_in += 5;
+ len -= 5;
+ if (strm.avail_in != 0) {
+ if (len < strm.avail_in)
+ break;
+ len -= strm.avail_in;
+ (void)deflate(&strm, Z_NO_FLUSH);
+ if (strm.avail_in != 0 || strm.avail_out == 0)
+ break;
+ }
+ }
+ (void)deflate(&strm, Z_SYNC_FLUSH);
+ (void)deflateEnd(&strm);
+ free(in);
+ if (len != 0 || strm.avail_out == 0) {
+ free(out);
+ return 1;
+ }
+
+ /* overwrite stored data with compressed data */
+ (void)lseek(log->fd, log->mark_off, SEEK_SET);
+ len = max - strm.avail_out;
+ if (write(log->fd, out, len) != len) {
+ free(out);
+ return 1;
+ }
+ free(out);
+
+ /* write last empty block, crc, and length */
+ log->mark_off = log->last_off = lseek(log->fd, 0, SEEK_CUR);
+ temp[0] = 1;
+ dice_ulg(0xffffL << 16, temp + 1);
+ dice_ulg(log->crc, temp + 5);
+ dice_ulg(log->len, temp + 9);
+ if (write(log->fd, temp, 13) != 13)
+ return 1;
+
+ /* truncate file to discard remaining stored data and old trailer */
+ ftruncate(log->fd, lseek(log->fd, 0, SEEK_CUR));
+
+ /* update extra field to point to new last empty block */
+ (void)lseek(log->fd, log->extra, SEEK_SET);
+ dice_off(log->mark_off, temp);
+ dice_off(log->last_off, temp + 8);
+ if (write(log->fd, temp, 16) != 16)
+ return 1;
+ return 0;
+}
+
+/* maximum accumulation of stored blocks before compressing */
+#define MAX_STORED 1048576
+
+/* close log object */
+int gzlog_close(void *obj)
+{
+ unsigned char temp[8];
+ gz_log *log;
+
+ /* check object */
+ log = (gz_log *)obj;
+ if (log == NULL || log->id != GZLOGID)
+ return 1;
+
+ /* go to start of most recent block being written */
+ (void)lseek(log->fd, log->last_off, SEEK_SET);
+
+ /* if some stuff was put there, update block */
+ if (log->stored) {
+ temp[0] = 0;
+ dice_ulg(log->stored + ((unsigned long)(~log->stored) << 16),
+ temp + 1);
+ if (write(log->fd, temp, 5) != 5)
+ return 1;
+ log->last_off = lseek(log->fd, log->stored, SEEK_CUR);
+ }
+
+ /* write last block (empty) */
+ if (write(log->fd, "\001\000\000\377\377", 5) != 5)
+ return 1;
+
+ /* write updated crc and uncompressed length */
+ dice_ulg(log->crc, temp);
+ dice_ulg(log->len, temp + 4);
+ if (write(log->fd, temp, 8) != 8)
+ return 1;
+
+ /* put offset of that last block in gzip extra block */
+ (void)lseek(log->fd, log->extra + 8, SEEK_SET);
+ dice_off(log->last_off, temp);
+ if (write(log->fd, temp, 8) != 8)
+ return 1;
+
+ /* if more than 1 MB stored, then time to compress it */
+ if (log->last_off - log->mark_off > MAX_STORED) {
+ if (recomp(log))
+ return 1;
+ }
+
+ /* unlock and close file */
+ log_clean(log);
+ return 0;
+}