socsvn commit: r255422 - soc2013/dpl/head/lib/libzcap
dpl at FreeBSD.org
dpl at FreeBSD.org
Thu Aug 1 19:54:43 UTC 2013
Author: dpl
Date: Thu Aug 1 19:54:43 2013
New Revision: 255422
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255422
Log:
Written some first functions to send commands to a secure zlib client.
deflateInit, deflate and deflateEnd.
Added:
soc2013/dpl/head/lib/libzcap/commands.c
Modified:
soc2013/dpl/head/lib/libzcap/capsicum.c
soc2013/dpl/head/lib/libzcap/commands.h
soc2013/dpl/head/lib/libzcap/deflate.c
Modified: soc2013/dpl/head/lib/libzcap/capsicum.c
==============================================================================
--- soc2013/dpl/head/lib/libzcap/capsicum.c Thu Aug 1 18:06:58 2013 (r255421)
+++ soc2013/dpl/head/lib/libzcap/capsicum.c Thu Aug 1 19:54:43 2013 (r255422)
@@ -72,93 +72,8 @@
*/
nvlist_t *
-command( int command, int args, ... )
+sendCommand( nvlist_t *nvl)
{
- nvlist_t *nvl, *nvlargs;
- va_list ap;
-
- /* If the child is not executing, do it. */
- /* It _should_ only be tested once.
- if (pid == 0)
- startChild();
-
- if( (nvlargs = nvlist_create(0)) == NULL ||
- (nvl = nvlist_create(0)) == NULL ) {
- perror("nvlist_create");
- return (NULL);
- }
- nvlist_add_number(nvl, "command", command);
-
- va_start(ap, args);
- // Set up args, and add it to nvl.
- if (command == DEFLATEINIT){
- } else if (command == DEFLATE ) {
- } else if (command == DEFLATEEND ) {
- } else if (command == INFLATEINIT ) {
- } else if (command == INFLATE ) {
- } else if (command == INFLATEEND ) {
- } else if (command == DEFLATEINIT2 ) {
- } else if (command == DEFLATESETDICTIONARY ) {
- } else if (command == DEFLATECOPY ) {
- } else if (command == DEFLATERESET ) {
- } else if (command == DEFLATEPARAMS ) {
- } else if (command == DEFLATETUNE ) {
- } else if (command == DEFLATEBOUND ) {
- } else if (command == DEFLATEPENDING ) {
- } else if (command == DEFLATEPRIME ) {
- } else if (command == DEFLATESETHEADER ) {
- } else if (command == INFLATEINIT2 ) {
- } else if (command == INFLATESETDICTIONARY ) {
- } else if (command == INFLATEGETDICTIONARY ) {
- } else if (command == INFLATESYNC ) {
- } else if (command == INFLATECOPY ) {
- } else if (command == INFLATERESET ) {
- } else if (command == INFLATERESET2 ) {
- } else if (command == INFLATEPRIME ) {
- } else if (command == INFLATEMARK ) {
- } else if (command == INFLATEGETHEADER ) {
- } else if (command == INFLATEBACKINIT ) {
- } else if (command == INFLATEBACK ) {
- } else if (command == INFLATEBACKEND ) {
- } else if (command == ZLIBCOMPILEFLAGS ) {
- } else if (command == COMPRESS ) {
- } else if (command == COMPRESS2 ) {
- } else if (command == COMPRESSBOUND ) {
- } else if (command == UNCOMPRESS ) {
- } else if (command == GZOPEN ) {
- } else if (command == GZDOPEN ) {
- } else if (command == GZBUFFER ) {
- } else if (command == GZSETPARAMS ) {
- } else if (command == GZREAD ) {
- } else if (command == GZWRITE ) {
- } else if (command == GZPRINTF ) {
- } else if (command == GZPUTS ) {
- } else if (command == GZGETS ) {
- } else if (command == GZPUTC ) {
- } else if (command == GZGETC ) {
- } else if (command == GZUNGETC ) {
- } else if (command == GZFLUSH ) {
- } else if (command == GZSEEK ) {
- } else if (command == GZREWIND ) {
- } else if (command == GZTELL ) {
- } else if (command == GZOFFSET ) {
- } else if (command == GZEOF ) {
- } else if (command == GZDIRECT ) {
- } else if (command == GZCLOSE ) {
- } else if (command == GZCLOSE_R ) {
- } else if (command == GZCLOSE_W ) {
- } else if (command == GZERROR ) {
- } else if (command == GZCLEARERR ) {
- } else if (command == ADLER32 ) {
- } else if (command == ADLER32_COMBINE ) {
- } else if (command == CRC32 ) {
- } else if (command == CRC32_COMBINE ) {
- } else { return NULL; }
-
- va_end(ap);
-
- /* send nvlist*/
- /* What about the whiles? */
while(1){
if( nvlist_send(sv[0], nvl) == 0 )
break;
Added: soc2013/dpl/head/lib/libzcap/commands.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ soc2013/dpl/head/lib/libzcap/commands.c Thu Aug 1 19:54:43 2013 (r255422)
@@ -0,0 +1,194 @@
+/*
+ This is just a template of the capsicumized functions.
+
+nvlist_t *
+zcapcmd_command( args )
+{
+ nvlist_t *nvl, *args, *results;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZCAPCMD_);
+
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+}
+
+*/
+
+extern pid;
+
+
+/* basic functions */
+nvlist_t *
+zcapcmd_deflateInit( z_streamp strm, int level,int method, int windowBits,
+ int strategy, const char *version, int stream_size )
+{
+ nvlist_t *nvl, *args, *results;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZCAPCMD_DEFLATEINIT);
+
+ nvlist_add_binary(args, "strm", strm, sizeof(z_streamp));
+ nvlist_add_number(args, "level", level);
+ nvlist_add_number(args, "method", method);
+ nvlist_add_number(args, "windowBits", windowBits);
+ nvlist_add_string(args, "version", version);
+ nvlist_add_number(args, "stream_size", stream_size);
+ nvlist_add_nvlist(nvl, "args", args);
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ int result = nvlist_take_number(nvlresults, "result");
+ nvlist_delete(nvlresults);
+}
+
+int
+zcapcmd_deflate( z_streamp strm, int flush )
+{
+ nvlist_t *nvl, *args, *results;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZCAPCMD_);
+
+ nvlist_add_binary(args, "strm", strm, sizeof(z_streamp));
+ nvlist_add_number(args, "flush", flush);
+ nvlist_add_nvlist(nvl, "args", args);
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ int result = nvlist_take_number(nvlresults, "result");
+ nvlist_delete(nvlresults);
+}
+
+nvlist_t *
+zcapcmd_deflateEnd(z_streamp strm)
+{
+ nvlist_t *nvl, *args, *nvlresults;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZLIBCMD_VAR);
+
+ nvlist_add_binary(args, "strm", strm, sizeof(z_streamp));
+ nvlist_add_nvlist(nvl, "args", args);
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ int result = nvlist_take_number(nvlresults, "result");
+ nvlist_delete(nvlresults);
+
+ return( result);
+}
+
+nvlist_t *
+zcapcmd_inflateInit( args )
+{
+ nvlist_t *nvl, *args, *results;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZLIBCMD_VAR);
+
+ /* set args */
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ /* Get result */
+}
+
+nvlist_t *
+zcapcmd_inflate( args )
+{
+ nvlist_t *nvl, *args, *results;
+
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZLIBCMD_VAR);
+
+ /* set args */
+
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ /* Get result */
+}
+
+nvlist_t *
+zcapcmd_inflateEnd( args )
+{
+ nvlist_t *nvl, *args, *results;
+
+ /* If the child is not executing, do it. */
+ /* It _should_ only be tested once.
+ if (pid == 0)
+ startChild();
+
+ if( (args = nvlist_create(0)) == NULL ||
+ (nvl = nvlist_create(0)) == NULL ) {
+ perror("nvlist_create");
+ return (NULL);
+ }
+ nvlist_add_number(nvl, "command", ZLIBCMD_VAR);
+
+ /* set args */
+
+
+ results = sendCommand(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(nvl);
+
+ /* Get result */
+}
+
Modified: soc2013/dpl/head/lib/libzcap/commands.h
==============================================================================
--- soc2013/dpl/head/lib/libzcap/commands.h Thu Aug 1 18:06:58 2013 (r255421)
+++ soc2013/dpl/head/lib/libzcap/commands.h Thu Aug 1 19:54:43 2013 (r255422)
@@ -43,9 +43,9 @@
zlibCompileFlags
// utility functions
- compress
- compress2
- compressBound
+ compress
+ compress2
+ compressBound
uncompress
// gzip file access functions
@@ -83,73 +83,72 @@
/* basic functions */
-#define DEFLATEINIT 0
-#define DEFLATE 1
-#define DEFLATEEND 2
-#define INFLATEINIT 3
-#define INFLATE 4
-#define INFLATEEND 5
+#define ZCAPCMD_DEFLATEINIT 0
+#define ZCAPCMD_DEFLATE 1
+#define ZCAPCMD_DEFLATEEND 2
+#define ZCAPCMD_INFLATEINIT 3
+#define ZCAPCMD_INFLATE 4
+#define ZCAPCMD_INFLATEEND 5
/* Advanced functions */
-#define DEFLATEINIT2
-#define DEFLATESETDICTIONARY 6
-#define DEFLATECOPY 7
-#define DEFLATERESET 8
-#define DEFLATEPARAMS 9
-#define DEFLATETUNE 10
-#define DEFLATEBOUND 11
-#define DEFLATEPENDING 12
-#define DEFLATEPRIME 13
-#define DEFLATESETHEADER 14
-#define INFLATEINIT2 15
-#define INFLATESETDICTIONARY 16
-#define INFLATEGETDICTIONARY 17
-#define INFLATESYNC 18
-#define INFLATECOPY 19
-#define INFLATERESET 20
-#define INFLATERESET2 21
-#define INFLATEPRIME 22
-#define INFLATEMARK 23
-#define INFLATEGETHEADER 24
-#define INFLATEBACKINIT 25
-#define INFLATEBACK 26
-#define INFLATEBACKEND 27
-#define ZLIBCOMPILEFLAGS 28
+#define ZCAPCMD_DEFLATESETDICTIONARY 6
+#define ZCAPCMD_DEFLATECOPY 7
+#define ZCAPCMD_DEFLATERESET 8
+#define ZCAPCMD_DEFLATEPARAMS 9
+#define ZCAPCMD_DEFLATETUNE 10
+#define ZCAPCMD_DEFLATEBOUND 11
+#define ZCAPCMD_DEFLATEPENDING 12
+#define ZCAPCMD_DEFLATEPRIME 13
+#define ZCAPCMD_DEFLATESETHEADER 14
+#define ZCAPCMD_INFLATEINIT2 15
+#define ZCAPCMD_INFLATESETDICTIONARY 16
+#define ZCAPCMD_INFLATEGETDICTIONARY 17
+#define ZCAPCMD_INFLATESYNC 18
+#define ZCAPCMD_INFLATECOPY 19
+#define ZCAPCMD_INFLATERESET 20
+#define ZCAPCMD_INFLATERESET2 21
+#define ZCAPCMD_INFLATEPRIME 22
+#define ZCAPCMD_INFLATEMARK 23
+#define ZCAPCMD_INFLATEGETHEADER 24
+#define ZCAPCMD_INFLATEBACKINIT 25
+#define ZCAPCMD_INFLATEBACK 26
+#define ZCAPCMD_INFLATEBACKEND 27
+#define ZCAPCMD_ZLIBCOMPILEFLAGS 28
/* utility functions */
-#define COMPRESS 29
-#define COMPRESS2 30
-#define COMPRESSBOUND 31
-#define UNCOMPRESS 32
+#define ZCAPCMD_COMPRESS 29
+#define ZCAPCMD_COMPRESS2 30
+#define ZCAPCMD_COMPRESSBOUND 31
+#define ZCAPCMD_UNCOMPRESS 32
/* gzip file access functions */
-#define GZOPEN 33
-#define GZDOPEN 34
-#define GZBUFFER 35
-#define GZSETPARAMS 36
-#define GZREAD 37
-#define GZWRITE 38
-#define GZPRINTF 39
-#define GZPUTS 40
-#define GZGETS 50
-#define GZPUTC 51
-#define GZGETC 52
-#define GZUNGETC 53
-#define GZFLUSH 54
-#define GZSEEK 55
-#define GZREWIND 56
-#define GZTELL 57
-#define GZOFFSET 58
-#define GZEOF 59
-#define GZDIRECT 60
-#define GZCLOSE 61
-#define GZCLOSE_R 62
-#define GZCLOSE_W 63
-#define GZERROR 64
-#define GZCLEARERR 65
+#define ZCAPCMD_GZOPEN 33
+#define ZCAPCMD_GZDOPEN 34
+#define ZCAPCMD_GZBUFFER 35
+#define ZCAPCMD_GZSETPARAMS 36
+#define ZCAPCMD_GZREAD 37
+#define ZCAPCMD_GZWRITE 38
+#define ZCAPCMD_GZPRINTF 39
+#define ZCAPCMD_GZPUTS 40
+#define ZCAPCMD_GZGETS 50
+#define ZCAPCMD_GZPUTC 51
+#define ZCAPCMD_GZGETC 52
+#define ZCAPCMD_GZUNGETC 53
+#define ZCAPCMD_GZFLUSH 54
+#define ZCAPCMD_GZSEEK 55
+#define ZCAPCMD_GZREWIND 56
+#define ZCAPCMD_GZTELL 57
+#define ZCAPCMD_GZOFFSET 58
+#define ZCAPCMD_GZEOF 59
+#define ZCAPCMD_GZDIRECT 60
+#define ZCAPCMD_GZCLOSE 61
+#define ZCAPCMD_GZCLOSE_R 62
+#define ZCAPCMD_GZCLOSE_W 63
+#define ZCAPCMD_GZERROR 64
+#define ZCAPCMD_GZCLEARERR 65
/* checksum functions */
-#define ADLER32 66
-#define ADLER32_COMBINE 67
-#define CRC32 68
-#define CRC32_COMBINE 69
+#define ZCAPCMD_ADLER32 66
+#define ZCAPCMD_ADLER32_COMBINE 67
+#define ZCAPCMD_CRC32 68
+#define ZCAPCMD_CRC32_COMBINE 69
Modified: soc2013/dpl/head/lib/libzcap/deflate.c
==============================================================================
--- soc2013/dpl/head/lib/libzcap/deflate.c Thu Aug 1 18:06:58 2013 (r255421)
+++ soc2013/dpl/head/lib/libzcap/deflate.c Thu Aug 1 19:54:43 2013 (r255422)
@@ -221,102 +221,8 @@
const char *version;
int stream_size;
{
- deflate_state *s;
- int wrap = 1;
- static const char my_version[] = ZLIB_VERSION;
-
- ushf *overlay;
- /* We overlay pending_buf and d_buf+l_buf. This works since the average
- * output size for (length,distance) codes is <= 24 bits.
- */
-
- if (version == Z_NULL || version[0] != my_version[0] ||
- stream_size != sizeof(z_stream)) {
- return Z_VERSION_ERROR;
- }
- if (strm == Z_NULL) return Z_STREAM_ERROR;
-
- strm->msg = Z_NULL;
- if (strm->zalloc == (alloc_func)0) {
-#ifdef Z_SOLO
- return Z_STREAM_ERROR;
-#else
- strm->zalloc = zcalloc;
- strm->opaque = (voidpf)0;
-#endif
- }
- if (strm->zfree == (free_func)0)
-#ifdef Z_SOLO
- return Z_STREAM_ERROR;
-#else
- strm->zfree = zcfree;
-#endif
-
-#ifdef FASTEST
- if (level != 0) level = 1;
-#else
- if (level == Z_DEFAULT_COMPRESSION) level = 6;
-#endif
-
- if (windowBits < 0) { /* suppress zlib wrapper */
- wrap = 0;
- windowBits = -windowBits;
- }
-#ifdef GZIP
- else if (windowBits > 15) {
- wrap = 2; /* write gzip wrapper instead */
- windowBits -= 16;
- }
-#endif
- if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
- windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
- strategy < 0 || strategy > Z_FIXED) {
- return Z_STREAM_ERROR;
- }
- if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
- s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
- if (s == Z_NULL) return Z_MEM_ERROR;
- strm->state = (struct internal_state FAR *)s;
- s->strm = strm;
-
- s->wrap = wrap;
- s->gzhead = Z_NULL;
- s->w_bits = windowBits;
- s->w_size = 1 << s->w_bits;
- s->w_mask = s->w_size - 1;
-
- s->hash_bits = memLevel + 7;
- s->hash_size = 1 << s->hash_bits;
- s->hash_mask = s->hash_size - 1;
- s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
-
- s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
- s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
- s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
-
- s->high_water = 0; /* nothing written to s->window yet */
-
- s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
-
- overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
- s->pending_buf = (uchf *) overlay;
- s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
-
- if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
- s->pending_buf == Z_NULL) {
- s->status = FINISH_STATE;
- strm->msg = ERR_MSG(Z_MEM_ERROR);
- deflateEnd (strm);
- return Z_MEM_ERROR;
- }
- s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
- s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
-
- s->level = level;
- s->strategy = strategy;
- s->method = (Byte)method;
-
- return deflateReset(strm);
+ return zcapcmd_deflateInit(strm, level, method, windowBits,
+ memLevel, strategy, version, stream_size);
}
/* ========================================================================= */
@@ -666,344 +572,14 @@
z_streamp strm;
int flush;
{
- int old_flush; /* value of flush param for previous deflate call */
- deflate_state *s;
-
- if (strm == Z_NULL || strm->state == Z_NULL ||
- flush > Z_BLOCK || flush < 0) {
- return Z_STREAM_ERROR;
- }
- s = strm->state;
-
- if (strm->next_out == Z_NULL ||
- (strm->next_in == Z_NULL && strm->avail_in != 0) ||
- (s->status == FINISH_STATE && flush != Z_FINISH)) {
- ERR_RETURN(strm, Z_STREAM_ERROR);
- }
- if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
-
- s->strm = strm; /* just in case */
- old_flush = s->last_flush;
- s->last_flush = flush;
-
- /* Write the header */
- if (s->status == INIT_STATE) {
-#ifdef GZIP
- if (s->wrap == 2) {
- strm->adler = crc32(0L, Z_NULL, 0);
- put_byte(s, 31);
- put_byte(s, 139);
- put_byte(s, 8);
- if (s->gzhead == Z_NULL) {
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, 0);
- put_byte(s, s->level == 9 ? 2 :
- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
- 4 : 0));
- put_byte(s, OS_CODE);
- s->status = BUSY_STATE;
- }
- else {
- put_byte(s, (s->gzhead->text ? 1 : 0) +
- (s->gzhead->hcrc ? 2 : 0) +
- (s->gzhead->extra == Z_NULL ? 0 : 4) +
- (s->gzhead->name == Z_NULL ? 0 : 8) +
- (s->gzhead->comment == Z_NULL ? 0 : 16)
- );
- put_byte(s, (Byte)(s->gzhead->time & 0xff));
- put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
- put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
- put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
- put_byte(s, s->level == 9 ? 2 :
- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
- 4 : 0));
- put_byte(s, s->gzhead->os & 0xff);
- if (s->gzhead->extra != Z_NULL) {
- put_byte(s, s->gzhead->extra_len & 0xff);
- put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
- }
- if (s->gzhead->hcrc)
- strm->adler = crc32(strm->adler, s->pending_buf,
- s->pending);
- s->gzindex = 0;
- s->status = EXTRA_STATE;
- }
- }
- else
-#endif
- {
- uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
- uInt level_flags;
-
- if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
- level_flags = 0;
- else if (s->level < 6)
- level_flags = 1;
- else if (s->level == 6)
- level_flags = 2;
- else
- level_flags = 3;
- header |= (level_flags << 6);
- if (s->strstart != 0) header |= PRESET_DICT;
- header += 31 - (header % 31);
-
- s->status = BUSY_STATE;
- putShortMSB(s, header);
-
- /* Save the adler32 of the preset dictionary: */
- if (s->strstart != 0) {
- putShortMSB(s, (uInt)(strm->adler >> 16));
- putShortMSB(s, (uInt)(strm->adler & 0xffff));
- }
- strm->adler = adler32(0L, Z_NULL, 0);
- }
- }
-#ifdef GZIP
- if (s->status == EXTRA_STATE) {
- if (s->gzhead->extra != Z_NULL) {
- uInt beg = s->pending; /* start of bytes to update crc */
-
- while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
- if (s->pending == s->pending_buf_size) {
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- flush_pending(strm);
- beg = s->pending;
- if (s->pending == s->pending_buf_size)
- break;
- }
- put_byte(s, s->gzhead->extra[s->gzindex]);
- s->gzindex++;
- }
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- if (s->gzindex == s->gzhead->extra_len) {
- s->gzindex = 0;
- s->status = NAME_STATE;
- }
- }
- else
- s->status = NAME_STATE;
- }
- if (s->status == NAME_STATE) {
- if (s->gzhead->name != Z_NULL) {
- uInt beg = s->pending; /* start of bytes to update crc */
- int val;
-
- do {
- if (s->pending == s->pending_buf_size) {
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- flush_pending(strm);
- beg = s->pending;
- if (s->pending == s->pending_buf_size) {
- val = 1;
- break;
- }
- }
- val = s->gzhead->name[s->gzindex++];
- put_byte(s, val);
- } while (val != 0);
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- if (val == 0) {
- s->gzindex = 0;
- s->status = COMMENT_STATE;
- }
- }
- else
- s->status = COMMENT_STATE;
- }
- if (s->status == COMMENT_STATE) {
- if (s->gzhead->comment != Z_NULL) {
- uInt beg = s->pending; /* start of bytes to update crc */
- int val;
-
- do {
- if (s->pending == s->pending_buf_size) {
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- flush_pending(strm);
- beg = s->pending;
- if (s->pending == s->pending_buf_size) {
- val = 1;
- break;
- }
- }
- val = s->gzhead->comment[s->gzindex++];
- put_byte(s, val);
- } while (val != 0);
- if (s->gzhead->hcrc && s->pending > beg)
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
- s->pending - beg);
- if (val == 0)
- s->status = HCRC_STATE;
- }
- else
- s->status = HCRC_STATE;
- }
- if (s->status == HCRC_STATE) {
- if (s->gzhead->hcrc) {
- if (s->pending + 2 > s->pending_buf_size)
- flush_pending(strm);
- if (s->pending + 2 <= s->pending_buf_size) {
- put_byte(s, (Byte)(strm->adler & 0xff));
- put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
- strm->adler = crc32(0L, Z_NULL, 0);
- s->status = BUSY_STATE;
- }
- }
- else
- s->status = BUSY_STATE;
- }
-#endif
-
- /* Flush as much pending output as possible */
- if (s->pending != 0) {
- flush_pending(strm);
- if (strm->avail_out == 0) {
- /* Since avail_out is 0, deflate will be called again with
- * more output space, but possibly with both pending and
- * avail_in equal to zero. There won't be anything to do,
- * but this is not an error situation so make sure we
- * return OK instead of BUF_ERROR at next call of deflate:
- */
- s->last_flush = -1;
- return Z_OK;
- }
-
- /* Make sure there is something to do and avoid duplicate consecutive
- * flushes. For repeated and useless calls with Z_FINISH, we keep
- * returning Z_STREAM_END instead of Z_BUF_ERROR.
- */
- } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
- flush != Z_FINISH) {
- ERR_RETURN(strm, Z_BUF_ERROR);
- }
-
- /* User must not provide more input after the first FINISH: */
- if (s->status == FINISH_STATE && strm->avail_in != 0) {
- ERR_RETURN(strm, Z_BUF_ERROR);
- }
-
- /* Start a new block or continue the current one.
- */
- if (strm->avail_in != 0 || s->lookahead != 0 ||
- (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
- block_state bstate;
-
- bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
- (s->strategy == Z_RLE ? deflate_rle(s, flush) :
- (*(configuration_table[s->level].func))(s, flush));
-
- if (bstate == finish_started || bstate == finish_done) {
- s->status = FINISH_STATE;
- }
- if (bstate == need_more || bstate == finish_started) {
- if (strm->avail_out == 0) {
- s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
- }
- return Z_OK;
- /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
- * of deflate should use the same flush parameter to make sure
- * that the flush is complete. So we don't have to output an
- * empty block here, this will be done at next call. This also
- * ensures that for a very small output buffer, we emit at most
- * one empty block.
- */
- }
- if (bstate == block_done) {
- if (flush == Z_PARTIAL_FLUSH) {
- _tr_align(s);
- } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
- _tr_stored_block(s, (char*)0, 0L, 0);
- /* For a full flush, this empty block will be recognized
- * as a special marker by inflate_sync().
- */
- if (flush == Z_FULL_FLUSH) {
- CLEAR_HASH(s); /* forget history */
- if (s->lookahead == 0) {
- s->strstart = 0;
- s->block_start = 0L;
- s->insert = 0;
- }
- }
- }
- flush_pending(strm);
- if (strm->avail_out == 0) {
- s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
- return Z_OK;
- }
- }
- }
- Assert(strm->avail_out > 0, "bug2");
-
- if (flush != Z_FINISH) return Z_OK;
- if (s->wrap <= 0) return Z_STREAM_END;
-
- /* Write the trailer */
-#ifdef GZIP
- if (s->wrap == 2) {
- put_byte(s, (Byte)(strm->adler & 0xff));
- put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
- put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
- put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
- put_byte(s, (Byte)(strm->total_in & 0xff));
- put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
- put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
- put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
- }
- else
-#endif
- {
- putShortMSB(s, (uInt)(strm->adler >> 16));
- putShortMSB(s, (uInt)(strm->adler & 0xffff));
- }
- flush_pending(strm);
- /* If avail_out is zero, the application will call deflate again
- * to flush the rest.
- */
- if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
- return s->pending != 0 ? Z_OK : Z_STREAM_END;
+ return zcapcmd_deflate(strm, flush);
}
/* ========================================================================= */
int ZEXPORT deflateEnd (strm)
z_streamp strm;
{
- int status;
-
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
-
- status = strm->state->status;
- if (status != INIT_STATE &&
- status != EXTRA_STATE &&
- status != NAME_STATE &&
- status != COMMENT_STATE &&
- status != HCRC_STATE &&
- status != BUSY_STATE &&
- status != FINISH_STATE) {
- return Z_STREAM_ERROR;
- }
-
- /* Deallocate in reverse order of allocations: */
- TRY_FREE(strm, strm->state->pending_buf);
- TRY_FREE(strm, strm->state->head);
- TRY_FREE(strm, strm->state->prev);
- TRY_FREE(strm, strm->state->window);
-
- ZFREE(strm, strm->state);
- strm->state = Z_NULL;
-
- return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
+ return zcapcmd_deflateEnd(strm);
}
/* =========================================================================
More information about the svn-soc-all
mailing list