r6129: - add our own MSZIP decompression implementation
[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 #include "lib/compression/mszip.h"
25
26 static NTSTATUS ndr_pull_compression_mszip_chunk(struct ndr_pull *ndrpull,
27                                                  struct ndr_push *ndrpush,
28                                                  struct decomp_state *decomp_state)
29 {
30         DATA_BLOB comp_chunk;
31         uint32_t comp_chunk_offset;
32         uint32_t comp_chunk_size;
33         DATA_BLOB plain_chunk;
34         uint32_t plain_chunk_offset;
35         uint32_t plain_chunk_size;
36         int ret;
37
38         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &plain_chunk_size));
39         if (plain_chunk_size > 0x00008000) {
40                 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZLIB plain chunk size %08X > 0x00008000 (PULL)", 
41                                       plain_chunk_size);
42         }
43
44         NDR_CHECK(ndr_pull_uint32(ndrpull, NDR_SCALARS, &comp_chunk_size));
45
46         DEBUG(10,("plain_chunk_size: %08X (%u) comp_chunk_size: %08X (%u)\n",
47                   plain_chunk_size, plain_chunk_size, comp_chunk_size, comp_chunk_size));
48
49         comp_chunk_offset = ndrpull->offset;
50         NDR_CHECK(ndr_pull_advance(ndrpull, comp_chunk_size));
51         comp_chunk.length = comp_chunk_size;
52         comp_chunk.data = ndrpull->data + comp_chunk_offset;
53
54         plain_chunk_offset = ndrpush->offset;
55         NDR_CHECK(ndr_push_zero(ndrpush, plain_chunk_size));
56         plain_chunk.length = plain_chunk_size;
57         plain_chunk.data = ndrpush->data + plain_chunk_offset;
58
59         ret = ZIPdecompress(decomp_state, &comp_chunk, &plain_chunk);
60         if (ret != DECR_OK) {
61                 return ndr_pull_error(ndrpull, NDR_ERR_COMPRESSION, "Bad ZIBdecompress() error %d (PULL)",
62                                       ret);
63         }
64
65         if ((plain_chunk_size < 0x00008000) || (ndrpull->offset+4 >= ndrpull->data_size)) {
66                 /* this is the last chunk */
67                 return NT_STATUS_OK;
68         }
69
70         return NT_STATUS_MORE_PROCESSING_REQUIRED;
71 }
72
73 static NTSTATUS ndr_pull_compression_mszip(struct ndr_pull *subndr,
74                                            struct ndr_pull *comndr,
75                                            ssize_t decompressed_len)
76 {
77         NTSTATUS status = NT_STATUS_MORE_PROCESSING_REQUIRED;
78         struct ndr_push *ndrpush;
79         DATA_BLOB uncompressed;
80         uint32_t payload_header[4];
81         uint32_t payload_size;
82         uint32_t payload_offset;
83         uint8_t *payload;
84         struct decomp_state *decomp_state;
85
86         ndrpush = ndr_push_init_ctx(subndr);
87         NT_STATUS_HAVE_NO_MEMORY(ndrpush);
88
89         decomp_state = ZIPdecomp_state(subndr);
90         NT_STATUS_HAVE_NO_MEMORY(decomp_state);
91
92         while (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
93                 status = ndr_pull_compression_mszip_chunk(subndr, ndrpush, decomp_state);
94         }
95         NT_STATUS_NOT_OK_RETURN(status);
96
97         uncompressed = ndr_push_blob(ndrpush);
98
99         if (uncompressed.length != decompressed_len) {
100                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad uncompressed_len [%u] != [%d] (PULL)",
101                                       uncompressed.length, decompressed_len);
102         }
103
104         *comndr = *subndr;
105         comndr->data            = uncompressed.data;
106         comndr->data_size       = uncompressed.length;
107         comndr->offset          = 0;
108
109         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[0]));
110         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[1]));
111         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[2]));
112         NDR_CHECK(ndr_pull_uint32(comndr, NDR_SCALARS, &payload_header[3]));
113
114         payload_size = payload_header[2];
115
116         /* TODO: check the first 4 bytes of the header */
117         if (payload_header[1] != 0xCCCCCCCC) {
118                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad payload_header[1] [0x%08X] != [0xCCCCCCCC] (PULL)",
119                                       payload_header[1]);
120         }
121
122         payload_offset = comndr->offset;
123         NDR_CHECK(ndr_pull_advance(comndr, payload_size));
124         payload = comndr->data + payload_offset;
125
126         comndr->data            = payload;
127         comndr->data_size       = payload_size;
128         comndr->offset          = 0;
129
130         return NT_STATUS_OK;
131 }
132
133 static NTSTATUS ndr_push_compression_mszip(struct ndr_push *subndr,
134                                           struct ndr_push *comndr)
135 {
136         return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad MSZIP compression is not supported yet (PUSH)");
137 }
138
139 /*
140   handle compressed subcontext buffers, which in midl land are user-marshalled, but
141   we use magic in pidl to make them easier to cope with
142 */
143 NTSTATUS ndr_pull_compression(struct ndr_pull *subndr,
144                               struct ndr_pull *comndr,
145                               enum ndr_compression_alg compression_alg,
146                               ssize_t decompressed_len)
147 {
148         comndr->flags = subndr->flags;
149
150         switch (compression_alg) {
151         case NDR_COMPRESSION_MSZIP:
152                 return ndr_pull_compression_mszip(subndr, comndr, decompressed_len);
153         default:
154                 return ndr_pull_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PULL)", 
155                                       compression_alg);
156         }
157         return NT_STATUS_OK;
158 }
159
160 /*
161   push a compressed subcontext
162 */
163 NTSTATUS ndr_push_compression(struct ndr_push *subndr,
164                               struct ndr_push *comndr,
165                               enum ndr_compression_alg compression_alg)
166 {
167         comndr->flags = subndr->flags;
168
169         switch (compression_alg) {
170         case NDR_COMPRESSION_MSZIP:
171                 return ndr_push_compression_mszip(subndr, comndr);
172         default:
173                 return ndr_push_error(subndr, NDR_ERR_COMPRESSION, "Bad compression algorithm %d (PUSH)", 
174                                       compression_alg);
175         }
176         return NT_STATUS_OK;
177 }