1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* libtext602
* Version: MPL 2.0 / LGPLv2.1+
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU Lesser General Public License Version 2.1 or later
* (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
* applicable instead of those above.
*
* For further information visit http://libtext602.sourceforge.net
*/
#include <cstdio>
#include "libtext602_utils.h"
uint8_t libtext602::readU8(librevenge::RVNGInputStream *input, bool /* bigEndian */)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
unsigned long numBytesRead;
uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);
if (p && numBytesRead == sizeof(uint8_t))
return *(uint8_t const *)(p);
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
uint16_t libtext602::readU16(librevenge::RVNGInputStream *input, bool bigEndian)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
unsigned long numBytesRead;
uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead);
if (p && numBytesRead == sizeof(uint16_t))
{
if (bigEndian)
return static_cast<uint16_t>((uint16_t)p[1]|((uint16_t)p[0]<<8));
return static_cast<uint16_t>((uint16_t)p[0]|((uint16_t)p[1]<<8));
}
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
uint32_t libtext602::readU32(librevenge::RVNGInputStream *input, bool bigEndian)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
unsigned long numBytesRead;
uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead);
if (p && numBytesRead == sizeof(uint32_t))
{
if (bigEndian)
return (uint32_t)p[3]|((uint32_t)p[2]<<8)|((uint32_t)p[1]<<16)|((uint32_t)p[0]<<24);
return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
}
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
uint64_t libtext602::readU64(librevenge::RVNGInputStream *input, bool bigEndian)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
unsigned long numBytesRead;
uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead);
if (p && numBytesRead == sizeof(uint64_t))
{
if (bigEndian)
return (uint64_t)p[7]|((uint64_t)p[6]<<8)|((uint64_t)p[5]<<16)|((uint64_t)p[4]<<24)|((uint64_t)p[3]<<32)|((uint64_t)p[2]<<40)|((uint64_t)p[1]<<48)|((uint64_t)p[0]<<56);
return (uint64_t)p[0]|((uint64_t)p[1]<<8)|((uint64_t)p[2]<<16)|((uint64_t)p[3]<<24)|((uint64_t)p[4]<<32)|((uint64_t)p[5]<<40)|((uint64_t)p[6]<<48)|((uint64_t)p[7]<<56);
}
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
const unsigned char *libtext602::readNBytes(librevenge::RVNGInputStream *const input, const unsigned long numBytes)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
unsigned long readBytes = 0;
const unsigned char *const s = input->read(numBytes, readBytes);
if (numBytes != readBytes)
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
return s;
}
void libtext602::skip(librevenge::RVNGInputStream *input, unsigned long numBytes)
{
if (!input || input->isEnd())
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
if (0 != input->seek(static_cast<long>(numBytes), librevenge::RVNG_SEEK_CUR))
{
TEXT602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
throw EndOfStreamException();
}
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|