hexmap.h 4 KB
/**
 * \file
 *
 * \author	Georg Hopp
 *
 * \copyright
 * Copyright © 2012  Georg Hopp
 *
 * 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 3 of the License, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __TR_HEXMAP_H__
#define __TR_HEXMAP_H__

/**
 * use the same trick as in the core utils to map characters
 * to values in a charset save manner.
 */
#define HEX(_)                       \
	((_) == '0' ? 0                  \
	 : (_) == '1' ? 1                \
	 : (_) == '2' ? 2                \
	 : (_) == '3' ? 3                \
	 : (_) == '4' ? 4                \
	 : (_) == '5' ? 5                \
	 : (_) == '6' ? 6                \
	 : (_) == '7' ? 7                \
	 : (_) == '8' ? 8                \
	 : (_) == '9' ? 9                \
	 : (_) == 'a' || (_) == 'A' ? 10 \
	 : (_) == 'b' || (_) == 'B' ? 11 \
	 : (_) == 'c' || (_) == 'C' ? 12 \
	 : (_) == 'd' || (_) == 'D' ? 13 \
	 : (_) == 'e' || (_) == 'E' ? 14 \
	 : (_) == 'f' || (_) == 'F' ? 15 \
	 : -1)

static const signed char map[256] = {
	HEX(0),  HEX(1),  HEX(2),  HEX(3),  HEX(4),  HEX(5),  HEX(6),  HEX(7),
	HEX(8),  HEX(9),  HEX(10), HEX(11), HEX(12), HEX(13), HEX(14), HEX(15),
	HEX(16), HEX(17), HEX(18), HEX(19), HEX(20), HEX(21), HEX(22), HEX(23),
	HEX(24), HEX(25), HEX(26), HEX(27), HEX(28), HEX(29), HEX(30), HEX(31),
	HEX(32), HEX(33), HEX(34), HEX(35), HEX(36), HEX(37), HEX(38), HEX(39),
	HEX(40), HEX(41), HEX(42), HEX(43), HEX(44), HEX(45), HEX(46), HEX(47),
	HEX(48), HEX(49), HEX(50), HEX(51), HEX(52), HEX(53), HEX(54), HEX(55),
	HEX(56), HEX(57), HEX(58), HEX(59), HEX(60), HEX(61), HEX(62), HEX(63),
	HEX(64), HEX(65), HEX(66), HEX(67), HEX(68), HEX(69), HEX(70), HEX(71),
	HEX(72), HEX(73), HEX(74), HEX(75), HEX(76), HEX(77), HEX(78), HEX(79),
	HEX(80), HEX(81), HEX(82), HEX(83), HEX(84), HEX(85), HEX(86), HEX(87),
	HEX(88), HEX(89), HEX(90), HEX(91), HEX(92), HEX(93), HEX(94), HEX(95),
	HEX(96), HEX(97), HEX(98), HEX(99), HEX(100),HEX(101),HEX(102),HEX(103),
	HEX(104),HEX(105),HEX(106),HEX(107),HEX(108),HEX(109),HEX(110),HEX(111),
	HEX(112),HEX(113),HEX(114),HEX(115),HEX(116),HEX(117),HEX(118),HEX(119),
	HEX(120),HEX(121),HEX(122),HEX(123),HEX(124),HEX(125),HEX(126),HEX(127),
	HEX(128),HEX(129),HEX(130),HEX(131),HEX(132),HEX(133),HEX(134),HEX(135),
	HEX(136),HEX(137),HEX(138),HEX(139),HEX(140),HEX(141),HEX(142),HEX(143),
	HEX(144),HEX(145),HEX(146),HEX(147),HEX(148),HEX(149),HEX(150),HEX(151),
	HEX(152),HEX(153),HEX(154),HEX(155),HEX(156),HEX(157),HEX(158),HEX(159),
	HEX(160),HEX(161),HEX(162),HEX(163),HEX(164),HEX(165),HEX(166),HEX(167),
	HEX(168),HEX(169),HEX(170),HEX(171),HEX(172),HEX(173),HEX(174),HEX(175),
	HEX(176),HEX(177),HEX(178),HEX(179),HEX(180),HEX(181),HEX(182),HEX(183),
	HEX(184),HEX(185),HEX(186),HEX(187),HEX(188),HEX(189),HEX(190),HEX(191),
	HEX(192),HEX(193),HEX(194),HEX(195),HEX(196),HEX(197),HEX(198),HEX(199),
	HEX(200),HEX(201),HEX(202),HEX(203),HEX(204),HEX(205),HEX(206),HEX(207),
	HEX(208),HEX(209),HEX(210),HEX(211),HEX(212),HEX(213),HEX(214),HEX(215),
	HEX(216),HEX(217),HEX(218),HEX(219),HEX(220),HEX(221),HEX(222),HEX(223),
	HEX(224),HEX(225),HEX(226),HEX(227),HEX(228),HEX(229),HEX(230),HEX(231),
	HEX(232),HEX(233),HEX(234),HEX(235),HEX(236),HEX(237),HEX(238),HEX(239),
	HEX(240),HEX(241),HEX(242),HEX(243),HEX(244),HEX(245),HEX(246),HEX(247),
	HEX(248),HEX(249),HEX(250),HEX(251),HEX(252),HEX(253),HEX(254),HEX(255)
};

static const char hexstr[16] = "0123456789abcdef";

#define HEXVAL(x)		((map[(int)((x)[0])] << 4) | map[(int)((x)[1])])

#endif //__TR_HEXMAP_H__

// vim: set ft=c ts=4 sw=4: