nmsg  0.9.0
zbuf.c
1 /*
2  * Copyright (c) 2009, 2011, 2012 by Farsight Security, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* Import. */
18 
19 #include "private.h"
20 
21 /* Private declarations. */
22 
23 typedef enum nmsg_zbuf_type {
24  nmsg_zbuf_type_deflate,
25  nmsg_zbuf_type_inflate
26 } nmsg_zbuf_type;
27 
28 struct nmsg_zbuf {
29  nmsg_zbuf_type type;
30  z_stream zs;
31 };
32 
33 /* Export. */
34 
35 nmsg_zbuf_t
37  int zret;
38  struct nmsg_zbuf *zb;
39 
40  zb = malloc(sizeof(*zb));
41  if (zb == NULL)
42  return (NULL);
43 
44  zb->type = nmsg_zbuf_type_deflate;
45  zb->zs.zalloc = Z_NULL;
46  zb->zs.zfree = Z_NULL;
47  zb->zs.opaque = Z_NULL;
48 
49  zret = deflateInit(&zb->zs, Z_DEFAULT_COMPRESSION);
50  if (zret != Z_OK) {
51  free(zb);
52  return (NULL);
53  }
54 
55  return (zb);
56 }
57 
58 nmsg_zbuf_t
60  int zret;
61  struct nmsg_zbuf *zb;
62 
63  zb = malloc(sizeof(*zb));
64  if (zb == NULL)
65  return (NULL);
66 
67  zb->type = nmsg_zbuf_type_inflate;
68  zb->zs.zalloc = Z_NULL;
69  zb->zs.zfree = Z_NULL;
70  zb->zs.opaque = Z_NULL;
71  zb->zs.avail_in = 0;
72  zb->zs.next_in = Z_NULL;
73 
74  zret = inflateInit(&zb->zs);
75  if (zret != Z_OK) {
76  free(zb);
77  return (NULL);
78  }
79 
80  return (zb);
81 }
82 
83 void
84 nmsg_zbuf_destroy(nmsg_zbuf_t *zb) {
85  if (*zb != NULL) {
86  if ((*zb)->type == nmsg_zbuf_type_deflate)
87  deflateEnd(&(*zb)->zs);
88  else if ((*zb)->type == nmsg_zbuf_type_inflate)
89  inflateEnd(&(*zb)->zs);
90  free(*zb);
91  *zb = NULL;
92  }
93 }
94 
96 nmsg_zbuf_deflate(nmsg_zbuf_t zb, size_t len, u_char *buf,
97  size_t *z_len, u_char *z_buf)
98 {
99  int zret;
100 
101  store_net32(z_buf, (uint32_t) len);
102  z_buf += 4;
103 
104  zb->zs.avail_in = len;
105  zb->zs.next_in = buf;
106  zb->zs.avail_out = *z_len;
107  zb->zs.next_out = z_buf;
108 
109  zret = deflate(&zb->zs, Z_FINISH);
110  assert(zret == Z_STREAM_END);
111  assert(zb->zs.avail_in == 0);
112  *z_len = *z_len - zb->zs.avail_out + sizeof(uint32_t);
113  assert(deflateReset(&zb->zs) == Z_OK);
114  assert(*z_len > 0);
115 
116  return (nmsg_res_success);
117 }
118 
119 nmsg_res
120 nmsg_zbuf_inflate(nmsg_zbuf_t zb, size_t z_len, u_char *z_buf,
121  size_t *u_len, u_char **u_buf)
122 {
123  int zret;
124  uint32_t my_ulen;
125 
126  load_net32(z_buf, &my_ulen);
127  z_buf += 4;
128  *u_len = my_ulen;
129 
130  *u_buf = malloc(*u_len);
131  if (*u_buf == NULL)
132  return (nmsg_res_memfail);
133 
134  zb->zs.avail_in = z_len;
135  zb->zs.next_in = z_buf;
136  zb->zs.avail_out = *u_len;
137  zb->zs.next_out = *u_buf;
138 
139  zret = inflate(&zb->zs, Z_NO_FLUSH);
140  if (zret != Z_STREAM_END || zb->zs.avail_out != 0) {
141  free(*u_buf);
142  return (nmsg_res_failure);
143  }
144  assert(inflateReset(&zb->zs) == Z_OK);
145 
146  return (nmsg_res_success);
147 }
void nmsg_zbuf_destroy(nmsg_zbuf_t *zb)
Destroy all resources associated with an nmsg_zbuf_t object.
Definition: zbuf.c:84
nmsg_res
nmsg result code
Definition: res.h:25
success
Definition: res.h:26
out of memory
Definition: res.h:29
nmsg_zbuf_t nmsg_zbuf_inflate_init(void)
Initialize an nmsg_zbuf_t object for inflation.
Definition: zbuf.c:59
nmsg_res nmsg_zbuf_deflate(nmsg_zbuf_t zb, size_t len, u_char *buf, size_t *z_len, u_char *z_buf)
Deflate a buffer.
Definition: zbuf.c:96
nmsg_zbuf_t nmsg_zbuf_deflate_init(void)
Initialize an nmsg_zbuf_t object for deflation.
Definition: zbuf.c:36
generic failure
Definition: res.h:27
nmsg_res nmsg_zbuf_inflate(nmsg_zbuf_t zb, size_t z_len, u_char *z_buf, size_t *u_len, u_char **u_buf)
Inflate a buffer.
Definition: zbuf.c:120