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 <WW8StructBase.hxx>
25 
26 namespace writerfilter {
27 namespace doctok {
28 using namespace ::com::sun::star;
29 
WW8StructBase(const WW8StructBase & rParent,sal_uInt32 nOffset,sal_uInt32 nCount)30 WW8StructBase::WW8StructBase(const WW8StructBase & rParent,
31               sal_uInt32 nOffset, sal_uInt32 nCount)
32 : mSequence(rParent.mSequence, nOffset, nCount), mpParent(0),
33   mpDocument(rParent.getDocument())
34 {
35     if (nOffset + nCount > rParent.getCount())
36     {
37         throw ExceptionOutOfBounds("WW8StructBase");
38     }
39 }
40 
Assign(const WW8StructBase & rSrc)41 WW8StructBase & WW8StructBase::Assign(const WW8StructBase & rSrc)
42 {
43     mSequence = rSrc.mSequence;
44     mpDocument = rSrc.mpDocument;
45 
46     return *this;
47 }
48 
setDocument(WW8DocumentImpl * pDocument)49 void WW8StructBase::setDocument(WW8DocumentImpl * pDocument)
50 {
51     mpDocument = pDocument;
52 }
53 
getDocument() const54 WW8DocumentImpl * WW8StructBase::getDocument() const
55 {
56     return mpDocument;
57 }
58 
getU8(sal_uInt32 nOffset) const59 sal_uInt8 WW8StructBase::getU8(sal_uInt32 nOffset) const
60 {
61     return doctok::getU8(mSequence, nOffset);
62 }
63 
getU16(sal_uInt32 nOffset) const64 sal_uInt16 WW8StructBase::getU16(sal_uInt32 nOffset) const
65 {
66     return doctok::getU16(mSequence, nOffset);
67 }
68 
getU32(sal_uInt32 nOffset) const69 sal_uInt32 WW8StructBase::getU32(sal_uInt32 nOffset) const
70 {
71     return doctok::getU32(mSequence, nOffset);
72 }
73 
getBit(sal_uInt32 nValue,sal_uInt16 nBit) const74 sal_Bool WW8StructBase::getBit(sal_uInt32 nValue, sal_uInt16 nBit) const
75 {
76     return (nValue & (1 << nBit)) != 0;
77 }
78 
getNibble(sal_uInt32 nValue,sal_uInt16 nShift) const79 sal_uInt8 WW8StructBase::getNibble(sal_uInt32 nValue,
80                                    sal_uInt16 nShift) const
81 {
82     return sal::static_int_cast<sal_uInt8>((nValue >> nShift) & 0xf);
83 }
84 
getU8(const WW8StructBase::Sequence & rSeq,sal_uInt32 nOffset)85 sal_uInt8 getU8(const WW8StructBase::Sequence & rSeq,
86                 sal_uInt32 nOffset)
87 {
88     return rSeq[nOffset];
89 }
90 
getU16(const WW8StructBase::Sequence & rSeq,sal_uInt32 nOffset)91 sal_uInt16 getU16(const WW8StructBase::Sequence & rSeq,
92                   sal_uInt32 nOffset)
93 {
94     return getU8(rSeq, nOffset) | (getU8(rSeq, nOffset + 1) << 8);
95 }
96 
getU32(const WW8StructBase::Sequence & rSeq,sal_uInt32 nOffset)97 sal_uInt32 getU32(const WW8StructBase::Sequence & rSeq,
98                   sal_uInt32 nOffset)
99 {
100     sal_uInt32 nResult = getU8(rSeq, nOffset);
101     nResult |= (getU8(rSeq, nOffset + 1) << 8);
102     nResult |=  (getU8(rSeq, nOffset + 2) << 16);
103     nResult |= (getU8(rSeq, nOffset + 3) << 24);
104 
105     return nResult;
106 }
107 
getString(sal_uInt32 nOffset,sal_uInt32 nCount) const108 rtl::OUString WW8StructBase::getString(sal_uInt32 nOffset, sal_uInt32 nCount)
109     const
110 {
111     rtl::OUString aResult;
112 
113     if (nOffset < getCount())
114     {
115         sal_uInt32 nCount1 = nCount;
116         if (nOffset + nCount * 2 > getCount())
117         {
118             nCount1 = (getCount() - nOffset) / 2;
119         }
120 
121         if (nCount1 > 0)
122         {
123             Sequence aSeq(mSequence, nOffset, nCount1 * 2);
124 
125             rtl_uString * pNew = 0;
126             rtl_uString_newFromStr_WithLength
127             (&pNew, reinterpret_cast<const sal_Unicode *>(&aSeq[0]),
128              nCount1);
129 
130             aResult = rtl::OUString(pNew);
131         }
132     }
133 
134     return aResult;
135 }
136 
137 WW8StructBase *
getRemainder(sal_uInt32 nOffset) const138 WW8StructBase::getRemainder(sal_uInt32 nOffset) const
139 {
140     WW8StructBase * pResult = NULL;
141 
142     sal_uInt32 nCount = getCount();
143     if (nCount > nOffset)
144     {
145         pResult = new WW8StructBase(*this, nOffset, nCount - nOffset);
146     }
147 
148     return pResult;
149 }
150 
151 
getString(sal_uInt32 nOffset) const152 rtl::OUString WW8StructBase::getString(sal_uInt32 nOffset) const
153 {
154     sal_uInt32 nCount = getU16(nOffset);
155 
156     return getString(nOffset + 2, nCount);
157 }
158 
WW8StructBaseTmpOffset(WW8StructBase * pStructBase)159 WW8StructBaseTmpOffset::WW8StructBaseTmpOffset
160 (WW8StructBase * pStructBase)
161 : mnOffset(0), mpStructBase(pStructBase)
162 {
163 }
164 
set(sal_uInt32 nOffset)165 sal_uInt32 WW8StructBaseTmpOffset::set(sal_uInt32 nOffset)
166 {
167     if (nOffset >= mpStructBase->getCount())
168         throw ExceptionOutOfBounds("WW8StructBaseTmpOffset::set");
169 
170     mnOffset = nOffset;
171 
172     return mnOffset;
173 }
174 
get() const175 sal_uInt32 WW8StructBaseTmpOffset::get() const
176 {
177     return mnOffset;
178 }
179 
inc(sal_uInt32 nOffset)180 sal_uInt32 WW8StructBaseTmpOffset::inc(sal_uInt32 nOffset)
181 {
182     if (mpStructBase->getCount() - mnOffset < nOffset)
183         throw ExceptionOutOfBounds("WW8StructBaseTmpOffset::inc");
184 
185     mnOffset += nOffset;
186 
187     return mnOffset;
188 }
189 
operator sal_uInt32() const190 WW8StructBaseTmpOffset::operator sal_uInt32() const
191 {
192     return mnOffset;
193 }
194 
195 }}
196