Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <string.h> // memset(), memcpy()
22 : #include <rtl/ustring.hxx>
23 : #include <com/sun/star/lang/Locale.hpp>
24 : #include <unotools/charclass.hxx>
25 : #include "sot/stg.hxx"
26 : #include "stgelem.hxx"
27 : #include "stgcache.hxx"
28 : #include "stgstrms.hxx"
29 : #include "stgdir.hxx"
30 : #include "stgio.hxx"
31 :
32 : static const sal_uInt16 nMaxLegalStr = 31;
33 :
34 : static sal_uInt8 cStgSignature[ 8 ] = { 0xD0,0xCF,0x11,0xE0,0xA1,0xB1,0x1A,0xE1 };
35 :
36 : ////////////////////////////// struct ClsId /////////////////////////////
37 :
38 6911 : SvStream& operator >>( SvStream& r, ClsId& rId )
39 : {
40 6911 : r >> rId.n1
41 13822 : >> rId.n2
42 13822 : >> rId.n3
43 13822 : >> rId.n4
44 13822 : >> rId.n5
45 13822 : >> rId.n6
46 13822 : >> rId.n7
47 13822 : >> rId.n8
48 13822 : >> rId.n9
49 13822 : >> rId.n10
50 13822 : >> rId.n11;
51 6911 : return r;
52 : }
53 :
54 3453 : SvStream& operator <<( SvStream& r, const ClsId& rId )
55 : {
56 : return
57 3453 : r << (sal_Int32) rId.n1
58 6906 : << (sal_Int16) rId.n2
59 6906 : << (sal_Int16) rId.n3
60 6906 : << (sal_uInt8) rId.n4
61 6906 : << (sal_uInt8) rId.n5
62 6906 : << (sal_uInt8) rId.n6
63 6906 : << (sal_uInt8) rId.n7
64 6906 : << (sal_uInt8) rId.n8
65 6906 : << (sal_uInt8) rId.n9
66 6906 : << (sal_uInt8) rId.n10
67 6906 : << (sal_uInt8) rId.n11;
68 : }
69 :
70 : ///////////////////////////// class StgHeader ////////////////////////////
71 :
72 2736 : StgHeader::StgHeader()
73 : : nVersion( 0 )
74 : , nByteOrder( 0 )
75 : , nPageSize( 0 )
76 : , nDataPageSize( 0 )
77 : , bDirty( 0 )
78 : , nFATSize( 0 )
79 : , nTOCstrm( 0 )
80 : , nReserved( 0 )
81 : , nThreshold( 0 )
82 : , nDataFAT( 0 )
83 : , nDataFATSize( 0 )
84 : , nMasterChain( 0 )
85 2736 : , nMaster( 0 )
86 : {
87 2736 : memset( cSignature, 0, sizeof( cSignature ) );
88 2736 : memset( &aClsId, 0, sizeof( ClsId ) );
89 2736 : memset( cReserved, 0, sizeof( cReserved ) );
90 2736 : memset( nMasterFAT, 0, sizeof( nMasterFAT ) );
91 2736 : }
92 :
93 167 : void StgHeader::Init()
94 : {
95 167 : memcpy( cSignature, cStgSignature, 8 );
96 167 : memset( &aClsId, 0, sizeof( ClsId ) );
97 167 : nVersion = 0x0003003B;
98 167 : nByteOrder = 0xFFFE;
99 167 : nPageSize = 9; // 512 bytes
100 167 : nDataPageSize = 6; // 64 bytes
101 167 : bDirty = 0;
102 167 : memset( cReserved, 0, sizeof( cReserved ) );
103 167 : nFATSize = 0;
104 167 : nTOCstrm = 0;
105 167 : nReserved = 0;
106 167 : nThreshold = 4096;
107 167 : nDataFAT = 0;
108 167 : nDataFATSize = 0;
109 167 : nMasterChain = STG_EOF;
110 :
111 167 : SetTOCStart( STG_EOF );
112 167 : SetDataFATStart( STG_EOF );
113 18370 : for( short i = 0; i < cFATPagesInHeader; i++ )
114 18203 : SetFATPage( i, STG_FREE );
115 167 : }
116 :
117 334 : bool StgHeader::Load( StgIo& rIo )
118 : {
119 334 : bool bResult = false;
120 334 : if ( rIo.GetStrm() )
121 : {
122 334 : SvStream& r = *rIo.GetStrm();
123 334 : bResult = Load( r );
124 334 : bResult = ( bResult && rIo.Good() );
125 : }
126 :
127 334 : return bResult;
128 : }
129 :
130 2566 : bool StgHeader::Load( SvStream& r )
131 : {
132 2566 : r.Seek( 0L );
133 2566 : r.Read( cSignature, 8 );
134 2566 : r >> aClsId // 08 Class ID
135 5132 : >> nVersion // 1A version number
136 5132 : >> nByteOrder // 1C Unicode byte order indicator
137 5132 : >> nPageSize // 1E 1 << nPageSize = block size
138 5132 : >> nDataPageSize; // 20 1 << this size == data block size
139 2566 : r.SeekRel( 10 );
140 2566 : r >> nFATSize // 2C total number of FAT pages
141 5132 : >> nTOCstrm // 30 starting page for the TOC stream
142 5132 : >> nReserved // 34
143 5132 : >> nThreshold // 38 minimum file size for big data
144 5132 : >> nDataFAT // 3C page # of 1st data FAT block
145 5132 : >> nDataFATSize // 40 # of data FATpages
146 5132 : >> nMasterChain // 44 chain to the next master block
147 5132 : >> nMaster; // 48 # of additional master blocks
148 282260 : for( short i = 0; i < cFATPagesInHeader; i++ )
149 279694 : r >> nMasterFAT[ i ];
150 :
151 2566 : return (r.GetErrorCode() == ERRCODE_NONE) && Check();
152 : }
153 :
154 345 : bool StgHeader::Store( StgIo& rIo )
155 : {
156 345 : if( !bDirty )
157 0 : return true;
158 :
159 345 : SvStream& r = *rIo.GetStrm();
160 345 : r.Seek( 0L );
161 345 : r.Write( cSignature, 8 );
162 345 : r << aClsId // 08 Class ID
163 690 : << nVersion // 1A version number
164 690 : << nByteOrder // 1C Unicode byte order indicator
165 690 : << nPageSize // 1E 1 << nPageSize = block size
166 690 : << nDataPageSize // 20 1 << this size == data block size
167 345 : << (sal_Int32) 0 << (sal_Int32) 0 << (sal_Int16) 0
168 690 : << nFATSize // 2C total number of FAT pages
169 690 : << nTOCstrm // 30 starting page for the TOC stream
170 690 : << nReserved // 34
171 690 : << nThreshold // 38 minimum file size for big data
172 690 : << nDataFAT // 3C page # of 1st data FAT block
173 690 : << nDataFATSize // 40 # of data FAT pages
174 690 : << nMasterChain // 44 chain to the next master block
175 690 : << nMaster; // 48 # of additional master blocks
176 37950 : for( short i = 0; i < cFATPagesInHeader; i++ )
177 37605 : r << nMasterFAT[ i ];
178 345 : bDirty = !rIo.Good();
179 345 : return !bDirty;
180 : }
181 :
182 3264 : static bool lcl_wontoverflow(short shift)
183 : {
184 3264 : return shift >= 0 && shift < (short)sizeof(short) * 8 - 1;
185 : }
186 :
187 3291 : static bool isKnownSpecial(sal_Int32 nLocation)
188 : {
189 3289 : return (nLocation == STG_FREE ||
190 1542 : nLocation == STG_EOF ||
191 4833 : nLocation == STG_FAT ||
192 3291 : nLocation == STG_MASTER);
193 : }
194 :
195 : // Perform thorough checks also on unknown variables
196 3381 : bool StgHeader::Check()
197 : {
198 3381 : return memcmp( cSignature, cStgSignature, 8 ) == 0
199 1632 : && (short) ( nVersion >> 16 ) == 3
200 1632 : && nPageSize == 9
201 1632 : && lcl_wontoverflow(nPageSize)
202 1632 : && lcl_wontoverflow(nDataPageSize)
203 1632 : && nFATSize > 0
204 1632 : && nTOCstrm >= 0
205 1632 : && nThreshold > 0
206 1632 : && ( isKnownSpecial(nDataFAT) || ( nDataFAT >= 0 && nDataFATSize > 0 ) )
207 1632 : && ( isKnownSpecial(nMasterChain) || nMasterChain >=0 )
208 5013 : && nMaster >= 0;
209 : }
210 :
211 115900 : sal_Int32 StgHeader::GetFATPage( short n ) const
212 : {
213 115900 : if( n >= 0 && n < cFATPagesInHeader )
214 115900 : return nMasterFAT[ n ];
215 : else
216 0 : return STG_EOF;
217 : }
218 :
219 18382 : void StgHeader::SetFATPage( short n, sal_Int32 nb )
220 : {
221 18382 : if( n >= 0 && n < cFATPagesInHeader )
222 : {
223 18382 : if( nMasterFAT[ n ] != nb )
224 18382 : bDirty = sal_True, nMasterFAT[ n ] = nb;
225 : }
226 18382 : }
227 :
228 860 : void StgHeader::SetTOCStart( sal_Int32 n )
229 : {
230 860 : if( n != nTOCstrm ) bDirty = sal_True, nTOCstrm = n;
231 860 : }
232 :
233 512 : void StgHeader::SetDataFATStart( sal_Int32 n )
234 : {
235 512 : if( n != nDataFAT ) bDirty = sal_True, nDataFAT = n;
236 512 : }
237 :
238 345 : void StgHeader::SetDataFATSize( sal_Int32 n )
239 : {
240 345 : if( n != nDataFATSize ) bDirty = sal_True, nDataFATSize = n;
241 345 : }
242 :
243 179 : void StgHeader::SetFATSize( sal_Int32 n )
244 : {
245 179 : if( n != nFATSize ) bDirty = sal_True, nFATSize = n;
246 179 : }
247 :
248 0 : void StgHeader::SetFATChain( sal_Int32 n )
249 : {
250 0 : if( n != nMasterChain )
251 0 : bDirty = sal_True, nMasterChain = n;
252 0 : }
253 :
254 0 : void StgHeader::SetMasters( sal_Int32 n )
255 : {
256 0 : if( n != nMaster ) bDirty = sal_True, nMaster = n;
257 0 : }
258 :
259 : ///////////////////////////// class StgEntry /////////////////////////////
260 :
261 15344 : bool StgEntry::Init()
262 : {
263 15344 : memset( nName, 0, sizeof( nName ) );
264 15344 : nNameLen = 0;
265 15344 : cType = 0;
266 15344 : cFlags = 0;
267 15344 : nLeft = 0;
268 15344 : nRight = 0;
269 15344 : nChild = 0;
270 15344 : memset( &aClsId, 0, sizeof( aClsId ) );
271 15344 : nFlags = 0;
272 15344 : nMtime[0] = 0; nMtime[1] = 0;
273 15344 : nAtime[0] = 0; nAtime[1] = 0;
274 15344 : nPage1 = 0;
275 15344 : nSize = 0;
276 15344 : nUnknown = 0;
277 :
278 15344 : SetLeaf( STG_LEFT, STG_FREE );
279 15344 : SetLeaf( STG_RIGHT, STG_FREE );
280 15344 : SetLeaf( STG_CHILD, STG_FREE );
281 15344 : SetLeaf( STG_DATA, STG_EOF );
282 15344 : return sal_True;
283 : }
284 :
285 19196 : static OUString ToUpperUnicode( const OUString & rStr )
286 : {
287 : // I don't know the locale, so en_US is hopefully fine
288 19196 : static CharClass aCC( LanguageTag( com::sun::star::lang::Locale( "en", "US", "" )) );
289 19196 : return aCC.uppercase( rStr );
290 : }
291 :
292 14996 : bool StgEntry::SetName( const OUString& rName )
293 : {
294 : // I don't know the locale, so en_US is hopefully fine
295 14996 : aName = ToUpperUnicode( rName );
296 14996 : if(aName.getLength() > nMaxLegalStr)
297 : {
298 0 : aName = aName.copy(0, nMaxLegalStr);
299 : }
300 :
301 : int i;
302 135905 : for( i = 0; i < aName.getLength() && i < 32; i++ )
303 : {
304 120909 : nName[ i ] = rName[ i ];
305 : }
306 388955 : while( i < 32 )
307 : {
308 358963 : nName[ i++ ] = 0;
309 : }
310 14996 : nNameLen = ( aName.getLength() + 1 ) << 1;
311 14996 : return true;
312 : }
313 :
314 20506 : sal_Int32 StgEntry::GetLeaf( StgEntryRef eRef ) const
315 : {
316 20506 : sal_Int32 n = -1;
317 20506 : switch( eRef )
318 : {
319 4200 : case STG_LEFT: n = nLeft; break;
320 4200 : case STG_RIGHT: n = nRight; break;
321 6402 : case STG_CHILD: n = nChild; break;
322 5704 : case STG_DATA: n = nPage1; break;
323 : }
324 20506 : return n;
325 : }
326 :
327 72780 : void StgEntry::SetLeaf( StgEntryRef eRef, sal_Int32 nPage )
328 : {
329 72780 : switch( eRef )
330 : {
331 17662 : case STG_LEFT: nLeft = nPage; break;
332 17662 : case STG_RIGHT: nRight = nPage; break;
333 17662 : case STG_CHILD: nChild = nPage; break;
334 19794 : case STG_DATA: nPage1 = nPage; break;
335 : }
336 72780 : }
337 :
338 158 : void StgEntry::SetClassId( const ClsId& r )
339 : {
340 158 : memcpy( &aClsId, &r, sizeof( ClsId ) );
341 158 : }
342 :
343 5082 : void StgEntry::GetName( OUString& rName ) const
344 : {
345 5082 : sal_uInt16 n = nNameLen;
346 5082 : if( n )
347 5081 : n = ( n >> 1 ) - 1;
348 5082 : rName = OUString(nName, n);
349 5082 : }
350 :
351 : // Compare two entries. Do this case-insensitive.
352 :
353 106076 : short StgEntry::Compare( const StgEntry& r ) const
354 : {
355 : /*
356 : short nRes = r.nNameLen - nNameLen;
357 : if( !nRes ) return strcmp( r.aName, aName );
358 : else return nRes;
359 : */
360 106076 : sal_Int32 nRes = r.nNameLen - nNameLen;
361 106076 : if( !nRes )
362 36818 : nRes = r.aName.compareTo( aName );
363 106076 : return (short)nRes;
364 : //return aName.CompareTo( r.aName );
365 : }
366 :
367 : // These load/store operations are a bit more complicated,
368 : // since they have to copy their contents into a packed structure.
369 :
370 4203 : bool StgEntry::Load( const void* pFrom, sal_uInt32 nBufSize )
371 : {
372 4203 : if ( nBufSize < 128 )
373 0 : return false;
374 :
375 4203 : SvMemoryStream r( (sal_Char*) pFrom, nBufSize, STREAM_READ );
376 138699 : for( short i = 0; i < 32; i++ )
377 134496 : r >> nName[ i ]; // 00 name as WCHAR
378 4203 : r >> nNameLen // 40 size of name in bytes including 00H
379 8406 : >> cType // 42 entry type
380 8406 : >> cFlags // 43 0 or 1 (tree balance?)
381 8406 : >> nLeft // 44 left node entry
382 8406 : >> nRight // 48 right node entry
383 8406 : >> nChild // 4C 1st child entry if storage
384 8406 : >> aClsId // 50 class ID (optional)
385 8406 : >> nFlags // 60 state flags(?)
386 8406 : >> nMtime[ 0 ] // 64 modification time
387 8406 : >> nMtime[ 1 ] // 64 modification time
388 8406 : >> nAtime[ 0 ] // 6C creation and access time
389 8406 : >> nAtime[ 1 ] // 6C creation and access time
390 8406 : >> nPage1 // 74 starting block (either direct or translated)
391 8406 : >> nSize // 78 file size
392 8406 : >> nUnknown; // 7C unknown
393 :
394 4203 : sal_uInt16 n = nNameLen;
395 4203 : if( n )
396 4201 : n = ( n >> 1 ) - 1;
397 :
398 4203 : if (n > nMaxLegalStr)
399 2 : return false;
400 :
401 4201 : if ((cType != STG_STORAGE) && ((nSize < 0) || (nPage1 < 0 && !isKnownSpecial(nPage1))))
402 : {
403 : // the size makes no sense for the substorage
404 : // TODO/LATER: actually the size should be an unsigned value, but in this case it would mean a stream of more than 2Gb
405 1 : return false;
406 : }
407 :
408 4200 : aName = OUString(nName , n);
409 : // I don't know the locale, so en_US is hopefully fine
410 4200 : aName = ToUpperUnicode( aName );
411 4200 : if(aName.getLength() > nMaxLegalStr)
412 : {
413 0 : aName = aName.copy(0, nMaxLegalStr);
414 : }
415 :
416 4200 : return true;
417 : }
418 :
419 3096 : void StgEntry::Store( void* pTo )
420 : {
421 3096 : SvMemoryStream r( (sal_Char *)pTo, 128, STREAM_WRITE );
422 102168 : for( short i = 0; i < 32; i++ )
423 99072 : r << nName[ i ]; // 00 name as WCHAR
424 3096 : r << nNameLen // 40 size of name in bytes including 00H
425 6192 : << cType // 42 entry type
426 6192 : << cFlags // 43 0 or 1 (tree balance?)
427 6192 : << nLeft // 44 left node entry
428 6192 : << nRight // 48 right node entry
429 6192 : << nChild // 4C 1st child entry if storage;
430 6192 : << aClsId // 50 class ID (optional)
431 6192 : << nFlags // 60 state flags(?)
432 6192 : << nMtime[ 0 ] // 64 modification time
433 6192 : << nMtime[ 1 ] // 64 modification time
434 6192 : << nAtime[ 0 ] // 6C creation and access time
435 6192 : << nAtime[ 1 ] // 6C creation and access time
436 6192 : << nPage1 // 74 starting block (either direct or translated)
437 6192 : << nSize // 78 file size
438 6192 : << nUnknown; // 7C unknown
439 3096 : }
440 :
441 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|