|
1 /* |
|
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * e32tools\petran\Szip\compress.cpp |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "deflate.h" |
|
21 |
|
22 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__) |
|
23 #include <fstream> |
|
24 #else //__MSVCDOTNET__ |
|
25 #include <fstream.h> |
|
26 #endif //__MSVCDOTNET__ |
|
27 |
|
28 #include <assert.h> |
|
29 #include "h_utl.h" |
|
30 |
|
31 class TFileOutput : public TBitOutput |
|
32 { |
|
33 enum {KBufSize=0x1000}; |
|
34 public: |
|
35 TFileOutput(ostream& os); |
|
36 void FlushL(); |
|
37 TUint32 iDataCount; |
|
38 private: |
|
39 void OverflowL(); |
|
40 private: |
|
41 ostream& iOutStream; |
|
42 TUint8 iBuf[KBufSize]; |
|
43 }; |
|
44 |
|
45 TFileOutput::TFileOutput(ostream& os) |
|
46 :iOutStream(os) |
|
47 { |
|
48 Set(iBuf,KBufSize); |
|
49 } |
|
50 |
|
51 void TFileOutput::OverflowL() |
|
52 // |
|
53 // empty the buffer and reset the pointers |
|
54 // |
|
55 { |
|
56 FlushL(); |
|
57 Set(iBuf,KBufSize); |
|
58 } |
|
59 |
|
60 void TFileOutput::FlushL() |
|
61 // |
|
62 // write out the contents of the buffer |
|
63 // |
|
64 { |
|
65 TInt len=Ptr()-iBuf; |
|
66 if (len) |
|
67 { |
|
68 iOutStream.write(reinterpret_cast<char *>(iBuf), len); // write extended header |
|
69 iDataCount += len; |
|
70 } |
|
71 } |
|
72 |
|
73 void DeflateCompress(char *bytes,TInt size,ostream &os) |
|
74 { |
|
75 TFileOutput* output=new TFileOutput(os); |
|
76 output->iDataCount = 0; |
|
77 DeflateL((TUint8*)bytes,size,*output); |
|
78 output->FlushL(); |
|
79 delete output; |
|
80 } |
|
81 |
|
82 TUint32 DeflateCompressCheck(char *bytes,TInt size,ostream &os) |
|
83 { |
|
84 TUint32 r = 0; |
|
85 TFileOutput* output=new TFileOutput(os); |
|
86 output->iDataCount = 0; |
|
87 DeflateL((TUint8*)bytes,size,*output); |
|
88 output->FlushL(); |
|
89 |
|
90 r = output->iDataCount; // Preserve the compressed count |
|
91 delete output; |
|
92 return r; // Return the compressed size |
|
93 } |
|
94 |
|
95 class TFileInput : public TBitInput |
|
96 { |
|
97 public: |
|
98 TFileInput(unsigned char* source,int size); |
|
99 |
|
100 private: |
|
101 void UnderflowL(); |
|
102 private: |
|
103 TUint8* iReadBuf; |
|
104 TInt iSize; |
|
105 }; |
|
106 |
|
107 TFileInput::TFileInput(unsigned char* source,int size) |
|
108 :iReadBuf(source),iSize(size) |
|
109 { |
|
110 Set(source,iSize*8); |
|
111 } |
|
112 |
|
113 |
|
114 void TFileInput::UnderflowL() |
|
115 { |
|
116 Print(ESevereError,"Buffer underflow on deflate\n"); |
|
117 } |
|
118 |
|
119 void InflateUnCompress(unsigned char* source, int sourcesize,unsigned char* dest, int destsize) |
|
120 { |
|
121 TFileInput* input = new TFileInput(source, sourcesize); |
|
122 CInflater* inflater=CInflater::NewLC(*input); |
|
123 inflater->ReadL(dest,destsize); |
|
124 delete inflater; |
|
125 delete input; |
|
126 } |
|
127 |
|
128 |