[xmlsec] Binary-Hex addition
Remy Lebeau
gambit47 at yahoo.com
Fri Sep 12 18:39:31 PDT 2003
Attached is the new files for the binary<->hex
conversion functions. Place the binhex.c file in the
\src folder, and the binhex.h file in the
\include\xmlsec folder.
In the case of the MSCrypto engine, whoever is working
on that, just update msx509.c to include binhex.h and
then use xmlSecBinaryToHexString() instead of
CryptBinaryToString().
Enjoy :-) Comments/Suggestions/Criticism welcome.
Gambit
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
-------------- next part --------------
/**
* XML Security Library (http://www.aleksey.com/xmlsec).
*
* Binary <-> Hex formatting/unformatting functions.
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2003 Remy Lebeau <remy at lebeausoftware.org>
*/
#include <xmlsec/binhex.h>
#include <xmlsec/errors.h>
/* table for converting bytes to hex digits */
static const unsigned char HexChars[] = "0123456789ABCDEF";
/* table for converting hex digits back to bytes */
static const int HexLookupTable[] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
/* few macros to simplify the code */
#define xmlSecBinaryHexChar1(b) HexChars[((b) & 0xF0) >> 4]
#define xmlSecBinaryHexChar2(b) HexChars[(b) & 0x0F]
#define xmlSecBinaryMakeByte(c1, c2) ( ((HexLookupTable[(c1)] & 0x0F) << 4) \
| (HexLookupTable[(c2)] & 0x0F) )
#define xmlSecIsHexChar(ch) ( HexLookupTable[(ch)] != -1 )
/* few macros to simplify the code */
/**
* xmlSecBinaryToHexString:
* @inBuf: pointer to the binary data to format
* @inBufSize: specifies the size, in bytes, of @inBuf
* @outBuf: string buffer to accept the formatting
* @outBufSize: specifies the number of characters in @outBuf
*
* Converts a block of binary data into a hex-encoded string.
*
* Returns 0 on success or a negative value if an error occurs.
*/
int
xmlSecBinaryToHexString(const xmlSecByte *inBuf, xmlSecSize inBufSize,
xmlChar *outBuf, int *outBufSize) {
xmlChar *ptr = NULL;
int ret = 0;
xmlSecSize i;
/* make sure that the byte count doubled will still fit inside
an xmlChar string without overflowing 'int' values */
xmlSecAssert2(inBufSize <= 0x3FFFFFFF, -1);
/* if only the size is requested, return it including the null terminator */
if (NULL == outBuf) {
xmlSecAssert2(outBufSize != NULL, -1);
*outBufSize = ((inBufSize*2)+1);
return(0);
}
xmlSecAssert2(outBufSize != NULL, -1);
xmlSecAssert2(*outBufSize >= (inBufSize*2), -1);
/* convert it now */
ptr = outBuf;
for(i = 0; i < inBufSize; ++i) {
*ptr++ = xmlSecBinaryHexChar1(inBuf[i]);
*ptr++ = xmlSecBinaryHexChar2(inBuf[i]);
ret += 2;
}
/* add \0 */
if (ret < *outBufSize) {
*ptr = 0;
}
/* finished */
*outBufSize = ret;
return(0);
}
/**
* xmlSecBinaryFromHexString:
* @inBuf: null-terminated string of formatted hex digits
* @outBuf: pointer to the buffer to receive the binary data
* @outBufSize: specifies the size, in bytes, of @outBuf
*
* Converts a hex-encoded string into a block of binary data.
*
* Returns 0 on success or a negative value if an error occurs.
*/
int
xmlSecBinaryFromHexString(const xmlChar *inBuf, xmlSecByte *outBuf, xmlSecSize *outBufSize) {
return(xmlSecBinaryFromHexStringLen(inBuf, xmlStrlen(inBuf), outBuf, outBufSize));
}
/**
* xmlSecBinaryFromHexStringLen:
* @inBuf: string buffer of formatted hex digits
* @inBufSize: length, in characters, of @inBuf
* @outBuf: pointer to the buffer to receive the binary data
* @outBufSize: specifies the size, in bytes, of @outBuf
*
* Converts a hex-encoded string into a block of binary data.
*
* Returns 0 on success or a negative value if an error occurs.
*/
int
xmlSecBinaryFromHexStringLen(const xmlChar *inBuf, int inBufSize,
xmlSecByte *outBuf, xmlSecSize *outBufSize) {
xmlSecByte *ptr = NULL;
int i, ret = 0;
/* if only the size is requested, return it */
if (NULL == outBuf) {
xmlSecAssert2(outBufSize != NULL, -1);
*outBufSize = (inBufSize/2);
return(0);
}
xmlSecAssert2(inBuf != NULL, -1);
xmlSecAssert2((inBufSize % 2) == 0, -1);
xmlSecAssert2(outBufSize != NULL, -1);
xmlSecAssert2(*outBufSize >= (inBufSize/2), -1);
/* convert it now */
ptr = outBuf;
for(i = 0; i < inBufSize; i += 2) {
xmlSecAssert2(xmlSecIsHexChar(inBuf[i]), -1);
xmlSecAssert2(xmlSecIsHexChar(inBuf[i+1]), -1);
*ptr++ = xmlSecBinaryMakeByte(inBuf[i], inBuf[i+1]);
++ret;
}
/* finished */
*outBufSize = ret;
return(0);
}
-------------- next part --------------
/**
* XML Security Library (http://www.aleksey.com/xmlsec).
*
* Binary <-> Hex formatting/unformatting functions.
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2003 Remy Lebeau <remy at lebeausoftware.org>
*/
#ifndef __XMLSEC_BINHEX_H__
#define __XMLSEC_BINHEX_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <xmlsec/xmlsec.h>
/**
* Standalone routines to do binary/hex conversions
*/
XMLSEC_EXPORT int xmlSecBinaryToHexString(const xmlSecByte *inBuf,
xmlSecSize inBufSize,
xmlChar *outBuf,
int *outBufSize);
XMLSEC_EXPORT int xmlSecBinaryFromHexString(const xmlChar *inBuf,
xmlSecByte *outBuf,
xmlSecSize *outBufSize);
XMLSEC_EXPORT int xmlSecBinaryFromHexStringLen(const xmlChar *inBuf,
int inBufSize,
xmlSecByte *outBuf,
xmlSecSize *outBufSize);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __XMLSEC_BINHEX_H__ */
More information about the xmlsec
mailing list