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_chunk(struct ndr_pull *ndrpull,
29 struct ndr_push *ndrpush,
30 struct z_stream_s *zs, int i)
33 uint32_t comp_chunk_offset;
34 uint32_t comp_chunk_size;
36 uint32_t plain_chunk_offset;
37 uint32_t plain_chunk_size;
42 /* I don't know why, this is needed... --metze */
43 if (i == 5) ndrpull->offset -=4;
45 NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &plain_chunk_size));
46 if (plain_chunk_size > 0x00008000) {
47 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZLIB plain chunk size %08X > 0x00008000 (PULL)",
51 NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &comp_chunk_size));
53 NDR_CHECK(ndr_pull_uint8(ndrpull, NDR_SCALARS, &C_CK_marker));
54 NDR_CHECK(ndr_pull_uint8(ndrpull, NDR_SCALARS, &K_CK_marker));
55 if (!(C_CK_marker == (uint8_t)'C' && K_CK_marker == (uint8_t)'K')) {
56 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZLIB invalid CK marker C[%02X] K[%02X] (PULL)",
57 C_CK_marker, K_CK_marker);
60 DEBUG(10,("plain_chunk_size: %08X (%u) comp_chunk_size: %08X (%u)\n",
61 plain_chunk_size, plain_chunk_size, comp_chunk_size, comp_chunk_size));
63 comp_chunk_offset = ndrpull->offset;
64 NDR_CHECK(ndr_pull_advance(ndrpull, comp_chunk_size));
65 comp_chunk = ndrpull->data + comp_chunk_offset;
67 plain_chunk_offset = ndrpush->offset;
68 NDR_CHECK(ndr_push_zero(ndrpush, plain_chunk_size));
69 plain_chunk = ndrpush->data + plain_chunk_offset;
71 dump_data(10, comp_chunk, 16);
73 zs->avail_in = comp_chunk_size;
74 zs->next_in = comp_chunk;
75 zs->next_out = plain_chunk;
76 zs->avail_out = plain_chunk_size;
79 ret = inflate(zs, Z_BLOCK);
80 if (ret == Z_STREAM_END) {
81 DEBUG(0,("comp_chunk_size: %u avail_in: %d, plain_chunk_size: %u, avail_out: %d\n",
82 comp_chunk_size, zs->avail_in, plain_chunk_size, zs->avail_out));
86 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflate error %d",
91 if ((plain_chunk_size < 0x00008000) || (ndrpull->offset+4 >= ndrpull->data_size)) {
92 /* this is the last chunk */
96 return NT_STATUS_MORE_PROCESSING_REQUIRED;
99 static NTSTATUS ndr_pull_compression_zlib(struct ndr_pull *subndr,
100 struct ndr_pull *comndr,
101 ssize_t decompressed_len)
103 NTSTATUS status = NT_STATUS_MORE_PROCESSING_REQUIRED;
104 struct ndr_push *ndrpush;
105 DATA_BLOB uncompressed;
106 struct z_stream_s zs;
112 ndrpush = ndr_push_init_ctx(subndr);
113 NT_STATUS_HAVE_NO_MEMORY(ndrpush);
115 ret = inflateInit2(&zs, -15);
117 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflateInit2 error %d",
121 while (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
122 status = ndr_pull_compression_zlib_chunk(subndr, ndrpush, &zs, i++);
125 NT_STATUS_NOT_OK_RETURN(status);
127 uncompressed = ndr_push_blob(ndrpush);
130 comndr->data = uncompressed.data;
131 comndr->data_size = uncompressed.length;
137 static NTSTATUS ndr_push_compression_zlib(struct ndr_push *subndr,
138 struct ndr_push *comndr)
141 DATA_BLOB outbuf = data_blob_talloc(comndr, NULL, comndr->offset + 10);
142 struct z_stream_s zs;
147 inbuf = ndr_push_blob(comndr);
149 zs.avail_in = inbuf.length;
150 zs.next_in = inbuf.data;
151 zs.next_out = outbuf.data+10;
152 zs.avail_out = outbuf.length-10;
154 ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
156 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PUSH) deflateInit2 error %d",
160 ret = deflate(&zs, Z_SYNC_FLUSH);
162 if (ret != Z_OK && ret != Z_STREAM_END) {
163 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) deflate error %d",
169 /* TODO: push the header here */
172 NDR_CHECK(ndr_push_bytes(subndr, outbuf.data, outbuf.length));
179 handle compressed subcontext buffers, which in midl land are user-marshalled, but
180 we use magic in pidl to make them easier to cope with
182 NTSTATUS ndr_pull_compression(struct ndr_pull *subndr,
183 struct ndr_pull *comndr,
184 enum ndr_compression_alg compression_alg,
185 ssize_t decompressed_len)
187 comndr->flags = subndr->flags;
189 switch (compression_alg) {
191 case NDR_COMPRESSION_ZLIB:
192 return ndr_pull_compression_zlib(subndr, comndr, decompressed_len);
195 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PULL)",
202 push a compressed subcontext
204 NTSTATUS ndr_push_compression(struct ndr_push *subndr,
205 struct ndr_push *comndr,
206 enum ndr_compression_alg compression_alg)
208 comndr->flags = subndr->flags;
210 switch (compression_alg) {
212 case NDR_COMPRESSION_ZLIB:
213 return ndr_push_compression_zlib(subndr, comndr);
216 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PUSH)",