2 Unix SMB/CIFS implementation.
4 libndr compression support
6 Copyright (C) Stefan Metzmacher 2005
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 static NTSTATUS ndr_pull_compression_zlib(struct ndr_pull *subndr,
29 struct ndr_pull *comndr,
30 ssize_t decompressed_len)
33 DATA_BLOB outbuf = data_blob_talloc(comndr, NULL, decompressed_len);
34 uint32_t outbuf_len = outbuf.length;
40 if (subndr->data_size < 10) {
41 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB compressed header (PULL) subcontext size %d",
45 inbuf.data = subndr->data+10;
46 inbuf.length = subndr->data_size-10;
48 zs.avail_in = inbuf.length;
49 zs.next_in = inbuf.data;
50 zs.next_out = outbuf.data;
51 zs.avail_out = outbuf.length;
53 ret = inflateInit2(&zs, 15);
55 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflateInit2 error %d",
60 ret = inflate(&zs, Z_SYNC_FLUSH);
61 if (ret == Z_STREAM_END) {
63 DEBUG(0,("inbuf.length: %d avail_in: %d, avail_out: %d\n", inbuf.length, zs.avail_in, zs.avail_out));
67 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflate error %d",
74 /* TODO: check if the decompressed_len == outbuf_len */
75 outbuf.length = outbuf_len - zs.avail_out;
77 if (outbuf.length < 16) {
78 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB uncompressed header (PULL) uncompressed size %d",
85 /* TODO: really decompress the data here */
87 comndr->data = outbuf.data;
88 comndr->data_size = outbuf.length;
94 static NTSTATUS ndr_push_compression_zlib(struct ndr_push *subndr,
95 struct ndr_push *comndr)
98 DATA_BLOB outbuf = data_blob_talloc(comndr, NULL, comndr->offset + 10);
104 inbuf = ndr_push_blob(comndr);
106 zs.avail_in = inbuf.length;
107 zs.next_in = inbuf.data;
108 zs.next_out = outbuf.data+10;
109 zs.avail_out = outbuf.length-10;
111 ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
113 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PUSH) deflateInit2 error %d",
117 ret = deflate(&zs, Z_SYNC_FLUSH);
119 if (ret != Z_OK && ret != Z_STREAM_END) {
120 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) deflate error %d",
126 /* TODO: push the header here */
129 NDR_CHECK(ndr_push_bytes(subndr, outbuf.data, outbuf.length));
136 handle compressed subcontext buffers, which in midl land are user-marshalled, but
137 we use magic in pidl to make them easier to cope with
139 NTSTATUS ndr_pull_compression(struct ndr_pull *subndr,
140 struct ndr_pull *comndr,
141 enum ndr_compression_alg compression_alg,
142 ssize_t decompressed_len)
144 comndr->flags = subndr->flags;
146 switch (compression_alg) {
148 case NDR_COMPRESSION_ZLIB:
149 return ndr_pull_compression_zlib(subndr, comndr, decompressed_len);
152 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PULL)",
159 push a compressed subcontext
161 NTSTATUS ndr_push_compression(struct ndr_push *subndr,
162 struct ndr_push *comndr,
163 enum ndr_compression_alg compression_alg)
165 comndr->flags = subndr->flags;
167 switch (compression_alg) {
169 case NDR_COMPRESSION_ZLIB:
170 return ndr_push_compression_zlib(subndr, comndr);
173 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PUSH)",