summaryrefslogtreecommitdiff
path: root/src/lib/libsw602_utils.cpp
blob: 1ae5dbf0543059bc7c92cccbcf4556f06e974c6b (plain)
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
133
134
135
136
137
138
139
140
141
142
143
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libsw602 project.
 *
 * 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/.
 */

#include "libsw602_utils.h"

#include <cstdarg>
#ifdef DEBUG
#include <cstdio>
#endif

namespace libsw602
{

uint8_t readU8(librevenge::RVNGInputStream &input, bool /* bigEndian */)
{
  if (input.isEnd())
  {
    SW602_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);
  SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
  throw EndOfStreamException();
}

uint16_t readU16(librevenge::RVNGInputStream &input, bool bigEndian)
{
  if (input.isEnd())
  {
    SW602_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));
  }
  SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
  throw EndOfStreamException();
}

uint32_t readU32(librevenge::RVNGInputStream &input, bool bigEndian)
{
  if (input.isEnd())
  {
    SW602_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);
  }
  SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
  throw EndOfStreamException();
}

uint64_t readU64(librevenge::RVNGInputStream &input, bool bigEndian)
{
  if (input.isEnd())
  {
    SW602_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);
  }
  SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
  throw EndOfStreamException();
}

const unsigned char *readNBytes(librevenge::RVNGInputStream &input, const unsigned long numBytes)
{
  if (input.isEnd())
  {
    SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
    throw EndOfStreamException();
  }

  unsigned long readBytes = 0;
  const unsigned char *const s = input.read(numBytes, readBytes);

  if (numBytes != readBytes)
  {
    SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
    throw EndOfStreamException();
  }

  return s;
}

void skip(librevenge::RVNGInputStream &input, unsigned long numBytes)
{
  if (input.isEnd())
  {
    SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
    throw EndOfStreamException();
  }

  if (0 != input.seek(static_cast<long>(numBytes), librevenge::RVNG_SEEK_CUR))
  {
    SW602_DEBUG_MSG(("Throwing EndOfStreamException\n"));
    throw EndOfStreamException();
  }
}

#ifdef DEBUG
void debugPrint(const char *const format, ...)
{
  va_list args;
  va_start(args, format);
  std::vfprintf(stderr, format, args);
  va_end(args);
}
#endif

}

/* vim:set shiftwidth=2 softtabstop=2 expandtab: */