|
1 /* |
|
2 * Copyright (c) 2008 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 "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: NVG Decoder source file |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "NVGIconData.h" |
|
19 |
|
20 //const TUint KEncodedDataLength = 1024; |
|
21 const TUint KEncodedDataGranularity = 64; |
|
22 |
|
23 CNVGIconData::CNVGIconData() |
|
24 : iNVGData(0), |
|
25 iNVGDataPtr(0, 0), |
|
26 iTotalRead(0), |
|
27 iReadStream(0) |
|
28 { |
|
29 } |
|
30 |
|
31 CNVGIconData::~CNVGIconData() |
|
32 { |
|
33 delete iNVGData; |
|
34 } |
|
35 |
|
36 CNVGIconData * CNVGIconData::NewL(TInt aLength) |
|
37 { |
|
38 CNVGIconData* self = CNVGIconData::NewLC(aLength); |
|
39 CleanupStack::Pop(self); |
|
40 return self; |
|
41 } |
|
42 |
|
43 CNVGIconData * CNVGIconData::NewLC(TInt aLength) |
|
44 { |
|
45 CNVGIconData* self = new (ELeave) CNVGIconData; |
|
46 CleanupStack::PushL(self); |
|
47 |
|
48 self->ConstructL(aLength); |
|
49 return self; |
|
50 } |
|
51 |
|
52 CNVGIconData * CNVGIconData::NewL(const TDesC8& aBuf) |
|
53 { |
|
54 CNVGIconData* self = CNVGIconData::NewLC(aBuf); |
|
55 CleanupStack::Pop(self); |
|
56 return self; |
|
57 } |
|
58 |
|
59 CNVGIconData * CNVGIconData::NewLC(const TDesC8& aBuf) |
|
60 { |
|
61 CNVGIconData* self = new (ELeave) CNVGIconData; |
|
62 CleanupStack::PushL(self); |
|
63 |
|
64 self->ConstructL(aBuf); |
|
65 |
|
66 return self; |
|
67 } |
|
68 |
|
69 CNVGIconData * CNVGIconData::NewL(TUint8 * aBuf, TInt aLength) |
|
70 { |
|
71 return CNVGIconData::NewL(TPtr8(aBuf, aLength, aLength)); |
|
72 } |
|
73 |
|
74 CNVGIconData * CNVGIconData::NewLC(TUint8 * aBuf, TInt aLength) |
|
75 { |
|
76 return CNVGIconData::NewLC(TPtr8(aBuf, aLength, aLength)); |
|
77 } |
|
78 |
|
79 void CNVGIconData::ConstructL(const TDesC8& aBuf) |
|
80 { |
|
81 iNVGDataPtr.Set((TUint8 *)aBuf.Ptr(), aBuf.Length(), aBuf.Length()); |
|
82 |
|
83 //set the reading pointers |
|
84 BeginRead(); |
|
85 } |
|
86 |
|
87 void CNVGIconData::ConstructL(TUint aLength) |
|
88 { |
|
89 iNVGData = HBufC8::NewL(aLength); |
|
90 iNVGDataPtr = iNVGData->Des(); |
|
91 } |
|
92 |
|
93 void CNVGIconData::EncodeDataL(const TAny *aData, TUint aLength) |
|
94 { |
|
95 TInt result = KErrNone; |
|
96 |
|
97 TInt encodedDataLength = iNVGDataPtr.Length() + aLength; |
|
98 TInt encodedDataMaxLength = iNVGDataPtr.MaxLength(); |
|
99 |
|
100 if (encodedDataLength >= encodedDataMaxLength) |
|
101 { |
|
102 result = ExpandEncodedData(encodedDataLength); |
|
103 if (result != KErrNone) |
|
104 { |
|
105 User::Leave(result); |
|
106 } |
|
107 |
|
108 iNVGDataPtr.Append((TUint8*)(aData), aLength); |
|
109 } |
|
110 else |
|
111 { |
|
112 iNVGDataPtr.Append((TUint8*)(aData), aLength); |
|
113 } |
|
114 } |
|
115 |
|
116 TInt CNVGIconData::EncodeData(const TAny *aData, TUint aLength) |
|
117 { |
|
118 TInt result = KErrNone; |
|
119 |
|
120 TInt encodedDataLength = iNVGDataPtr.Length() + aLength; |
|
121 TInt encodedDataMaxLength = iNVGDataPtr.MaxLength(); |
|
122 |
|
123 if (encodedDataLength >= encodedDataMaxLength) |
|
124 { |
|
125 result = ExpandEncodedData(encodedDataLength); |
|
126 if (result == KErrNone) |
|
127 { |
|
128 iNVGDataPtr.Append((TUint8*)(aData), aLength); |
|
129 } |
|
130 } |
|
131 else |
|
132 { |
|
133 iNVGDataPtr.Append((TUint8*)(aData), aLength); |
|
134 } |
|
135 |
|
136 return result; |
|
137 } |
|
138 |
|
139 TInt CNVGIconData::ExpandEncodedData(TUint aNewLength) |
|
140 { |
|
141 TInt result = KErrNone; |
|
142 |
|
143 TInt encodedDataMaxLength = iNVGDataPtr.MaxLength(); |
|
144 TUint granularities = ((aNewLength - encodedDataMaxLength) / KEncodedDataGranularity) + 1; |
|
145 |
|
146 HBufC8 * tmpBuf = HBufC8::New(encodedDataMaxLength + granularities * KEncodedDataGranularity); |
|
147 if (tmpBuf == 0) |
|
148 { |
|
149 result = KErrNoMemory; |
|
150 } |
|
151 else |
|
152 { |
|
153 TPtr8 tmpBufPtr (tmpBuf->Des()); |
|
154 tmpBufPtr.Copy(*iNVGData); |
|
155 |
|
156 delete iNVGData; |
|
157 iNVGData = tmpBuf; |
|
158 iNVGDataPtr.Set(iNVGData->Des()); |
|
159 } |
|
160 |
|
161 return result; |
|
162 } |
|
163 |
|
164 void CNVGIconData::BeginRead() |
|
165 { |
|
166 iDataLength = iNVGDataPtr.Length(); |
|
167 iTotalRead = 0; |
|
168 iReadStream = (TUint8 *)iNVGDataPtr.Ptr(); |
|
169 } |
|
170 |
|
171 void CNVGIconData::EndRead() |
|
172 { |
|
173 } |
|
174 |
|
175 #define STR_TO_OTHER_DIR(TOTYPE) do {\ |
|
176 TOTYPE data = *(TOTYPE *)&iReadStream[iTotalRead];\ |
|
177 iTotalRead += sizeof(TOTYPE);\ |
|
178 return data;\ |
|
179 } while (0) |
|
180 |
|
181 |
|
182 #define STR_TO_OTHER_IDIR(TOTYPE) do {\ |
|
183 TOTYPE data;\ |
|
184 TUint8 * dataPtr = (TUint8 *)&data;\ |
|
185 for (TInt i = 0; i < sizeof(TOTYPE); ++i)\ |
|
186 {\ |
|
187 dataPtr[i] = iReadStream[iTotalRead+i];\ |
|
188 }\ |
|
189 iTotalRead += sizeof(TOTYPE);\ |
|
190 return data;\ |
|
191 } while (0) |
|
192 |
|
193 #define STR_TO_OTHER(TOTYPE) do {\ |
|
194 CheckOutOfBoundL(sizeof(TOTYPE));\ |
|
195 if (reinterpret_cast<int>(&iReadStream[iTotalRead]) & (sizeof(TOTYPE) - 1))\ |
|
196 {\ |
|
197 STR_TO_OTHER_IDIR(TOTYPE);\ |
|
198 }\ |
|
199 else\ |
|
200 {\ |
|
201 STR_TO_OTHER_DIR(TOTYPE);\ |
|
202 }\ |
|
203 } while (0) |
|
204 |
|
205 |
|
206 TInt16 CNVGIconData::ReadInt16L() |
|
207 { |
|
208 STR_TO_OTHER(TInt16); |
|
209 } |
|
210 |
|
211 TInt32 CNVGIconData::ReadInt32L() |
|
212 { |
|
213 STR_TO_OTHER(TInt32); |
|
214 } |
|
215 |
|
216 TInt8 CNVGIconData::ReadInt8L() |
|
217 { |
|
218 CheckOutOfBoundL(sizeof(TInt8)); |
|
219 STR_TO_OTHER_DIR(TInt8); |
|
220 } |
|
221 |
|
222 TReal32 CNVGIconData::ReadReal32L() |
|
223 { |
|
224 STR_TO_OTHER(TReal32); |
|
225 } |
|
226 |
|
227 TReal64 CNVGIconData::ReadReal64L() |
|
228 { |
|
229 CheckOutOfBoundL(sizeof(TReal64)); |
|
230 STR_TO_OTHER_IDIR(TReal64); |
|
231 } |
|
232 |
|
233 void CNVGIconData::ReadL(TDes8 &aDes, TInt aLength) |
|
234 { |
|
235 CheckOutOfBoundL(aLength); |
|
236 aDes.Copy(&iReadStream[iTotalRead], aLength); |
|
237 iTotalRead += aLength; |
|
238 } |
|
239 |
|
240 void CNVGIconData::ReadL(TUint8 *aPtr, TInt aLength) |
|
241 { |
|
242 CheckOutOfBoundL(aLength); |
|
243 memcpy(aPtr, &iReadStream[iTotalRead], aLength); |
|
244 iTotalRead += aLength; |
|
245 } |
|
246 |
|
247 void CNVGIconData::SkipL(TInt aLength) |
|
248 { |
|
249 CheckOutOfBoundL(aLength); |
|
250 iTotalRead += aLength; |
|
251 } |