Coverage Report

Created: 2025-10-12 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open5gs/lib/core/ogs-conv.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
3
 *
4
 * This file is part of Open5GS.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
#include "core-config-private.h"
21
22
#if HAVE_CTYPE_H
23
#include <ctype.h>
24
#endif
25
26
#if HAVE_LIMITS_H
27
#include <limits.h>
28
#endif
29
30
#include "ogs-core.h"
31
32
int ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len)
33
0
{
34
0
    int i = 0, j = 0, k = 0, hex;
35
0
    uint8_t *out_p = out;
36
37
0
    while(i < in_len && j < out_len) {
38
0
        if (!isspace(in[i])) {
39
0
            hex = isdigit(in[i]) ? in[i] - '0' :
40
0
                islower(in[i]) ? in[i] - 'a' + 10 : in[i] - 'A' + 10;
41
0
            if ((k & 0x1) == 0) {
42
0
                out_p[j] = (hex << 4);
43
0
            } else {
44
0
                out_p[j] |= hex;
45
0
                j++;
46
0
            }
47
0
            k++;
48
0
        }
49
0
        i++;
50
0
    }
51
52
0
    return j;
53
0
}
54
55
void *ogs_hex_to_ascii(const void *in, int in_len, void *out, int out_len)
56
0
{
57
0
    char *p, *last;
58
0
    int i = 0, l, off = 0;
59
60
0
    p = out;
61
0
    last = p + out_len;
62
0
    p[0] = 0;
63
64
0
    l = (in_len - off) > out_len ? out_len : in_len - off;
65
0
    for (i = 0; i < l; i++) {
66
0
        p = ogs_slprintf(p, last, "%02x", ((char*)in)[off+i] & 0xff);
67
0
    }
68
69
0
    return out;
70
0
}
71
72
void *ogs_uint64_to_buffer(uint64_t num, int size, void *buffer)
73
0
{
74
0
    int i;
75
0
    uint8_t *buffer_p = buffer;
76
0
    for (i = 0; i < size; i++)
77
0
        buffer_p[i] = (num >> ((size-1-i) * 8)) & 0xff;
78
79
0
    return buffer;
80
0
}
81
82
uint64_t ogs_buffer_to_uint64(void *buffer, int size)
83
0
{
84
0
    uint64_t num = 0;
85
0
    uint8_t *buffer_p = buffer;
86
0
    int i;
87
88
0
    for (i = 0; i < size; i++) {
89
0
        num |= (((uint64_t)buffer_p[i]) << ((size-1-i) * 8));
90
0
    }
91
92
0
    return num;
93
0
}
94
95
void *ogs_bcd_to_buffer(const char *in, void *out, int *out_len)
96
0
{
97
0
    int i = 0;
98
0
    uint8_t *out_p = out;
99
0
    int in_len = strlen(in);
100
101
0
    for (i = 0; i < in_len; i++) {
102
0
        if (i & 0x01)
103
0
            out_p[i>>1] = out_p[i>>1] | (((in[i] - 0x30) << 4) & 0xF0);
104
0
        else
105
0
            out_p[i>>1] = (in[i] - 0x30) & 0x0F;
106
0
    }
107
108
0
    *out_len = (in_len + 1) / 2;
109
0
    if (in_len & 0x01) {
110
0
        out_p[(*out_len)-1] |= 0xF0;
111
0
    }
112
113
0
    return out;
114
0
}
115
116
void *ogs_bcd_to_buffer_reverse_order(const char *in, void *out, int *out_len)
117
0
{
118
0
    int i = 0;
119
0
    uint8_t *out_p = out;
120
0
    int in_len = strlen(in);
121
122
0
    for (i = 0; i < in_len; i++) {
123
0
        if (i & 0x01)
124
0
            out_p[i>>1] = out_p[i>>1] | ((in[i] - 0x30) & 0x0F);
125
0
        else
126
0
            out_p[i>>1] = ((in[i] - 0x30) << 4) & 0xF0;
127
0
    }
128
129
0
    *out_len = (in_len + 1) / 2;
130
0
    if (in_len & 0x01) {
131
0
        out_p[(*out_len)-1] |= 0xF0;
132
0
    }
133
134
0
    return out;
135
0
}
136
137
void *ogs_buffer_to_bcd(uint8_t *in, int in_len, void *out)
138
0
{
139
0
    int i = 0;
140
0
    uint8_t *out_p = out;
141
142
0
    for (i = 0; i < in_len-1; i++) {
143
0
        out_p[i*2] = 0x30 + (in[i] & 0x0F);
144
0
        out_p[i*2+1] = 0x30 + ((in[i] & 0xF0) >> 4);
145
0
    }
146
147
0
    if ((in[i] & 0xF0) == 0xF0) {
148
0
        out_p[i*2] = 0x30 + (in[i] & 0x0F);
149
0
        out_p[i*2+1] = 0;
150
0
    } else {
151
0
        out_p[i*2] = 0x30 + (in[i] & 0x0F);
152
0
        out_p[i*2+1] = 0x30 + ((in[i] & 0xF0) >> 4);
153
0
        out_p[i*2+2] = 0;
154
0
    }
155
156
0
    return out;
157
0
}
158
159
char ogs_from_hex(char ch)
160
0
{
161
0
    return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
162
0
}
163
164
char ogs_to_hex(char ch)
165
0
{
166
0
    static char hex[] = "0123456789ABCDEF";
167
0
    return hex[ch & 15];
168
0
}
169
170
char *ogs_uint24_to_0string(ogs_uint24_t x)
171
0
{
172
0
    return ogs_msprintf("%06x", x.v);
173
0
}
174
175
char *ogs_uint28_to_0string(uint32_t x)
176
0
{
177
0
    return ogs_msprintf("%07x", x);
178
0
}
179
180
char *ogs_uint32_to_0string(uint32_t x)
181
0
{
182
0
    return ogs_msprintf("%08x", x);
183
0
}
184
185
char *ogs_uint36_to_0string(uint64_t x)
186
0
{
187
0
    return ogs_msprintf("%09llx", (long long)x);
188
0
}
189
190
char *ogs_uint64_to_0string(uint64_t x)
191
0
{
192
0
    return ogs_msprintf("%016llx", (long long)x);
193
0
}
194
195
char *ogs_uint64_to_string(uint64_t x)
196
0
{
197
0
    char *str, *p, *dup;
198
199
0
    str = ogs_uint64_to_0string(x);
200
0
    if (!str) {
201
0
        ogs_error("ogs_uint64_to_0string[%lld] failed", (long long)x);
202
0
        return NULL;
203
0
    }
204
205
0
    p = ogs_left_trimcharacter(str, '0');
206
0
    if (!p) {
207
0
        ogs_error("ogs_left_trimcharacter[%s] failld", str);
208
0
        return NULL;
209
0
    }
210
211
0
    dup = ogs_strdup(p);
212
0
    ogs_free(str);
213
214
0
    return dup;
215
0
}
216
217
ogs_uint24_t ogs_uint24_from_string(char *str, int base)
218
0
{
219
0
    ogs_uint24_t x;
220
221
0
    ogs_assert(str);
222
223
0
    x.v = ogs_uint64_from_string(str, base);
224
0
    return x;
225
0
}
226
227
uint64_t ogs_uint64_from_string(char *str, int base)
228
0
{
229
0
    uint64_t x;
230
231
0
    ogs_assert(str);
232
233
0
    if (strlen(str) == 0)
234
0
        return 0;
235
236
0
    errno = 0;
237
0
    x = strtoll(str, NULL, base);
238
239
0
    if ((errno == ERANGE && (x == LONG_MAX || x == LONG_MIN)) ||
240
0
            (errno != 0 && x == 0)) {
241
0
        ogs_log_message(OGS_LOG_FATAL, ogs_errno, "strtoll()) failed [%lld]",
242
0
                (long long)x);
243
0
        ogs_assert_if_reached();
244
0
    }
245
246
0
    return x;
247
0
}
248
249
double *ogs_alloc_double(double value)
250
0
{
251
0
    double *mem = (double *)ogs_calloc(1, sizeof(double));
252
0
    if (!mem) {
253
0
        ogs_error("No memory");
254
0
        return NULL;
255
0
    }
256
257
0
    *mem = value;
258
259
0
    return mem;
260
0
}