# # # delete "botan/gzip.cpp" # # delete "botan/gzip.h" # # add_file "botan/zlib.cpp" # content [9d1bffb2c1a97d0a748728fc71149915b800626d] # # add_file "botan/zlib.h" # content [5978ef7a06edd722ec0753031a46e7fa7e5d0091] # # patch "Makefile.am" # from [bc8dfa5f0e4ca9c9c523a1604b9e3f7a1e71ec0e] # to [4a8d06219a629c88091a92152d74794199ef4f9f] # # patch "transforms.cc" # from [2a2510681a0dccdc471ef508dc51f5c568d58963] # to [64f9277a430093b47750aa8439cdf21119899809] # # patch "transforms.hh" # from [54ea9d901a7f870c2e5ee46b5c3a99e868eb1ad9] # to [7921ed6a36d3de22c748c692e0296f18eff94432] # ============================================================ --- botan/zlib.cpp 9d1bffb2c1a97d0a748728fc71149915b800626d +++ botan/zlib.cpp 9d1bffb2c1a97d0a748728fc71149915b800626d @@ -0,0 +1,266 @@ +/************************************************* +* Zlib Compressor Source File * +* (C) 1999-2007 The Botan Project * +*************************************************/ + +#include +#include +#include +#include + +namespace Botan { + +namespace { + +/************************************************* +* Allocation Information for Zlib * +*************************************************/ +class Zlib_Alloc_Info + { + public: + std::map current_allocs; + Allocator* alloc; + + Zlib_Alloc_Info() { alloc = Allocator::get(false); } + }; + +/************************************************* +* Allocation Function for Zlib * +*************************************************/ +void* zlib_malloc(void* info_ptr, unsigned int n, unsigned int size) + { + Zlib_Alloc_Info* info = static_cast(info_ptr); + void* ptr = info->alloc->allocate(n * size); + info->current_allocs[ptr] = n * size; + return ptr; + } + +/************************************************* +* Allocation Function for Zlib * +*************************************************/ +void zlib_free(void* info_ptr, void* ptr) + { + Zlib_Alloc_Info* info = static_cast(info_ptr); + std::map::const_iterator i = info->current_allocs.find(ptr); + if(i == info->current_allocs.end()) + throw Invalid_Argument("zlib_free: Got pointer not allocated by us"); + info->alloc->deallocate(ptr, i->second); + } + +} + +/************************************************* +* Wrapper Type for Zlib z_stream * +*************************************************/ +class Zlib_Stream + { + public: + z_stream stream; + + Zlib_Stream() + { + std::memset(&stream, 0, sizeof(z_stream)); + stream.zalloc = zlib_malloc; + stream.zfree = zlib_free; + stream.opaque = new Zlib_Alloc_Info; + } + ~Zlib_Stream() + { + Zlib_Alloc_Info* info = static_cast(stream.opaque); + delete info; + std::memset(&stream, 0, sizeof(z_stream)); + } + }; + +/************************************************* +* Zlib_Compression Constructor * +*************************************************/ +Zlib_Compression::Zlib_Compression(u32bit l) : + level((l >= 9) ? 9 : l), buffer(DEFAULT_BUFFERSIZE) + { + zlib = 0; + } + +/************************************************* +* Start Compressing with Zlib * +*************************************************/ +void Zlib_Compression::start_msg() + { + clear(); + zlib = new Zlib_Stream; + if(deflateInit(&(zlib->stream), level) != Z_OK) + throw Exception("Zlib_Compression: Memory allocation error"); + } + +/************************************************* +* Compress Input with Zlib * +*************************************************/ +void Zlib_Compression::write(const byte input[], u32bit length) + { + zlib->stream.next_in = (Bytef*)input; + zlib->stream.avail_in = length; + + while(zlib->stream.avail_in != 0) + { + zlib->stream.next_out = (Bytef*)buffer.begin(); + zlib->stream.avail_out = buffer.size(); + deflate(&(zlib->stream), Z_NO_FLUSH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + } + +/************************************************* +* Finish Compressing with Zlib * +*************************************************/ +void Zlib_Compression::end_msg() + { + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + int rc = Z_OK; + while(rc != Z_STREAM_END) + { + zlib->stream.next_out = (Bytef*)buffer.begin(); + zlib->stream.avail_out = buffer.size(); + rc = deflate(&(zlib->stream), Z_FINISH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + clear(); + } + +/************************************************* +* Flush the Zlib Compressor * +*************************************************/ +void Zlib_Compression::flush() + { + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + while(true) + { + zlib->stream.next_out = (Bytef*)buffer.begin(); + zlib->stream.avail_out = buffer.size(); + deflate(&(zlib->stream), Z_FULL_FLUSH); + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + if(zlib->stream.avail_out == buffer.size()) break; + } + } + +/************************************************* +* Clean up Compression Context * +*************************************************/ +void Zlib_Compression::clear() + { + if(zlib) + { + deflateEnd(&(zlib->stream)); + delete zlib; + zlib = 0; + } + + buffer.clear(); + } + +/************************************************* +* Zlib_Decompression Constructor * +*************************************************/ +Zlib_Decompression::Zlib_Decompression() : buffer(DEFAULT_BUFFERSIZE) + { + zlib = 0; + no_writes = true; + } + +/************************************************* +* Start Decompressing with Zlib * +*************************************************/ +void Zlib_Decompression::start_msg() + { + clear(); + zlib = new Zlib_Stream; + if(inflateInit(&(zlib->stream)) != Z_OK) + throw Exception("Zlib_Decompression: Memory allocation error"); + } + +/************************************************* +* Decompress Input with Zlib * +*************************************************/ +void Zlib_Decompression::write(const byte input[], u32bit length) + { + if(length) no_writes = false; + + zlib->stream.next_in = (Bytef*)input; + zlib->stream.avail_in = length; + + while(zlib->stream.avail_in != 0) + { + zlib->stream.next_out = (Bytef*)buffer.begin(); + zlib->stream.avail_out = buffer.size(); + + int rc = inflate(&(zlib->stream), Z_SYNC_FLUSH); + if(rc != Z_OK && rc != Z_STREAM_END) + { + clear(); + if(rc == Z_DATA_ERROR) + throw Decoding_Error("Zlib_Decompression: Data integrity error"); + if(rc == Z_NEED_DICT) + throw Decoding_Error("Zlib_Decompression: Need preset dictionary"); + if(rc == Z_MEM_ERROR) + throw Exception("Zlib_Decompression: Memory allocation error"); + throw Exception("Zlib_Decompression: Unknown decompress error"); + } + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + if(rc == Z_STREAM_END) + { + u32bit read_from_block = length - zlib->stream.avail_in; + start_msg(); + zlib->stream.next_in = (Bytef*)input + read_from_block; + zlib->stream.avail_in = length - read_from_block; + input += read_from_block; + length -= read_from_block; + } + } + } + +/************************************************* +* Finish Decompressing with Zlib * +*************************************************/ +void Zlib_Decompression::end_msg() + { + if(no_writes) return; + zlib->stream.next_in = 0; + zlib->stream.avail_in = 0; + + int rc = Z_OK; + while(rc != Z_STREAM_END) + { + zlib->stream.next_out = (Bytef*)buffer.begin(); + zlib->stream.avail_out = buffer.size(); + rc = inflate(&(zlib->stream), Z_SYNC_FLUSH); + if(rc != Z_OK && rc != Z_STREAM_END) + { + clear(); + throw Exception("Zlib_Decompression: Error finalizing decompression"); + } + send(buffer.begin(), buffer.size() - zlib->stream.avail_out); + } + clear(); + } + +/************************************************* +* Clean up Decompression Context * +*************************************************/ +void Zlib_Decompression::clear() + { + no_writes = true; + + if(zlib) + { + inflateEnd(&(zlib->stream)); + delete zlib; + zlib = 0; + } + + buffer.clear(); + } + +} ============================================================ --- botan/zlib.h 5978ef7a06edd722ec0753031a46e7fa7e5d0091 +++ botan/zlib.h 5978ef7a06edd722ec0753031a46e7fa7e5d0091 @@ -0,0 +1,55 @@ +/************************************************* +* Zlib Compressor Header File * +* (C) 1999-2007 The Botan Project * +*************************************************/ + +#ifndef BOTAN_EXT_ZLIB_H__ +#define BOTAN_EXT_ZLIB_H__ + +#include + +namespace Botan { + +/************************************************* +* Zlib Compression Filter * +*************************************************/ +class Zlib_Compression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + void flush(); + + Zlib_Compression(u32bit = 6); + ~Zlib_Compression() { clear(); } + private: + void clear(); + const u32bit level; + SecureVector buffer; + class Zlib_Stream* zlib; + }; + +/************************************************* +* Zlib Decompression Filter * +*************************************************/ +class Zlib_Decompression : public Filter + { + public: + void write(const byte input[], u32bit length); + void start_msg(); + void end_msg(); + + Zlib_Decompression(); + ~Zlib_Decompression() { clear(); } + private: + void clear(); + SecureVector buffer; + class Zlib_Stream* zlib; + bool no_writes; + }; + +} + +#endif ============================================================ --- Makefile.am bc8dfa5f0e4ca9c9c523a1604b9e3f7a1e71ec0e +++ Makefile.am 4a8d06219a629c88091a92152d74794199ef4f9f @@ -126,7 +126,7 @@ BOTAN_SOURCES = \ botan/exceptn.cpp botan/filter.cpp botan/filters.cpp \ botan/fips140.cpp \ botan/get_algo.cpp botan/get_enc.cpp botan/get_pbe.cpp \ - botan/gzip.cpp botan/hash_id.cpp botan/hex.cpp botan/hmac.cpp \ + botan/zlib.cpp botan/hash_id.cpp botan/hex.cpp botan/hmac.cpp \ botan/if_algo.cpp botan/inifile.cpp botan/init_def.cpp \ botan/kdf.cpp botan/keypair.cpp botan/look_pk.cpp \ botan/make_prm.cpp botan/mdx_hash.cpp \ @@ -173,7 +173,7 @@ BOTAN_SOURCES = \ botan/pem.h botan/s2k.h botan/x509_key.h botan/asn1_obj.h \ botan/cfb.h botan/modebase.h \ botan/pipe.h botan/x509_obj.h \ - botan/asn1_oid.h botan/eax.h botan/gzip.h \ + botan/asn1_oid.h botan/eax.h botan/zlib.h \ botan/mode_pad.h botan/pk_algs.h botan/secmem.h \ botan/x509find.h botan/x509self.h botan/config.h botan/ecb.h \ botan/hex.h botan/pk_core.h botan/secqueue.h \ ============================================================ --- transforms.cc 2a2510681a0dccdc471ef508dc51f5c568d58963 +++ transforms.cc 64f9277a430093b47750aa8439cdf21119899809 @@ -20,7 +20,7 @@ #include #include "botan/botan.h" -#include "botan/gzip.h" +#include "botan/zlib.h" #include "botan/sha160.h" #include "cleanup.hh" @@ -132,8 +132,8 @@ SPECIALIZE_XFORM(Botan::Hex_Decoder, Bot SPECIALIZE_XFORM(Botan::Base64_Decoder, Botan::IGNORE_WS); SPECIALIZE_XFORM(Botan::Hex_Encoder, Botan::Hex_Encoder::Lowercase); SPECIALIZE_XFORM(Botan::Hex_Decoder, Botan::IGNORE_WS); -SPECIALIZE_XFORM(Botan::Gzip_Compression,); -SPECIALIZE_XFORM(Botan::Gzip_Decompression,); +SPECIALIZE_XFORM(Botan::Zlib_Compression,); +SPECIALIZE_XFORM(Botan::Zlib_Decompression,); template void pack(T const & in, base64< gzip > & out) @@ -143,7 +143,7 @@ void pack(T const & in, base64< gzip try { - Botan::Pipe pipe(new Botan::Gzip_Compression(), + Botan::Pipe pipe(new Botan::Zlib_Compression(), new Botan::Base64_Encoder); pipe.process_msg(in()); tmp = pipe.read_all_as_string(); @@ -164,7 +164,7 @@ void unpack(base64< gzip > const & in try { Botan::Pipe pipe(new Botan::Base64_Decoder(), - new Botan::Gzip_Decompression()); + new Botan::Zlib_Decompression()); pipe.process_msg(in()); tmp = pipe.read_all_as_string(); out = T(tmp); ============================================================ --- transforms.hh 54ea9d901a7f870c2e5ee46b5c3a99e868eb1ad9 +++ transforms.hh 7921ed6a36d3de22c748c692e0296f18eff94432 @@ -22,8 +22,8 @@ namespace Botan { class Base64_Decoder; class Hex_Encoder; class Hex_Decoder; - class Gzip_Compression; - class Gzip_Decompression; + class Zlib_Compression; + class Zlib_Decompression; } // this generic template cannot actually be used, except with the @@ -41,8 +41,8 @@ template<> std::string xform std::string xform(std::string const &); template<> std::string xform(std::string const &); template<> std::string xform(std::string const &); -template<> std::string xform(std::string const &); -template<> std::string xform(std::string const &); +template<> std::string xform(std::string const &); +template<> std::string xform(std::string const &); // base64 encoding @@ -75,16 +75,16 @@ void encode_gzip(T const & in, gzip & template void encode_gzip(T const & in, gzip & out) -{ out = gzip(xform(in())); } +{ out = gzip(xform(in())); } template void decode_gzip(gzip const & in, T & out) -{ out = T(xform(in())); } +{ out = T(xform(in())); } // string variant for netsync template void encode_gzip(std::string const & in, gzip & out) -{ out = xform(in); } +{ out = xform(in); } // both at once (this is relatively common) // these are usable for T = data and T = delta