aboutsummaryrefslogtreecommitdiff
path: root/include/hex_conversion.h
blob: 483cf26680ad0ba979d547c724ed15f48b479819 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* SPDX-License-Identifier: Apache-2.0 */

/* Copyright 2025 Thorsten Töpper
 *
 * vim:ts=4:sw=4:expandtab
 */
#ifndef HEX_CONVERSION_H
#define HEX_CONVERSION_H

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#ifdef DEBUGBUILD
#include "output.h"
#endif

#define ishex_macro(c) ((c>='0' && c <= '9') || (c>='A' && c <= 'F') || (c>='a' && c <= 'f'))

int convert_line(char *s);
int ishex(unsigned char c);
int ishex_string(const char *s, size_t l);
unsigned char *convert_to_binary(char *hex, unsigned char *out);
char *convert_from_binary(unsigned char *bin, size_t l, char *out);

/* short inline functions are fine in header */
inline int convert_line(char *s) {
    size_t i = 0, l = 0;
    if (s == NULL)
        return -1;
    l=strlen(s);
    for (i=0; i<l; i++) {
        s[i] = (char)toupper(s[i]);
        if ((s[i] == '\r' && (l-i<3)) || (s[i] == '\n' && i==l-1)) {
            s[i] = '\0';
            break;
        }
    }
    return 0;
}

inline int ishex(unsigned char c) {
    if ((c>='0' && c <= '9') || (c>='A' && c <= 'F') || (c>='a' && c <= 'f')) {
        return 1;
    }
    return 0;
};

inline int ishex_string(const char *s, size_t l) {
    size_t i = 0;
    if (s == 0)
        return 0;
    if (l == 0)
        l = strlen(s);
    for (; i<l; i++) {
        if ( ! ishex_macro(s[i]) )
            return 0;
    }
    return 1;
}

inline unsigned char *convert_to_binary(char *hex, unsigned char *out) {
    char tmp[3] = {0,0,0};
    size_t length, i;
    if (hex == NULL) return NULL;
    length=strlen(hex);
    if ( (length==0) || (length%2 == 1)) return NULL;
    for (i=0; i<length; i++) {
        if ( ! ishex_macro(hex[i]) ) {
#ifdef DEBUGBUILD
            LOGERR("Incompatible string '%s'\n", hex);
#endif
            return NULL;
        }
    }
    if (out == NULL && ((out = calloc((length/2),sizeof(char))) == NULL)) {
#ifdef DEBUGBUILD
        LOGERR("ERROR: Failed to allocate %lu bytes\n", (length/2));
#endif
        return NULL;
    }
    for (i=0;i<length;i+=2) {
        tmp[0] = hex[i];
        tmp[1] = hex[i+1];
        out[i/2] = (unsigned char) strtol(tmp, NULL, 16);
    }
    return out;
}

/* Use a large buffer and complex method, as with a simple
 * way there regularly were corrupt results with gcc -O2. */
inline char *convert_from_binary(unsigned char *bin, size_t l, char *out) {
    char tmp[24];
    size_t i,pos;
    if (bin == NULL || l == 0) return NULL;
    if (out == NULL && (out = calloc(((l*2)+1),sizeof(char))) == NULL) {
#ifdef DEBUGBUILD
        LOGERR("ERROR: Failed to allocate %lu bytes\n", ((l*2)+1));
#endif
        return NULL;
    }
    for (i=0; i<l; i++) {
        /* Keep in mind this format is not only two characters. */
        sprintf(tmp, "%02X", (unsigned char)bin[i]);
        pos=strlen(tmp);
        out[i*2] = tmp[pos-2];
        out[(i*2)+1] = tmp[pos-1];
    }
    return out;
}

#endif