You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

190 lines
5.8 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MIN(a,b) a<b ? a : b
  5. unsigned short char_to_hex[256] = {
  6. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  11. // 50
  12. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00,
  13. 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  14. 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c,
  17. 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. // 100: this rest shouldn't be needed
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  34. };
  35. unsigned char int_to_base64[65] = {
  36. 'A','B','C','D','E','F','G','H','I','J',
  37. 'K','L','M','N','O','P','Q','R','S','T',
  38. 'U','V','W','X','Y','Z','a','b','c','d',
  39. 'e','f','g','h','i','j','k','l','m','n',
  40. 'o','p','q','r','s','t','u','v','w','x',
  41. 'y','z','0','1','2','3','4','5','6','7',
  42. '8','9','+','/','='
  43. };
  44. // data stores 24 bits
  45. struct int24 {
  46. unsigned int data:24;
  47. };
  48. void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray)
  49. {
  50. unsigned char hex;
  51. for(unsigned long i=0; i<iArrayLen; i+=2)
  52. {
  53. hex = char_to_hex[ iArray[i ] ] * 16;
  54. hex += char_to_hex[ iArray[i+1] ];
  55. oHexArray[i/2]=hex;
  56. }
  57. }
  58. void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf )
  59. {
  60. for(unsigned i=0; i<iHexArrayLen; i++)
  61. {
  62. sprintf(oStringBuf+(i*2), "%x", iHexArray[i]);
  63. }
  64. }
  65. void read_bytes(const char* hex_buff, int24& o_data, unsigned len=3)
  66. {
  67. unsigned char* tmp = (unsigned char*)malloc(len);
  68. convert_string_to_hex(hex_buff, len*2, tmp);
  69. for(unsigned i=0; i<len; ++i)
  70. {
  71. o_data.data <<= 8;
  72. o_data.data |= tmp[i];
  73. }
  74. free(tmp);
  75. }
  76. /* -----------------------------------------------------------------------------
  77. * @brief Converts HEX to Base64
  78. *
  79. * @param hex_buf : buffer with hexes to be converted
  80. * base64_buff : output buffer. has to be \0 initialized
  81. * hex_buff_len: length of hex_buff
  82. *
  83. * @returns number of characters written to base64_buf
  84. *
  85. * @remarks remarks
  86. *
  87. *------------------------------------------------------------------------------ */
  88. unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len )
  89. {
  90. unsigned j=0;
  91. const unsigned unaligned = hex_buff_len % 6;
  92. unsigned len_aligned = hex_buff_len - unaligned;
  93. int24 data;
  94. unsigned char idx1,idx2,idx3,idx4;
  95. for(unsigned i=0; i<len_aligned; i+=6)
  96. {
  97. data.data = 0;
  98. read_bytes(hex_buff+i, data, 3);
  99. idx1 = 0x3F & (data.data >> 6*3);
  100. idx2 = 0x3F & (data.data >> 6*2);
  101. idx3 = 0x3F & (data.data >> 6);
  102. idx4 = 0x3F & data.data;
  103. base64_buff[j++] = int_to_base64[idx1];
  104. base64_buff[j++] = int_to_base64[idx2];
  105. base64_buff[j++] = int_to_base64[idx3];
  106. base64_buff[j++] = int_to_base64[idx4];
  107. }
  108. if(unaligned)
  109. {
  110. const unsigned rest_bytes = unaligned/2;
  111. data.data = 0;
  112. read_bytes(hex_buff+len_aligned, data, rest_bytes);
  113. if(rest_bytes==1)
  114. {
  115. if( ( data.data & (data.data & 0x3F) ) == data.data)
  116. {
  117. base64_buff[j++] = int_to_base64[ data.data ];
  118. }
  119. else
  120. {
  121. idx1 =(data.data >> 2 ) & 0x3F;
  122. idx2 = data.data & 0x3;
  123. base64_buff[j++] = int_to_base64[ idx1 ];
  124. base64_buff[j++] = int_to_base64[ idx2 ];
  125. }
  126. }
  127. else if(rest_bytes==2)
  128. {
  129. idx1 =(data.data >> 10 ) & 0x3F;
  130. idx2 =(data.data >> 4 ) & 0x3F;
  131. idx3 =(data.data ) & 15;
  132. base64_buff[j++] = int_to_base64[ idx1 ];
  133. base64_buff[j++] = int_to_base64[ idx2 ];
  134. base64_buff[j++] = int_to_base64[ idx3 ];
  135. }
  136. }
  137. return j;
  138. }
  139. /* -----------------------------------------------------------------------------
  140. * @brief xor_strings
  141. *
  142. * @param iLeftString : String of HEX numbers. Length of string needs to be even
  143. * @param iRightString: Second string. Has to be same length as iLeftString
  144. * @param oXorArray : Array will keep result of XOR. Array needs to be pre
  145. * allocated with length at least strlen(iLeftString)/2
  146. *
  147. * @remarks remarks
  148. *
  149. -------------------------------------------------------------------------------- */
  150. void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray)
  151. {
  152. const size_t aInLen = strlen(iLeftString);
  153. // Both arrays need to have same length
  154. if(strlen(iRightString) != aInLen) return;
  155. // Length of both tables needs to be even
  156. if(aInLen % 2 != 0) return;
  157. unsigned char* xor_array1 = (unsigned char*)malloc( aInLen );
  158. unsigned char* xor_array2 = (unsigned char*)malloc( aInLen );
  159. convert_string_to_hex(iLeftString, aInLen, xor_array1);
  160. convert_string_to_hex(iRightString, aInLen, xor_array2);
  161. for(size_t i=0; i<aInLen/2; ++i) {
  162. oXorArray[i] = xor_array1[i] ^ xor_array2[i];
  163. }
  164. free(xor_array1);
  165. free(xor_array2);
  166. }