mariadb/third_party/xz-4.999.9beta/debug/hex2bin.c
Yoni Fogel 6fa092ca83 closes [t:4913]. Modelines now synchronized in every source/header file (and always top two lines)
git-svn-id: file:///svn/toku/tokudb/xz-4.999.9beta@43762 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-17 00:02:29 -04:00

55 lines
1.1 KiB
C

/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: expandtab:ts=8:sw=4:softtabstop=4:
///////////////////////////////////////////////////////////////////////////////
//
/// \file hex2bin.c
/// \brief Converts hexadecimal input strings to binary
//
// Author: Lasse Collin
//
// This file has been put into the public domain.
// You can do whatever you want with this file.
//
///////////////////////////////////////////////////////////////////////////////
#include "sysdefs.h"
#include <stdio.h>
#include <ctype.h>
static int
getbin(int x)
{
if (x >= '0' && x <= '9')
return x - '0';
if (x >= 'A' && x <= 'F')
return x - 'A' + 10;
return x - 'a' + 10;
}
int
main(void)
{
while (true) {
int byte = getchar();
if (byte == EOF)
return 0;
if (!isxdigit(byte))
continue;
const int digit = getchar();
if (digit == EOF || !isxdigit(digit)) {
fprintf(stderr, "Invalid input\n");
return 1;
}
byte = (getbin(byte) << 4) | getbin(digit);
if (putchar(byte) == EOF) {
perror(NULL);
return 1;
}
}
}