jabberd2  2.2.16
util/hex.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
00004  *                         Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 /* simple hex conversion functions */
00022 
00023 #include "util.h"
00024 
00026 void hex_from_raw(char *in, int inlen, char *out) {
00027     int i, h, l;
00028 
00029     for(i = 0; i < inlen; i++) {
00030         h = in[i] & 0xf0;
00031         h >>= 4;
00032         l = in[i] & 0x0f;
00033         out[i * 2] = (h >= 0x0 && h <= 0x9) ? (h + 0x30) : (h + 0x57);
00034         out[i * 2 + 1] = (l >= 0x0 && l <= 0x9) ? (l + 0x30) : (l + 0x57);
00035     }
00036     out[i * 2] = '\0';
00037 }
00038 
00040 int hex_to_raw(char *in, int inlen, char *out) {
00041     int i, o, h, l;
00042 
00043     /* need +ve even input */
00044     if(inlen == 0 || (inlen / 2 * 2) != inlen)
00045         return 1;
00046 
00047     for(i = o = 0; i < inlen; i += 2, o++) {
00048         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;
00049         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;
00050 
00051         if(h < 0 || l < 0)
00052             return 1;
00053 
00054         out[o] = (h << 4) + l;
00055     }
00056 
00057     return 0;
00058 }