Coverage Report

Created: 2024-07-09 06:09

/proc/self/cwd/pw_varint/varint_c.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2023 The Pigweed Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
// use this file except in compliance with the License. You may obtain a copy of
5
// the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
// License for the specific language governing permissions and limitations under
13
// the License.
14
15
#include "pw_varint/varint.h"
16
17
#define VARINT_ENCODE_FUNCTION_BODY(bits)                        \
18
179k
  size_t written = 0;                                            \
19
179k
  uint8_t* buffer = (uint8_t*)output;                            \
20
179k
                                                                 \
21
873k
  do {                                                           \
22
873k
    if (written >= output_size_bytes) {                          \
23
0
      return 0u;                                                 \
24
0
    }                                                            \
25
873k
    buffer[written++] = pw_varint_EncodeOneByte##bits(&integer); \
26
873k
  } while (integer != 0u);                                       \
27
179k
                                                                 \
28
179k
  buffer[written - 1] &= 0x7f;                                   \
29
179k
  return written
30
31
size_t pw_varint_Encode32(uint32_t integer,
32
                          void* output,
33
0
                          size_t output_size_bytes) {
34
0
  VARINT_ENCODE_FUNCTION_BODY(32);
35
0
}
36
37
size_t pw_varint_Encode64(uint64_t integer,
38
                          void* output,
39
179k
                          size_t output_size_bytes) {
40
179k
  VARINT_ENCODE_FUNCTION_BODY(64);
41
179k
}
42
43
#define VARINT_DECODE_FUNCTION_BODY(bits)                                     \
44
0
  uint##bits##_t value = 0;                                                   \
45
0
  size_t count = 0;                                                           \
46
0
  const uint8_t* buffer = (const uint8_t*)(input);                            \
47
0
                                                                              \
48
0
  /* Only read to the end of the buffer or largest possible encoded size. */  \
49
0
  const size_t max_count =                                                    \
50
0
      input_size_bytes < PW_VARINT_MAX_INT##bits##_SIZE_BYTES                 \
51
0
          ? input_size_bytes                                                  \
52
0
          : PW_VARINT_MAX_INT##bits##_SIZE_BYTES;                             \
53
0
                                                                              \
54
0
  bool keep_going;                                                            \
55
0
  do {                                                                        \
56
0
    if (count >= max_count) {                                                 \
57
0
      return 0;                                                               \
58
0
    }                                                                         \
59
0
                                                                              \
60
0
    keep_going = pw_varint_DecodeOneByte##bits(buffer[count], count, &value); \
61
0
    count += 1;                                                               \
62
0
  } while (keep_going);                                                       \
63
0
                                                                              \
64
0
  *output = value;                                                            \
65
0
  return count
66
67
size_t pw_varint_Decode32(const void* input,
68
                          size_t input_size_bytes,
69
0
                          uint32_t* output) {
70
0
  VARINT_DECODE_FUNCTION_BODY(32);
71
0
}
72
73
size_t pw_varint_Decode64(const void* input,
74
                          size_t input_size_bytes,
75
0
                          uint64_t* output) {
76
0
  VARINT_DECODE_FUNCTION_BODY(64);
77
0
}