|
1 // Copyright (c) 2003-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 // Name : MessageReader.cpp |
|
15 // Part of : SigComp / dispatcher |
|
16 // SigComp message reader |
|
17 // Version : 1.0 |
|
18 // |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 // INCLUDE FILES |
|
24 #include "MessageReader.h" |
|
25 #include "Sigcomp.h" |
|
26 |
|
27 |
|
28 // ============================ MEMBER FUNCTIONS ============================== |
|
29 |
|
30 CMessageReader::CMessageReader(const TDesC8& aMessage, |
|
31 TBool aStreamBasedProtocol) : |
|
32 iMessage(aMessage), |
|
33 iStreamBasedProtocol(aStreamBasedProtocol) |
|
34 { |
|
35 } |
|
36 |
|
37 TInt CMessageReader::ReadByte(TUint& aByte) |
|
38 { |
|
39 |
|
40 if (iStreamBasedProtocol) |
|
41 { |
|
42 if (iQuoteBytes > 0) |
|
43 { |
|
44 if (iPos < iMessage.Length()) |
|
45 { |
|
46 aByte = iMessage[iPos]; |
|
47 iPos++; |
|
48 iQuoteBytes--; |
|
49 return KErrNone; |
|
50 } |
|
51 else |
|
52 { |
|
53 return KErrTooBig; |
|
54 } |
|
55 } |
|
56 else |
|
57 { |
|
58 if (iPos < iMessage.Length()) |
|
59 { |
|
60 aByte = iMessage[iPos]; |
|
61 iPos++; |
|
62 |
|
63 if (aByte == 0xff) |
|
64 { |
|
65 if (iPos < iMessage.Length()) |
|
66 { |
|
67 aByte = iMessage[iPos]; |
|
68 iPos++; |
|
69 |
|
70 if (aByte <= 0x7f) |
|
71 { |
|
72 iQuoteBytes = aByte; |
|
73 aByte = 0xff; |
|
74 return KErrNone; |
|
75 } |
|
76 else if (aByte == 0xff) |
|
77 { |
|
78 return KErrEof; |
|
79 } |
|
80 else // aByte: 0x80-0xfe |
|
81 { |
|
82 return CSigComp::EDecompressionFailure; |
|
83 } |
|
84 } |
|
85 else |
|
86 { |
|
87 return KErrTooBig; |
|
88 } |
|
89 } |
|
90 else |
|
91 { |
|
92 return KErrNone; |
|
93 } |
|
94 } |
|
95 else |
|
96 { |
|
97 return KErrTooBig; |
|
98 } |
|
99 } |
|
100 } |
|
101 else |
|
102 { |
|
103 if (iPos < iMessage.Length()) |
|
104 { |
|
105 aByte = iMessage[iPos]; |
|
106 iPos++; |
|
107 return KErrNone; |
|
108 } |
|
109 else |
|
110 { |
|
111 return KErrTooBig; |
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 TInt CMessageReader::ReadBlock(TDes8& aBlock) |
|
117 { |
|
118 return ReadBlock(aBlock, aBlock.MaxLength()); |
|
119 } |
|
120 |
|
121 TInt CMessageReader::ReadBlock(TDes8& aBlock, TInt aLength) |
|
122 { |
|
123 |
|
124 if (iStreamBasedProtocol) |
|
125 { |
|
126 aBlock.SetLength(0); |
|
127 |
|
128 TInt i; |
|
129 TUint byte; |
|
130 for (i = 0; i < aLength; i++) |
|
131 { |
|
132 TInt retval = ReadByte(byte); |
|
133 if (retval == KErrNone) |
|
134 { |
|
135 aBlock.Append(byte); |
|
136 } |
|
137 else |
|
138 { |
|
139 return retval; |
|
140 } |
|
141 } |
|
142 |
|
143 return KErrNone; |
|
144 } |
|
145 else |
|
146 { |
|
147 |
|
148 TInt len = aLength; |
|
149 |
|
150 if ((iPos + len) > iMessage.Length()) |
|
151 { |
|
152 len = iMessage.Length() - iPos; |
|
153 } |
|
154 |
|
155 aBlock.Copy(iMessage.Mid(iPos, len)); |
|
156 iPos += len; |
|
157 |
|
158 if (len == aLength) |
|
159 { |
|
160 return KErrNone; |
|
161 } |
|
162 else |
|
163 { |
|
164 return KErrTooBig; |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 TInt CMessageReader::Avail(TInt aLength) |
|
170 { |
|
171 |
|
172 if (iStreamBasedProtocol) |
|
173 { |
|
174 TInt quote = iQuoteBytes; |
|
175 TInt pos = iPos; |
|
176 |
|
177 TInt i; |
|
178 TUint byte; |
|
179 |
|
180 for (i = 0; i < aLength; i++) |
|
181 { |
|
182 TInt retval = ReadByte(byte); |
|
183 if (retval != KErrNone) |
|
184 { |
|
185 iQuoteBytes = quote; |
|
186 iPos = pos; |
|
187 return retval; |
|
188 } |
|
189 } |
|
190 |
|
191 iQuoteBytes = quote; |
|
192 iPos = pos; |
|
193 |
|
194 return KErrNone; |
|
195 } |
|
196 else |
|
197 { |
|
198 if ((iPos + aLength) <= iMessage.Length()) |
|
199 { |
|
200 return KErrNone; |
|
201 } |
|
202 else |
|
203 { |
|
204 return KErrTooBig; |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 TInt CMessageReader::SkipDelimiters(TUint aDelToSkip) |
|
210 { |
|
211 if (iStreamBasedProtocol) |
|
212 { |
|
213 TUint i; |
|
214 for (i = 0; i < aDelToSkip; i++) |
|
215 { |
|
216 if (iPos < iMessage.Length()) |
|
217 { |
|
218 if (iMessage[iPos] == 0xff) |
|
219 { |
|
220 if ((iPos+1) < iMessage.Length()) |
|
221 { |
|
222 if (iMessage[iPos+1] == 0xff) |
|
223 { |
|
224 iPos += 2; |
|
225 } |
|
226 else |
|
227 { |
|
228 return KErrNone; |
|
229 } |
|
230 } |
|
231 else |
|
232 { |
|
233 return KErrTooBig; |
|
234 } |
|
235 } |
|
236 else |
|
237 { |
|
238 return KErrNone; |
|
239 } |
|
240 } |
|
241 else |
|
242 { |
|
243 return KErrNone; |
|
244 } |
|
245 } |
|
246 return KErrNone; |
|
247 } |
|
248 else |
|
249 { |
|
250 return KErrNone; |
|
251 } |
|
252 } |
|
253 |
|
254 TInt CMessageReader::Pos() |
|
255 { |
|
256 return iPos; |
|
257 } |