Coverage Report

Created: 2026-11-12 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/base85.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "base85.h"
3
4
static const char en85[] = {
5
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
6
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
7
  'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
8
  'U', 'V', 'W', 'X', 'Y', 'Z',
9
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
10
  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
11
  'u', 'v', 'w', 'x', 'y', 'z',
12
  '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
13
  ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
14
  '|', '}', '~'
15
};
16
17
static char de85[256];
18
static void prep_base85(void)
19
0
{
20
0
  if (de85['Z'])
21
0
    return;
22
0
  for (size_t i = 0; i < ARRAY_SIZE(en85); i++) {
23
0
    int ch = en85[i];
24
0
    de85[ch] = i + 1;
25
0
  }
26
0
}
27
28
int decode_85(char *dst, const char *buffer, int len)
29
0
{
30
0
  prep_base85();
31
32
0
  while (len) {
33
0
    unsigned acc = 0;
34
0
    int de, cnt = 4;
35
0
    unsigned char ch;
36
0
    do {
37
0
      ch = *buffer++;
38
0
      de = de85[ch];
39
0
      if (--de < 0)
40
0
        return error("invalid base85 alphabet %c", ch);
41
0
      acc = acc * 85 + de;
42
0
    } while (--cnt);
43
0
    ch = *buffer++;
44
0
    de = de85[ch];
45
0
    if (--de < 0)
46
0
      return error("invalid base85 alphabet %c", ch);
47
    /* Detect overflow. */
48
0
    if (0xffffffff / 85 < acc ||
49
0
        0xffffffff - de < (acc *= 85))
50
0
      return error("invalid base85 sequence %.5s", buffer-5);
51
0
    acc += de;
52
53
0
    cnt = (len < 4) ? len : 4;
54
0
    len -= cnt;
55
0
    do {
56
0
      acc = (acc << 8) | (acc >> 24);
57
0
      *dst++ = acc;
58
0
    } while (--cnt);
59
0
  }
60
61
0
  return 0;
62
0
}
63
64
static void encode_85(char *buf, const unsigned char *data, int bytes)
65
0
{
66
0
  while (bytes) {
67
0
    unsigned acc = 0;
68
0
    int cnt;
69
0
    for (cnt = 24; cnt >= 0; cnt -= 8) {
70
0
      unsigned ch = *data++;
71
0
      acc |= ch << cnt;
72
0
      if (--bytes == 0)
73
0
        break;
74
0
    }
75
0
    for (cnt = 4; cnt >= 0; cnt--) {
76
0
      int val = acc % 85;
77
0
      acc /= 85;
78
0
      buf[cnt] = en85[val];
79
0
    }
80
0
    buf += 5;
81
0
  }
82
83
0
  *buf = 0;
84
0
}
85
86
/*
87
 * Split the provided data into chunks of at most 52 bytes. Each chunk
88
 * is base85-encoded into a line with the following format:
89
 *  - Byte 0: encodes the chunk size (1-52) as a letter: 'A'-'Z' for
90
 *    1-26 bytes, 'a'-'z' for 27-52 bytes.
91
 *  - Bytes 1 onwards: base85-encoded data (up to 65 bytes).
92
 *  - Followed by a newline and NUL terminator.
93
 * The output callback is called once per line.
94
 */
95
void chunk_and_encode85(const unsigned char *data, unsigned long len,
96
      each_encoded_line_fn output, void *output_data)
97
0
{
98
0
  while (len) {
99
0
    int line_len;
100
0
    int bytes = (BASE85_CHUNK_SIZE < len) ? BASE85_CHUNK_SIZE : len;
101
0
    char line[71];
102
0
    len -= bytes;
103
0
    if (bytes <= 26)
104
0
      line[0] = bytes + 'A' - 1;
105
0
    else
106
0
      line[0] = bytes - 26 + 'a' - 1;
107
0
    encode_85(line + 1, data, bytes);
108
0
    data = data + bytes;
109
110
0
    line_len = strlen(line);
111
0
    line[line_len++] = '\n';
112
0
    line[line_len] = '\0';
113
0
    output(line, line_len, output_data);
114
0
  }
115
0
}