Fix up new OpenLDAP MMR code.
[tprouty/samba.git] / source4 / librpc / ndr / ndr_compression.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr compression support
5
6    Copyright (C) Stefan Metzmacher 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/compression/mszip.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/ndr/ndr_compression.h"
26
27 static enum ndr_err_code ndr_pull_compression_mszip_chunk(struct ndr_pull *ndrpull,
28                                                  struct ndr_push *ndrpush,
29                                                  struct decomp_state *decomp_state,
30                                                  bool *last)
31 {
32         DATA_BLOB comp_chunk;
33         uint32_t comp_chunk_offset;
34         uint32_t comp_chunk_size;
35         DATA_BLOB plain_chunk;
36         uint32_t plain_chunk_offset;
37         uint32_t plain_chunk_size;
38         int ret;
39
40         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &plain_chunk_size));
41         if (plain_chunk_size > 0x00008000) {
42                 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad MSZIP plain chunk size %08X > 0x00008000 (PULL)", 
43                                       plain_chunk_size);
44         }
45
46         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &comp_chunk_size));
47
48         DEBUG(10,("MSZIP plain_chunk_size: %08X (%u) comp_chunk_size: %08X (%u)\n",
49                   plain_chunk_size, plain_chunk_size, comp_chunk_size, comp_chunk_size));
50
51         comp_chunk_offset = ndrpull->offset;
52         NDR_CHECK(ndr_pull_advance(ndrpull, comp_chunk_size));
53         comp_chunk.length = comp_chunk_size;
54         comp_chunk.data = ndrpull->data + comp_chunk_offset;
55
56         plain_chunk_offset = ndrpush->offset;
57         NDR_CHECK(ndr_push_zero(ndrpush, plain_chunk_size));
58         plain_chunk.length = plain_chunk_size;
59         plain_chunk.data = ndrpush->data + plain_chunk_offset;
60
61         ret = ZIPdecompress(decomp_state, &comp_chunk, &plain_chunk);
62         if (ret != DECR_OK) {
63                 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZIPdecompress() error %d (PULL)",
64                                       ret);
65         }
66
67         if ((plain_chunk_size < 0x00008000) || (ndrpull->offset+4 >= ndrpull->data_size)) {
68                 /* this is the last chunk */
69                 *last = true;
70         }
71
72         return NDR_ERR_SUCCESS;
73 }
74
75 static enum ndr_err_code ndr_pull_compression_mszip(struct ndr_pull *subndr,
76                                            struct ndr_pull **_comndr,
77                                            ssize_t decompressed_len)
78 {
79         struct ndr_push *ndrpush;
80         struct ndr_pull *comndr;
81         DATA_BLOB uncompressed;
82         uint32_t payload_header[4];
83         uint32_t payload_size;
84         uint32_t payload_offset;
85         uint8_t *payload;
86         struct decomp_state *decomp_state;
87         bool last = false;
88
89         ndrpush = ndr_push_init_ctx(subndr, subndr->iconv_convenience);
90         NDR_ERR_HAVE_NO_MEMORY(ndrpush);
91
92         decomp_state = ZIPdecomp_state(subndr);
93         NDR_ERR_HAVE_NO_MEMORY(decomp_state);
94
95         while (!last) {
96                 NDR_CHECK(ndr_pull_compression_mszip_chunk(subndr, ndrpush, decomp_state, &last));
97         }
98
99         uncompressed = ndr_push_blob(ndrpush);
100
101         if (uncompressed.length != decompressed_len) {
102                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad MSZIP uncompressed_len [%u] != [%d] (PULL)",
103                                       (int)uncompressed.length, (int)decompressed_len);
104         }
105
106         comndr = talloc_zero(subndr, struct ndr_pull);
107         NDR_ERR_HAVE_NO_MEMORY(comndr);
108         comndr->flags           = subndr->flags;
109         comndr->current_mem_ctx = subndr->current_mem_ctx;
110
111         comndr->data            = uncompressed.data;
112         comndr->data_size       = uncompressed.length;
113         comndr->offset          = 0;
114
115         comndr->iconv_convenience = talloc_reference(comndr, subndr->iconv_convenience);
116
117         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[0]));
118         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[1]));
119         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[2]));
120         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[3]));
121
122         if (payload_header[0] != 0x00081001) {
123                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad MSZIP payload_header[0] [0x%08X] != [0x00081001] (PULL)",
124                                       payload_header[0]);
125         }
126         if (payload_header[1] != 0xCCCCCCCC) {
127                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad MSZIP payload_header[1] [0x%08X] != [0xCCCCCCCC] (PULL)",
128                                       payload_header[1]);
129         }
130
131         payload_size = payload_header[2];
132
133         if (payload_header[3] != 0x00000000) {
134                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad MSZIP payload_header[3] [0x%08X] != [0x00000000] (PULL)",
135                                       payload_header[3]);
136         }
137
138         payload_offset = comndr->offset;
139         NDR_CHECK(ndr_pull_advance(comndr, payload_size));
140         payload = comndr->data + payload_offset;
141
142         comndr->data            = payload;
143         comndr->data_size       = payload_size;
144         comndr->offset          = 0;
145
146         *_comndr = comndr;
147         return NDR_ERR_SUCCESS;
148 }
149
150 static enum ndr_err_code ndr_push_compression_mszip(struct ndr_push *subndr,
151                                            struct ndr_push *comndr)
152 {
153         return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Sorry MSZIP compression is not supported yet (PUSH)");
154 }
155
156 static enum ndr_err_code ndr_pull_compression_xpress_chunk(struct ndr_pull *ndrpull,
157                                                   struct ndr_push *ndrpush,
158                                                   bool *last)
159 {
160         DATA_BLOB comp_chunk;
161         uint32_t comp_chunk_offset;
162         uint32_t comp_chunk_size;
163         uint32_t plain_chunk_size;
164
165         comp_chunk_offset = ndrpull->offset;
166
167         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &plain_chunk_size));
168         if (plain_chunk_size > 0x00010000) {
169                 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad XPRESS plain chunk size %08X > 0x00010000 (PULL)", 
170                                       plain_chunk_size);
171         }
172
173         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &comp_chunk_size));
174
175         NDR_CHECK(ndr_pull_advance(ndrpull, comp_chunk_size));
176         comp_chunk.length = comp_chunk_size + 8;
177         comp_chunk.data = ndrpull->data + comp_chunk_offset;
178
179         DEBUG(10,("XPRESS plain_chunk_size: %08X (%u) comp_chunk_size: %08X (%u)\n",
180                   plain_chunk_size, plain_chunk_size, comp_chunk_size, comp_chunk_size));
181
182         /* For now, we just copy over the compressed blob */
183         NDR_CHECK(ndr_push_bytes(ndrpush, comp_chunk.data, comp_chunk.length));
184
185         if ((plain_chunk_size < 0x00010000) || (ndrpull->offset+4 >= ndrpull->data_size)) {
186                 /* this is the last chunk */
187                 *last = true;
188         }
189
190         return NDR_ERR_SUCCESS;
191 }
192
193 static enum ndr_err_code ndr_pull_compression_xpress(struct ndr_pull *subndr,
194                                             struct ndr_pull **_comndr,
195                                             ssize_t decompressed_len)
196 {
197         struct ndr_push *ndrpush;
198         struct ndr_pull *comndr;
199         DATA_BLOB uncompressed;
200         bool last = false;
201
202         ndrpush = ndr_push_init_ctx(subndr, subndr->iconv_convenience);
203         NDR_ERR_HAVE_NO_MEMORY(ndrpush);
204
205         while (!last) {
206                 NDR_CHECK(ndr_pull_compression_xpress_chunk(subndr, ndrpush, &last));
207         }
208
209         uncompressed = ndr_push_blob(ndrpush);
210
211         comndr = talloc_zero(subndr, struct ndr_pull);
212         NDR_ERR_HAVE_NO_MEMORY(comndr);
213         comndr->flags           = subndr->flags;
214         comndr->current_mem_ctx = subndr->current_mem_ctx;
215
216         comndr->data            = uncompressed.data;
217         comndr->data_size       = uncompressed.length;
218         comndr->offset          = 0;
219
220         comndr->iconv_convenience = talloc_reference(comndr, subndr->iconv_convenience);
221
222         *_comndr = comndr;
223         return NDR_ERR_SUCCESS;
224 }
225
226 static enum ndr_err_code ndr_push_compression_xpress(struct ndr_push *subndr,
227                                             struct ndr_push *comndr)
228 {
229         return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "XPRESS compression is not supported yet (PUSH)");
230 }
231
232 /*
233   handle compressed subcontext buffers, which in midl land are user-marshalled, but
234   we use magic in pidl to make them easier to cope with
235 */
236 enum ndr_err_code ndr_pull_compression_start(struct ndr_pull *subndr,
237                                     struct ndr_pull **_comndr,
238                                     enum ndr_compression_alg compression_alg,
239                                     ssize_t decompressed_len)
240 {
241         switch (compression_alg) {
242         case NDR_COMPRESSION_MSZIP:
243                 return ndr_pull_compression_mszip(subndr, _comndr, decompressed_len);
244         case NDR_COMPRESSION_XPRESS:
245                 return ndr_pull_compression_xpress(subndr, _comndr, decompressed_len);
246         default:
247                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PULL)", 
248                                       compression_alg);
249         }
250         return NDR_ERR_SUCCESS;
251 }
252
253 enum ndr_err_code ndr_pull_compression_end(struct ndr_pull *subndr,
254                                   struct ndr_pull *comndr,
255                                   enum ndr_compression_alg compression_alg,
256                                   ssize_t decompressed_len)
257 {
258         return NDR_ERR_SUCCESS;
259 }
260
261 /*
262   push a compressed subcontext
263 */
264 enum ndr_err_code ndr_push_compression_start(struct ndr_push *subndr,
265                                     struct ndr_push **_comndr,
266                                     enum ndr_compression_alg compression_alg,
267                                     ssize_t decompressed_len)
268 {
269         struct ndr_push *comndr;
270
271         comndr = ndr_push_init_ctx(subndr, subndr->iconv_convenience);
272         NDR_ERR_HAVE_NO_MEMORY(comndr);
273         comndr->flags   = subndr->flags;
274
275         *_comndr = comndr;
276         return NDR_ERR_SUCCESS;
277 }
278
279 /*
280   push a compressed subcontext
281 */
282 enum ndr_err_code ndr_push_compression_end(struct ndr_push *subndr,
283                                   struct ndr_push *comndr,
284                                   enum ndr_compression_alg compression_alg,
285                                   ssize_t decompressed_len)
286 {
287         switch (compression_alg) {
288         case NDR_COMPRESSION_MSZIP:
289                 return ndr_push_compression_mszip(subndr, comndr);
290         case NDR_COMPRESSION_XPRESS:
291                 return ndr_push_compression_xpress(subndr, comndr);
292         default:
293                 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PUSH)", 
294                                       compression_alg);
295         }
296         return NDR_ERR_SUCCESS;
297 }