1 /*----------------------------------------------------------------------------
2 Name: xmlBlasterZlib.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: SOCKET internal header (not included directly by clients)
6 Author: "Marcel Ruff" <xmlBlaster@marcelruff.info>
7 See: http://www.gzip.org/zlib/
8 -----------------------------------------------------------------------------*/
9 #ifndef XMLBLASTER_ZLIB_H
10 #define XMLBLASTER_ZLIB_H
11
12 #ifdef __cplusplus
13 #ifndef XMLBLASTER_C_COMPILE_AS_CPP /* 'g++ -DXMLBLASTER_C_COMPILE_AS_CPP ...' allows to compile the lib as C++ code */
14 extern "C" {
15 #endif
16 #endif
17
18 #include <util/basicDefs.h>
19 #if XMLBLASTER_ZLIB==1
20 # include <zlib.h>
21 #endif
22
23 #define XMLBLASTER_ZLIB_WRITE_COMPBUFFER_LEN 20000
24
25 /**
26 * Helper struct to compress a byte buffer before putting it into the socket.
27 *
28 * We need exactly one instance of this for each socket
29 * and the access must be synchronized to be thread safe.
30 *
31 * Different instances of this struct are thread safe.
32 * @see http://www.gzip.org/zlib/
33 */
34 typedef struct XmlBlasterZlibWriteBuffers {
35 int debug; /**< 0: no debugging, 1: switch on debugging */
36 #if XMLBLASTER_ZLIB==1
37 z_stream c_stream; /**< zlib compression stream structure */
38 Bytef compBuffer[XMLBLASTER_ZLIB_WRITE_COMPBUFFER_LEN]; /**< buffer to hold temporary the outgoing compressed bytes */
39 #endif
40 } XmlBlasterZlibWriteBuffers;
41
42
43 #define XMLBLASTER_ZLIB_READ_COMPBUFFER_LEN 20000
44
45 /**
46 * Helper struct to uncompress a byte buffer coming from the socket connection.
47 *
48 * We need exactly one instance of this for each socket
49 * and the access must be synchronized to be thread safe.
50 *
51 * Different instances of this struct are thread safe.
52 */
53 typedef struct XmlBlasterZlibReadBuffers {
54 int debug; /**< 0: no debugging, 1: switch on debugging */
55 #if XMLBLASTER_ZLIB==1
56 z_stream c_stream; /**< zlib compression stream structure */
57 Bytef compBuffer[XMLBLASTER_ZLIB_READ_COMPBUFFER_LEN]; /**< buffer to hold temporary the incoming compressed bytes */
58 Bytef *currCompBufferP; /**< Pointer into compBuffer, points on current start position */
59 uInt currCompBytes; /**< Number of compressed bytes in compBuffer, starting at currCompBufferP */
60 #endif
61 } XmlBlasterZlibReadBuffers;
62
63
64 /**
65 * Call only once for a socket connection stream.
66 * @return Z_OK==0 or error from deflateInit()
67 */
68 /*Dll_Export*/extern int xmlBlaster_initZlibWriter(XmlBlasterZlibWriteBuffers *zlibWriteBufP);
69
70 /**
71 * Compress given bytes with zlib and write them to the socket
72 * @param fd The socket descriptor
73 * @param ptr The buffer with raw bytes
74 * @param nbytes The number of bytes in 'ptr'
75 * @return Number of bytes written
76 */
77 extern ssize_t xmlBlaster_writenCompressed(XmlBlasterZlibWriteBuffers *zlibWriteBufP, const int fd, const char * const ptr, size_t const nbytes);
78
79 /**
80 * @see xmlBlasterZlib.h
81 */
82 extern int xmlBlaster_endZlibWriter(XmlBlasterZlibWriteBuffers *zlibWriteBufP);
83
84 /**
85 * Call only once for a socket connection stream.
86 * @return Z_OK==0 or error from inflateInit()
87 */
88 extern int xmlBlaster_initZlibReader(XmlBlasterZlibReadBuffers *zlibReadBufP);
89
90 /**
91 * Read compressed data from the socket and uncompress it.
92 * @param zlibReadBufP Struct holding necessary variables to use zlib
93 * @param fd The socket descriptor
94 * @param ptr The empty buffer which gets filled with raw bytes from socket (out parameter)
95 * @param nbytes The max. size of 'ptr'
96 * @return number of bytes read, -1 is EOF
97 */
98 extern ssize_t xmlBlaster_readnCompressed(XmlBlasterZlibReadBuffers *zlibReadBufP, int fd, char *ptr, size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP2);
99
100 /**
101 * Cleanup after socket is closed.
102 * @param zlibReadBufP Struct holding necessary variables to use zlib
103 */
104 extern int xmlBlaster_endZlibReader(XmlBlasterZlibReadBuffers *zlibReadBufP);
105
106 #ifdef __cplusplus
107 #ifndef XMLBLASTER_C_COMPILE_AS_CPP
108 } /* extern "C" */
109 #endif
110 #endif
111
112 #endif /* XMLBLASTER_ZLIB_H */
syntax highlighted by Code2HTML, v. 0.9.1