Coverage Report

Created: 2026-10-28 06:34

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
169M
{
18
169M
  if (!input || input->isEnd())
19
44.7k
  {
20
44.7k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
21
44.7k
    throw EndOfStreamException();
22
44.7k
  }
23
169M
  unsigned long numBytesRead;
24
169M
  uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);
25
26
169M
  if (p && numBytesRead == sizeof(uint8_t))
27
169M
    return *(uint8_t const *)(p);
28
0
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
29
0
  throw EndOfStreamException();
30
169M
}
31
32
uint16_t libvisio::readU16(librevenge::RVNGInputStream *input)
33
1.01G
{
34
1.01G
  if (!input || input->isEnd())
35
347k
  {
36
347k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
37
347k
    throw EndOfStreamException();
38
347k
  }
39
1.00G
  unsigned long numBytesRead;
40
1.00G
  uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead);
41
42
1.00G
  if (p && numBytesRead == sizeof(uint16_t))
43
1.00G
    return (uint16_t)p[0]|((uint16_t)p[1]<<8);
44
53.7k
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
45
53.7k
  throw EndOfStreamException();
46
1.00G
}
47
48
int16_t libvisio::readS16(librevenge::RVNGInputStream *input)
49
873M
{
50
873M
  return (int16_t)readU16(input);
51
873M
}
52
53
uint32_t libvisio::readU32(librevenge::RVNGInputStream *input)
54
83.3M
{
55
83.3M
  if (!input || input->isEnd())
56
28.5k
  {
57
28.5k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
58
28.5k
    throw EndOfStreamException();
59
28.5k
  }
60
83.2M
  unsigned long numBytesRead;
61
83.2M
  uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead);
62
63
83.2M
  if (p && numBytesRead == sizeof(uint32_t))
64
83.2M
    return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
65
13.6k
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
66
13.6k
  throw EndOfStreamException();
67
83.2M
}
68
69
int32_t libvisio::readS32(librevenge::RVNGInputStream *input)
70
77.6k
{
71
77.6k
  return (int32_t)readU32(input);
72
77.6k
}
73
74
uint64_t libvisio::readU64(librevenge::RVNGInputStream *input)
75
85.9M
{
76
85.9M
  if (!input || input->isEnd())
77
91.5k
  {
78
91.5k
    VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
79
91.5k
    throw EndOfStreamException();
80
91.5k
  }
81
85.8M
  unsigned long numBytesRead;
82
85.8M
  uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead);
83
84
85.8M
  if (p && numBytesRead == sizeof(uint64_t))
85
85.8M
    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
2.59k
  VSD_DEBUG_MSG(("Throwing EndOfStreamException\n"));
87
2.59k
  throw EndOfStreamException();
88
85.8M
}
89
90
double libvisio::readDouble(librevenge::RVNGInputStream *input)
91
85.9M
{
92
85.9M
  union
93
85.9M
  {
94
85.9M
    uint64_t u;
95
85.9M
    double d;
96
85.9M
  } tmpUnion;
97
98
85.9M
  tmpUnion.u = readU64(input);
99
100
85.9M
  return tmpUnion.d;
101
85.9M
}
102
103
const librevenge::RVNGString libvisio::getColourString(const Colour &c)
104
598k
{
105
598k
  librevenge::RVNGString sColour;
106
598k
  sColour.sprintf("#%.2x%.2x%.2x", c.r, c.g, c.b);
107
598k
  return sColour;
108
598k
}
109
110
unsigned long libvisio::getRemainingLength(librevenge::RVNGInputStream *const input)
111
287k
{
112
287k
  if (!input)
113
0
    throw EndOfStreamException();
114
115
287k
  const long begin = input->tell();
116
117
287k
  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
287k
  const long end = input->tell();
124
125
287k
  input->seek(begin, librevenge::RVNG_SEEK_SET);
126
127
287k
  if (end < begin)
128
0
    throw EndOfStreamException();
129
287k
  return static_cast<unsigned long>(end - begin);
130
287k
}
131
132
void libvisio::appendUCS4(librevenge::RVNGString &text, UChar32 ucs4Character)
133
240M
{
134
  // Convert carriage returns to new line characters
135
240M
  if (ucs4Character == (UChar32) 0x0d || ucs4Character == (UChar32) 0x0e)
136
184k
    ucs4Character = (UChar32) '\n';
137
138
240M
  unsigned char outbuf[U8_MAX_LENGTH+1];
139
240M
  int i = 0;
140
240M
  U8_APPEND_UNSAFE(&outbuf[0], i, ucs4Character);
141
240M
  outbuf[i] = 0;
142
143
240M
  text.append((char *)outbuf);
144
240M
}
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: */