Coverage Report

Created: 2020-05-07 18:36

/proc/self/cwd/c/basics.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright 2020 Google LLC
3
4
Use of this source code is governed by a BSD-style
5
license that can be found in the LICENSE file or at
6
https://developers.google.com/open-source/licenses/bsd
7
*/
8
9
#include "basics.h"
10
11
#include "system.h"
12
13
void put_be24(byte *out, uint32_t i)
14
1.54k
{
15
1.54k
  out[0] = (byte)((i >> 16) & 0xff);
16
1.54k
  out[1] = (byte)((i >> 8) & 0xff);
17
1.54k
  out[2] = (byte)(i & 0xff);
18
1.54k
}
19
20
uint32_t get_be24(byte *in)
21
5.22k
{
22
5.22k
  return (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |
23
5.22k
         (uint32_t)(in[2]);
24
5.22k
}
25
26
void put_be16(uint8_t *out, uint16_t i)
27
781
{
28
781
  out[0] = (uint8_t)((i >> 8) & 0xff);
29
781
  out[1] = (uint8_t)(i & 0xff);
30
781
}
31
32
int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
33
1.45k
{
34
1.45k
  size_t lo = 0;
35
1.45k
  size_t hi = sz;
36
1.45k
37
1.45k
  /* invariant: (hi == sz) || f(hi) == true
38
1.45k
     (lo == 0 && f(0) == true) || fi(lo) == false
39
1.45k
   */
40
1.86k
  while (hi - lo > 1) {
41
417
    size_t mid = lo + (hi - lo) / 2;
42
417
43
417
    int val = f(mid, args);
44
417
    if (val) {
45
404
      hi = mid;
46
404
    } else {
47
13
      lo = mid;
48
13
    }
49
417
  }
50
1.45k
51
1.45k
  if (lo == 0) {
52
1.44k
    if (f(0, args)) {
53
1.29k
      return 0;
54
1.29k
    } else {
55
149
      return 1;
56
149
    }
57
9
  }
58
9
59
9
  return hi;
60
9
}
61
62
void free_names(char **a)
63
824
{
64
824
  char **p = a;
65
824
  if (p == NULL) {
66
274
    return;
67
274
  }
68
2.22k
  while (*p) {
69
1.67k
    reftable_free(*p);
70
1.67k
    p++;
71
1.67k
  }
72
550
  reftable_free(a);
73
550
}
74
75
int names_length(char **names)
76
209
{
77
209
  int len = 0;
78
209
  char **p = names;
79
828
  while (*p) {
80
619
    p++;
81
619
    len++;
82
619
  }
83
209
  return len;
84
209
}
85
86
void parse_names(char *buf, int size, char ***namesp)
87
389
{
88
389
  char **names = NULL;
89
389
  int names_cap = 0;
90
389
  int names_len = 0;
91
389
92
389
  char *p = buf;
93
389
  char *end = buf + size;
94
1.62k
  while (p < end) {
95
1.23k
    char *next = strchr(p, '\n');
96
1.23k
    if (next != NULL) {
97
1.23k
      *next = 0;
98
1.23k
    } else {
99
1
      next = end;
100
1
    }
101
1.23k
    if (p < next) {
102
1.23k
      if (names_len == names_cap) {
103
885
        names_cap = 2 * names_cap + 1;
104
885
        names = reftable_realloc(
105
885
          names, names_cap * sizeof(char *));
106
885
      }
107
1.23k
      names[names_len++] = xstrdup(p);
108
1.23k
    }
109
1.23k
    p = next + 1;
110
1.23k
  }
111
389
112
389
  if (names_len == names_cap) {
113
157
    names_cap = 2 * names_cap + 1;
114
157
    names = reftable_realloc(names, names_cap * sizeof(char *));
115
157
  }
116
389
117
389
  names[names_len] = NULL;
118
389
  *namesp = names;
119
389
}
120
121
int names_equal(char **a, char **b)
122
3
{
123
10
  while (*a && *b) {
124
8
    if (strcmp(*a, *b)) {
125
1
      return 0;
126
1
    }
127
7
128
7
    a++;
129
7
    b++;
130
7
  }
131
3
132
3
  return *a == *b;
133
3
}
134
135
const char *reftable_error_str(int err)
136
{
137
  static char buf[250];
138
  switch (err) {
139
  case REFTABLE_IO_ERROR:
140
    return "I/O error";
141
  case REFTABLE_FORMAT_ERROR:
142
    return "corrupt reftable file";
143
  case REFTABLE_NOT_EXIST_ERROR:
144
    return "file does not exist";
145
  case REFTABLE_LOCK_ERROR:
146
    return "data is outdated";
147
  case REFTABLE_API_ERROR:
148
    return "misuse of the reftable API";
149
  case REFTABLE_ZLIB_ERROR:
150
    return "zlib failure";
151
  case REFTABLE_NAME_CONFLICT:
152
    return "file/directory conflict";
153
  case REFTABLE_REFNAME_ERROR:
154
    return "invalid refname";
155
  case -1:
156
    return "general error";
157
  default:
158
    snprintf(buf, sizeof(buf), "unknown error code %d", err);
159
    return buf;
160
  }
161
}
162
163
int reftable_error_to_errno(int err)
164
{
165
  switch (err) {
166
  case REFTABLE_IO_ERROR:
167
    return EIO;
168
  case REFTABLE_FORMAT_ERROR:
169
    return EFAULT;
170
  case REFTABLE_NOT_EXIST_ERROR:
171
    return ENOENT;
172
  case REFTABLE_LOCK_ERROR:
173
    return EBUSY;
174
  case REFTABLE_API_ERROR:
175
    return EINVAL;
176
  case REFTABLE_ZLIB_ERROR:
177
    return EDOM;
178
  default:
179
    return ERANGE;
180
  }
181
}
182
183
void *(*reftable_malloc_ptr)(size_t sz) = &malloc;
184
void *(*reftable_realloc_ptr)(void *, size_t) = &realloc;
185
void (*reftable_free_ptr)(void *) = &free;
186
187
void *reftable_malloc(size_t sz)
188
17.8k
{
189
17.8k
  return (*reftable_malloc_ptr)(sz);
190
17.8k
}
191
192
void *reftable_realloc(void *p, size_t sz)
193
56.6k
{
194
56.6k
  return (*reftable_realloc_ptr)(p, sz);
195
56.6k
}
196
197
void reftable_free(void *p)
198
77.0k
{
199
77.0k
  reftable_free_ptr(p);
200
77.0k
}
201
202
void *reftable_calloc(size_t sz)
203
8.26k
{
204
8.26k
  void *p = reftable_malloc(sz);
205
8.26k
  memset(p, 0, sz);
206
8.26k
  return p;
207
8.26k
}
208
209
void reftable_set_alloc(void *(*malloc)(size_t),
210
      void *(*realloc)(void *, size_t), void (*free)(void *))
211
0
{
212
0
  reftable_malloc_ptr = malloc;
213
0
  reftable_realloc_ptr = realloc;
214
0
  reftable_free_ptr = free;
215
0
}