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 : #include "biffhelper.hxx"
21 :
22 : #include <rtl/math.hxx>
23 : #include <rtl/tencinfo.h>
24 : #include "biffinputstream.hxx"
25 : #include "worksheethelper.hxx"
26 : #include "oox/helper/binaryoutputstream.hxx"
27 :
28 : namespace oox {
29 : namespace xls {
30 :
31 : // ============================================================================
32 :
33 : using ::rtl::OUString;
34 : using ::rtl::OUStringBuffer;
35 :
36 : // ============================================================================
37 :
38 : namespace {
39 :
40 : const sal_Int32 BIFF_RK_100FLAG = 0x00000001;
41 : const sal_Int32 BIFF_RK_INTFLAG = 0x00000002;
42 : const sal_Int32 BIFF_RK_VALUEMASK = 0xFFFFFFFC;
43 :
44 : const sal_Int32 BITMAPFILEHEADER_SIZE = 14;
45 : const sal_Int32 BITMAPCOREHEADER_SIZE = 12;
46 : const sal_Int32 BITMAPINFOHEADER_SIZE = 40;
47 :
48 : const sal_uInt16 BIFF_IMGDATA_WMF = 2;
49 : const sal_uInt16 BIFF_IMGDATA_DIB = 9;
50 : const sal_uInt16 BIFF_IMGDATA_NATIVE = 14;
51 :
52 : // ----------------------------------------------------------------------------
53 :
54 : union DecodedDouble
55 : {
56 : double mfValue;
57 : sal_math_Double maStruct;
58 :
59 0 : inline explicit DecodedDouble() {}
60 0 : inline explicit DecodedDouble( double fValue ) : mfValue( fValue ) {}
61 : };
62 :
63 : } // namespace
64 :
65 : // ============================================================================
66 :
67 : // conversion -----------------------------------------------------------------
68 :
69 0 : /*static*/ double BiffHelper::calcDoubleFromRk( sal_Int32 nRkValue )
70 : {
71 0 : DecodedDouble aDecDbl( 0.0 );
72 0 : if( getFlag( nRkValue, BIFF_RK_INTFLAG ) )
73 : {
74 0 : sal_Int32 nTemp = nRkValue >> 2;
75 0 : setFlag< sal_Int32 >( nTemp, 0xE0000000, nRkValue < 0 );
76 0 : aDecDbl.mfValue = nTemp;
77 : }
78 : else
79 : {
80 0 : aDecDbl.maStruct.w32_parts.msw = static_cast< sal_uInt32 >( nRkValue & BIFF_RK_VALUEMASK );
81 : }
82 :
83 0 : if( getFlag( nRkValue, BIFF_RK_100FLAG ) )
84 0 : aDecDbl.mfValue /= 100.0;
85 :
86 0 : return aDecDbl.mfValue;
87 : }
88 :
89 0 : /*static*/ double BiffHelper::calcDoubleFromError( sal_uInt8 nErrorCode )
90 : {
91 0 : sal_uInt16 nApiError = 0x7FFF;
92 0 : switch( nErrorCode )
93 : {
94 0 : case BIFF_ERR_NULL: nApiError = 521; break;
95 0 : case BIFF_ERR_DIV0: nApiError = 532; break;
96 0 : case BIFF_ERR_VALUE: nApiError = 519; break;
97 0 : case BIFF_ERR_REF: nApiError = 524; break;
98 0 : case BIFF_ERR_NAME: nApiError = 525; break;
99 0 : case BIFF_ERR_NUM: nApiError = 503; break;
100 0 : case BIFF_ERR_NA: nApiError = 0x7FFF; break;
101 : default: OSL_FAIL( "BiffHelper::calcDoubleFromError - unknown error code" );
102 : }
103 0 : DecodedDouble aDecDbl;
104 0 : ::rtl::math::setNan( &aDecDbl.mfValue );
105 0 : aDecDbl.maStruct.nan_parts.fraction_lo = nApiError;
106 0 : return aDecDbl.mfValue;
107 : }
108 :
109 : // BIFF12 import --------------------------------------------------------------
110 :
111 0 : /*static*/ OUString BiffHelper::readString( SequenceInputStream& rStrm, bool b32BitLen, bool bAllowNulChars )
112 : {
113 0 : OUString aString;
114 0 : if( !rStrm.isEof() )
115 : {
116 0 : sal_Int32 nCharCount = b32BitLen ? rStrm.readValue< sal_Int32 >() : rStrm.readValue< sal_Int16 >();
117 : // string length -1 is often used to indicate a missing string
118 : OSL_ENSURE( !rStrm.isEof() && (nCharCount >= -1), "BiffHelper::readString - invalid string length" );
119 0 : if( !rStrm.isEof() && (nCharCount > 0) )
120 : {
121 : // SequenceInputStream always supports getRemaining()
122 0 : nCharCount = ::std::min( nCharCount, static_cast< sal_Int32 >( rStrm.getRemaining() / 2 ) );
123 0 : aString = rStrm.readUnicodeArray( nCharCount, bAllowNulChars );
124 : }
125 : }
126 0 : return aString;
127 : }
128 :
129 : // BIFF2-BIFF8 import ---------------------------------------------------------
130 :
131 0 : /*static*/ bool BiffHelper::isBofRecord( BiffInputStream& rStrm )
132 : {
133 : return
134 0 : (rStrm.getRecId() == BIFF2_ID_BOF) ||
135 0 : (rStrm.getRecId() == BIFF3_ID_BOF) ||
136 0 : (rStrm.getRecId() == BIFF4_ID_BOF) ||
137 0 : (rStrm.getRecId() == BIFF5_ID_BOF);
138 : }
139 :
140 0 : /*static*/ bool BiffHelper::skipRecordBlock( BiffInputStream& rStrm, sal_uInt16 nEndRecId )
141 : {
142 0 : sal_uInt16 nStartRecId = rStrm.getRecId();
143 0 : while( rStrm.startNextRecord() && (rStrm.getRecId() != nEndRecId) )
144 0 : if( rStrm.getRecId() == nStartRecId )
145 0 : skipRecordBlock( rStrm, nEndRecId );
146 0 : return !rStrm.isEof() && (rStrm.getRecId() == nEndRecId);
147 : }
148 :
149 : } // namespace xls
150 9 : } // namespace oox
151 :
152 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|