Branch data 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 : :
21 : : #include "buffer.hxx"
22 : : #include "sbcomp.hxx"
23 : :
24 : : const static sal_uInt32 UP_LIMIT=0xFFFFFF00L;
25 : :
26 : : // The SbiBuffer will be expanded in increments of at least 16 Bytes.
27 : : // This is necessary, because many classes emanate from a buffer length
28 : : // of x*16 Bytes.
29 : :
30 : 65 : SbiBuffer::SbiBuffer( SbiParser* p, short n )
31 : : {
32 : 65 : pParser = p;
33 : 65 : n = ( (n + 15 ) / 16 ) * 16;
34 [ - + ]: 65 : if( !n ) n = 16;
35 : 65 : pBuf = NULL;
36 : 65 : pCur = NULL;
37 : 65 : nInc = n;
38 : : nSize =
39 : 65 : nOff = 0;
40 : 65 : }
41 : :
42 : 65 : SbiBuffer::~SbiBuffer()
43 : : {
44 [ - + ]: 65 : delete[] pBuf;
45 : 65 : }
46 : :
47 : : // Reach out the buffer
48 : : // This lead to the deletion of the buffer!
49 : :
50 : 65 : char* SbiBuffer::GetBuffer()
51 : : {
52 : 65 : char* p = pBuf;
53 : 65 : pBuf = NULL;
54 : 65 : pCur = NULL;
55 : 65 : return p;
56 : : }
57 : :
58 : : // Test, if the buffer can contain n Bytes.
59 : : // In case of doubt it will be enlarged
60 : :
61 : 83968 : bool SbiBuffer::Check( sal_uInt16 n )
62 : : {
63 [ - + ]: 83968 : if( !n ) return true;
64 [ + + ]: 83968 : if( ( static_cast<sal_uInt32>( nOff )+ n ) > static_cast<sal_uInt32>( nSize ) )
65 : : {
66 [ - + ]: 157 : if( nInc == 0 )
67 : 0 : return false;
68 : 157 : sal_uInt16 nn = 0;
69 [ + + ]: 314 : while( nn < n ) nn = nn + nInc;
70 : : char* p;
71 [ - + ]: 157 : if( ( static_cast<sal_uInt32>( nSize ) + nn ) > UP_LIMIT ) p = NULL;
72 : 157 : else p = new char [nSize + nn];
73 [ - + ]: 157 : if( !p )
74 : : {
75 : 0 : pParser->Error( SbERR_PROG_TOO_LARGE );
76 : 0 : nInc = 0;
77 [ # # ]: 0 : delete[] pBuf; pBuf = NULL;
78 : 0 : return false;
79 : : }
80 : : else
81 : : {
82 [ + + ]: 157 : if( nSize ) memcpy( p, pBuf, nSize );
83 [ + + ]: 157 : delete[] pBuf;
84 : 157 : pBuf = p;
85 : 157 : pCur = pBuf + nOff;
86 : 157 : nSize = nSize + nn;
87 : : }
88 : : }
89 : 83968 : return true;
90 : : }
91 : :
92 : : // Patch of a Location
93 : :
94 : 0 : void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val )
95 : : {
96 [ # # ]: 0 : if( ( off + sizeof( sal_uInt32 ) ) < nOff )
97 : : {
98 : 0 : sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF );
99 : 0 : sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 );
100 : 0 : sal_uInt8* p = (sal_uInt8*) pBuf + off;
101 : 0 : *p++ = (char) ( val1 & 0xFF );
102 : 0 : *p++ = (char) ( val1 >> 8 );
103 : 0 : *p++ = (char) ( val2 & 0xFF );
104 : 0 : *p = (char) ( val2 >> 8 );
105 : : }
106 : 0 : }
107 : :
108 : : // Forward References upon label und procedures
109 : : // establish a linkage. The beginning of the linkage is at the passed parameter,
110 : : // the end of the linkage is 0.
111 : :
112 : 1154 : void SbiBuffer::Chain( sal_uInt32 off )
113 : : {
114 [ + + ][ + - ]: 1154 : if( off && pBuf )
115 : : {
116 : : sal_uInt8 *ip;
117 : 689 : sal_uInt32 i = off;
118 : 689 : sal_uInt32 val1 = (nOff & 0xFFFF);
119 : 689 : sal_uInt32 val2 = (nOff >> 16);
120 [ + + ]: 851 : do
121 : : {
122 : 851 : ip = (sal_uInt8*) pBuf + i;
123 : 851 : sal_uInt8* pTmp = ip;
124 : 851 : i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
125 : :
126 [ - + ]: 851 : if( i >= nOff )
127 : : {
128 : 0 : pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" );
129 : 0 : break;
130 : : }
131 : 851 : *ip++ = (char) ( val1 & 0xFF );
132 : 851 : *ip++ = (char) ( val1 >> 8 );
133 : 851 : *ip++ = (char) ( val2 & 0xFF );
134 : 851 : *ip = (char) ( val2 >> 8 );
135 : : } while( i );
136 : : }
137 : 1154 : }
138 : :
139 : 0 : bool SbiBuffer::operator +=( sal_Int8 n )
140 : : {
141 [ # # ]: 0 : if( Check( 1 ) )
142 : : {
143 : 0 : *pCur++ = (char) n; nOff++; return true;
144 : 0 : } else return false;
145 : : }
146 : :
147 : 19258 : bool SbiBuffer::operator +=( sal_uInt8 n )
148 : : {
149 [ + - ]: 19258 : if( Check( 1 ) )
150 : : {
151 : 19258 : *pCur++ = (char) n; nOff++; return true;
152 : 19258 : } else return false;
153 : : }
154 : :
155 : 0 : bool SbiBuffer::operator +=( sal_Int16 n )
156 : : {
157 [ # # ]: 0 : if( Check( 2 ) )
158 : : {
159 : 0 : *pCur++ = (char) ( n & 0xFF );
160 : 0 : *pCur++ = (char) ( n >> 8 );
161 : 0 : nOff += 2; return true;
162 : 0 : } else return false;
163 : : }
164 : :
165 : 43140 : bool SbiBuffer::operator +=( sal_uInt16 n )
166 : : {
167 [ + - ]: 43140 : if( Check( 2 ) )
168 : : {
169 : 43140 : *pCur++ = (char) ( n & 0xFF );
170 : 43140 : *pCur++ = (char) ( n >> 8 );
171 : 43140 : nOff += 2; return true;
172 : 43140 : } else return false;
173 : : }
174 : :
175 : 21570 : bool SbiBuffer::operator +=( sal_uInt32 n )
176 : : {
177 [ + - ]: 21570 : if( Check( 4 ) )
178 : : {
179 : 21570 : sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF );
180 : 21570 : sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 );
181 [ + - ][ + - ]: 21570 : if ( operator +=( n1 ) && operator +=( n2 ) )
[ + - ]
182 : 21570 : return true;
183 : 0 : return true;
184 : : }
185 : 21570 : return false;
186 : : }
187 : :
188 : 0 : bool SbiBuffer::operator +=( sal_Int32 n )
189 : : {
190 : 0 : return operator +=( (sal_uInt32) n );
191 : : }
192 : :
193 : :
194 : 0 : bool SbiBuffer::operator +=( const String& n )
195 : : {
196 : 0 : sal_uInt16 l = n.Len() + 1;
197 [ # # ]: 0 : if( Check( l ) )
198 : : {
199 [ # # ][ # # ]: 0 : rtl::OString aByteStr(rtl::OUStringToOString(n, osl_getThreadTextEncoding()));
[ # # ]
200 : 0 : memcpy( pCur, aByteStr.getStr(), l );
201 : 0 : pCur += l;
202 : 0 : nOff = nOff + l;
203 : 0 : return true;
204 : : }
205 : 0 : else return false;
206 : : }
207 : :
208 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|