0
|
1 |
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\kernel\crash_gzip.cpp
|
|
15 |
// Wrapper class for using Zlib with the crash logger to create GZip
|
|
16 |
// compatible output
|
|
17 |
//
|
|
18 |
//
|
|
19 |
|
|
20 |
#include "crash_gzip.h"
|
|
21 |
|
|
22 |
IMPORT_C TInt64 CrashTime();
|
|
23 |
|
|
24 |
//********** NOTE ************************************************************
|
|
25 |
// This wrapper depends heavily on the version of Zlib being 1.2.3.
|
|
26 |
// If a different verison of Zlib is used then the fake alloc functions
|
|
27 |
// will need to be checked carefully for changes in the buffer sizes etc.
|
|
28 |
//****************************************************************************
|
|
29 |
|
|
30 |
/** Name of the file containing the crash log
|
|
31 |
@internalComponent
|
|
32 |
*/
|
|
33 |
_LIT8(KCrashLogFileName, "crashlog.txt");
|
|
34 |
|
|
35 |
|
|
36 |
static deflate_state State; /** Store of current state of deflate*/
|
|
37 |
static TInt BufFlags=0; /** Store of 'allocated' buffers flags*/
|
|
38 |
|
|
39 |
|
|
40 |
/** Fake heap alloc function to pass static buffers to deflateInit2_
|
|
41 |
@internalComponent
|
|
42 |
*/
|
|
43 |
|
|
44 |
|
|
45 |
static voidpf NonAllocFunc(voidpf , uInt items, uInt size)
|
|
46 |
{
|
|
47 |
|
|
48 |
static TInt8 Buf1[KCrashZlibBuf1Size]; /** Static 'heap' buffer1*/
|
|
49 |
static TInt8 Buf2[KCrashZlibBuf2Size]; /** Static 'heap' buffer2*/
|
|
50 |
static TInt8 Buf3[KCrashZlibBuf3Size]; /** Static 'heap' buffer3*/
|
|
51 |
static TInt8 Buf4[KCrashZlibBuf4Size]; /** Static 'heap' buffer4*/
|
|
52 |
|
|
53 |
int bytes = items*size;
|
|
54 |
|
|
55 |
if(bytes == sizeof(deflate_state) && !(BufFlags&1))
|
|
56 |
{BufFlags |= 1;
|
|
57 |
return (voidpf)&State;
|
|
58 |
}
|
|
59 |
else if (bytes == KCrashZlibBuf1Size && !(BufFlags&2))
|
|
60 |
{BufFlags |= 2;
|
|
61 |
return (voidpf)Buf1;
|
|
62 |
}
|
|
63 |
else if (bytes == KCrashZlibBuf2Size && !(BufFlags&4))
|
|
64 |
{BufFlags |= 4;
|
|
65 |
return (voidpf)Buf2;
|
|
66 |
}
|
|
67 |
else if (bytes == KCrashZlibBuf3Size && !(BufFlags&8))
|
|
68 |
{BufFlags |= 8;
|
|
69 |
return (voidpf)Buf3;
|
|
70 |
}
|
|
71 |
else if (bytes == KCrashZlibBuf4Size && !(BufFlags&0x10))
|
|
72 |
{BufFlags |= 0x10;
|
|
73 |
return (voidpf)Buf4;
|
|
74 |
}
|
|
75 |
return Z_NULL;
|
|
76 |
}
|
|
77 |
|
|
78 |
/** Dummy free function to be used by deflate
|
|
79 |
@internalComponent
|
|
80 |
*/
|
|
81 |
static void NonFreeFunc(voidpf , voidpf ){};
|
|
82 |
|
|
83 |
|
|
84 |
/** Initialises deflate algorithm and sets the gzip header
|
|
85 |
@internalComponent
|
|
86 |
*/
|
|
87 |
TBool MCrashGzip::Init()
|
|
88 |
{
|
|
89 |
iOutBufLen = 0;
|
|
90 |
iMaxBytes = 0;
|
|
91 |
iBytesOut = 0;
|
|
92 |
|
|
93 |
memclr(&State, sizeof State);
|
|
94 |
BufFlags=0;
|
|
95 |
|
|
96 |
// set max number of out bytes using virtual method of the base class
|
|
97 |
iMaxBytes = GetMaxLength();
|
|
98 |
|
|
99 |
// ensure input buffer is empty and all of output buffer is accessible
|
|
100 |
// Set deflate to point to the output buffer for output
|
|
101 |
iInBuf.SetLength(0);
|
|
102 |
iOutBuf.SetLength(KCrashGzipOutBufSize);
|
|
103 |
iDeflateStrm.next_out = &iOutBuf[0];
|
|
104 |
iDeflateStrm.avail_out = KCrashGzipOutBufSize;
|
|
105 |
|
|
106 |
// pass the pseudo 'heap' allocation functions to deflate
|
|
107 |
iDeflateStrm.zalloc = (alloc_func)NonAllocFunc;
|
|
108 |
iDeflateStrm.zfree = (free_func)NonFreeFunc;
|
|
109 |
iDeflateStrm.opaque = (voidpf)0;
|
|
110 |
|
|
111 |
// Initialise deflate to create a GZip compatible output
|
|
112 |
TInt err=deflateInit2(&iDeflateStrm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
|
113 |
(KCrashZlibWinBits+16), KCrashZlibMemLvl,
|
|
114 |
Z_DEFAULT_STRATEGY);
|
|
115 |
if (err != Z_OK)
|
|
116 |
{return EFalse;
|
|
117 |
}
|
|
118 |
|
|
119 |
// Set the GZIP header information
|
|
120 |
static gz_header gzHeader;
|
|
121 |
gzHeader.text = 0;
|
|
122 |
|
|
123 |
// GZIP like Unix time starts from 1970AD when SystemTime starts from 0AD hence the adjustment.
|
|
124 |
TInt64 secsSince0AD = CrashTime();;
|
|
125 |
TInt64 secsSince1970AD = secsSince0AD - KYear1970ADInSeconds;
|
|
126 |
|
|
127 |
gzHeader.time = (uLong) secsSince1970AD;
|
|
128 |
gzHeader.xflags = 0;
|
|
129 |
gzHeader.os = 255;
|
|
130 |
gzHeader.extra = Z_NULL;
|
|
131 |
gzHeader.name = (Bytef*) KCrashLogFileName().Ptr();
|
|
132 |
gzHeader.name_max = KCrashLogFileName().Length();
|
|
133 |
gzHeader.comment = Z_NULL;
|
|
134 |
gzHeader.hcrc = 0;
|
|
135 |
err = deflateSetHeader(&iDeflateStrm, &gzHeader);
|
|
136 |
if (err != Z_OK )
|
|
137 |
{return EFalse;
|
|
138 |
}
|
|
139 |
return ETrue;
|
|
140 |
}
|
|
141 |
|
|
142 |
TBool MCrashGzip::Write(const TDesC8& aDes)
|
|
143 |
{
|
|
144 |
// Point delfate to the new data to be compressed
|
|
145 |
iDeflateStrm.next_in = (Bytef*)aDes.Ptr();
|
|
146 |
iDeflateStrm.avail_in = aDes.Length();
|
|
147 |
|
|
148 |
// Keep processing input data until it has all been processed or
|
|
149 |
// the output limit has been reached. Output limit is set to be the
|
|
150 |
// max no. of bytes that been stored minus the number of bytes that deflate
|
|
151 |
// would need to guarantee that it will be able to complete the GZIP file
|
|
152 |
// and footer successfully
|
|
153 |
while ( iDeflateStrm.avail_in > 0 &&
|
|
154 |
(iMaxBytes == -1 || iBytesOut < iMaxBytes-KCrashGzipFooterMax-(KCrashGzipOutBufSize-(TInt)iDeflateStrm.avail_out)))
|
|
155 |
{
|
|
156 |
// perform compression of the log data
|
|
157 |
// can't really do anything if this fails so just return truncated but the GZIP
|
|
158 |
// file will be corrupt and no footer info etc.
|
|
159 |
if (deflate(&iDeflateStrm, Z_NO_FLUSH) != Z_OK)
|
|
160 |
{
|
|
161 |
return ETrue;
|
|
162 |
}
|
|
163 |
|
|
164 |
// Was the output buffer filled?
|
|
165 |
if (iDeflateStrm.avail_out == 0)
|
|
166 |
{// output buffer full so write the data to the flash and empty it
|
|
167 |
Out(iOutBuf);
|
|
168 |
iBytesOut += KCrashGzipOutBufSize;
|
|
169 |
// Set the any new compressed data to be added to the start of the output buffer
|
|
170 |
iDeflateStrm.next_out = &iOutBuf[0];
|
|
171 |
iDeflateStrm.avail_out = KCrashGzipOutBufSize;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
// if all the data couldn't be output it was truncated
|
|
176 |
return (iDeflateStrm.avail_in > 0)? (TBool)ETrue : (TBool)EFalse;
|
|
177 |
}
|
|
178 |
|
|
179 |
void MCrashGzip::FlushEnd()
|
|
180 |
{
|
|
181 |
// Force deflate to flush any buffered data until all buffered data
|
|
182 |
// has been output by deflate indicated by it returning Z_STREAM_END
|
|
183 |
iDeflateStrm.avail_in = 0;
|
|
184 |
TInt err = ~Z_STREAM_END;
|
|
185 |
while (err != Z_STREAM_END)
|
|
186 |
{
|
|
187 |
err = deflate(&iDeflateStrm, Z_FINISH);
|
|
188 |
|
|
189 |
// Update for any flushed data and write the new output data to flash
|
|
190 |
iOutBuf.SetLength(KCrashGzipOutBufSize - iDeflateStrm.avail_out);
|
|
191 |
Out(iOutBuf);
|
|
192 |
iOutBuf.SetLength(KCrashGzipOutBufSize);
|
|
193 |
iDeflateStrm.next_out = &iOutBuf[0];
|
|
194 |
iDeflateStrm.avail_out = KCrashGzipOutBufSize;
|
|
195 |
}
|
|
196 |
|
|
197 |
// Stop the deflate algorithm
|
|
198 |
deflateEnd(&iDeflateStrm);
|
|
199 |
}
|
|
200 |
|
|
201 |
TUint32 MCrashGzip::GetDataCompressed()
|
|
202 |
{
|
|
203 |
return iDeflateStrm.total_in;
|
|
204 |
}
|
|
205 |
|