socsvn commit: r256694 - soc2013/dpl/head/lib/libzcap/zlibworker
dpl at FreeBSD.org
dpl at FreeBSD.org
Thu Aug 29 15:57:15 UTC 2013
Author: dpl
Date: Thu Aug 29 15:57:15 2013
New Revision: 256694
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=256694
Log:
Made zlibworker.c a little more more style(9)ish.
Now we have to use the correct data structures, and not those in zlib.h if we want this to work.
Added:
soc2013/dpl/head/lib/libzcap/zlibworker/commands.h
Modified:
soc2013/dpl/head/lib/libzcap/zlibworker/commands.c
soc2013/dpl/head/lib/libzcap/zlibworker/zlibworker.c
Modified: soc2013/dpl/head/lib/libzcap/zlibworker/commands.c
==============================================================================
--- soc2013/dpl/head/lib/libzcap/zlibworker/commands.c Thu Aug 29 15:06:01 2013 (r256693)
+++ soc2013/dpl/head/lib/libzcap/zlibworker/commands.c Thu Aug 29 15:57:15 2013 (r256694)
@@ -1,12 +1,14 @@
-#include "../commands.h"
-/* We need the internal_state structs */
-#include "../deflate.h"
-#include <zlib.h>
+#include <err.h>
#include <nv.h>
-
#include <stdlib.h>
#include <string.h>
-#include <err.h>
+#include <zlib.h>
+
+#include "commands.h"
+/* XXX */
+/* We need to keep all the recieved structs in an array, to store them safely */
+struct gzFile_s **gzContainer = malloc(5 * sizseof(struct gzFile_s**));
+z_stream **streamContainer = malloc(5 * sizseof(struct gzFile_s**));
nvlist_t * args;
extern int zero;
@@ -15,6 +17,10 @@
size_t gzsize = sizeof(struct gzFile_s);
size_t zstreamsize = sizeof(z_stream);
+/*
+ * All the functions in this file implement its related
+ * zlib functions.
+ */
/* Basic functions */
void
@@ -27,16 +33,16 @@
if ((stream = malloc(zstreamsize)) == NULL)
err(1, "deflateInit: Can't allocate memory");
- zstrmtemp = (z_streamp)nvlist_take_binary(args, "strm", &zstreamsize);
+ zstrmtemp = (z_streamp)nvlist_get_binary(args, "strm", &zstreamsize);
memcpy(stream, zstrmtemp , zstreamsize);
ret = deflateInit2_(stream,
- nvlist_take_number(args, "level"),
- nvlist_take_number(args, "method"),
- nvlist_take_number(args, "windowBits"),
- nvlist_take_number(args, "memLevel"),
- nvlist_take_number(args, "strategy"),
- nvlist_take_string(args, "version"),
- nvlist_take_number(args, "stream_size")
+ nvlist_get_number(args, "level"),
+ nvlist_get_number(args, "method"),
+ nvlist_get_number(args, "windowBits"),
+ nvlist_get_number(args, "memLevel"),
+ nvlist_get_number(args, "strategy"),
+ nvlist_get_string(args, "version"),
+ nvlist_get_number(args, "stream_size")
);
nvlist_add_number(result, "result", ret);
@@ -57,14 +63,16 @@
z_streamp zstrmtemp;
/* We save the z_stream into memory. */
- zstrmtemp = (z_streamp)nvlist_take_binary(args, "strm", &zstreamsize);
+ zstrmtemp = (z_streamp)nvlist_get_binary(args, "strm", &zstreamsize);
fprintf(stderr, "zlibworker: zcapcmd_deflateEnd: zstrmtemp: %p\n", zstrmtemp);
ret = deflateEnd(zstrmtemp);
fprintf(stderr, "zlibworker: zcapcmd_deflateEnd: ret: %lu\n", ret);
nvlist_add_number(result, "result", ret );
nvlist_add_binary(result, "newstrm", zstrmtemp, zstreamsize);
- free(zstrmtemp->state->strm);
+ /* XXX */
+ /* The first element of state is a pointer that points back to the strm */
+ free(*(zstrmtemp->state);
}
void
@@ -77,12 +85,12 @@
if ((stream = malloc(zstreamsize)) == NULL)
err(1, "deflateInit: Can't allocate memory");
- zstrmtemp = (z_streamp)nvlist_take_binary(args, "strm", &zstreamsize);
+ zstrmtemp = (z_streamp)nvlist_get_binary(args, "strm", &zstreamsize);
memcpy(stream, zstrmtemp , zstreamsize);
ret = inflateInit2_(stream,
- nvlist_take_number(args, "windowBits"),
- nvlist_take_string(args, "version"),
- nvlist_take_number(args, "stream_size")
+ nvlist_get_number(args, "windowBits"),
+ nvlist_get_string(args, "version"),
+ nvlist_get_number(args, "stream_size")
);
nvlist_add_number(result, "result", ret);
@@ -102,7 +110,7 @@
/*uLong ret = -1;
ret = inflateEnd(
- nvlist_take_number(args, "sourceLen")
+ nvlist_get_number(args, "sourceLen")
);
nvlist_add_number(result, "result", ret ); */
}
@@ -221,15 +229,9 @@
zcapcmd_compressBound(nvlist_t *args, nvlist_t *result)
{
uLong ret = -1;
- uLong number = 0;
-
- if ( nvlist_exists(args, "sourceLen") ) {
- number = nvlist_take_number(args, "sourceLen");
- ret = compressBound(number);
- } else
- err(1, "compressBound: sourceLen doesn't exist");
+ ret = compressBound(nvlist_get_number(args, "sourceLen"));
- nvlist_add_number(result, "result", ret );
+ nvlist_add_number(result, "result", ret);
}
/* gzip file functions */
@@ -239,8 +241,8 @@
void *ret = NULL;
ret = gzdopen(
- nvlist_take_descriptor(args, "fd"),
- nvlist_take_string(args, "mode")
+ nvlist_get_descriptor(args, "fd"),
+ nvlist_get_string(args, "mode")
);
nvlist_add_binary(result, "result", ret, gzsize);
@@ -252,8 +254,8 @@
int ret = -1;
ret = gzbuffer(
- nvlist_take_binary(args, "file", &gzsize),
- nvlist_take_number(args, "size")
+ (gzFile)nvlist_get_binary(args, "file", &gzsize),
+ nvlist_get_number(args, "size")
);
nvlist_add_number(result, "result", ret);
@@ -265,9 +267,9 @@
int ret = -1;
ret = gzsetparams(
- nvlist_take_binary(args, "file", &gzsize),
- nvlist_take_number(args, "level"),
- nvlist_take_number(args, "strategy")
+ (gzFile)nvlist_get_binary(args, "file", &gzsize),
+ nvlist_get_number(args, "level"),
+ nvlist_get_number(args, "strategy")
);
nvlist_add_number(result, "result", ret);
@@ -279,9 +281,9 @@
int ret = -1;
int len = 0;
- len = nvlist_take_number(args, "len");
+ len = nvlist_get_number(args, "len");
ret = gzread(
- nvlist_take_binary(args, "file", &gzsize),
+ (gzFile)nvlist_get_binary(args, "file", &gzsize),
data,
len
);
@@ -302,7 +304,8 @@
memcpy(data, nvlist_take_binary(args, "data", &len), len);
ret = gzwrite(
nvlist_take_binary(args, "file", &gzsize),
- data, len);
+ data, len
+ );
nvlist_add_number(result, "result", ret);
zero = 1;
@@ -405,7 +408,7 @@
gzFile file = NULL;
file = nvlist_take_binary(args, "file", &gzsize);
- switch( nvlist_take_number(args, "command") ){
+ switch(nvlist_take_number(args, "command")) {
case (ZCAPCMD_GZGETC):
ret = gzgetc(file);
break;
@@ -478,7 +481,7 @@
uLong adler2 = nvlist_take_number(args, "adler2");
z_off_t len2 = nvlist_take_number(args, "len2");
ret = adler32_combine(adler1, adler2, len2);
- fprintf("zlibworker: adler32_combine: %d\n", ret);
+ fprintf(stderr, "zlibworker: adler32_combine: %lu\n", ret);
nvlist_add_number(result, "result", ret );
}
Added: soc2013/dpl/head/lib/libzcap/zlibworker/commands.h
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ soc2013/dpl/head/lib/libzcap/zlibworker/commands.h Thu Aug 29 15:57:15 2013 (r256694)
@@ -0,0 +1,82 @@
+/*
+ This is a list of all the capsizumized zlib interfaces.
+ Every one of the capsicumized functions will have a define.
+ This list is taken from zlib.h, in this same directory.
+
+ All this defines represent the commands passed to the real
+ zlib listening through a program, and it will recognize them.
+ Also, the defines have been checked for not being duplicates.
+
+ Since the only things that we can Capsicumize are: deflate()
+ and inflate(), we only have to care about sending those to
+ commands (related to the basic functions, and utility functions.
+
+*/
+
+#define SOCKETFILENO 3
+
+#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 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 ZCAPCMD_COMPRESSBOUND 29
+
+/* gzip file access functions */
+#define ZCAPCMD_GZOPEN 30
+#define ZCAPCMD_GZBUFFER 31
+#define ZCAPCMD_GZSETPARAMS 32
+#define ZCAPCMD_GZREAD 33
+#define ZCAPCMD_GZWRITE 34
+#define ZCAPCMD_GZPRINTF 35
+#define ZCAPCMD_GZPUTS 36
+#define ZCAPCMD_GZGETS 46
+#define ZCAPCMD_GZPUTC 47
+#define ZCAPCMD_GZGETC 48
+#define ZCAPCMD_GZUNGETC 49
+#define ZCAPCMD_GZFLUSH 50
+#define ZCAPCMD_GZSEEK 51
+#define ZCAPCMD_GZREWIND 52
+#define ZCAPCMD_GZTELL 53
+#define ZCAPCMD_GZOFFSET 54
+#define ZCAPCMD_GZEOF 55
+#define ZCAPCMD_GZDIRECT 56
+#define ZCAPCMD_GZCLOSE 57
+#define ZCAPCMD_GZCLOSE_R 58
+#define ZCAPCMD_GZCLOSE_W 59
+#define ZCAPCMD_GZERROR 60
+#define ZCAPCMD_GZCLEARERR 61
+
+/* checksum functions */
+#define ZCAPCMD_ADLER32 62
+#define ZCAPCMD_ADLER32_COMBINE 63
+#define ZCAPCMD_CRC32 64
+#define ZCAPCMD_CRC32_COMBINE 65
Modified: soc2013/dpl/head/lib/libzcap/zlibworker/zlibworker.c
==============================================================================
--- soc2013/dpl/head/lib/libzcap/zlibworker/zlibworker.c Thu Aug 29 15:06:01 2013 (r256693)
+++ soc2013/dpl/head/lib/libzcap/zlibworker/zlibworker.c Thu Aug 29 15:57:15 2013 (r256694)
@@ -1,12 +1,16 @@
-#include <zlib.h>
-#include <nv.h>
#include <sys/capability.h>
+#include <err.h>
+#include <nv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <err.h>
-#include "../commands.h"
+#include <zlib.h>
+
+#include "commands.h"
+
+int main(int argc, char *argv[]);
+static void destroy(nvlist_t *nvl, nvlist_t *args, nvlist_t *results);
/* Basic functions */
extern void zcapcmd_deflateInit(nvlist_t *args, nvlist_t *result);
@@ -66,28 +70,47 @@
extern void zcapcmd_crc32(nvlist_t *args, nvlist_t *result);
extern void zcapcmd_crc32_combine(nvlist_t *args, nvlist_t *result);
-/* Points to a 5kb buffer */
+/* Points to a 5kb buffer to be used when passing data around. */
void * data;
-/* Zero == 1 when we need to zero out data */
+/* Zero is set when we need to zero out data */
int zero = 0;
+/* Deletes nvlists */
+static void
+destroy(nvlist_t *nvl, nvlist_t *args, nvlist_t *results)
+{
+ nvlist_destroy(nvl);
+ nvlist_destroy(args);
+ nvlist_destroy(results);
+}
+
+/*
+ * This program recieves "commands" from zcaplib. The commands are nothing
+ * more than a nvlist with an integer, and other nvlist, called "args".
+ * "args" contains the needed argument for the functions. If the library
+ * needs to pass data back and forth (like when using deflate(), or
+ * inflate(), it does in 5Kb buffers. In most cases zlibworker keeps the
+ * good version of a zlib used struct. The user only has its pointers
+ * (that, when used inside zlibworker make complete sense).
+ */
int
main(int argc, char *argv[])
{
nvlist_t *nvl, *args, *result;
/* Sandbox the process */
- cap_enter();
+ if (cap_enter() < 0)
+ err(1, "Couldn't enter capability mode");
if ((data = calloc(5*1024, 1)) == NULL)
err(1, "malloc\n");
- while(1) {
+ for(;;) {
if ((result = nvlist_create(0)) == NULL)
err(1, "Can't create result.\n");
fprintf(stderr, "DEBUG: zlibworker waiting command\n");
- if ((nvl = nvlist_recv(3)) != NULL) {
+ if ((nvl = nvlist_recv(SOCKETFILENO)) != NULL) {
if (!nvlist_exists(nvl, "command"))
err(1, "No command.\n");
@@ -103,7 +126,7 @@
fprintf(stderr, "DEBUG: zlibworker got command\n");
// Switch for "command"
// Get args, and call the real lib.
- switch( nvlist_take_number(nvl, "command") ){
+ switch (nvlist_take_number(nvl, "command")){
/* Basic functions */
case (ZCAPCMD_DEFLATEINIT):
zcapcmd_deflateInit(args, result);
@@ -234,6 +257,7 @@
/*
* All this commands have gzFile as argument,
* and return an int (or void, in the last case).
+ * Also, fallthrough.
*/
case (ZCAPCMD_GZGETC):
case (ZCAPCMD_GZREWIND):
@@ -269,14 +293,14 @@
}
fprintf(stderr, "DEBUG: zlibworker about to send result\n");
- if( nvlist_send(3, result) != 0 )
+ if (nvlist_send(SOCKETFILENO, result) != 0)
err(1, "Couldn't send response\n");
fprintf(stderr, "DEBUG: zlibworker has sent result back\n");
- nvlist_destroy(result);
- nvlist_destroy(args);
- nvlist_destroy(nvl);
- if (zero) memset(data, 0, 5*1024);
+
+ destroy(nvl, args, result);
+ if (zero)
+ memset(data, 0, 5*1024);
}
- return (0);
+ return(0);
}
\ No newline at end of file
More information about the svn-soc-all
mailing list