File: hexdecode.c

package info (click to toggle)
dq 20251001-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,376 kB
  • sloc: ansic: 13,644; python: 651; makefile: 382; sh: 336
file content (26 lines) | stat: -rw-r--r-- 664 bytes parent folder | download | duplicates (5)
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
#include "hexdecode.h"

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

int hexdecode(unsigned char *y, long long len, const unsigned char *x, long long xlen) {

    int digit0;
    int digit1;

    if (!x) return 0;
    while (len > 0 && xlen >= 2) {
        digit0 = hexdigit(x[0]); if (digit0 == -1) return 0;
        digit1 = hexdigit(x[1]); if (digit1 == -1) return 0;
        *y++ = digit1 + 16 * digit0;
        --len;
        x += 2;
        xlen -= 2;
    }
    if (xlen != 0) return 0;
    return 1;
}