Coverage Report

Created: 2026-12-01 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/simdutf/src/generic/base64_lines.h
Line
Count
Source
1
/**
2
 * Fast path for base64 split into lines of a fixed length (MIME, PEM).
3
 *
4
 * Inspired by ffbase64 by Peter Fors, a decoder specialized for MIME base64
5
 * with 76-character CRLF lines: https://github.com/vtlmks/ffbase64
6
 */
7
namespace simdutf {
8
namespace SIMDUTF_IMPLEMENTATION {
9
namespace {
10
namespace base64_lines {
11
12
struct step {
13
  uint64_t blend;
14
  uint16_t expect;
15
  uint16_t mask;
16
  uint8_t gap;
17
  uint8_t advance;
18
  uint8_t check;
19
};
20
21
constexpr size_t max_cycle = 128;
22
constexpr size_t max_group = 4;
23
24
struct progress {
25
  size_t read;
26
  size_t written;
27
};
28
29
struct plan {
30
  size_t cycle;
31
  size_t eol;
32
  size_t skip;  // line-ending characters at src, before the first block
33
  size_t start; // step of the first block
34
  step steps[max_cycle + max_group - 1];
35
};
36
37
0
simdutf_really_inline uint16_t check_eol(const char *src, const step &s) {
38
0
  uint16_t word;
39
0
  std::memcpy(&word, src + s.check, sizeof(word));
40
0
  return uint16_t((word ^ s.expect) & s.mask);
41
0
}
42
43
simdutf_really_inline bool has_room(const char *src, const char *srcend,
44
0
                                    size_t offset = 0) {
45
0
  return size_t(srcend - src) >= offset + 68;
46
0
}
47
48
// Plans for lines of 64 to max_line characters (max_line <= 1022).
49
//
50
// The first line starts `consumed` characters before src. The caller has
51
// already decoded those characters as plain base64 blocks, so they hold no
52
// line break; consumed is a multiple of 64. The plan resumes at step
53
// consumed / 64 of the cycle, or, if the first line ends exactly at src,
54
// after its line ending (skip), at the start of the cycle.
55
static inline bool make_plan(const char *src, size_t len, plan &p,
56
                             size_t min_len, size_t consumed = 0,
57
0
                             size_t max_line = 1022) {
58
0
  if (len < min_len || consumed > max_line) {
59
0
    return false;
60
0
  }
61
0
  const size_t limit = max_line + 2 - consumed;
62
0
  const void *newline = std::memchr(src, '\n', len < limit ? len : limit);
63
0
  if (newline == nullptr) {
64
0
    return false;
65
0
  }
66
0
  const size_t r = size_t(static_cast<const char *>(newline) - src);
67
0
  const size_t eol = (r > 0 && src[r - 1] == '\r') ? 2 : 1;
68
0
  const size_t line = consumed + r + 1 - eol;
69
0
  if (line < 64 || line > max_line || line < consumed) {
70
0
    return false;
71
0
  }
72
0
  size_t g = line, h = 64;
73
0
  while (h != 0) {
74
0
    const size_t t = g % h;
75
0
    g = h;
76
0
    h = t;
77
0
  }
78
0
  const size_t cycle = line / g;
79
0
  if (cycle > max_cycle) {
80
0
    return false;
81
0
  }
82
0
  p.cycle = cycle;
83
0
  p.eol = eol;
84
  // When line == consumed, the first line ends at src: skip its line ending
85
  // and start a new cycle (consumed / 64 == cycle).
86
0
  p.skip = line == consumed ? eol : 0;
87
0
  p.start = line == consumed ? 0 : consumed / 64;
88
0
  const uint8_t eol_bytes[2] = {uint8_t(eol == 2 ? '\r' : '\n'), uint8_t('\n')};
89
0
  const uint8_t full_bytes[2] = {0xff, uint8_t(eol == 2 ? 0xff : 0)};
90
0
  uint16_t expect;
91
0
  uint16_t full;
92
0
  std::memcpy(&expect, eol_bytes, sizeof(expect));
93
0
  std::memcpy(&full, full_bytes, sizeof(full));
94
0
  size_t to_gap = line;
95
0
  for (size_t i = 0; i < cycle; i++) {
96
0
    step &s = p.steps[i];
97
0
    s.expect = expect;
98
0
    if (to_gap <= 64) {
99
0
      s.gap = uint8_t(to_gap);
100
0
      s.blend = to_gap == 64 ? 0 : ~uint64_t(0) << to_gap;
101
0
      s.advance = uint8_t(64 + eol);
102
0
      s.check = uint8_t(to_gap);
103
0
      s.mask = full;
104
0
      to_gap = line - (64 - to_gap);
105
0
    } else {
106
0
      s.gap = 64;
107
0
      s.blend = 0;
108
0
      s.advance = 64;
109
0
      s.check = 0;
110
0
      s.mask = 0;
111
0
      to_gap -= 64;
112
0
    }
113
0
  }
114
0
  for (size_t i = cycle; i < cycle + max_group - 1; i++) {
115
0
    p.steps[i] = p.steps[i % cycle];
116
0
  }
117
0
  return true;
118
0
}
119
120
} // namespace base64_lines
121
} // unnamed namespace
122
} // namespace SIMDUTF_IMPLEMENTATION
123
} // namespace simdutf