aboutsummaryrefslogtreecommitdiffstats
path: root/libopkg/string_util.c
blob: f4fd1657b62e620d5635b599e1ce831bfc3c53a7 (plain)
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
/* vi: set expandtab sw=4 sts=4: */
/* string_util.c - convenience routines for common string operations

   Copyright (C) 2015 Paul Barker

   SPDX-License-Identifier: GPL-2.0-or-later

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, 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.
*/

#include "config.h"

#include "string_util.h"
#include "xfuncs.h"

char *bin_to_hex(const void *bin_data, size_t len)
{
    const unsigned char *src = (const unsigned char *)bin_data;
    char *buf = xmalloc(2 * len + 1);
    int i;

    static const unsigned char bin2hex[16] = {
        '0', '1', '2', '3',
        '4', '5', '6', '7',
        '8', '9', 'a', 'b',
        'c', 'd', 'e', 'f'
    };

    for (i = 0; i < len; i++) {
        buf[i * 2] = bin2hex[src[i] >> 4];
        buf[i * 2 + 1] = bin2hex[src[i] & 0xf];
    }

    buf[len * 2] = '\0';
    return buf;
}