|
1 /* |
|
2 * Copyright (c) 2007-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 "basefile.h" |
|
29 #include "exception.h" |
|
30 #include "utils.h" |
|
31 #include <stdio.h> |
|
32 #include <assert.h> |
|
33 #include <malloc.h> |
|
34 // malloc suite used because realloc required |
|
35 |
|
36 TSISStream::size_type TSISStream::iChunk = 2048; |
|
37 // A magic value which should be related to the probable page size |
|
38 |
|
39 |
|
40 |
|
41 TSISStream::TSISStream () : |
|
42 iBuffer (NULL), |
|
43 iSize (0), |
|
44 iOffset (0), |
|
45 iDataLength (0) |
|
46 { |
|
47 } |
|
48 |
|
49 |
|
50 TSISStream::~TSISStream () |
|
51 { |
|
52 if (iBuffer != NULL) |
|
53 { |
|
54 free (iBuffer); |
|
55 } |
|
56 } |
|
57 |
|
58 |
|
59 |
|
60 void TSISStream::reserve (const size_type aSize) |
|
61 { |
|
62 if (iOffset + aSize > iSize) |
|
63 { |
|
64 size_type bloat = ((aSize / iChunk) + 1) * iChunk; |
|
65 iBuffer = reinterpret_cast <TUint8*> (realloc (iBuffer, iSize + bloat)); |
|
66 CSISException::ThrowIf (iBuffer == NULL, CSISException::EMemory, "cannot increase stream buffer size"); |
|
67 iSize += bloat; |
|
68 } |
|
69 assert (! IsBadWritePtr (iBuffer, iSize)); |
|
70 } |
|
71 |
|
72 |
|
73 void TSISStream::read (TUint8* aBuffer, const size_type aSize) |
|
74 { |
|
75 CSISException::ThrowIf (iOffset + aSize > iSize, |
|
76 CSISException::EMemory, |
|
77 "attempt to read beyond the end of buffer"); |
|
78 CSISException::ThrowIf (iOffset + aSize > iDataLength, |
|
79 CSISException::EMemory, |
|
80 "attempt to read beyond the max write pointer"); |
|
81 memcpy (aBuffer, &iBuffer [iOffset], aSize); |
|
82 iOffset += aSize; |
|
83 } |
|
84 |
|
85 |
|
86 void TSISStream::write (const TUint8* aBuffer, const size_type aSize) |
|
87 { |
|
88 reserve (aSize); |
|
89 memcpy (&iBuffer [iOffset], aBuffer, aSize); |
|
90 iOffset += aSize; |
|
91 if (iOffset > iDataLength) |
|
92 { |
|
93 iDataLength = iOffset; |
|
94 } |
|
95 } |
|
96 |
|
97 void TSISStream::seek (const TSISStream::pos_type aPos, const std::ios_base::seekdir aRel) |
|
98 { |
|
99 TSISStream::pos_type to; |
|
100 switch (aRel) |
|
101 { |
|
102 case std::ios_base::beg : |
|
103 to = aPos; |
|
104 break; |
|
105 case std::ios_base::cur : |
|
106 to = iOffset + aPos; |
|
107 break; |
|
108 case std::ios_base::end : |
|
109 to = iDataLength + aPos; |
|
110 break; |
|
111 default : |
|
112 assert (false); |
|
113 return; |
|
114 } |
|
115 if (to >= iSize) |
|
116 { |
|
117 reserve (to - iSize); |
|
118 } |
|
119 iOffset = to; |
|
120 } |
|
121 |
|
122 |
|
123 bool TSISStream::import (HANDLE aFile, TUint64* filesize) |
|
124 { |
|
125 try { |
|
126 DWORD dwHigh = 0; |
|
127 DWORD dwLow = ::GetFileSize(aFile,&dwHigh); |
|
128 TInt64 size = (dwHigh << 32) | dwLow; |
|
129 reserve (size); |
|
130 if (filesize) |
|
131 { |
|
132 *filesize = size; |
|
133 } |
|
134 size_t obtained = 0; |
|
135 ::ReadFile(aFile,iBuffer,size,(DWORD*)&obtained,0); |
|
136 iDataLength = obtained; |
|
137 return (size == obtained); |
|
138 } |
|
139 catch (...) |
|
140 { |
|
141 return false; |
|
142 } |
|
143 } |
|
144 |
|
145 |
|
146 bool TSISStream::exportfile (HANDLE aFile) |
|
147 { |
|
148 try |
|
149 { |
|
150 size_t written = 0; |
|
151 ::WriteFile(aFile,iBuffer,length(),(DWORD*)&written,0); |
|
152 return (written == length ()); |
|
153 } |
|
154 catch (...) |
|
155 { |
|
156 return false; |
|
157 } |
|
158 } |
|
159 |
|
160 void TSISStream::reset () |
|
161 { |
|
162 if (iBuffer != NULL) |
|
163 { |
|
164 free (iBuffer); |
|
165 iBuffer = NULL; |
|
166 } |
|
167 iSize = 0; |
|
168 iOffset = 0; |
|
169 iDataLength = 0; |
|
170 } |
|
171 |