2019-10-27 08:41:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of Open5GS.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "core-config-private.h"
|
|
|
|
|
|
|
|
#if HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
#if HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
#include "ogs-core.h"
|
|
|
|
|
|
|
|
void *ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len)
|
|
|
|
{
|
|
|
|
int i = 0, j = 0, k = 0, hex;
|
|
|
|
uint8_t *out_p = out;
|
|
|
|
|
2019-11-18 11:21:20 +00:00
|
|
|
while(i < in_len && j < out_len) {
|
|
|
|
if (!isspace(in[i])) {
|
2019-10-27 08:41:14 +00:00
|
|
|
hex = isdigit(in[i]) ? in[i] - '0' :
|
|
|
|
islower(in[i]) ? in[i] - 'a' + 10 : in[i] - 'A' + 10;
|
2019-11-18 11:21:20 +00:00
|
|
|
if ((k & 0x1) == 0) {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[j] = (hex << 4);
|
2019-11-18 11:21:20 +00:00
|
|
|
} else {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[j] |= hex;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ogs_hex_to_ascii(void *in, int in_len, void *out, int out_len)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i = 0, l, off = 0;
|
|
|
|
|
|
|
|
p = out;
|
|
|
|
p[0] = 0;
|
|
|
|
|
|
|
|
l = (in_len - off) > out_len ? out_len : in_len - off;
|
2019-11-18 11:21:20 +00:00
|
|
|
for (i = 0; i < l; i++) {
|
2020-06-04 18:12:05 +00:00
|
|
|
p += sprintf(p, "%02x", ((char*)in)[off+i] & 0xff);
|
2019-10-27 08:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ogs_uint64_to_buffer(uint64_t num, int size, void *buffer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t *buffer_p = buffer;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
buffer_p[i] = (num >> ((size-1-i) * 8)) & 0xff;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ogs_buffer_to_uint64(void *buffer, int size)
|
|
|
|
{
|
|
|
|
uint64_t num = 0;
|
|
|
|
uint8_t *buffer_p = buffer;
|
|
|
|
int i;
|
|
|
|
|
2019-11-18 11:21:20 +00:00
|
|
|
for (i = 0; i < size; i++) {
|
2019-10-27 08:41:14 +00:00
|
|
|
num |= (((uint64_t)buffer_p[i]) << ((size-1-i) * 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ogs_bcd_to_buffer(const char *in, void *out, int *out_len)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
uint8_t *out_p = out;
|
|
|
|
int in_len = strlen(in);
|
|
|
|
|
2019-11-18 11:21:20 +00:00
|
|
|
for (i = 0; i < in_len; i++) {
|
2019-10-27 08:41:14 +00:00
|
|
|
if (i & 0x01)
|
|
|
|
out_p[i>>1] = out_p[i>>1] | (((in[i] - 0x30) << 4) & 0xF0);
|
|
|
|
else
|
|
|
|
out_p[i>>1] = (in[i] - 0x30) & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = (in_len + 1) / 2;
|
2019-11-18 11:21:20 +00:00
|
|
|
if (in_len & 0x01) {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[(*out_len)-1] |= 0xF0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-06-27 04:46:06 +00:00
|
|
|
void *ogs_bcd_to_buffer_reverse_order(const char *in, void *out, int *out_len)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
uint8_t *out_p = out;
|
|
|
|
int in_len = strlen(in);
|
|
|
|
|
|
|
|
for (i = 0; i < in_len; i++) {
|
|
|
|
if (i & 0x01)
|
|
|
|
out_p[i>>1] = out_p[i>>1] | ((in[i] - 0x30) & 0x0F);
|
|
|
|
else
|
|
|
|
out_p[i>>1] = ((in[i] - 0x30) << 4) & 0xF0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = (in_len + 1) / 2;
|
|
|
|
if (in_len & 0x01) {
|
|
|
|
out_p[(*out_len)-1] |= 0xF0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-10-27 08:41:14 +00:00
|
|
|
void *ogs_buffer_to_bcd(uint8_t *in, int in_len, void *out)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
uint8_t *out_p = out;
|
|
|
|
|
2019-11-18 11:21:20 +00:00
|
|
|
for (i = 0; i < in_len-1; i++) {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[i*2] = 0x30 + (in[i] & 0x0F);
|
|
|
|
out_p[i*2+1] = 0x30 + ((in[i] & 0xF0) >> 4);
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:21:20 +00:00
|
|
|
if ((in[i] & 0xF0) == 0xF0) {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[i*2] = 0x30 + (in[i] & 0x0F);
|
|
|
|
out_p[i*2+1] = 0;
|
2019-11-18 11:21:20 +00:00
|
|
|
} else {
|
2019-10-27 08:41:14 +00:00
|
|
|
out_p[i*2] = 0x30 + (in[i] & 0x0F);
|
|
|
|
out_p[i*2+1] = 0x30 + ((in[i] & 0xF0) >> 4);
|
|
|
|
out_p[i*2+2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:00:37 +00:00
|
|
|
char ogs_from_hex(char ch)
|
|
|
|
{
|
|
|
|
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
|
|
|
|
}
|
2020-07-09 05:38:09 +00:00
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint24_to_0string(ogs_uint24_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
|
|
|
return ogs_msprintf("%06x", x.v);
|
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint28_to_0string(uint32_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
|
|
|
return ogs_msprintf("%07x", x);
|
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint32_to_0string(uint32_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
|
|
|
return ogs_msprintf("%08x", x);
|
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint36_to_0string(uint64_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
|
|
|
return ogs_msprintf("%09llx", (long long)x);
|
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint64_to_0string(uint64_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
2021-01-01 02:07:08 +00:00
|
|
|
return ogs_msprintf("%016llx", (long long)x);
|
2020-07-09 05:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
char *ogs_uint64_to_string(uint64_t x)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
2021-01-01 02:07:08 +00:00
|
|
|
char *str, *p;
|
2020-07-09 05:38:09 +00:00
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
str = ogs_uint64_to_0string(x);
|
2020-07-09 05:38:09 +00:00
|
|
|
ogs_assert(str);
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
p = ogs_left_trimcharacter(str, '0');
|
|
|
|
ogs_assert(p);
|
2020-07-09 05:38:09 +00:00
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
ogs_free(str);
|
|
|
|
return ogs_strdup(p);
|
2020-07-09 05:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
ogs_uint24_t ogs_uint24_from_string(char *str)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
2021-01-01 02:07:08 +00:00
|
|
|
ogs_uint24_t x;
|
2020-07-09 05:38:09 +00:00
|
|
|
|
|
|
|
ogs_assert(str);
|
2021-01-01 02:07:08 +00:00
|
|
|
|
|
|
|
x.v = ogs_uint64_from_string(str);
|
|
|
|
return x;
|
2020-07-09 05:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
uint64_t ogs_uint64_from_string(char *str)
|
2020-07-09 05:38:09 +00:00
|
|
|
{
|
|
|
|
uint64_t x;
|
|
|
|
|
|
|
|
ogs_assert(str);
|
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
if (strlen(str) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
x = strtoll(str, NULL, 16);
|
|
|
|
|
|
|
|
if ((errno == ERANGE && (x == LONG_MAX || x == LONG_MIN)) ||
|
|
|
|
(errno != 0 && x == 0)) {
|
|
|
|
ogs_log_message(OGS_LOG_FATAL, ogs_errno, "strtoll()) failed [%lld]",
|
|
|
|
(long long)x);
|
|
|
|
ogs_assert_if_reached();
|
|
|
|
}
|
2020-07-09 05:38:09 +00:00
|
|
|
|
2021-01-01 02:07:08 +00:00
|
|
|
return x;
|
2020-07-09 05:38:09 +00:00
|
|
|
}
|