f09007a7154d9195e3b6d900fa85f5090a26193d
[bbaumbach/samba-autobuild/.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #ifdef HAVE_ZLIB
26 #include <zlib.h>
27
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)
31 {
32         uint8_t *comp_chunk;
33         uint32_t comp_chunk_offset;
34         uint32_t comp_chunk_size;
35         uint8_t *plain_chunk;
36         uint32_t plain_chunk_offset;
37         uint32_t plain_chunk_size;
38         uint8_t C_CK_marker;
39         uint8_t K_CK_marker;
40         int ret;
41
42         /* I don't know why, this is needed... --metze */
43         if (i == 5) ndrpull->offset -=4;
44
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)", 
48                                       plain_chunk_size);
49         }
50
51         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &comp_chunk_size));
52
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);
58         }
59
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));
62
63         comp_chunk_offset = ndrpull->offset;
64         NDR_CHECK(ndr_pull_advance(ndrpull, comp_chunk_size));
65         comp_chunk = ndrpull->data + comp_chunk_offset;
66
67         plain_chunk_offset = ndrpush->offset;
68         NDR_CHECK(ndr_push_zero(ndrpush, plain_chunk_size));
69         plain_chunk = ndrpush->data + plain_chunk_offset;
70
71         dump_data(10, comp_chunk, 16);
72
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;
77
78         while (True) {
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));
83                         break;
84                 }
85                 if (ret != Z_OK) {
86                         return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflate error %d", 
87                                       ret);
88                 }
89         }
90
91         if ((plain_chunk_size < 0x00008000) || (ndrpull->offset+4 >= ndrpull->data_size)) {
92                 /* this is the last chunk */
93                 return NT_STATUS_OK;
94         }
95
96         return NT_STATUS_MORE_PROCESSING_REQUIRED;
97 }
98
99 static NTSTATUS ndr_pull_compression_zlib(struct ndr_pull *subndr,
100                                           struct ndr_pull *comndr,
101                                           ssize_t decompressed_len)
102 {
103         NTSTATUS status = NT_STATUS_MORE_PROCESSING_REQUIRED;
104         struct ndr_push *ndrpush;
105         DATA_BLOB uncompressed;
106         struct z_stream_s zs;
107         int ret;
108         int i = 0;
109
110         ZERO_STRUCT(zs);
111
112         ndrpush = ndr_push_init_ctx(subndr);
113         NT_STATUS_HAVE_NO_MEMORY(ndrpush);
114
115         ret = inflateInit2(&zs, -15);
116         if (ret != Z_OK) {
117                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) inflateInit2 error %d", 
118                                       ret);
119         }
120
121         while (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
122                 status = ndr_pull_compression_zlib_chunk(subndr, ndrpush, &zs, i++);
123         }
124         inflateEnd(&zs);
125         NT_STATUS_NOT_OK_RETURN(status);
126
127         uncompressed = ndr_push_blob(ndrpush);
128
129         *comndr = *subndr;
130         comndr->data            = uncompressed.data;
131         comndr->data_size       = uncompressed.length;
132         comndr->offset          = 0;
133
134         return NT_STATUS_OK;
135 }
136
137 static NTSTATUS ndr_push_compression_zlib(struct ndr_push *subndr,
138                                           struct ndr_push *comndr)
139 {
140         DATA_BLOB inbuf;
141         DATA_BLOB outbuf = data_blob_talloc(comndr, NULL, comndr->offset + 10);
142         struct z_stream_s zs;
143         int ret;
144
145         ZERO_STRUCT(zs);
146
147         inbuf = ndr_push_blob(comndr);
148
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;
153
154         ret = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
155         if (ret != Z_OK) {
156                 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PUSH) deflateInit2 error %d", 
157                                       ret);
158         }
159
160         ret = deflate(&zs, Z_SYNC_FLUSH);
161
162         if (ret != Z_OK && ret != Z_STREAM_END) {
163                 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad ZLIB (PULL) deflate error %d", 
164                                       ret);
165         }
166
167         deflateEnd(&zs);
168
169         /* TODO: push the header here */
170
171
172         NDR_CHECK(ndr_push_bytes(subndr, outbuf.data, outbuf.length));
173
174         return NT_STATUS_OK;
175 }
176 #endif
177
178 /*
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
181 */
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)
186 {
187         comndr->flags = subndr->flags;
188
189         switch (compression_alg) {
190 #ifdef HAVE_ZLIB
191         case NDR_COMPRESSION_ZLIB:
192                 return ndr_pull_compression_zlib(subndr, comndr, decompressed_len);
193 #endif
194         default:
195                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PULL)", 
196                                       compression_alg);
197         }
198         return NT_STATUS_OK;
199 }
200
201 /*
202   push a compressed subcontext
203 */
204 NTSTATUS ndr_push_compression(struct ndr_push *subndr,
205                               struct ndr_push *comndr,
206                               enum ndr_compression_alg compression_alg)
207 {
208         comndr->flags = subndr->flags;
209
210         switch (compression_alg) {
211 #ifdef HAVE_ZLIB
212         case NDR_COMPRESSION_ZLIB:
213                 return ndr_push_compression_zlib(subndr, comndr);
214 #endif
215         default:
216                 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PUSH)", 
217                                       compression_alg);
218         }
219         return NT_STATUS_OK;
220 }