Coverage Report

Created: 2026-08-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/woff2/src/buffer.h
Line
Count
Source
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* The parts of ots.h & opentype-sanitiser.h that we need, taken from the
8
   https://code.google.com/p/ots/ project. */
9
10
#ifndef WOFF2_BUFFER_H_
11
#define WOFF2_BUFFER_H_
12
13
#if defined(_WIN32)
14
#include <stdlib.h>
15
typedef signed char int8_t;
16
typedef unsigned char uint8_t;
17
typedef short int16_t;
18
typedef unsigned short uint16_t;
19
typedef int int32_t;
20
typedef unsigned int uint32_t;
21
typedef __int64 int64_t;
22
typedef unsigned __int64 uint64_t;
23
#define ntohl(x) _byteswap_ulong (x)
24
#define ntohs(x) _byteswap_ushort (x)
25
#define htonl(x) _byteswap_ulong (x)
26
#define htons(x) _byteswap_ushort (x)
27
#else
28
#include <arpa/inet.h>
29
#include <stdint.h>
30
#endif
31
32
#include <cstdio>
33
#include <cstdlib>
34
#include <cstring>
35
#include <limits>
36
37
namespace woff2 {
38
39
#if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
40
18.5k
#define FONT_COMPRESSION_FAILURE() false
41
#else
42
#define FONT_COMPRESSION_FAILURE() \
43
  woff2::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
44
inline bool Failure(const char *f, int l, const char *fn) {
45
  fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
46
  fflush(stderr);
47
  return false;
48
}
49
#endif
50
51
// -----------------------------------------------------------------------------
52
// Buffer helper class
53
//
54
// This class perform some trival buffer operations while checking for
55
// out-of-bounds errors. As a family they return false if anything is amiss,
56
// updating the current offset otherwise.
57
// -----------------------------------------------------------------------------
58
class Buffer {
59
 public:
60
  Buffer(const uint8_t *data, size_t len)
61
105k
      : buffer_(data),
62
105k
        length_(len),
63
105k
        offset_(0) { }
64
65
204k
  bool Skip(size_t n_bytes) {
66
204k
    return Read(NULL, n_bytes);
67
204k
  }
68
69
297k
  bool Read(uint8_t *data, size_t n_bytes) {
70
297k
    if (n_bytes > 1024 * 1024 * 1024) {
71
0
      return FONT_COMPRESSION_FAILURE();
72
0
    }
73
297k
    if ((offset_ + n_bytes > length_) ||
74
297k
        (offset_ > length_ - n_bytes)) {
75
216
      return FONT_COMPRESSION_FAILURE();
76
216
    }
77
297k
    if (data) {
78
92.2k
      std::memcpy(data, buffer_ + offset_, n_bytes);
79
92.2k
    }
80
297k
    offset_ += n_bytes;
81
297k
    return true;
82
297k
  }
83
84
5.38M
  inline bool ReadU8(uint8_t *value) {
85
5.38M
    if (length_ < 1 || offset_ > length_ - 1) {
86
504
      return FONT_COMPRESSION_FAILURE();
87
504
    }
88
5.38M
    *value = buffer_[offset_];
89
5.38M
    ++offset_;
90
5.38M
    return true;
91
5.38M
  }
92
93
222k
  bool ReadU16(uint16_t *value) {
94
222k
    if (length_ < 2 || offset_ > length_ - 2) {
95
86
      return FONT_COMPRESSION_FAILURE();
96
86
    }
97
222k
    std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
98
222k
    *value = ntohs(*value);
99
222k
    offset_ += 2;
100
222k
    return true;
101
222k
  }
102
103
91.1k
  bool ReadS16(int16_t *value) {
104
91.1k
    return ReadU16(reinterpret_cast<uint16_t*>(value));
105
91.1k
  }
106
107
0
  bool ReadU24(uint32_t *value) {
108
0
    if (length_ < 3 || offset_ > length_ - 3) {
109
0
      return FONT_COMPRESSION_FAILURE();
110
0
    }
111
0
    *value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
112
0
        static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
113
0
        static_cast<uint32_t>(buffer_[offset_ + 2]);
114
0
    offset_ += 3;
115
0
    return true;
116
0
  }
117
118
109k
  bool ReadU32(uint32_t *value) {
119
109k
    if (length_ < 4 || offset_ > length_ - 4) {
120
87
      return FONT_COMPRESSION_FAILURE();
121
87
    }
122
109k
    std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
123
109k
    *value = ntohl(*value);
124
109k
    offset_ += 4;
125
109k
    return true;
126
109k
  }
127
128
0
  bool ReadS32(int32_t *value) {
129
0
    return ReadU32(reinterpret_cast<uint32_t*>(value));
130
0
  }
131
132
0
  bool ReadTag(uint32_t *value) {
133
0
    if (length_ < 4 || offset_ > length_ - 4) {
134
0
      return FONT_COMPRESSION_FAILURE();
135
0
    }
136
0
    std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
137
0
    offset_ += 4;
138
0
    return true;
139
0
  }
140
141
0
  bool ReadR64(uint64_t *value) {
142
0
    if (length_ < 8 || offset_ > length_ - 8) {
143
0
      return FONT_COMPRESSION_FAILURE();
144
0
    }
145
0
    std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
146
0
    offset_ += 8;
147
0
    return true;
148
0
  }
149
150
182k
  const uint8_t *buffer() const { return buffer_; }
151
371k
  size_t offset() const { return offset_; }
152
181k
  size_t length() const { return length_; }
153
154
0
  bool set_offset(size_t newoffset) {
155
0
    if (newoffset > length_) {
156
0
      return FONT_COMPRESSION_FAILURE();
157
0
    }
158
0
    offset_ = newoffset;
159
0
    return true;
160
0
  }
161
162
 private:
163
  const uint8_t * const buffer_;
164
  const size_t length_;
165
  size_t offset_;
166
};
167
168
} // namespace woff2
169
170
#endif  // WOFF2_BUFFER_H_