2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file saml/saml2/binding/SAML2Redirect.h
24 * SAML 2.0 HTTP Redirect compression functionality.
28 #include "saml2/binding/SAML2Redirect.h"
31 #include <xmltooling/logging.h>
32 #include <xmltooling/util/NDC.h>
34 using namespace xmltooling::logging;
39 voidpf saml_zalloc(void* opaque, uInt items, uInt size)
41 return malloc(items*size);
44 void saml_zfree(void* opaque, voidpf addr)
51 char* opensaml::saml2p::deflate(char* in, unsigned int in_len, unsigned int* out_len)
54 xmltooling::NDC ndc("deflate");
56 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect.zlib");
59 memset(&z, 0, sizeof(z_stream));
61 z.zalloc = saml_zalloc;
64 z.next_in = (Bytef*)in;
68 int ret = deflateInit2(&z, 9, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
70 log.error("zlib deflateInit2 failed with error code (%d)", ret);
74 int dlen = in_len + (in_len >> 8) + 12; /* orig_size * 1.001 + 12 */
75 char* out = new char[dlen];
76 z.next_out = (Bytef*)out;
79 ret = deflate(&z, Z_FINISH);
80 if (ret != Z_STREAM_END) {
82 log.error("zlib deflateInit2 failed with error code (%d)", ret);
86 *out_len = z.total_out;
91 unsigned int opensaml::saml2p::inflate(char* in, unsigned int in_len, ostream& out)
94 xmltooling::NDC ndc("inflate");
96 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect.zlib");
99 memset(&z, 0, sizeof(z_stream));
101 z.zalloc = saml_zalloc;
102 z.zfree = saml_zfree;
104 z.next_in = (Bytef*)in;
107 int dlen = in_len << 3; /* guess inflated size: orig_size * 8 */
108 Byte* buf = new Byte[dlen];
109 memset(buf, 0, dlen);
113 int ret = inflateInit2(&z, -15);
115 log.error("zlib inflateInit2 failed with error code (%d)", ret);
122 while (--iter) { /* Make sure we can never be caught in infinite loop */
123 ret = inflate(&z, Z_SYNC_FLUSH);
126 diff = z.next_out - buf;
129 out << *(z.next_out++);
132 case Z_OK: /* avail_out should be 0 now. Time to dump the buffer. */
133 diff = z.next_out - buf;
136 out << *(z.next_out++);
137 memset(buf, 0, dlen);
145 log.error("zlib inflate failed with error code (%d)", ret);
151 int out_len = z.total_out;