1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <tools/stream.hxx>
24 #include <rtl/ref.hxx>
32 // The FAT class performs FAT operations on an underlying storage stream.
33 // This stream is either the physical FAT stream (bPhys == true ) or a normal
34 // storage stream, which then holds the FAT for small data allocations.
38 StgStrm
& rStrm
; // underlying stream
39 sal_Int32 nMaxPage
; // highest page allocated so far
40 short nPageSize
; // physical page size
41 short nEntries
; // FAT entries per page
42 short nOffset
; // current offset within page
43 sal_Int32 nLimit
; // search limit recommendation
44 bool bPhys
; // true: physical FAT
45 rtl::Reference
< StgPage
> GetPhysPage( sal_Int32 nPage
);
46 bool MakeChain( sal_Int32 nStart
, sal_Int32 nPages
);
47 bool InitNew( sal_Int32 nPage1
);
49 StgFAT( StgStrm
& rStrm
, bool bMark
);
50 sal_Int32
FindBlock( sal_Int32
& nPages
);
51 sal_Int32
GetNextPage( sal_Int32 nPg
);
52 sal_Int32
AllocPages( sal_Int32 nStart
, sal_Int32 nPages
);
53 bool FreePages( sal_Int32 nStart
, bool bAll
);
54 sal_Int32
GetMaxPage() { return nMaxPage
; }
55 void SetLimit( sal_Int32 n
) { nLimit
= n
; }
58 // The base stream class provides basic functionality for seeking
59 // and accessing the data on a physical basis. It uses the built-in
60 // FAT class for the page allocations.
62 class StgStrm
{ // base class for all streams
64 StgIo
& rIo
; // I/O system
65 StgFAT
* pFat
; // FAT stream for allocations
66 StgDirEntry
* pEntry
; // dir entry (for ownership)
67 sal_Int32 nStart
; // 1st data page
68 sal_Int32 nSize
; // stream size in bytes
69 sal_Int32 nPos
; // current byte position
70 sal_Int32 nPage
; // current logical page
71 short nOffset
; // offset into current page
72 short nPageSize
; // logical page size
73 std::vector
<sal_Int32
> m_aPagesCache
;
74 void scanBuildPageChainCache(sal_Int32
*pOptionalCalcSize
= NULL
);
75 bool Copy( sal_Int32 nFrom
, sal_Int32 nBytes
);
79 StgIo
& GetIo() { return rIo
; }
80 sal_Int32
GetPos() const { return nPos
; }
81 sal_Int32
GetStart() const { return nStart
; }
82 sal_Int32
GetSize() const { return nSize
; }
83 sal_Int32
GetPage() const { return nPage
; }
84 short GetPageSize() const { return nPageSize
; }
85 sal_Int32
GetPages() const;
86 short GetOffset() const { return nOffset
;}
87 void SetEntry( StgDirEntry
& );
88 virtual bool SetSize( sal_Int32
);
89 virtual bool Pos2Page( sal_Int32 nBytePos
);
90 virtual sal_Int32
Read( void*, sal_Int32
) { return 0; }
91 virtual sal_Int32
Write( const void*, sal_Int32
) { return 0; }
92 virtual rtl::Reference
< StgPage
> GetPhysPage( sal_Int32 nBytePos
, bool bForce
= false );
93 virtual bool IsSmallStrm() const { return false; }
96 // The FAT stream class provides physical access to the master FAT.
97 // Since this access is implemented as a StgStrm, we can use the
100 class StgFATStrm
: public StgStrm
{ // the master FAT stream
101 virtual bool Pos2Page( sal_Int32 nBytePos
);
102 bool SetPage( short, sal_Int32
);
104 StgFATStrm( StgIo
& );
105 virtual ~StgFATStrm() {}
106 using StgStrm::GetPage
;
107 sal_Int32
GetPage( short, bool, sal_uInt16
*pnMasterAlloc
= 0);
108 virtual bool SetSize( sal_Int32
);
109 virtual rtl::Reference
< StgPage
> GetPhysPage( sal_Int32 nBytePos
, bool bForce
= false );
112 // The stream has a size increment which normally is 1, but which can be
113 // set to any value is you want the size to be incremented by certain values.
115 class StgDataStrm
: public StgStrm
// a physical data stream
117 short nIncr
; // size adjust increment
118 void Init( sal_Int32 nBgn
, sal_Int32 nLen
);
120 StgDataStrm( StgIo
&, sal_Int32 nBgn
, sal_Int32 nLen
=-1 );
121 StgDataStrm( StgIo
&, StgDirEntry
& );
122 void* GetPtr( sal_Int32 nPos
, bool bForce
, bool bDirty
);
123 void SetIncrement( short n
) { nIncr
= n
; }
124 virtual bool SetSize( sal_Int32
);
125 virtual sal_Int32
Read( void*, sal_Int32
);
126 virtual sal_Int32
Write( const void*, sal_Int32
);
129 // The small stream class provides access to streams with a size < 4096 bytes.
130 // This stream is a StgStream containing small pages. The FAT for this stream
131 // is also a StgStream. The start of the FAT is in the header at DataRootPage,
132 // the stream itself is pointed to by the root entry (it holds start & size).
134 class StgSmallStrm
: public StgStrm
// a logical data stream
136 StgStrm
* pData
; // the data stream
137 void Init( sal_Int32 nBgn
, sal_Int32 nLen
);
139 StgSmallStrm( StgIo
&, sal_Int32 nBgn
, sal_Int32 nLen
);
140 StgSmallStrm( StgIo
&, StgDirEntry
& );
141 virtual sal_Int32
Read( void*, sal_Int32
);
142 virtual sal_Int32
Write( const void*, sal_Int32
);
143 virtual bool IsSmallStrm() const { return true; }
146 class StgTmpStrm
: public SvMemoryStream
150 using SvMemoryStream::GetData
;
151 virtual sal_uLong
GetData( void* pData
, sal_uLong nSize
);
152 virtual sal_uLong
PutData( const void* pData
, sal_uLong nSize
);
153 virtual sal_uLong
SeekPos( sal_uLong nPos
);
154 virtual void FlushData();
157 StgTmpStrm( sal_uLong
=16 );
159 bool Copy( StgTmpStrm
& );
160 void SetSize( sal_uLong
);
161 sal_uLong
GetSize() const;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */