1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "WW8DocumentImpl.hxx"
25 #include "WW8ResourceModelImpl.hxx"
26 #include <doctok/resourceids.hxx>
27 #include <resourcemodel/QNameToString.hxx>
28 
29 namespace writerfilter {
30 namespace doctok
31 {
32 
33 
operator !=(const WW8PropertySetIterator & rA,const WW8PropertySetIterator & rB)34 bool operator != (const WW8PropertySetIterator & rA,
35                   const WW8PropertySetIterator & rB)
36 {
37     return ! (rA.equal(rB));
38 }
39 
~WW8Property()40 WW8Property::~WW8Property()
41 {
42 }
43 
~WW8PropertySet()44 WW8PropertySet::~WW8PropertySet()
45 {
46 }
47 
~WW8PropertySetIterator()48 WW8PropertySetIterator::~WW8PropertySetIterator()
49 {
50 }
51 
WW8PropertyImpl(WW8Stream & rStream,sal_uInt32 nOffset,sal_uInt32 nCount)52 WW8PropertyImpl::WW8PropertyImpl(WW8Stream & rStream,
53                                  sal_uInt32 nOffset, sal_uInt32 nCount)
54 : WW8StructBase(rStream, nOffset, nCount)
55 {
56 }
57 
WW8PropertyImpl(const WW8StructBase & rBase,sal_uInt32 nOffset,sal_uInt32 nCount)58 WW8PropertyImpl::WW8PropertyImpl(const WW8StructBase & rBase,
59                                  sal_uInt32 nOffset,
60                                  sal_uInt32 nCount)
61 : WW8StructBase(rBase, nOffset, nCount)
62 {
63 }
64 
WW8PropertyImpl(WW8StructBase * pBase,sal_uInt32 nOffset,sal_uInt32 nCount)65 WW8PropertyImpl::WW8PropertyImpl(WW8StructBase * pBase,
66                                  sal_uInt32 nOffset,
67                                  sal_uInt32 nCount)
68 : WW8StructBase(pBase, nOffset, nCount)
69 {
70 }
71 
~WW8PropertyImpl()72 WW8PropertyImpl::~WW8PropertyImpl()
73 {
74 }
75 
getParam() const76 sal_uInt32 WW8PropertyImpl::getParam() const
77 {
78     sal_uInt32 nResult = 0;
79 
80     switch (get_spra())
81     {
82     case 0:
83     case 1:
84         nResult = getU8(2);
85 
86         break;
87 
88     case 2:
89     case 4:
90     case 5:
91         nResult = getU16(2);
92 
93         break;
94 
95     case 3:
96         nResult = getU32(2);
97 
98         break;
99 
100     case 7:
101         nResult = getU16(2) + (getU8(4) << 16);
102 
103         break;
104 
105     default:
106         break;
107     }
108 
109     return nResult;
110 }
111 
getParams() const112 WW8Stream::Sequence WW8PropertyImpl::getParams() const
113 {
114     return WW8Stream::Sequence(mSequence, 2, getCount() - 2);
115 }
116 
getByteLength() const117 sal_uInt32 WW8PropertyImpl::getByteLength() const
118 {
119     sal_uInt32 nParamSize = 0;
120 
121     switch (get_spra())
122     {
123     case 0:
124     case 1:
125         nParamSize = 1;
126 
127         break;
128 
129     case 2:
130     case 4:
131     case 5:
132         nParamSize = 2;
133 
134         break;
135 
136     case 3:
137         nParamSize = 4;
138 
139         break;
140 
141     case 7:
142         nParamSize = 3;
143 
144         break;
145     default:
146         switch (getId())
147         {
148         case 0xd608:
149                 nParamSize = getU16(2) + 1;
150                 break;
151         default:
152                 nParamSize = getU8(2) + 1;
153                 break;
154         }
155 
156         break;
157     }
158 
159     return nParamSize + 2;
160 }
161 
getParamOffset() const162 sal_uInt32 WW8PropertyImpl::getParamOffset() const
163 {
164     sal_uInt32 nReturn = 0;
165 
166     if (get_spra() == 6)
167         nReturn = 1;
168 
169     switch (getId())
170     {
171     case 0xd608:
172     case 0xd609:
173         nReturn = 3;
174 
175         break;
176 
177     default:
178         break;
179     }
180 
181     return nReturn;
182 }
183 
dump(OutputWithDepth<string> & o) const184 void WW8PropertyImpl::dump(OutputWithDepth<string> & o) const
185 {
186     o.addItem(toString());
187 }
188 
toString() const189 string WW8PropertyImpl::toString() const
190 {
191     string aResult;
192 
193     aResult += "<sprmcommon";
194 
195     char sBuffer[256];
196     snprintf(sBuffer, sizeof(sBuffer), " id=\"%" SAL_PRIuUINT32 "\"", getId());
197     aResult += sBuffer;
198     aResult += " name=\"";
199     aResult += (*SprmIdToString::Instance())(getId());
200     aResult += "\"";
201     snprintf(sBuffer, sizeof(sBuffer), " sgc=\"%x\"", get_sgc());
202     aResult += sBuffer;
203     snprintf(sBuffer, sizeof(sBuffer), " spra=\"%x\"", get_spra());
204     aResult += sBuffer;
205     snprintf(sBuffer, sizeof(sBuffer), " size=\"%" SAL_PRIxUINT32 "\"", getByteLength());
206     aResult += sBuffer;
207     snprintf(sBuffer, sizeof(sBuffer), " param=\"%" SAL_PRIxUINT32 "\"", getParam());
208     aResult += sBuffer;
209     aResult += ">\n";
210 
211     aResult += mSequence.toString();
212 
213     aResult += "</sprmcommon>";
214 
215     return aResult;
216 }
217 
WW8PropertySetImpl(WW8Stream & rStream,sal_uInt32 nOffset,sal_uInt32 nCount,bool bPap)218 WW8PropertySetImpl::WW8PropertySetImpl(WW8Stream & rStream,
219                                        sal_uInt32 nOffset,
220                                        sal_uInt32 nCount,
221                                        bool bPap)
222 : WW8StructBase(rStream, nOffset, nCount), mbPap(bPap)
223 {
224 }
225 
WW8PropertySetImpl(const WW8StructBase & rBase,sal_uInt32 nOffset,sal_uInt32 nCount,bool bPap)226 WW8PropertySetImpl::WW8PropertySetImpl(const WW8StructBase & rBase,
227                                        sal_uInt32 nOffset,
228                                        sal_uInt32 nCount,
229                                        bool bPap)
230 : WW8StructBase(rBase, nOffset, nCount), mbPap(bPap)
231 {
232 }
233 
~WW8PropertySetImpl()234 WW8PropertySetImpl::~WW8PropertySetImpl()
235 {
236 }
237 
isPap() const238 bool WW8PropertySetImpl::isPap() const
239 {
240     return mbPap;
241 }
242 
get_istd() const243 sal_uInt32 WW8PropertySetImpl::get_istd() const
244 {
245     sal_uInt32 nResult = 0;
246 
247     if (mbPap)
248         nResult = getU16(0);
249 
250     return nResult;
251 }
252 
begin()253 WW8PropertySetIterator::Pointer_t WW8PropertySetImpl::begin()
254 {
255     return WW8PropertySetIterator::Pointer_t
256         (new WW8PropertySetIteratorImpl(this, mbPap ? 2 : 0));
257 }
258 
end()259 WW8PropertySetIterator::Pointer_t WW8PropertySetImpl::end()
260 {
261     return WW8PropertySetIterator::Pointer_t
262         (new WW8PropertySetIteratorImpl(this, getCount()));
263 }
264 
~WW8PropertySetIteratorImpl()265 WW8PropertySetIteratorImpl::~WW8PropertySetIteratorImpl()
266 {
267 }
268 
269 WW8Property::Pointer_t
getAttribute(sal_uInt32 nOffset) const270 WW8PropertySetImpl::getAttribute(sal_uInt32 nOffset) const
271 {
272     WW8PropertyImpl aTmpAttr(*this, nOffset, 3);
273 
274     sal_uInt32 nLength = aTmpAttr.getByteLength();
275 
276     if (nOffset + nLength > getCount())
277         nLength = getCount() - nOffset;
278 
279     return WW8Property::Pointer_t
280         (new WW8PropertyImpl(*this, nOffset, nLength));
281 }
282 
dump(OutputWithDepth<string> & o) const283 void WW8PropertySetImpl::dump(OutputWithDepth<string> & o) const
284 {
285     WW8StructBase::dump(o);
286 
287     WW8PropertySetIterator::Pointer_t pIt =
288         const_cast<WW8PropertySetImpl *>(this)->begin();
289     WW8PropertySetIterator::Pointer_t pItEnd =
290         const_cast<WW8PropertySetImpl *>(this)->end();
291 
292     while((*pIt) != (*pItEnd))
293     {
294         WW8Property::Pointer_t pAttr = pIt->get();
295 
296         pAttr->dump(o);
297         ++(*pIt);
298     }
299 }
300 
dots(ostream & o)301 void WW8PropertySetImpl::dots(ostream & o)
302 {
303     WW8PropertySetIterator::Pointer_t pIt = begin();
304     WW8PropertySetIterator::Pointer_t pItEnd = end();
305 
306     while((*pIt) != (*pItEnd))
307     {
308         WW8Property::Pointer_t pAttr = pIt->get();
309 
310         o << "." << endl;
311 
312         ++(*pIt);
313     }
314 }
315 
getType() const316 string WW8PropertySetImpl::getType() const
317 {
318     return "WW8PropertySetImpl";
319 }
320 
resolveLocal(Sprm & sprm,Properties & rHandler)321 void WW8PropertySetImpl::resolveLocal(Sprm & sprm, Properties & rHandler)
322 {
323     switch (sprm.getId())
324     {
325     case 0x6a03:
326         {
327             Value::Pointer_t pValue = sprm.getValue();
328             getDocument()->setPicLocation(pValue->getInt());
329             getDocument()->setPicIsData(false);
330         }
331         break;
332     case 0x806:
333         {
334             getDocument()->setPicIsData(true);
335         }
336         break;
337     case 0x6646:
338         {
339             WW8Stream::Pointer_t pStream = getDocument()->getDataStream();
340 
341             if (pStream.get() != NULL)
342             {
343                 Value::Pointer_t pValue = sprm.getValue();
344                 sal_uInt32 nOffset = pValue->getInt();
345                 WW8StructBase aStruct(*pStream, nOffset, 2);
346                 sal_uInt16 nCount = aStruct.getU16(0);
347 
348                 {
349                     WW8PropertySetImpl * pPropSet =
350                     new WW8PropertySetImpl(*pStream, nOffset + 2, nCount);
351 
352                     pPropSet->resolve(rHandler);
353                 }
354             }
355         }
356         break;
357     default:
358         break;
359     }
360 }
361 
resolve(Properties & rHandler)362 void WW8PropertySetImpl::resolve(Properties & rHandler)
363 {
364     if (getCount() >= (isPap() ? 5U : 3U))
365     {
366         WW8PropertySetIterator::Pointer_t pIt = begin();
367         WW8PropertySetIterator::Pointer_t pItEnd = end();
368 
369         if (isPap())
370         {
371             WW8Value::Pointer_t pValue = createValue(getU16(0));
372             rHandler.attribute(NS_rtf::LN_ISTD, *pValue);
373         }
374 
375         while((*pIt) != (*pItEnd))
376         {
377             WW8Sprm aSprm(pIt->get());
378 
379             rHandler.sprm(aSprm);
380 
381             resolveLocal(aSprm, rHandler);
382 
383             ++(*pIt);
384         }
385     }
386 }
387 
operator ++()388 WW8PropertySetIterator & WW8PropertySetIteratorImpl::operator++ ()
389 {
390     WW8Property::Pointer_t pTmpAttr = mpAttrSet->getAttribute(mnOffset);
391 
392     mnOffset += dynamic_cast<WW8PropertyImpl *>(pTmpAttr.get())->
393         getByteLength();
394 
395     if (mnOffset > mpAttrSet->getCount() ||
396         mpAttrSet->getCount() - mnOffset < 3)
397         mnOffset = mpAttrSet->getCount();
398 
399     return *this;
400 }
401 
get() const402 WW8Property::Pointer_t WW8PropertySetIteratorImpl::get() const
403 {
404     return mpAttrSet->getAttribute(mnOffset);
405 }
406 
equal(const WW8PropertySetIterator & rIt) const407 bool WW8PropertySetIteratorImpl::equal
408 (const WW8PropertySetIterator & rIt) const
409 {
410     const WW8PropertySetIteratorImpl & rMyIt =
411         dynamic_cast<const WW8PropertySetIteratorImpl &>(rIt);
412 
413     return mpAttrSet == rMyIt.mpAttrSet && mnOffset == rMyIt.mnOffset;
414 }
415 
toString() const416 string WW8PropertySetIteratorImpl::toString() const
417 {
418     string sResult = "";
419 
420     char sBuffer[256];
421 
422     snprintf(sBuffer, sizeof(sBuffer), "(%" SAL_PRIuUINT32 ")", mnOffset);
423     sResult += sBuffer;
424 
425     return sResult;
426 }
427 
428 }}
429