jabberd2  2.3.3
hex.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 /* simple hex conversion functions */
22 
23 #include "util.h"
24 
26 void hex_from_raw(const unsigned char *in, int inlen, char *out) {
27  int i, h, l;
28 
29  for(i = 0; i < inlen; i++) {
30  h = in[i] & 0xf0;
31  h >>= 4;
32  l = in[i] & 0x0f;
33  out[i * 2] = (h >= 0x0 && h <= 0x9) ? (h + 0x30) : (h + 0x57);
34  out[i * 2 + 1] = (l >= 0x0 && l <= 0x9) ? (l + 0x30) : (l + 0x57);
35  }
36  out[i * 2] = '\0';
37 }
38 
40 int hex_to_raw(const char *in, int inlen, char *out) {
41  int i, o, h, l;
42 
43  /* need +ve even input */
44  if(inlen == 0 || (inlen / 2 * 2) != inlen)
45  return 1;
46 
47  for(i = o = 0; i < inlen; i += 2, o++) {
48  h = (in[i] >= 0x30 && in[i] <= 0x39) ? (in[i] - 0x30) : (in[i] >= 0x41 && in[i] <= 0x64) ? (in[i] - 0x36) : (in[i] >= 0x61 && in[i] <= 0x66) ? (in[i] - 0x56) : -1;
49  l = (in[i + 1] >= 0x30 && in[i + 1] <= 0x39) ? (in[i + 1] - 0x30) : (in[i + 1] >= 0x41 && in[i + 1] <= 0x64) ? (in[i + 1] - 0x36) : (in[i + 1] >= 0x61 && in[i + 1] <= 0x66) ? (in[i + 1] - 0x56) : -1;
50 
51  if(h < 0 || l < 0)
52  return 1;
53 
54  out[o] = (h << 4) + l;
55  }
56 
57  return 0;
58 }
int hex_to_raw(const char *in, int inlen, char *out)
turn hex into raw - out must be (inlen/2)
Definition: hex.c:40
void hex_from_raw(const unsigned char *in, int inlen, char *out)
turn raw into hex - out must be (inlen*2)+1
Definition: hex.c:26