Coverage Report

Created: 2025-10-12 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rnp/src/lib/utils.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2017-2021 [Ribose Inc](https://www.ribose.com).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
 * POSSIBILITY OF SUCH DAMAGE.
25
 */
26
#ifndef RNP_UTILS_H_
27
#define RNP_UTILS_H_
28
29
#include <stdio.h>
30
#include <limits.h>
31
#include "logging.h"
32
33
/* number of elements in an array */
34
2.41k
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
35
36
/*
37
 * @params
38
 * array:       array of the structures to lookup
39
 * id_field     name of the field to compare against
40
 * ret_field    filed to return
41
 * lookup_value lookup value
42
 * ret          return value
43
 */
44
#define ARRAY_LOOKUP_BY_ID(array, id_field, ret_field, lookup_value, ret) \
45
0
    do {                                                                  \
46
0
        for (size_t i__ = 0; i__ < ARRAY_SIZE(array); i__++) {            \
47
0
            if ((array)[i__].id_field == (lookup_value)) {                \
48
0
                (ret) = (array)[i__].ret_field;                           \
49
0
                break;                                                    \
50
0
            }                                                             \
51
0
        }                                                                 \
52
0
    } while (0)
53
54
/* Portable way to convert bits to bytes */
55
56
0
#define BITS_TO_BYTES(b) (((b) + (CHAR_BIT - 1)) / CHAR_BIT)
57
58
/* Read big-endian 16-bit value from buf */
59
inline uint16_t
60
read_uint16(const uint8_t *buf)
61
0
{
62
0
    return ((uint16_t) buf[0] << 8) | buf[1];
63
0
}
64
65
/* Read big-endian 32-bit value from buf */
66
inline uint32_t
67
read_uint32(const uint8_t *buf)
68
0
{
69
0
    return ((uint32_t) buf[0] << 24) | ((uint32_t) buf[1] << 16) | ((uint32_t) buf[2] << 8) |
70
0
           (uint32_t) buf[3];
71
0
}
72
73
/* Store big-endian 16-bit value val in buf */
74
inline void
75
write_uint16(uint8_t *buf, uint16_t val)
76
0
{
77
0
    buf[0] = val >> 8;
78
0
    buf[1] = val & 0xff;
79
0
}
80
81
/* Store big-endian 32-bit value val in buf */
82
inline void
83
write_uint32(uint8_t *buf, uint32_t val)
84
0
{
85
0
    buf[0] = (uint8_t)(val >> 24) & 0xff;
86
0
    buf[1] = (uint8_t)(val >> 16) & 0xff;
87
0
    buf[2] = (uint8_t)(val >> 8) & 0xff;
88
0
    buf[3] = (uint8_t)(val >> 0) & 0xff;
89
0
}
90
91
inline void
92
write_uint64(uint8_t *buf, uint64_t val)
93
0
{
94
0
    buf[0] = (uint8_t)(val >> 56) & 0xff;
95
0
    buf[1] = (uint8_t)(val >> 48) & 0xff;
96
0
    buf[2] = (uint8_t)(val >> 40) & 0xff;
97
0
    buf[3] = (uint8_t)(val >> 32) & 0xff;
98
0
    buf[4] = (uint8_t)(val >> 24) & 0xff;
99
0
    buf[5] = (uint8_t)(val >> 16) & 0xff;
100
0
    buf[6] = (uint8_t)(val >> 8) & 0xff;
101
0
    buf[7] = (uint8_t)(val >> 0) & 0xff;
102
0
}
103
104
inline char *
105
getenv_logname(void)
106
0
{
107
0
    char *name = getenv("LOGNAME");
108
0
    if (!name) {
109
0
        name = getenv("USER");
110
0
    }
111
0
    return name;
112
0
}
113
114
#endif