put size in front of compressed data
| 2682 | |
| 2683 | // put size in front of compressed data |
| 2684 | int Compress(const byte* in, int sz, input_buffer& buffer) |
| 2685 | { |
| 2686 | byte tmp[LENGTH_SZ]; |
| 2687 | z_stream c_stream; /* compression stream */ |
| 2688 | |
| 2689 | buffer.allocate(sz + sizeof(uint16) + COMPRESS_EXTRA); |
| 2690 | |
| 2691 | c_stream.zalloc = myAlloc; |
| 2692 | c_stream.zfree = myFree; |
| 2693 | c_stream.opaque = (voidpf)0; |
| 2694 | |
| 2695 | c_stream.next_in = const_cast<byte*>(in); |
| 2696 | c_stream.avail_in = sz; |
| 2697 | c_stream.next_out = buffer.get_buffer() + sizeof(tmp); |
| 2698 | c_stream.avail_out = buffer.get_capacity() - sizeof(tmp); |
| 2699 | |
| 2700 | if (deflateInit(&c_stream, 8) != Z_OK) return -1; |
| 2701 | int err = deflate(&c_stream, Z_FINISH); |
| 2702 | deflateEnd(&c_stream); |
| 2703 | if (err != Z_OK && err != Z_STREAM_END) return -1; |
| 2704 | |
| 2705 | c16toa(sz, tmp); |
| 2706 | memcpy(buffer.get_buffer(), tmp, sizeof(tmp)); |
| 2707 | buffer.add_size(c_stream.total_out + sizeof(tmp)); |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | // get uncompressed size in front |
no test coverage detected