Coverage Report

Created: 2026-03-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvisio/src/lib/libvisio_utils.cpp
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/*
3
 * This file is part of the libvisio project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include "libvisio_utils.h"
11
12
#include <cstdarg>
13
#include <cstdio>
14
#include "VSDInternalStream.h"
15
16
uint8_t libvisio::readU8(librevenge::RVNGInputStream *input)
17
166M
{
18
166M
  if (!input || input->isEnd())
19
59.8k
  {
20
59.8k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
21
59.8k
    throw EndOfStreamException();
22
59.8k
  }
23
166M
  unsigned long numBytesRead;
24
166M
  uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);
25
26
166M
  if (p && numBytesRead == sizeof(uint8_t))
27
166M
    return *(uint8_t const *)(p);
28
0
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
29
0
  throw EndOfStreamException();
30
166M
}
31
32
uint16_t libvisio::readU16(librevenge::RVNGInputStream *input)
33
1.53G
{
34
1.53G
  if (!input || input->isEnd())
35
1.37M
  {
36
1.37M
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
37
1.37M
    throw EndOfStreamException();
38
1.37M
  }
39
1.53G
  unsigned long numBytesRead;
40
1.53G
  uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead);
41
42
1.53G
  if (p && numBytesRead == sizeof(uint16_t))
43
1.53G
    return (uint16_t)p[0]|((uint16_t)p[1]<<8);
44
524k
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
45
524k
  throw EndOfStreamException();
46
1.53G
}
47
48
int16_t libvisio::readS16(librevenge::RVNGInputStream *input)
49
1.31G
{
50
1.31G
  return (int16_t)readU16(input);
51
1.31G
}
52
53
uint32_t libvisio::readU32(librevenge::RVNGInputStream *input)
54
240M
{
55
240M
  if (!input || input->isEnd())
56
127k
  {
57
127k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
58
127k
    throw EndOfStreamException();
59
127k
  }
60
240M
  unsigned long numBytesRead;
61
240M
  uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead);
62
63
240M
  if (p && numBytesRead == sizeof(uint32_t))
64
239M
    return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
65
132k
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
66
132k
  throw EndOfStreamException();
67
240M
}
68
69
int32_t libvisio::readS32(librevenge::RVNGInputStream *input)
70
48.8k
{
71
48.8k
  return (int32_t)readU32(input);
72
48.8k
}
73
74
uint64_t libvisio::readU64(librevenge::RVNGInputStream *input)
75
65.8M
{
76
65.8M
  if (!input || input->isEnd())
77
92.1k
  {
78
92.1k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
79
92.1k
    throw EndOfStreamException();
80
92.1k
  }
81
65.7M
  unsigned long numBytesRead;
82
65.7M
  uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead);
83
84
65.7M
  if (p && numBytesRead == sizeof(uint64_t))
85
65.7M
    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);
86
992
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
87
992
  throw EndOfStreamException();
88
65.7M
}
89
90
double libvisio::readDouble(librevenge::RVNGInputStream *input)
91
65.8M
{
92
65.8M
  union
93
65.8M
  {
94
65.8M
    uint64_t u;
95
65.8M
    double d;
96
65.8M
  } tmpUnion;
97
98
65.8M
  tmpUnion.u = readU64(input);
99
100
65.8M
  return tmpUnion.d;
101
65.8M
}
102
103
const librevenge::RVNGString libvisio::getColourString(const Colour &c)
104
1.52M
{
105
1.52M
  librevenge::RVNGString sColour;
106
1.52M
  sColour.sprintf("#%.2x%.2x%.2x", c.r, c.g, c.b);
107
1.52M
  return sColour;
108
1.52M
}
109
110
unsigned long libvisio::getRemainingLength(librevenge::RVNGInputStream *const input)
111
303k
{
112
303k
  if (!input)
113
0
    throw EndOfStreamException();
114
115
303k
  const long begin = input->tell();
116
117
303k
  if (input->seek(0, librevenge::RVNG_SEEK_END) != 0)
118
0
  {
119
    // librevenge::RVNG_SEEK_END does not work. Use the harder way.
120
0
    while (!input->isEnd())
121
0
      readU8(input);
122
0
  }
123
303k
  const long end = input->tell();
124
125
303k
  input->seek(begin, librevenge::RVNG_SEEK_SET);
126
127
303k
  if (end < begin)
128
0
    throw EndOfStreamException();
129
303k
  return static_cast<unsigned long>(end - begin);
130
303k
}
131
132
void libvisio::appendUCS4(librevenge::RVNGString &text, UChar32 ucs4Character)
133
158M
{
134
  // Convert carriage returns to new line characters
135
158M
  if (ucs4Character == (UChar32) 0x0d || ucs4Character == (UChar32) 0x0e)
136
42.5k
    ucs4Character = (UChar32) '\n';
137
138
158M
  unsigned char outbuf[U8_MAX_LENGTH+1];
139
158M
  int i = 0;
140
158M
  U8_APPEND_UNSAFE(&outbuf[0], i, ucs4Character);
141
158M
  outbuf[i] = 0;
142
143
158M
  text.append((char *)outbuf);
144
158M
}
145
146
void libvisio::debugPrint(const char *format, ...)
147
0
{
148
0
  va_list args;
149
0
  va_start(args, format);
150
0
  std::vfprintf(stderr, format, args);
151
  va_end(args);
152
0
}
153
154
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */