1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /*************************************************************************
59 ************************************************************************/
61 #include "lwpobjstrm.hxx"
62 #include "lwptools.hxx"
63 #include <boost/scoped_array.hpp>
66 * @descr ctor() from LwpSvStream
68 LwpObjectStream::LwpObjectStream(LwpSvStream
*pStrm
, bool isCompressed
, sal_uInt16 size
)
69 :m_pContentBuf(NULL
), m_nBufSize(size
), m_nReadPos(0),
70 m_pStrm(pStrm
), m_bCompressed(isCompressed
)
72 assert(size
<IO_BUFFERSIZE
);
76 * @descr read object data from stream
78 void LwpObjectStream::ReadStream()
90 * @descr read object data from stream to buffer
92 void LwpObjectStream::Read2Buffer()
103 boost::scoped_array
<sal_uInt8
> xCompressBuf(new sal_uInt8
[m_nBufSize
]);
105 sal_uInt8
* pCompressBuffer
= xCompressBuf
.get();
106 memset(pCompressBuffer
, 0, m_nBufSize
);
107 m_pStrm
->Read(pCompressBuffer
, m_nBufSize
);
109 sal_uInt8 pTempDst
[IO_BUFFERSIZE
];
110 m_nBufSize
= DecompressBuffer(pTempDst
, pCompressBuffer
, m_nBufSize
);
111 assert( m_nBufSize
< IO_BUFFERSIZE
);
113 m_pContentBuf
= AllocBuffer(m_nBufSize
);
114 memcpy(m_pContentBuf
, pTempDst
, m_nBufSize
);
115 //delete [] pTempDst;
120 m_pContentBuf
= AllocBuffer(m_nBufSize
);
121 m_pStrm
->Read(m_pContentBuf
, m_nBufSize
);
125 * @descr alloc size of buffer
127 sal_uInt8
* LwpObjectStream::AllocBuffer(sal_uInt16 size
)
131 return m_SmallBuffer
;
135 return new sal_uInt8
[size
];
139 * @descr signal complete to release object buffer
141 void LwpObjectStream::ReadComplete()
146 LwpObjectStream::~LwpObjectStream()
151 * @descr release object buffer
153 void LwpObjectStream::ReleaseBuffer()
160 delete [] m_pContentBuf
;
161 m_pContentBuf
= NULL
;
166 * @descr read len bytes from object stream to buffer
168 sal_uInt16
LwpObjectStream::QuickRead(void* buf
, sal_uInt16 len
)
171 if( len
> m_nBufSize
- m_nReadPos
)
174 len
= m_nBufSize
- m_nReadPos
;
176 if( m_pContentBuf
&& len
)
178 memcpy(buf
, m_pContentBuf
+m_nReadPos
, len
);
184 * @descr SeekRel pos in object stream/buffer
186 void LwpObjectStream::SeekRel(sal_uInt16 pos
)
188 if( pos
> m_nBufSize
- m_nReadPos
)
189 pos
= m_nBufSize
- m_nReadPos
;
193 * @descr Seek to pos in object buffer/buffer
195 bool LwpObjectStream::Seek( sal_uInt16 pos
)
197 if (pos
< m_nBufSize
)
206 * @descr Quick read sal_Bool
208 bool LwpObjectStream::QuickReadBool(bool *pFailure
)
211 sal_uInt16 nRead
= QuickRead(aValue
, sizeof(aValue
));
213 *pFailure
= (nRead
!= sizeof(aValue
));
214 return static_cast<bool>(SVBT16ToShort(aValue
));
217 * @descr Quick read sal_uInt32
219 sal_uInt32
LwpObjectStream::QuickReaduInt32(bool *pFailure
)
222 sal_uInt16 nRead
= QuickRead(aValue
, sizeof(aValue
));
224 *pFailure
= (nRead
!= sizeof(aValue
));
225 return SVBT32ToUInt32(aValue
);
228 * @descr Quick read sal_uInt32
230 sal_uInt16
LwpObjectStream::QuickReaduInt16(bool *pFailure
)
233 sal_uInt16 nRead
= QuickRead(aValue
, sizeof(aValue
));
235 *pFailure
= (nRead
!= sizeof(aValue
));
236 return SVBT16ToShort(aValue
);
239 * @descr Quick read sal_Int32
241 sal_Int32
LwpObjectStream::QuickReadInt32(bool *pFailure
)
244 sal_uInt16 nRead
= QuickRead(aValue
, sizeof(aValue
));
246 *pFailure
= (nRead
!= sizeof(aValue
));
247 return static_cast<sal_Int32
>(SVBT32ToUInt32(aValue
));
250 * @descr Quick read sal_Int16
252 sal_Int16
LwpObjectStream::QuickReadInt16(bool *pFailure
)
255 sal_uInt16 nRead
= QuickRead(aValue
, sizeof(aValue
));
257 *pFailure
= (nRead
!= sizeof(aValue
));
259 return static_cast<sal_Int16
>(SVBT16ToShort(aValue
));
262 * @descr Quick read sal_uInt8
264 sal_uInt8
LwpObjectStream::QuickReaduInt8(bool *pFailure
)
266 sal_uInt8 aValue
= 0;
267 sal_uInt16 nRead
= QuickRead(&aValue
, sizeof(aValue
));
269 *pFailure
= (nRead
!= sizeof(aValue
));
273 * @descr Quick read double
275 double LwpObjectStream::QuickReadDouble(bool *pFailure
)
282 memset(s
.c
, 0, sizeof(s
.c
));
283 sal_uInt16 nRead
= QuickRead(s
.c
, sizeof(s
.c
));
285 *pFailure
= (nRead
!= sizeof(s
.c
));
286 #if defined(OSL_BIGENDIAN)
287 for (size_t i
= 0; i
< 4; ++i
)
288 std::swap(s
.c
[i
], s
.c
[7-i
]);
293 * @descr skip extra bytes
295 void LwpObjectStream::SkipExtra()
297 sal_uInt16 extra
= QuickReaduInt16();
299 extra
= QuickReaduInt16();
302 * @descr check if extra bytes
304 sal_uInt16
LwpObjectStream::CheckExtra()
306 return QuickReaduInt16();
309 * @descr decompress data buffer from pSrc to pDst
310 * Refer to the CAmiPro40File::DecompressObject(~) in LWP
312 sal_uInt16
LwpObjectStream::DecompressBuffer(sal_uInt8
* pDst
, sal_uInt8
* pSrc
, sal_uInt16 Size
)
315 sal_uInt32 DstSize
= 0;
319 switch (*pSrc
& 0xC0)
324 // where zzzzzz is the count - 1 of compressed 0 bytes
326 Cnt
= (*pSrc
++ & 0x3F) + 1;
327 if (DstSize
+Cnt
>= IO_BUFFERSIZE
)
328 throw BadDecompress();
329 memset(pDst
, 0, Cnt
);
336 // 1 - 8 zeros followed by 1 - 8 non-zero
337 // Code 01zzznnn binary
338 // where zzz is the count - 1 of compressed zero bytes
339 // and nnn is the count - 1 of following non-zero bytes
341 Cnt
= ((*pSrc
& 0x38) >> 3) + 1;
342 if (DstSize
+Cnt
>= IO_BUFFERSIZE
)
343 throw BadDecompress();
344 memset(pDst
, 0, Cnt
);
347 Cnt
= (*pSrc
++ & 0x07) + 1;
349 throw BadDecompress();
351 if (DstSize
+Cnt
>= IO_BUFFERSIZE
)
352 throw BadDecompress();
353 memcpy(pDst
, pSrc
, Cnt
);
360 // 1 0 followed by 1 - 64 bytes of non-zero
361 // Code 0x80 (or 0x40 if 8 or less non-zero)
362 // Code 10nnnnnn binary
363 // where nnnnnn is the count - 1 of following non-zero bytes
367 // fall through into next case!
370 // 1 - 64 bytes of non-zero
371 // Code = 11nnnnnn binary
372 // nnnnnn is the count less 1 of following non-zero bytes
374 Cnt
= (*pSrc
++ & 0x3F) + 1;
376 throw BadDecompress();
378 if (DstSize
+Cnt
>= IO_BUFFERSIZE
)
379 throw BadDecompress();
380 memcpy(pDst
, pSrc
, Cnt
);
386 assert(DstSize
< IO_BUFFERSIZE
);
387 if (DstSize
>= IO_BUFFERSIZE
)
388 throw BadDecompress();
391 return static_cast<sal_uInt16
>(DstSize
);
394 * @descr quick read string with 1252
396 OUString
LwpObjectStream::QuickReadStringPtr()
400 diskSize
= QuickReaduInt16();
401 QuickReaduInt16(); //len
404 rtl_TextEncoding rEncode
= RTL_TEXTENCODING_MS_1252
;
405 LwpTools::QuickReadUnicode(this, str
, diskSize
-sizeof(diskSize
), rEncode
);
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */