|
1 /* |
|
2 * Copyright (c) 2004-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 * Note: This file may contain code to generate corrupt files for test purposes. |
|
16 * Such code is excluded from production builds by use of compiler defines; |
|
17 * it is recommended that such code should be removed if this code is ever published publicly. |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 /** |
|
23 @file |
|
24 @internalComponent |
|
25 @released |
|
26 */ |
|
27 |
|
28 #include "header.h" |
|
29 #include "utility.h" |
|
30 |
|
31 |
|
32 |
|
33 static const TUint32 KBigBit = 0x80000000; |
|
34 static const TUint32 KSmallMask = ~KBigBit; |
|
35 static const unsigned short KSizeShift = 31; |
|
36 static const TUint32 KHalfMask = 0xFFFFFFFF; |
|
37 |
|
38 |
|
39 void CSISHeader::BigToSmalls (CSISUInt32& aSmall, CSISUInt32& aBig) const |
|
40 { |
|
41 #ifdef GENERATE_ERRORS |
|
42 assert (IsBigHeader () || CSISFieldRoot::IsBugSet (CSISFieldRoot::EBug32As64)); |
|
43 #else // GENERATE_ERRORS |
|
44 assert (IsBigHeader ()); |
|
45 #endif // GENERATE_ERRORS |
|
46 |
|
47 RawBigToSmalls (iDataSize, aSmall, aBig); |
|
48 } |
|
49 |
|
50 |
|
51 bool CSISHeader::IsBigHeader () const |
|
52 { |
|
53 return (iDataSize >= KBigBit); |
|
54 } |
|
55 |
|
56 bool CSISHeader::IsBigHeader (const TUint64& aSize) |
|
57 { |
|
58 return (aSize >= KBigBit); |
|
59 } |
|
60 |
|
61 void CSISHeader::Read (TSISStream& aFile, const TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType) |
|
62 { |
|
63 iDataType = static_cast <CSISFieldRoot::TFieldType> ( |
|
64 RawRead (iDataSize, aFile, aContainerSize, aArrayType)); |
|
65 } |
|
66 |
|
67 void CSISHeader::Write (TSISStream& aFile, const bool aIsArrayElement) const |
|
68 { |
|
69 RawWrite (static_cast <TUint32> (iDataType), iDataSize, aFile, aIsArrayElement); |
|
70 } |
|
71 |
|
72 CSISHeader::TFieldSize CSISHeader::ByteCount (const bool aInsideArray) const |
|
73 { |
|
74 return RawByteCount (iDataSize, aInsideArray); |
|
75 } |
|
76 |
|
77 void CSISHeader::Dump (std::ostream& aStream, const int aLevel) const |
|
78 { |
|
79 aStream << "|" << iDataType << "," << iDataSize << "|"; |
|
80 } |
|
81 |
|
82 void CSISHeader::CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const |
|
83 { |
|
84 RawCalculateCrc (iDataType, iDataSize, aCRC, aIsArrayElement); |
|
85 } |
|
86 |
|
87 |
|
88 void CSISHeader::RawBigToSmalls (const TUint64& aSize, CSISUInt32& aSmall, CSISUInt32& aBig) |
|
89 { |
|
90 #ifdef GENERATE_ERRORS |
|
91 assert ((aSize >= KBigBit) || CSISFieldRoot::IsBugSet (CSISFieldRoot::EBug32As64)); |
|
92 #else |
|
93 assert (aSize >= KBigBit); |
|
94 #endif |
|
95 // this isn't a big number |
|
96 aSmall = static_cast <TUint32> ((aSize & KSmallMask) | KBigBit); |
|
97 aBig = static_cast <TUint32> ((aSize >> KSizeShift) & KHalfMask); |
|
98 } |
|
99 |
|
100 |
|
101 TUint32 CSISHeader::RawRead ( TUint64& aSize, |
|
102 TSISStream& aFile, |
|
103 const TFieldSize& aContainerSize, |
|
104 const CSISFieldRoot::TFieldType aArrayType) |
|
105 { |
|
106 TUint32 type = static_cast <TUint32> (aArrayType); |
|
107 for (;;) |
|
108 { |
|
109 ReadFiller (aFile); |
|
110 if (aArrayType == CSISFieldRoot::ESISUndefined) |
|
111 { |
|
112 aFile >> type; |
|
113 } |
|
114 CSISUInt32 smaller; |
|
115 smaller.Read (aFile, aContainerSize, aArrayType); |
|
116 if ((smaller & KBigBit) == 0) |
|
117 { |
|
118 aSize = smaller; |
|
119 } |
|
120 else |
|
121 { |
|
122 smaller = smaller & KSmallMask; |
|
123 CSISUInt32 bigger; |
|
124 bigger.Read (aFile, aContainerSize, aArrayType); |
|
125 aSize = smaller + (bigger << KSizeShift); |
|
126 } |
|
127 if (type < CSISFieldRoot::ESISUnknown) |
|
128 { |
|
129 return type; |
|
130 } |
|
131 aFile.seek (aSize, std::ios_base::cur); |
|
132 } |
|
133 } |
|
134 |
|
135 void CSISHeader::RawWrite (const TUint32 aType, const TUint64& aSize, TSISStream& aFile, const bool aIsArrayElement) |
|
136 { |
|
137 WriteFiller (aFile); |
|
138 if (! aIsArrayElement) |
|
139 { |
|
140 #ifdef GENERATE_ERRORS |
|
141 if (CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugDuffFieldType)) |
|
142 { |
|
143 aFile << static_cast <TUint32> (rand ()); |
|
144 } |
|
145 else |
|
146 #endif // GENERATE_ERRORS |
|
147 { |
|
148 aFile << aType; |
|
149 } |
|
150 } |
|
151 CSISUInt32 smaller; |
|
152 #ifdef GENERATE_ERRORS |
|
153 if ((aSize < KBigBit) && ! CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBug32As64)) |
|
154 #else // GENERATE_ERRORS |
|
155 if (aSize < KBigBit) |
|
156 #endif // GENERATE_ERRORS |
|
157 { |
|
158 #ifdef GENERATE_ERRORS |
|
159 if (CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugInvalidLength)) |
|
160 { |
|
161 smaller = static_cast <TUint32> (rand () & KSmallMask); |
|
162 } |
|
163 else |
|
164 if (CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugNegativeLength)) |
|
165 { |
|
166 smaller = KSmallMask; |
|
167 } |
|
168 else |
|
169 #endif // GENERATE_ERRORS |
|
170 { |
|
171 smaller = static_cast <TUint32> (aSize & KSmallMask); |
|
172 } |
|
173 smaller.Write (aFile, false); |
|
174 } |
|
175 else |
|
176 { |
|
177 CSISUInt32 bigger; |
|
178 RawBigToSmalls (aSize, smaller, bigger); |
|
179 smaller.Write (aFile, false); |
|
180 bigger.Write (aFile, false); |
|
181 } |
|
182 } |
|
183 |
|
184 CSISHeader::TFieldSize CSISHeader::RawByteCount (const TUint64& aSize, const bool aInsideArray) |
|
185 { |
|
186 CSISHeader::TFieldSize size (aInsideArray ? 0 : CSISFieldRoot::SizeOfFieldType ()); |
|
187 if (aSize >= KBigBit) |
|
188 { |
|
189 size += sizeof (TUint32) * 2; |
|
190 } |
|
191 else |
|
192 { |
|
193 size += sizeof (TUint32); |
|
194 } |
|
195 return size; |
|
196 } |
|
197 |
|
198 void CSISHeader::RawCalculateCrc (const TUint32 aType, const TUint64& aSize, TCRC& aCRC, const bool aIsArrayElement) |
|
199 { |
|
200 if (! aIsArrayElement) |
|
201 { |
|
202 CSISUInt32 (aType).CalculateCrc (aCRC, false); |
|
203 } |
|
204 if (aSize < KBigBit) |
|
205 { |
|
206 CSISUInt32 (static_cast <TUint32> (aSize & KSmallMask)).CalculateCrc (aCRC, false); |
|
207 } |
|
208 else |
|
209 { |
|
210 CSISUInt32 smaller; |
|
211 CSISUInt32 bigger; |
|
212 RawBigToSmalls (aSize, smaller, bigger); |
|
213 smaller.CalculateCrc (aCRC, false); |
|
214 bigger.CalculateCrc (aCRC, false); |
|
215 } |
|
216 } |
|
217 |
|
218 |
|
219 bool CSISHeader::IsNextAsExpected ( TSISStream& aFile, CSISHeader& aHeader, |
|
220 const CSISFieldRoot::TFieldType aExpectedType, |
|
221 const CSISFieldRoot::TFieldType aArrayType, |
|
222 const bool aPeek) |
|
223 { |
|
224 TSISStream::pos_type pos = aFile.tell (); |
|
225 ReadFiller (aFile); |
|
226 aHeader.Read (aFile, sizeof (CSISHeader), aArrayType); |
|
227 bool reply = (aHeader.DataType () == aExpectedType); |
|
228 if (aPeek || ! reply) |
|
229 { |
|
230 aFile.seek (pos); |
|
231 } |
|
232 return reply; |
|
233 } |
|
234 |