File: util.h

package info (click to toggle)
ocaml-sha 1.15.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: ansic: 1,090; ml: 568; makefile: 16
file content (35 lines) | stat: -rw-r--r-- 654 bytes parent folder | download | duplicates (6)
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
#ifndef UTIL_H
#define UTIL_H

static int hex_to_int(char c)
{
    if ('0' <= c && c <= '9')
        return c - '0';
    else if ('a' <= c && c <= 'f')
        return c - 'a' + 10;
    else if ('A' <= c && c <= 'F')
        return c - 'A' + 10;
    else
        return -1;
}

static int of_hex(unsigned char *dst, const char *src, int n)
{
    int i;

    if (n % 2 != 0)
        return -1;
    for (i = 0; i < n/2; i++) {
        int a, b;
        a = hex_to_int(src[i*2]);
        if(a < 0)
            return -1;
        b = hex_to_int(src[i*2 + 1]);
        if(b < 0)
            return -1;
        dst[i] = a*16 + b;
    }
    return n/2;
}

#endif