merge the formfield patch from ooo-build
[ooovba.git] / lotuswordpro / source / filter / lwpobjstrm.cxx
blob2dc1e5734986568876fc5313929fb56261268667
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,
28 * MA 02111-1307 USA
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 /*************************************************************************
56 * Change History
57 Jan 2005 Created
58 ************************************************************************/
60 #include "lwpobjstrm.hxx"
61 #include "lwptools.hxx"
63 /**
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);
71 ReadStream();
73 /**
74 * @descr read object data from stream
76 void LwpObjectStream::ReadStream()
78 if(m_nBufSize == 0)
80 m_pContentBuf = NULL;
82 else
84 Read2Buffer();
87 /**
88 * @descr read object data from stream to buffer
90 void LwpObjectStream::Read2Buffer()
92 if( m_pContentBuf )
94 ReleaseBuffer();
97 m_nReadPos = 0;
99 if( m_bCompressed )
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;
115 else
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)
126 if(size<=100)
128 return(m_SmallBuffer);
130 else
132 return (new sal_uInt8[size]);
136 * @descr signal complete to release object buffer
138 void LwpObjectStream::ReadComplete()
140 ReleaseBuffer();
143 LwpObjectStream::~LwpObjectStream()
145 ReleaseBuffer();
148 * @descr release object buffer
150 void LwpObjectStream::ReleaseBuffer()
153 if(m_nBufSize>100)
155 if(m_pContentBuf)
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 )
169 assert(false);
170 len = m_nBufSize - m_nReadPos;
172 if( m_pContentBuf && len)
174 memcpy(buf, m_pContentBuf+m_nReadPos, len);
175 m_nReadPos += len;
177 return 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;
186 m_nReadPos +=pos;
189 * @descr Seek to pos in object buffer/buffer
191 BOOL LwpObjectStream::Seek( sal_uInt16 pos)
193 if(pos>=0 && pos<m_nBufSize)
195 m_nReadPos = pos;
196 return TRUE;
198 return FALSE;
202 * @descr Quick read sal_Bool
204 sal_Bool LwpObjectStream::QuickReadBool()
206 sal_uInt16 nValue;
207 QuickRead(&nValue, 2);
208 return (sal_Bool)(nValue != 0);
211 * @descr Quick read sal_uInt32
213 sal_uInt32 LwpObjectStream::QuickReaduInt32()
215 sal_uInt32 nValue;
216 QuickRead(&nValue, sizeof(nValue));
217 return nValue;
220 * @descr Quick read sal_uInt32
222 sal_uInt16 LwpObjectStream::QuickReaduInt16()
224 sal_uInt16 nValue;
225 QuickRead(&nValue, sizeof(nValue));
226 return nValue;
229 * @descr Quick read sal_Int32
231 sal_Int32 LwpObjectStream::QuickReadInt32()
233 sal_Int32 nValue;
234 QuickRead(&nValue, sizeof(nValue));
235 return nValue;
238 * @descr Quick read sal_Int16
240 sal_Int16 LwpObjectStream::QuickReadInt16()
242 sal_Int16 nValue;
243 QuickRead(&nValue, sizeof(nValue));
244 return nValue;
247 * @descr Quick read sal_Int8
249 sal_Int8 LwpObjectStream::QuickReadInt8()
251 sal_Int8 nValue;
252 QuickRead(&nValue, sizeof(nValue));
253 return nValue;
256 * @descr Quick read sal_uInt8
258 sal_uInt8 LwpObjectStream::QuickReaduInt8()
260 sal_uInt8 nValue;
261 QuickRead(&nValue, sizeof(nValue));
262 return nValue;
265 * @descr skip extra bytes
267 void LwpObjectStream::SkipExtra()
269 sal_uInt16 extra;
271 QuickRead(&extra, sizeof(extra));
272 while (extra != 0)
274 assert(false);
275 QuickRead(&extra, sizeof(extra));
279 * @descr check if extra bytes
281 sal_uInt16 LwpObjectStream::CheckExtra()
283 sal_uInt16 extra;
284 QuickRead(&extra, sizeof(extra));
285 return 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)
293 sal_uInt16 Cnt;
294 sal_uInt32 DstSize = 0;
296 while (Size)
298 switch (*pSrc & 0xC0)
300 case 0x00:
301 // 1 - 64 bytes of 0
302 // Code 00zzzzzz
303 // where zzzzzz is the count - 1 of compressed 0 bytes
305 Cnt = (*pSrc++ & 0x3F) + 1;
306 memset(pDst, 0, Cnt);
307 pDst += Cnt;
308 DstSize += Cnt;
309 Size--;
310 break;
312 case 0x40:
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);
320 pDst += Cnt;
321 DstSize += Cnt;
322 Cnt = (*pSrc++ & 0x07) + 1;
323 memcpy(pDst, pSrc, Cnt);
324 pDst += Cnt;
325 DstSize += Cnt;
326 pSrc += Cnt;
327 Size -= Cnt + 1;
328 break;
330 case 0x80:
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
336 *pDst++ = 0;
337 DstSize++;
338 // fall thru into next case!
340 case 0xC0:
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);
347 pDst += Cnt;
348 DstSize += Cnt;
349 pSrc += Cnt;
350 Size -= Cnt + 1;
351 break;
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();
367 OUString str;
368 rtl_TextEncoding rEncode = RTL_TEXTENCODING_MS_1252;
369 LwpTools::QuickReadUnicode(this, str, diskSize-sizeof(diskSize), rEncode);
370 return str;