int GZDecompress(Stream& out, Stream& in, int size, Gate2<int, int> progress) { byte buffer[10]; if(!in.GetAll(buffer, 10) || buffer[0] != GZ_MAGIC1 || buffer[1] != GZ_MAGIC2) { out.SetError(); return -1; } int flags = buffer[3]; if(buffer[2] != Z_DEFLATED || (flags & RESERVED) != 0) return false; size -= 10; if(flags & EXTRA_FIELD) { int len = in.Get16le(); in.SeekCur(len); size -= len; } if(flags & ORIG_NAME) size -= sSkipZ(in); if(flags & COMMENT) size -= sSkipZ(in); if(flags & HEAD_CRC) { in.Get16le(); size -= 2; } if(in.IsEof() || size < 0) { out.SetError(); return -1; } dword crc; int sz = ZDecompress(out, in, size, progress, true, &crc); return sz < 0 || in.Get32le() != (int)crc || in.Get32le() != sz ? -1 : sz; }
String ZDecompress(const void *data, int len, Gate2<int, int> progress) { StringStream out; MemReadStream in(data, len); ZDecompress(out, in, progress); return out; }
int StaticCompressor::decompressBuffer(unsigned char *plainBuffer, const unsigned int plainSize, const unsigned char *compressedBuffer, const unsigned int compressedSize) { #ifdef TEST *logofs << "StaticCompressor: Called for buffer at " << (void *) plainBuffer << ".\n" << logofs_flush; #endif unsigned int checkSize = plainSize; int result = ZDecompress(&decompressionStream_, plainBuffer, &checkSize, compressedBuffer, compressedSize); if (result != Z_OK) { #ifdef PANIC *logofs << "StaticCompressor: PANIC! Failure decompressing buffer. " << "Error is '" << zError(result) << "'.\n" << logofs_flush; #endif return -1; } if (plainSize != checkSize) { #ifdef PANIC *logofs << "StaticCompressor: PANIC! Expected decompressed size was " << plainSize << " while it is " << checkSize << ".\n" << logofs_flush; #endif cerr << "Error" << ": Expected decompressed size was " << plainSize << " while it is " << checkSize << ".\n"; return -1; } #ifdef TEST *logofs << "StaticCompressor: Decompressed buffer from " << compressedSize << " to " << plainSize << " bytes.\n" << logofs_flush; #endif return 1; }
NAMESPACE_UPP static Topic ReadTopic(const char *text) { Topic topic; CParser p(text); try { if(p.Id("topic")) { topic.title = p.ReadString(); p.Char(';'); topic.text = p.GetPtr(); return topic; } while(!p.IsEof()) { if(p.Id("TITLE")) { p.PassChar('('); topic.title = p.ReadString(); p.PassChar(')'); } else if(p.Id("REF")) { p.PassChar('('); p.ReadString(); p.PassChar(')'); } else if(p.Id("TOPIC_TEXT")) { p.PassChar('('); topic.text << p.ReadString(); p.PassChar(')'); } else if(p.Id("COMPRESSED")) { StringBuffer b; b.Reserve(1024); while(p.IsInt()) { b.Cat(p.ReadInt()); p.PassChar(','); } topic.text = ZDecompress(~b, b.GetLength()); } else { topic.text << p.GetPtr(); break; } } } catch(CParser::Error e) { topic.text = String::GetVoid(); topic.title = e; } return topic; }
static String sZet(FileIn& in, int offset, int len) { in.Seek(offset); return ZDecompress(in.Get(len)); }
int StaticCompressor::decompressBuffer(unsigned char *plainBuffer, unsigned int plainSize, const unsigned char *&compressedBuffer, unsigned int &compressedSize, DecodeBuffer &decodeBuffer) { #ifdef DEBUG *logofs << "StaticCompressor: Called for buffer at " << (void *) plainBuffer << ".\n" << logofs_flush; #endif unsigned int value; decodeBuffer.decodeBoolValue(value); if (value == 0) { memcpy(plainBuffer, decodeBuffer.decodeMemory(plainSize), plainSize); return 0; } unsigned int checkSize = plainSize; decodeBuffer.decodeValue(value, 32, 14); compressedSize = value; decodeBuffer.decodeValue(value, 32, 14); checkSize = value; // // If caller needs the original compressed // data it must copy this to its own buffer // before using any further decode function. // compressedBuffer = decodeBuffer.decodeMemory(compressedSize); int result = ZDecompress(&decompressionStream_, plainBuffer, &checkSize, compressedBuffer, compressedSize); if (result != Z_OK) { #ifdef PANIC *logofs << "StaticCompressor: PANIC! Failure decompressing buffer. " << "Error is '" << zError(result) << "'.\n" << logofs_flush; #endif cerr << "Error" << ": Failure decompressing buffer. " << "Error is '" << zError(result) << "'.\n"; return -1; } else if (plainSize != checkSize) { #ifdef PANIC *logofs << "StaticCompressor: PANIC! Expected decompressed size was " << plainSize << " while it is " << checkSize << ".\n" << logofs_flush; #endif cerr << "Error" << ": Expected decompressed size was " << plainSize << " while it is " << checkSize << ".\n"; return -1; } return 1; }
int UnpackRgb(T_geometry *geometry, unsigned char method, unsigned char *src_data, int src_size, int dst_bpp, int dst_width, int dst_height, unsigned char *dst_data, int dst_size) { if (*src_data == 0) { if (dst_size != src_size - 1) { #ifdef TEST *logofs << "UnpackRgb: PANIC! Invalid destination size " << dst_size << " with source " << src_size << ".\n" << logofs_flush; #endif return -1; } #ifdef TEST *logofs << "UnpackRgb: Expanding " << src_size - 1 << " bytes of plain RGB data.\n" << logofs_flush; #endif memcpy(dst_data, src_data + 1, src_size - 1); return 1; } unsigned int check_size = dst_size; int result = ZDecompress(&unpackStream, dst_data, &check_size, src_data + 1, src_size - 1); if (result != Z_OK) { #ifdef PANIC *logofs << "UnpackRgb: PANIC! Failure decompressing RGB data. " << "Error is '" << zError(result) << "'.\n" << logofs_flush; #endif cerr << "Error" << ": Failure decompressing RGB data. " << "Error is '" << zError(result) << "'.\n"; return -1; } else if (check_size != (unsigned int) dst_size) { #ifdef PANIC *logofs << "UnpackRgb: PANIC! Size mismatch in RGB data. " << "Resulting size is " << check_size << " with " << "expected size " << dst_size << ".\n" << logofs_flush; #endif cerr << "Error" << ": Size mismatch in RGB data. " << "Resulting size is " << check_size << " with " << "expected size " << dst_size << ".\n"; return -1; } #ifdef TEST *logofs << "UnpackRgb: Decompressed " << src_size - 1 << " bytes to " << dst_size << " bytes of RGB data.\n" << logofs_flush; #endif return 1; }
String ZDecompress(const String& s, Gate2<int, int> progress) { return ZDecompress(~s, s.GetCount(), progress); }
int ZDecompress(Stream& out, Stream& in, Gate2<int, int> progress) { return ZDecompress(out, in, (int)in.GetLeft(), progress); }