|
@@ -5,35 +5,35 @@ |
|
|
#include "common.h" |
|
|
#include "common.h" |
|
|
|
|
|
|
|
|
static const unsigned short char_to_hex[256] = { |
|
|
static const unsigned short char_to_hex[256] = { |
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
|
|
|
|
|
// 50 |
|
|
|
|
|
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
|
|
|
|
|
0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, |
|
|
|
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
|
|
|
|
|
// 50 |
|
|
|
|
|
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
|
|
|
|
|
0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, |
|
|
0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
|
|
|
// 100: this rest shouldn't be needed |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
|
|
|
|
|
|
|
// 100: this rest shouldn't be needed |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -46,71 +46,71 @@ static const unsigned short char_to_hex[256] = { |
|
|
-------------------------------------------------------------------------------- */ |
|
|
-------------------------------------------------------------------------------- */ |
|
|
int c2b(char T) |
|
|
int c2b(char T) |
|
|
{ |
|
|
{ |
|
|
if(T>96) // a... |
|
|
|
|
|
return T-71; //// 26=97-71 |
|
|
|
|
|
else if(T>64) // A... |
|
|
|
|
|
return T-65; //// 0=65-65 |
|
|
|
|
|
else if(T=='=') |
|
|
|
|
|
return 64; |
|
|
|
|
|
else if(T>=48) // 0... |
|
|
|
|
|
return T+4; //// 52=47+5 |
|
|
|
|
|
else if(T=='/') // / |
|
|
|
|
|
return 63; |
|
|
|
|
|
else if(T=='+') // + |
|
|
|
|
|
return 62; |
|
|
|
|
|
else |
|
|
|
|
|
return 0xFF; |
|
|
|
|
|
|
|
|
if(T>96) // a... |
|
|
|
|
|
return T-71; //// 26=97-71 |
|
|
|
|
|
else if(T>64) // A... |
|
|
|
|
|
return T-65; //// 0=65-65 |
|
|
|
|
|
else if(T=='=') |
|
|
|
|
|
return 64; |
|
|
|
|
|
else if(T>=48) // 0... |
|
|
|
|
|
return T+4; //// 52=47+5 |
|
|
|
|
|
else if(T=='/') // / |
|
|
|
|
|
return 63; |
|
|
|
|
|
else if(T=='+') // + |
|
|
|
|
|
return 62; |
|
|
|
|
|
else |
|
|
|
|
|
return 0xFF; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static const unsigned char int_to_base64[65] = { |
|
|
static const unsigned char int_to_base64[65] = { |
|
|
'A','B','C','D','E','F','G','H','I','J', |
|
|
|
|
|
'K','L','M','N','O','P','Q','R','S','T', |
|
|
|
|
|
'U','V','W','X','Y','Z','a','b','c','d', |
|
|
|
|
|
'e','f','g','h','i','j','k','l','m','n', |
|
|
|
|
|
'o','p','q','r','s','t','u','v','w','x', |
|
|
|
|
|
'y','z','0','1','2','3','4','5','6','7', |
|
|
|
|
|
'8','9','+','/','=' |
|
|
|
|
|
|
|
|
'A','B','C','D','E','F','G','H','I','J', |
|
|
|
|
|
'K','L','M','N','O','P','Q','R','S','T', |
|
|
|
|
|
'U','V','W','X','Y','Z','a','b','c','d', |
|
|
|
|
|
'e','f','g','h','i','j','k','l','m','n', |
|
|
|
|
|
'o','p','q','r','s','t','u','v','w','x', |
|
|
|
|
|
'y','z','0','1','2','3','4','5','6','7', |
|
|
|
|
|
'8','9','+','/','=' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char* oHexArray) |
|
|
void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char* oHexArray) |
|
|
{ |
|
|
{ |
|
|
unsigned char hex; |
|
|
|
|
|
for(unsigned long i=0; i<iArrayLen; i+=2) |
|
|
|
|
|
{ |
|
|
|
|
|
hex = char_to_hex[ iArray[i ] ] * 16; |
|
|
|
|
|
hex += char_to_hex[ iArray[i+1] ]; |
|
|
|
|
|
|
|
|
unsigned char hex; |
|
|
|
|
|
for(unsigned long i=0; i<iArrayLen; i+=2) |
|
|
|
|
|
{ |
|
|
|
|
|
hex = char_to_hex[ iArray[i ] ] * 16; |
|
|
|
|
|
hex += char_to_hex[ iArray[i+1] ]; |
|
|
|
|
|
|
|
|
oHexArray[i/2]=hex; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
oHexArray[i/2]=hex; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char* oStringBuf ) |
|
|
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char* oStringBuf ) |
|
|
{ |
|
|
{ |
|
|
for(unsigned i=0; i<iHexArrayLen; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
sprintf(oStringBuf+(i*2), "%.2x", iHexArray[i]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for(unsigned i=0; i<iHexArrayLen; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
sprintf(oStringBuf+(i*2), "%.2x", iHexArray[i]); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void read_bytes(const char* hex_buff, struct int24& o_data, unsigned len=3) |
|
|
void read_bytes(const char* hex_buff, struct int24& o_data, unsigned len=3) |
|
|
{ |
|
|
{ |
|
|
unsigned char* tmp = (unsigned char*)malloc(len); |
|
|
|
|
|
convert_string_to_hex(hex_buff, len*2, tmp); |
|
|
|
|
|
for(unsigned i=0; i<len; ++i) |
|
|
|
|
|
{ |
|
|
|
|
|
o_data.data <<= 8; |
|
|
|
|
|
o_data.data |= tmp[i]; |
|
|
|
|
|
} |
|
|
|
|
|
free(tmp); |
|
|
|
|
|
|
|
|
unsigned char* tmp = (unsigned char*)malloc(len); |
|
|
|
|
|
convert_string_to_hex(hex_buff, len*2, tmp); |
|
|
|
|
|
for(unsigned i=0; i<len; ++i) |
|
|
|
|
|
{ |
|
|
|
|
|
o_data.data <<= 8; |
|
|
|
|
|
o_data.data |= tmp[i]; |
|
|
|
|
|
} |
|
|
|
|
|
free(tmp); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------- |
|
|
/* ----------------------------------------------------------------------------- |
|
|
* @brief Converts HEX to Base64 |
|
|
* @brief Converts HEX to Base64 |
|
|
* |
|
|
* |
|
|
* @param hex_buf : buffer with hexes to be converted |
|
|
|
|
|
* base64_buff : output buffer. has to be \0 initializedhttps://www.facebook.com/iz.ka.735 |
|
|
|
|
|
* hex_buff_len: length of hex_buff |
|
|
|
|
|
|
|
|
* @param hex_buf : buffer with hexes to be converted |
|
|
|
|
|
* base64_buff : output buffer. has to be \0 initializedhttps://www.facebook.com/iz.ka.735 |
|
|
|
|
|
* hex_buff_len: length of hex_buff |
|
|
* |
|
|
* |
|
|
* @returns number of characters written to base64_buf |
|
|
* @returns number of characters written to base64_buf |
|
|
* |
|
|
* |
|
@@ -119,140 +119,181 @@ void read_bytes(const char* hex_buff, struct int24& o_data, unsigned len=3) |
|
|
*------------------------------------------------------------------------------ */ |
|
|
*------------------------------------------------------------------------------ */ |
|
|
unsigned hex_to_base64(const char* hex_buff, char* base64_buff, unsigned hex_buff_len ) |
|
|
unsigned hex_to_base64(const char* hex_buff, char* base64_buff, unsigned hex_buff_len ) |
|
|
{ |
|
|
{ |
|
|
unsigned j=0; |
|
|
|
|
|
const unsigned unaligned = hex_buff_len % 6; |
|
|
|
|
|
unsigned len_aligned = hex_buff_len - unaligned; |
|
|
|
|
|
|
|
|
unsigned j=0; |
|
|
|
|
|
const unsigned unaligned = hex_buff_len % 6; |
|
|
|
|
|
unsigned len_aligned = hex_buff_len - unaligned; |
|
|
|
|
|
|
|
|
int24 data; |
|
|
|
|
|
unsigned char idx1,idx2,idx3,idx4; |
|
|
|
|
|
for(unsigned i=0; i<len_aligned; i+=6) |
|
|
|
|
|
{ |
|
|
|
|
|
data.data = 0; |
|
|
|
|
|
read_bytes(hex_buff+i, data, 3); |
|
|
|
|
|
|
|
|
int24 data; |
|
|
|
|
|
unsigned char idx1,idx2,idx3,idx4; |
|
|
|
|
|
for(unsigned i=0; i<len_aligned; i+=6) |
|
|
|
|
|
{ |
|
|
|
|
|
data.data = 0; |
|
|
|
|
|
read_bytes(hex_buff+i, data, 3); |
|
|
|
|
|
|
|
|
idx1 = 0x3F & (data.data >> 6*3); |
|
|
|
|
|
idx2 = 0x3F & (data.data >> 6*2); |
|
|
|
|
|
idx3 = 0x3F & (data.data >> 6); |
|
|
|
|
|
idx4 = 0x3F & data.data; |
|
|
|
|
|
|
|
|
idx1 = 0x3F & (data.data >> 6*3); |
|
|
|
|
|
idx2 = 0x3F & (data.data >> 6*2); |
|
|
|
|
|
idx3 = 0x3F & (data.data >> 6); |
|
|
|
|
|
idx4 = 0x3F & data.data; |
|
|
|
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx1]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx2]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx3]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx4]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx1]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx2]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx3]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[idx4]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if(unaligned) |
|
|
|
|
|
{ |
|
|
|
|
|
const unsigned rest_bytes = unaligned/2; |
|
|
|
|
|
data.data = 0; |
|
|
|
|
|
read_bytes(hex_buff+len_aligned, data, rest_bytes); |
|
|
|
|
|
|
|
|
if(unaligned) |
|
|
|
|
|
{ |
|
|
|
|
|
const unsigned rest_bytes = unaligned/2; |
|
|
|
|
|
data.data = 0; |
|
|
|
|
|
read_bytes(hex_buff+len_aligned, data, rest_bytes); |
|
|
|
|
|
|
|
|
if(rest_bytes==1) |
|
|
|
|
|
{ |
|
|
|
|
|
if( ( data.data & (data.data & 0x3F) ) == data.data) |
|
|
|
|
|
{ |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ data.data ]; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
idx1 =(data.data >> 2 ) & 0x3F; |
|
|
|
|
|
idx2 = data.data & 0x3; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx1 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx2 ]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if(rest_bytes==2) |
|
|
|
|
|
{ |
|
|
|
|
|
idx1 =(data.data >> 10 ) & 0x3F; |
|
|
|
|
|
idx2 =(data.data >> 4 ) & 0x3F; |
|
|
|
|
|
idx3 =(data.data ) & 15; |
|
|
|
|
|
|
|
|
if(rest_bytes==1) |
|
|
|
|
|
{ |
|
|
|
|
|
if( ( data.data & (data.data & 0x3F) ) == data.data) |
|
|
|
|
|
{ |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ data.data ]; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
idx1 =(data.data >> 2 ) & 0x3F; |
|
|
|
|
|
idx2 = data.data & 0x3; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx1 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx2 ]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if(rest_bytes==2) |
|
|
|
|
|
{ |
|
|
|
|
|
idx1 =(data.data >> 10 ) & 0x3F; |
|
|
|
|
|
idx2 =(data.data >> 4 ) & 0x3F; |
|
|
|
|
|
idx3 =(data.data ) & 15; |
|
|
|
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx1 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx2 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx3 ]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return j; |
|
|
|
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx1 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx2 ]; |
|
|
|
|
|
base64_buff[j++] = int_to_base64[ idx3 ]; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return (j-1>j) ? 0 : j-1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------- |
|
|
/* ----------------------------------------------------------------------------- |
|
|
* @brief xor_strings |
|
|
* @brief xor_strings |
|
|
* |
|
|
* |
|
|
* @param iLeftString : String of HEX numbers. Length of string needs to be even |
|
|
* @param iLeftString : String of HEX numbers. Length of string needs to be even |
|
|
* @param iRightString: Second string. Has to be same length as iLeftString |
|
|
|
|
|
* @param oXorArray : Array will keep result of XOR. Array needs to be pre |
|
|
|
|
|
* allocated with length at least strlen(iLeftString)/2 |
|
|
|
|
|
|
|
|
* @param iRightString: Second string. Has to be same length as iLeftString |
|
|
|
|
|
* @param oXorArray : Array will keep result of XOR. Array needs to be pre |
|
|
|
|
|
* allocated with length at least strlen(iLeftString)/2 |
|
|
* |
|
|
* |
|
|
* @remarks remarks |
|
|
* @remarks remarks |
|
|
* |
|
|
* |
|
|
-------------------------------------------------------------------------------- */ |
|
|
-------------------------------------------------------------------------------- */ |
|
|
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char* oXorArray) |
|
|
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char* oXorArray) |
|
|
{ |
|
|
{ |
|
|
const size_t aInLen = strlen(iLeftString); |
|
|
|
|
|
|
|
|
const size_t aInLen = strlen(iLeftString); |
|
|
|
|
|
|
|
|
// Both arrays need to have same length |
|
|
|
|
|
if(strlen(iRightString) != aInLen) return; |
|
|
|
|
|
|
|
|
// Both arrays need to have same length |
|
|
|
|
|
if(strlen(iRightString) != aInLen) return; |
|
|
|
|
|
|
|
|
// Length of both tables needs to be even |
|
|
|
|
|
if(aInLen % 2 != 0) return; |
|
|
|
|
|
|
|
|
// Length of both tables needs to be even |
|
|
|
|
|
if(aInLen % 2 != 0) return; |
|
|
|
|
|
|
|
|
unsigned char* xor_array1 = (unsigned char*)malloc( aInLen ); |
|
|
|
|
|
unsigned char* xor_array2 = (unsigned char*)malloc( aInLen ); |
|
|
|
|
|
|
|
|
unsigned char* xor_array1 = (unsigned char*)malloc( aInLen ); |
|
|
|
|
|
unsigned char* xor_array2 = (unsigned char*)malloc( aInLen ); |
|
|
|
|
|
|
|
|
convert_string_to_hex(iLeftString, aInLen, xor_array1); |
|
|
|
|
|
convert_string_to_hex(iRightString, aInLen, xor_array2); |
|
|
|
|
|
|
|
|
convert_string_to_hex(iLeftString, aInLen, xor_array1); |
|
|
|
|
|
convert_string_to_hex(iRightString, aInLen, xor_array2); |
|
|
|
|
|
|
|
|
for(size_t i=0; i<aInLen/2; ++i) { |
|
|
|
|
|
oXorArray[i] = xor_array1[i] ^ xor_array2[i]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
for(size_t i=0; i<aInLen/2; ++i) { |
|
|
|
|
|
oXorArray[i] = xor_array1[i] ^ xor_array2[i]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
free(xor_array1); |
|
|
|
|
|
free(xor_array2); |
|
|
|
|
|
|
|
|
free(xor_array1); |
|
|
|
|
|
free(xor_array2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
struct int24 base64_4char_to_hex(const char (&T)[4]) |
|
|
struct int24 base64_4char_to_hex(const char (&T)[4]) |
|
|
{ |
|
|
{ |
|
|
unsigned char a = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); |
|
|
|
|
|
unsigned char b = ((c2b(T[1]) & 0xF ) << 4) | (c2b(T[2]) >> 2); |
|
|
|
|
|
unsigned char c = ((c2b(T[2]) & 3 ) << 6 ) | (c2b(T[3]) & 0x3F); |
|
|
|
|
|
struct int24 t; |
|
|
|
|
|
t.data = (a<<16) | (b<<8) | c; |
|
|
|
|
|
return t; |
|
|
|
|
|
|
|
|
unsigned char a = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); |
|
|
|
|
|
unsigned char b = ((c2b(T[1]) & 0xF ) << 4) | (c2b(T[2]) >> 2); |
|
|
|
|
|
unsigned char c = ((c2b(T[2]) & 3 ) << 6 ) | (c2b(T[3]) & 0x3F); |
|
|
|
|
|
struct int24 t; |
|
|
|
|
|
t.data = (a<<16) | (b<<8) | c; |
|
|
|
|
|
return t; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------- |
|
|
/* ----------------------------------------------------------------------------- |
|
|
* @brief Converts string encoded in base64 to HEX |
|
|
* @brief Converts string encoded in base64 to HEX |
|
|
* |
|
|
* |
|
|
* @param i_string: Base64 encoded string. |
|
|
* @param i_string: Base64 encoded string. |
|
|
* @param i_string_len: % 4 must == 0 |
|
|
|
|
|
* @param o_hex_array: array to be populated with HEX's. Caller must ensure |
|
|
|
|
|
* that array has required size. Method doesn't do any checks |
|
|
|
|
|
|
|
|
* @param i_string_len: string length |
|
|
|
|
|
* @param o_hex_array: array to be populated with HEX's. Caller must ensure |
|
|
|
|
|
* that array has required size. Method doesn't do any checks |
|
|
* |
|
|
* |
|
|
* @returns -1 on failure, on success possitive value equal to number of bytes encoded |
|
|
* @returns -1 on failure, on success possitive value equal to number of bytes encoded |
|
|
* |
|
|
* |
|
|
-------------------------------------------------------------------------------- */ |
|
|
-------------------------------------------------------------------------------- */ |
|
|
int base64_to_hex( const unsigned char* const i_string, |
|
|
|
|
|
int i_string_len, |
|
|
|
|
|
uint8_t* o_hex_array ) |
|
|
|
|
|
|
|
|
int base64_to_hex( const unsigned char* const i_string, |
|
|
|
|
|
int i_string_len, |
|
|
|
|
|
uint8_t* o_hex_array ) |
|
|
{ |
|
|
{ |
|
|
int pointer = 0; |
|
|
|
|
|
int hex_pointer = 0; |
|
|
|
|
|
char array[4]; |
|
|
|
|
|
|
|
|
int pointer = 0; |
|
|
|
|
|
int hex_pointer = 0; |
|
|
|
|
|
char array[4]; |
|
|
|
|
|
struct int24 tmp; |
|
|
|
|
|
while(pointer<i_string_len) |
|
|
|
|
|
{ |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
tmp = base64_4char_to_hex(array); |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data >> 16; |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data >> 8; |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data; |
|
|
|
|
|
} |
|
|
|
|
|
return hex_pointer; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// OZAPTF: Expects output to be big enough |
|
|
|
|
|
void b64_to_hex( |
|
|
|
|
|
const uint8_t* const input, |
|
|
|
|
|
uint32_t inputLen, |
|
|
|
|
|
uint8_t* output, |
|
|
|
|
|
uint32_t* outputLen) |
|
|
|
|
|
{ |
|
|
|
|
|
uint32_t in_cursor=0; |
|
|
|
|
|
uint32_t out_cursor=0; |
|
|
|
|
|
uint8_t T[4]; |
|
|
|
|
|
|
|
|
struct int24 tmp; |
|
|
|
|
|
while(pointer<i_string_len) |
|
|
|
|
|
{ |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
array[pointer%4] = i_string[pointer]; pointer++; |
|
|
|
|
|
|
|
|
if(inputLen==0) { |
|
|
|
|
|
*outputLen=0; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
tmp = base64_4char_to_hex(array); |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data >> 16; |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data >> 8; |
|
|
|
|
|
o_hex_array[hex_pointer++] = tmp.data; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
while(in_cursor+2 < inputLen) { |
|
|
|
|
|
T[0] = input[in_cursor++]; |
|
|
|
|
|
T[1] = input[in_cursor++]; |
|
|
|
|
|
|
|
|
return hex_pointer; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ( (in_cursor<inputLen) && (input[in_cursor]!='=')) { |
|
|
|
|
|
T[2] = input[in_cursor++]; |
|
|
|
|
|
|
|
|
|
|
|
if ( (in_cursor<inputLen) && (input[in_cursor] !='=')) { |
|
|
|
|
|
T[3] = input[in_cursor++]; |
|
|
|
|
|
output[out_cursor++] = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); |
|
|
|
|
|
output[out_cursor++] = ((c2b(T[1]) & 0x0F ) << 4) | (c2b(T[2]) >> 2); |
|
|
|
|
|
output[out_cursor++] = ((c2b(T[2]) & 0x03 ) << 6 )| (c2b(T[3]) & 0x3F); |
|
|
|
|
|
} else { |
|
|
|
|
|
// decode 3 chars |
|
|
|
|
|
output[out_cursor++] = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); |
|
|
|
|
|
output[out_cursor++] = ((c2b(T[1]) & 0x0F ) << 4) | (c2b(T[2]) >> 2); |
|
|
|
|
|
output[out_cursor++] = ((c2b(T[2]) & 0x03 ) << 6 ); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// decode 2 chars |
|
|
|
|
|
output[out_cursor++] = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); |
|
|
|
|
|
output[out_cursor++] = ((c2b(T[1]) & 0xF ) << 4); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
*outputLen = out_cursor-1; |
|
|
|
|
|
} |