~ubuntu-branches/ubuntu/trusty/advancecomp/trusty

« back to all changes in this revision

Viewing changes to 7z/7zdeflate.cc

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ozarowski
  • Date: 2006-05-13 21:15:49 UTC
  • Revision ID: james.westby@ubuntu.com-20060513211549-2vu7peis643ojcm5
Tags: upstream-1.15
ImportĀ upstreamĀ versionĀ 1.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "7z.h"
 
2
 
 
3
#include "DeflateEncoder.h"
 
4
#include "DeflateDecoder.h"
 
5
 
 
6
#include "zlib.h"
 
7
 
 
8
bool compress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ()
 
9
{
 
10
        try {
 
11
                NDeflate::NEncoder::CCoder cc;
 
12
 
 
13
                if (cc.SetEncoderNumPasses(num_passes) != S_OK)
 
14
                        return false;
 
15
 
 
16
                if (cc.SetEncoderNumFastBytes(num_fast_bytes) != S_OK)
 
17
                        return false;
 
18
 
 
19
                ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
 
20
                ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);
 
21
 
 
22
                UINT64 in_size_l = in_size;
 
23
 
 
24
                if (cc.Code(&in, &out, &in_size_l) != S_OK)
 
25
                        return false;
 
26
 
 
27
                out_size = out.size_get();
 
28
 
 
29
                if (out.overflow_get())
 
30
                        return false;
 
31
 
 
32
                return true;
 
33
        } catch (...) {
 
34
                return false;
 
35
        }
 
36
}
 
37
 
 
38
bool decompress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size) throw () {
 
39
        try {
 
40
                NDeflate::NDecoder::CCoder cc;
 
41
 
 
42
                ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
 
43
                ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);
 
44
 
 
45
                UINT64 in_size_l = in_size;
 
46
                UINT64 out_size_l = out_size;
 
47
 
 
48
                if (cc.Code(&in, &out, &in_size_l, &out_size_l) != S_OK)
 
49
                        return false;
 
50
 
 
51
                if (out.size_get() != out_size || out.overflow_get())
 
52
                        return false;
 
53
 
 
54
                return true;
 
55
        } catch (...) {
 
56
                return false;
 
57
        }
 
58
}
 
59
 
 
60
bool compress_rfc1950_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ()
 
61
{
 
62
        if (out_size < 6)
 
63
                return false;
 
64
 
 
65
        // 8 - deflate
 
66
        // 7 - 32k window
 
67
        // 3 - max compression
 
68
        unsigned header = (8 << 8) | (7 << 12) | (3 << 6);
 
69
 
 
70
        header += 31 - (header % 31);
 
71
 
 
72
        out_data[0] = (header >> 8) & 0xFF;
 
73
        out_data[1] = header & 0xFF;
 
74
        out_data += 2;
 
75
 
 
76
        unsigned size = out_size - 6;
 
77
        if (!compress_deflate_7z(in_data, in_size, out_data, size, num_passes, num_fast_bytes)) {
 
78
                return false;
 
79
        }
 
80
        out_data += size;
 
81
 
 
82
        unsigned adler = adler32(adler32(0,0,0), in_data, in_size);
 
83
 
 
84
        out_data[0] = (adler >> 24) & 0xFF;
 
85
        out_data[1] = (adler >> 16) & 0xFF;
 
86
        out_data[2] = (adler >> 8) & 0xFF;
 
87
        out_data[3] = adler & 0xFF;
 
88
 
 
89
        out_size = size + 6;
 
90
 
 
91
        return true;
 
92
}