|
1 // Copyright (c) 2004-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 "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 // |
|
15 |
|
16 /** @file |
|
17 @internalTechnology */ |
|
18 #include <caf/data.h> |
|
19 #include "MngPanic.h" |
|
20 #include "Crc32.h" |
|
21 |
|
22 #include "MngReadStream.h" |
|
23 |
|
24 static inline |
|
25 void Big2SmallEndian(const TInt32 aBig, TInt32& aSmall) |
|
26 { |
|
27 aSmall = ((aBig&0xff)<<24) | ((aBig&0xff00)<<8) | ((aBig&0xff0000)>>8) | ((aBig>>24)&0xff); |
|
28 } |
|
29 |
|
30 static inline |
|
31 void Big2SmallEndian(const TUint16 aBig, TUint16& aSmall) |
|
32 { |
|
33 aSmall = TUint16( ((aBig&0xff)<<8) | ((aBig>>8)&0xff) ); |
|
34 } |
|
35 |
|
36 /*static*/ |
|
37 CMngFileReadStream* CMngFileReadStream::NewL(ContentAccess::CData* aCafData) |
|
38 { |
|
39 CleanupStack::PushL(aCafData); |
|
40 CMngFileReadStream* self=new (ELeave) CMngFileReadStream(aCafData); |
|
41 CleanupStack::Pop(); |
|
42 CleanupStack::PushL(self); |
|
43 self->ConstructL(); |
|
44 CleanupStack::Pop(self); |
|
45 return self; |
|
46 } |
|
47 |
|
48 /*static*/ |
|
49 CMngFileReadStream* CMngFileReadStream::NewL(const TDesC8& aData) |
|
50 { |
|
51 CMngFileReadStream* self=new (ELeave) CMngFileReadStream(); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(aData); |
|
54 CleanupStack::Pop(self); |
|
55 return self; |
|
56 } |
|
57 |
|
58 void CMngFileReadStream::ConstructL() |
|
59 { |
|
60 ASSERT(iCafData); |
|
61 TInt len=0; |
|
62 iCafData->DataSizeL(len); |
|
63 iOwnedData = HBufC8::NewL(len); |
|
64 TPtr8 ptr(iOwnedData->Des()); |
|
65 User::LeaveIfError(iCafData->Read(ptr)); |
|
66 delete iCafData; |
|
67 iCafData = NULL; |
|
68 ConstructL(ptr); |
|
69 } |
|
70 |
|
71 void CMngFileReadStream::ConstructL(const TDesC8& aData) |
|
72 { |
|
73 iMemBuf.Set(aData); |
|
74 } |
|
75 |
|
76 CMngFileReadStream::~CMngFileReadStream() |
|
77 { |
|
78 delete iOwnedData; |
|
79 delete iCafData; |
|
80 } |
|
81 |
|
82 void CMngFileReadStream::AddRef() |
|
83 { |
|
84 ++iRefCount; |
|
85 } |
|
86 |
|
87 void CMngFileReadStream::Release() |
|
88 { |
|
89 if (--iRefCount==0) |
|
90 { |
|
91 delete this; |
|
92 } |
|
93 } |
|
94 |
|
95 TInt CMngFileReadStream::DataAvailable() |
|
96 { |
|
97 return iMemBuf.Length() - iPosition; |
|
98 } |
|
99 |
|
100 void CMngFileReadStream::WaitForData(TInt aDataNeeded, TRequestStatus& aReq) |
|
101 { |
|
102 TRequestStatus* pReq=&aReq; |
|
103 if (iPosition + aDataNeeded <= DataAvailable() ) |
|
104 { |
|
105 User::RequestComplete(pReq, KErrNone); |
|
106 } |
|
107 else |
|
108 { |
|
109 User::RequestComplete(pReq, KErrUnderflow); |
|
110 } |
|
111 } |
|
112 |
|
113 void CMngFileReadStream::SeekL(TInt aPosition) |
|
114 { |
|
115 if (aPosition >= iPosition + DataAvailable() ) |
|
116 { |
|
117 User::Leave(KErrEof); |
|
118 } |
|
119 iPosition = aPosition; |
|
120 } |
|
121 |
|
122 TInt CMngFileReadStream::Tell() |
|
123 { |
|
124 return iPosition; |
|
125 } |
|
126 |
|
127 void CMngFileReadStream::Read(TDes8& aBuf, TRequestStatus& aReq, TInt32* aCrc) |
|
128 { |
|
129 TRequestStatus* pReq=&aReq; |
|
130 if (aBuf.MaxLength() > DataAvailable()) |
|
131 { |
|
132 User::RequestComplete(pReq, KErrEof); |
|
133 } |
|
134 else |
|
135 { |
|
136 aBuf.Copy(&iMemBuf[iPosition], aBuf.MaxLength()); |
|
137 aBuf.SetLength(aBuf.MaxLength()); |
|
138 iPosition +=aBuf.Length(); |
|
139 if (NULL!=aCrc) |
|
140 { |
|
141 Crc32::Calc(*aCrc, aBuf.Ptr(), aBuf.Length()); |
|
142 } |
|
143 User::RequestComplete(pReq, KErrNone); |
|
144 } |
|
145 } |
|
146 |
|
147 void CMngFileReadStream::ReadL(TDes8& aBuf, TInt32* aCrc) |
|
148 { |
|
149 if (aBuf.MaxLength() > DataAvailable()) |
|
150 { |
|
151 User::Leave(KErrEof); |
|
152 } |
|
153 else |
|
154 { |
|
155 aBuf.Copy(&iMemBuf[iPosition], aBuf.MaxLength()); |
|
156 aBuf.SetLength(aBuf.MaxLength()); |
|
157 iPosition +=aBuf.Length(); |
|
158 if (NULL!=aCrc) |
|
159 { |
|
160 Crc32::Calc(*aCrc, aBuf.Ptr(), aBuf.Length()); |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 |
|
166 void CMngFileReadStream::ReadInt32L(TInt32& aNumber, TInt32* aCrc) |
|
167 { |
|
168 if ( TInt(sizeof(aNumber)) > DataAvailable()) |
|
169 { |
|
170 User::Leave(KErrUnderflow); |
|
171 } |
|
172 Mem::Copy(&aNumber, &iMemBuf[iPosition], sizeof(aNumber) ); |
|
173 if (NULL!=aCrc) |
|
174 { |
|
175 Crc32::Calc(*aCrc, &aNumber, sizeof(aNumber) ); |
|
176 } |
|
177 Big2SmallEndian(aNumber,aNumber); |
|
178 iPosition +=sizeof(aNumber); |
|
179 } |
|
180 |
|
181 void CMngFileReadStream::ReadUint16L(TUint16& aNumber, TInt32* aCrc) |
|
182 { |
|
183 if ( TInt(sizeof(aNumber)) > DataAvailable()) |
|
184 { |
|
185 User::Leave(KErrUnderflow); |
|
186 } |
|
187 Mem::Copy(&aNumber, &iMemBuf[iPosition], sizeof(aNumber) ); |
|
188 if (NULL!=aCrc) |
|
189 { |
|
190 Crc32::Calc(*aCrc, &aNumber, sizeof(aNumber) ); |
|
191 } |
|
192 Big2SmallEndian(aNumber,aNumber); |
|
193 iPosition +=sizeof(aNumber); |
|
194 } |
|
195 |
|
196 void CMngFileReadStream::ReadUint8L(TUint8& aNumber, TInt32* aCrc) |
|
197 { |
|
198 if (TInt(sizeof(aNumber)) > DataAvailable()) |
|
199 { |
|
200 User::Leave(KErrUnderflow); |
|
201 } |
|
202 aNumber = iMemBuf[iPosition++]; |
|
203 if (NULL!=aCrc) |
|
204 { |
|
205 Crc32::Calc(*aCrc, &aNumber, sizeof(aNumber) ); |
|
206 } |
|
207 } |
|
208 |
|
209 void CMngFileReadStream::ReadInt32L(TInt32* aFirst, TInt aCount, TInt32* aCrc) |
|
210 { |
|
211 const TInt Size = aCount*sizeof(*aFirst); |
|
212 if (Size > DataAvailable()) |
|
213 { |
|
214 User::Leave(KErrUnderflow); |
|
215 } |
|
216 Mem::Copy(aFirst, &iMemBuf[iPosition], Size); |
|
217 if (NULL!=aCrc) |
|
218 { |
|
219 Crc32::Calc(*aCrc, aFirst, Size ); |
|
220 } |
|
221 for (; aCount; --aCount,++aFirst) |
|
222 { |
|
223 Big2SmallEndian(*aFirst, *aFirst); |
|
224 } |
|
225 iPosition +=Size; |
|
226 } |
|
227 |
|
228 void CMngFileReadStream::ReadUint16L(TUint16* aFirst,TInt aCount, TInt32* aCrc) |
|
229 { |
|
230 const TInt Size = aCount*sizeof(*aFirst); |
|
231 if (Size > DataAvailable()) |
|
232 { |
|
233 User::Leave(KErrUnderflow); |
|
234 } |
|
235 Mem::Copy(aFirst, &iMemBuf[iPosition], Size); |
|
236 if (NULL!=aCrc) |
|
237 { |
|
238 Crc32::Calc(*aCrc, aFirst, Size ); |
|
239 } |
|
240 for (; aCount; --aCount,++aFirst) |
|
241 { |
|
242 Big2SmallEndian(*aFirst, *aFirst); |
|
243 } |
|
244 iPosition +=Size; |
|
245 } |
|
246 |
|
247 void CMngFileReadStream::ReadUint8L(TUint8* aFirst, TInt aCount, TInt32* aCrc) |
|
248 { |
|
249 if (aCount > DataAvailable()) |
|
250 { |
|
251 User::Leave(KErrUnderflow); |
|
252 } |
|
253 Mem::Copy(aFirst, &iMemBuf[iPosition], aCount); |
|
254 if (NULL!=aCrc) |
|
255 { |
|
256 Crc32::Calc(*aCrc, aFirst, aCount ); |
|
257 } |
|
258 iPosition +=aCount; |
|
259 } |
|
260 |
|
261 /*static*/ |
|
262 CMngIclReadStream* CMngIclReadStream::NewL(MDataFeed& aDataSupplier) |
|
263 { |
|
264 CMngIclReadStream* self=new (ELeave) CMngIclReadStream(aDataSupplier); |
|
265 CleanupStack::PushL(self); |
|
266 self->ConstructL(); |
|
267 CleanupStack::Pop(self); |
|
268 return self; |
|
269 } |
|
270 |
|
271 void CMngIclReadStream::ConstructL() |
|
272 { |
|
273 RestartL(); |
|
274 } |
|
275 |
|
276 void CMngIclReadStream::RestartL() |
|
277 { |
|
278 Reset(); |
|
279 User::LeaveIfError( iDataSupplier.LockBuffer(iOverallPosition,iMemBuf,64) ); |
|
280 } |
|
281 |
|
282 void CMngIclReadStream::WaitForData(TInt aDataNeeded, TRequestStatus& aReq) |
|
283 { |
|
284 TRequestStatus* pReq=&aReq; |
|
285 if (aDataNeeded <= iMemBuf.Length() - iPosition) |
|
286 { |
|
287 User::RequestComplete(pReq, KErrNone); |
|
288 return; |
|
289 } |
|
290 TInt err=iDataSupplier.LockBuffer(iOverallPosition+iPosition, iMemBuf, aDataNeeded); |
|
291 if (err == KErrNone) |
|
292 { |
|
293 iOverallPosition+=iPosition; |
|
294 iPosition=0; |
|
295 } |
|
296 User::RequestComplete(pReq, err); |
|
297 } |
|
298 |
|
299 TInt CMngIclReadStream::Tell() |
|
300 { |
|
301 return iOverallPosition+iPosition; |
|
302 } |
|
303 |
|
304 void CMngIclReadStream::SeekL(TInt aPosition) |
|
305 { |
|
306 if (aPosition<0) |
|
307 { |
|
308 __DEBUGGER(); |
|
309 User::Leave(KErrNotSupported); |
|
310 return; |
|
311 } |
|
312 if (aPosition - iOverallPosition > DataAvailable() || aPosition < iOverallPosition) |
|
313 { |
|
314 iPosition = 0; |
|
315 iOverallPosition= aPosition; |
|
316 iMemBuf.Set(KNullDesC8); |
|
317 } |
|
318 else |
|
319 { |
|
320 iPosition = aPosition - iOverallPosition; |
|
321 } |
|
322 } |
|
323 |
|
324 void CMngIclReadStream::InvalidateBuffer() |
|
325 { |
|
326 iMemBuf.Set(KNullDesC8); |
|
327 } |
|
328 |
|
329 void CMngIclReadStream::Reset() |
|
330 { |
|
331 InvalidateBuffer(); |
|
332 iOverallPosition= 0; |
|
333 iPosition = 0; |
|
334 } |
|
335 |