2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file saml/saml2/binding/SAML2Redirect.h
20 * SAML 2.0 HTTP Redirect compression functionality
24 #include "saml2/binding/SAML2Redirect.h"
26 #include <zlib/zlib.h>
27 #include <log4cpp/Category.hh>
28 #include <xmltooling/util/NDC.h>
30 using namespace log4cpp;
35 voidpf saml_zalloc(void* opaque, uInt items, uInt size)
37 return malloc(items*size);
40 void saml_zfree(void* opaque, voidpf addr)
47 char* opensaml::saml2p::deflate(char* in, unsigned int in_len, unsigned int* out_len)
50 xmltooling::NDC ndc("deflate");
52 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect.zlib");
55 memset(&z, 0, sizeof(z_stream));
57 z.zalloc = saml_zalloc;
60 z.next_in = (Bytef*)in;
64 int ret = deflateInit2(&z, 9, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
66 log.error("zlib deflateInit2 failed with error code (%d)", ret);
70 int dlen = in_len + (in_len >> 8) + 12; /* orig_size * 1.001 + 12 */
71 char* out = new char[dlen];
72 z.next_out = (Bytef*)out;
75 ret = deflate(&z, Z_FINISH);
76 if (ret != Z_STREAM_END) {
78 log.error("zlib deflateInit2 failed with error code (%d)", ret);
82 *out_len = z.total_out;
87 unsigned int opensaml::saml2p::inflate(char* in, unsigned int in_len, ostream& out)
90 xmltooling::NDC ndc("inflate");
92 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect.zlib");
95 memset(&z, 0, sizeof(z_stream));
97 z.zalloc = saml_zalloc;
100 z.next_in = (Bytef*)in;
103 int dlen = in_len << 3; /* guess inflated size: orig_size * 8 */
104 Byte* buf = new Byte[dlen];
105 memset(buf, 0, dlen);
109 int ret = inflateInit2(&z, -15);
111 log.error("zlib inflateInit2 failed with error code (%d)", ret);
117 while (--iter) { /* Make sure we can never be caught in infinite loop */
118 ret = inflate(&z, Z_SYNC_FLUSH);
121 ret = z.next_out - buf;
124 out << *(z.next_out++);
127 case Z_OK: /* avail_out should be 0 now. Time to dump the buffer. */
128 ret = z.next_out - buf;
131 out << *(z.next_out++);
132 memset(buf, 0, dlen);
140 log.error("zlib inflate failed with error code (%d)", ret);
146 int out_len = z.total_out;