From: fredrik Date: Sun, 17 Jun 2007 18:01:13 +0000 (+0000) Subject: Better hexdecode. X-Git-Tag: 0.4~7 X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=b9707562a7a426204bda9a32843eb3677230c91d;p=doldaconnect.git Better hexdecode. git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/doldaconnect@1079 959494ce-11ee-0310-bf91-de5d638817bd --- diff --git a/common/utils.c b/common/utils.c index 6dac3dc..d7ae752 100644 --- a/common/utils.c +++ b/common/utils.c @@ -475,41 +475,39 @@ char *hexencode(char *data, size_t datalen) char *hexdecode(char *data, size_t *len) { - char *buf, this; + char *buf, this, bit; size_t bufsize, bufdata; buf = NULL; bufsize = bufdata = 0; - for(; *data; data++) + for(bit = 4, this = 0; *data; data++) { if((*data >= 'A') && (*data <= 'F')) { - this = (this & 0x0F) | ((*data - 'A' + 10) << 4); + this |= (this & 0x0F) | ((*data - 'A' + 10) << bit); + } else if((*data >= 'a') && (*data <= 'f')) { + this |= (this & 0x0F) | ((*data - 'a' + 10) << bit); } else if((*data >= '0') && (*data <= '9')) { - this = (this & 0x0F) | ((*data - '0') << 4); + this |= (this & 0x0F) | ((*data - '0') << bit); + } else if(*data == '\n') { + continue; } else { if(buf != NULL) free(buf); return(NULL); } - data++; - if(!*data) - { - if(buf != NULL) - free(buf); - return(NULL); - } - if((*data >= 'A') && (*data <= 'F')) - { - this = (this & 0xF0) | (*data - 'A' + 10); - } else if((*data >= '0') && (*data <= '9')) { - this = (this & 0xF0) | (*data - '0'); + if(bit == 4) { + bit = 0; } else { - if(buf != NULL) - free(buf); - return(NULL); + bit = 4; + addtobuf(buf, this); + this = 0; } - addtobuf(buf, this); + } + if(bit != 4) { + if(buf != NULL) + free(buf); + return(NULL); } addtobuf(buf, 0); if(len != NULL)