|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define MIN(a,b) a<b ? a : b
-
- 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,
- 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
- };
-
- 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','+','/','='
- };
-
- // data stores 24 bits
- struct int24 {
- unsigned int data:24;
- };
-
- 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] ];
-
- oHexArray[i/2]=hex;
- }
- }
-
- 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), "%x", iHexArray[i]);
- }
- }
-
- void read_bytes(const char* hex_buff, 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);
- }
-
- /* -----------------------------------------------------------------------------
- * @brief Converts HEX to Base64
- *
- * @param hex_buf : buffer with hexes to be converted
- * base64_buff : output buffer. has to be \0 initialized
- * hex_buff_len: length of hex_buff
- *
- * @returns number of characters written to base64_buf
- *
- * @remarks remarks
- *
- *------------------------------------------------------------------------------ */
- 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;
-
- 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;
-
- 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(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;
- }
-
-
- /* -----------------------------------------------------------------------------
- * @brief xor_strings
- *
- * @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
- *
- * @remarks remarks
- *
- -------------------------------------------------------------------------------- */
- void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray)
- {
- const size_t aInLen = strlen(iLeftString);
-
- // 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;
-
- 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);
-
- for(size_t i=0; i<aInLen/2; ++i) {
- oXorArray[i] = xor_array1[i] ^ xor_array2[i];
- }
-
- free(xor_array1);
- free(xor_array2);
- }
|