1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
55 /*************************************************************************
58 ************************************************************************/
60 #include "lwpobjstrm.hxx"
61 #include "lwptools.hxx"
64 * @descr ctor() from LwpSvStream
66 LwpObjectStream::LwpObjectStream(LwpSvStream
*pStrm
, BOOL isCompressed
, sal_uInt16 size
)
67 :m_pContentBuf(NULL
), m_nBufSize(size
), m_nReadPos(0),
68 m_pStrm(pStrm
), m_bCompressed(isCompressed
)
70 assert(size
<IO_BUFFERSIZE
);
74 * @descr read object data from stream
76 void LwpObjectStream::ReadStream()
88 * @descr read object data from stream to buffer
90 void LwpObjectStream::Read2Buffer()
101 sal_uInt8
* pCompressBuffer
= new sal_uInt8
[m_nBufSize
];
102 m_pStrm
->Read(pCompressBuffer
, m_nBufSize
);
104 sal_uInt8 pTempDst
[IO_BUFFERSIZE
];
105 m_nBufSize
= DecompressBuffer(pTempDst
, pCompressBuffer
, m_nBufSize
);
106 assert( m_nBufSize
< IO_BUFFERSIZE
);
107 delete [] pCompressBuffer
;
108 pCompressBuffer
= NULL
;
110 m_pContentBuf
= AllocBuffer(m_nBufSize
);
111 memcpy(m_pContentBuf
, pTempDst
, m_nBufSize
);
112 //delete [] pTempDst;
117 m_pContentBuf
= AllocBuffer(m_nBufSize
);
118 m_pStrm
->Read(m_pContentBuf
, m_nBufSize
);
122 * @descr alloc size of buffer
124 sal_uInt8
* LwpObjectStream::AllocBuffer(sal_uInt16 size
)
128 return(m_SmallBuffer
);
132 return (new sal_uInt8
[size
]);
136 * @descr signal complete to release object buffer
138 void LwpObjectStream::ReadComplete()
143 LwpObjectStream::~LwpObjectStream()
148 * @descr release object buffer
150 void LwpObjectStream::ReleaseBuffer()
157 delete [] m_pContentBuf
;
158 m_pContentBuf
= NULL
;
163 * @descr read len bytes from object stream to buffer
165 sal_uInt16
LwpObjectStream::QuickRead(void* buf
, sal_uInt16 len
)
167 if( len
> m_nBufSize
- m_nReadPos
)
170 len
= m_nBufSize
- m_nReadPos
;
172 if( m_pContentBuf
&& len
)
174 memcpy(buf
, m_pContentBuf
+m_nReadPos
, len
);
180 * @descr SeekRel pos in object stream/buffer
182 void LwpObjectStream::SeekRel(sal_uInt16 pos
)
184 if( pos
> m_nBufSize
- m_nReadPos
)
185 pos
= m_nBufSize
- m_nReadPos
;
189 * @descr Seek to pos in object buffer/buffer
191 BOOL
LwpObjectStream::Seek( sal_uInt16 pos
)
193 if(pos
>=0 && pos
<m_nBufSize
)
202 * @descr Quick read sal_Bool
204 sal_Bool
LwpObjectStream::QuickReadBool()
207 QuickRead(&nValue
, 2);
208 return (sal_Bool
)(nValue
!= 0);
211 * @descr Quick read sal_uInt32
213 sal_uInt32
LwpObjectStream::QuickReaduInt32()
216 QuickRead(&nValue
, sizeof(nValue
));
220 * @descr Quick read sal_uInt32
222 sal_uInt16
LwpObjectStream::QuickReaduInt16()
225 QuickRead(&nValue
, sizeof(nValue
));
229 * @descr Quick read sal_Int32
231 sal_Int32
LwpObjectStream::QuickReadInt32()
234 QuickRead(&nValue
, sizeof(nValue
));
238 * @descr Quick read sal_Int16
240 sal_Int16
LwpObjectStream::QuickReadInt16()
243 QuickRead(&nValue
, sizeof(nValue
));
247 * @descr Quick read sal_Int8
249 sal_Int8
LwpObjectStream::QuickReadInt8()
252 QuickRead(&nValue
, sizeof(nValue
));
256 * @descr Quick read sal_uInt8
258 sal_uInt8
LwpObjectStream::QuickReaduInt8()
261 QuickRead(&nValue
, sizeof(nValue
));
265 * @descr skip extra bytes
267 void LwpObjectStream::SkipExtra()
271 QuickRead(&extra
, sizeof(extra
));
275 QuickRead(&extra
, sizeof(extra
));
279 * @descr check if extra bytes
281 sal_uInt16
LwpObjectStream::CheckExtra()
284 QuickRead(&extra
, sizeof(extra
));
288 * @descr decompress data buffer from pSrc to pDst
289 * Refer to the CAmiPro40File::DecompressObject(~) in LWP
291 sal_uInt16
LwpObjectStream::DecompressBuffer(sal_uInt8
* pDst
, sal_uInt8
* pSrc
, sal_uInt16 Size
)
294 sal_uInt32 DstSize
= 0;
298 switch (*pSrc
& 0xC0)
303 // where zzzzzz is the count - 1 of compressed 0 bytes
305 Cnt
= (*pSrc
++ & 0x3F) + 1;
306 memset(pDst
, 0, Cnt
);
313 // 1 - 8 zeros followed by 1 - 8 non-zero
314 // Code 01zzznnn binary
315 // where zzz is the count - 1 of compressed zero bytes
316 // and nnn is the count - 1 of following non-zero bytes
318 Cnt
= ((*pSrc
& 0x38) >> 3) + 1;
319 memset(pDst
, 0, Cnt
);
322 Cnt
= (*pSrc
++ & 0x07) + 1;
323 memcpy(pDst
, pSrc
, Cnt
);
331 // 1 0 followed by 1 - 64 bytes of non-zero
332 // Code 0x80 (or 0x40 if 8 or less non-zero)
333 // Code 10nnnnnn binary
334 // where nnnnnn is the count - 1 of following non-zero bytes
338 // fall thru into next case!
341 // 1 - 64 bytes of non-zero
342 // Code = 11nnnnnn binary
343 // nnnnnn is the count less 1 of following non-zero bytes
345 Cnt
= (*pSrc
++ & 0x3F) + 1;
346 memcpy(pDst
, pSrc
, Cnt
);
353 assert(DstSize
< IO_BUFFERSIZE
);
355 return(static_cast<sal_uInt16
>(DstSize
));
358 * @descr quick read string with 1252
360 OUString
LwpObjectStream::QuickReadStringPtr(void)
362 sal_uInt16 len
, diskSize
;
364 diskSize
= QuickReaduInt16();
365 len
= QuickReaduInt16();
368 rtl_TextEncoding rEncode
= RTL_TEXTENCODING_MS_1252
;
369 LwpTools::QuickReadUnicode(this, str
, diskSize
-sizeof(diskSize
), rEncode
);