From 3fa2e6d50393d0acd683a23989a5b49e615adc66 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Wed, 8 Oct 2008 17:26:26 +0200 Subject: piano: Faster hex-encode algo Don't worry about it; you will not notice the speed improvement on modern hardware ;) Same as hex-decode, but inverted and more complex. --- libpiano/src/crypt.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'libpiano') diff --git a/libpiano/src/crypt.c b/libpiano/src/crypt.c index 43edd0c..7e2bb80 100644 --- a/libpiano/src/crypt.c +++ b/libpiano/src/crypt.c @@ -149,7 +149,6 @@ void PianoBytesToInts (const char *strInput, unsigned int **retArrInts, /* we must not read beyond the end of the string, so be a bit * paranoid */ - shift = 24; i = 0; j = 0; while (i < strInputN) { @@ -212,10 +211,17 @@ void PianoEncipherInts (const unsigned int *plainInts, size_t plainIntsN, char *PianoIntsToHexString (const unsigned int *arrInts, size_t arrIntsN) { /* 4 bytes as hex (= 8 chars) */ char *hexStr = calloc (arrIntsN * 4 * 2 + 1, sizeof (*hexStr)); - size_t i; - - for (i = 0; i < arrIntsN; i++) { - snprintf (hexStr+i*4*2, arrIntsN * 4 * 2 + 1, "%08x", arrInts[i]); + size_t i, writePos; + unsigned char *intMap = (unsigned char *) arrInts; + size_t intMapN = arrIntsN * sizeof (*arrInts); + + for (i = 0; i < intMapN; i++) { + /* we need to swap the bytes again */ + writePos = i + (4 - (i % 4) * 2) - 1; + hexStr[writePos*2] = (intMap[i] & 0xf0) < 0xa0 ? (intMap[i] >> 4) + + '0' : (intMap[i] >> 4) + 'a' - 10; + hexStr[writePos*2+1] = (intMap[i] & 0x0f) < 0x0a ? (intMap[i] & 0x0f) + + '0' : (intMap[i] & 0x0f) + 'a' - 10; } return hexStr; } -- cgit v1.2.3