1 /* libwpg
2 * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
3 * Copyright (C) 2004 Marc Oude Kotte (marc@solcon.nl)
4 * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1301 USA
20 *
21 * For further information visit http://libwpg.sourceforge.net
22 */
24 /* "This product is not manufactured, approved, or supported by
25 * Corel Corporation or Corel Corporation Limited."
26 */
28 #include "WPGXParser.h"
30 using namespace libwpg;
32 WPGXParser::WPGXParser(WPGInputStream *input, WPGPaintInterface* painter):
33 m_input(input), m_painter(painter)
34 {
35 }
37 unsigned char WPGXParser::readU8()
38 {
39 return m_input->getc();
40 }
42 unsigned short WPGXParser::readU16()
43 {
44 unsigned short p0 = (unsigned short)readU8();
45 unsigned short p1 = (unsigned short)readU8();
46 return p0|(p1<<8);
47 }
49 unsigned long WPGXParser::readU32()
50 {
51 unsigned long p0 = (unsigned short)readU8();
52 unsigned long p1 = (unsigned short)readU8();
53 unsigned long p2 = (unsigned short)readU8();
54 unsigned long p3 = (unsigned short)readU8();
55 return p0|(p1<<8)|(p2<<16)|(p3<<24);
56 }
58 char WPGXParser::readS8()
59 {
60 return (char)m_input->getc();
61 }
63 short WPGXParser::readS16()
64 {
65 short p0 = readU8();
66 short p1 = readS8();
67 return p0|(p1<<8);
68 }
70 long WPGXParser::readS32()
71 {
72 long p0 = readU8();
73 long p1 = readU8();
74 long p2 = readU8();
75 long p3 = readS8();
76 return p0|(p1<<8)|(p2<<16)|(p3<<24);
77 }
79 unsigned int WPGXParser::readVariableLengthInteger()
80 {
81 // read a byte
82 unsigned char value8 = readU8();
83 // if it's in the range 0-0xFE, then we have a 8-bit value
84 if (value8<=0xFE) {
85 return (unsigned int)value8;
86 } else {
87 // now read a 16 bit value
88 unsigned short value16 = readU16();
89 // if the MSB is 1, we have a 32 bit value
90 if (value16>>15) {
91 // read the next 16 bit value (LSB part, in value16 resides the MSB part)
92 unsigned long lvalue16 = readU16();
93 unsigned long value32 = value16 & 0x7fff; // mask out the MSB
94 return (value32<<16)+lvalue16;
95 } else {
96 // we have a 16 bit value, return it
97 return (unsigned int)value16;
98 }
99 }
101 // unreachable
102 return 0;
103 }