ua_base64.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. begin library headers
  3. */
  4. /*
  5. cdecode.h - c header for a base64 decoding algorithm
  6. This is part of the libb64 project, and has been placed in the public domain.
  7. For details, see http://sourceforge.net/projects/libb64
  8. */
  9. #ifndef BASE64_CDECODE_H
  10. #define BASE64_CDECODE_H
  11. typedef enum {
  12. step_a, step_b, step_c, step_d
  13. } base64_decodestep;
  14. typedef struct {
  15. base64_decodestep step;
  16. char plainchar;
  17. } base64_decodestate;
  18. void base64_init_decodestate(base64_decodestate *state_in);
  19. int base64_decode_value(char value_in);
  20. int base64_decode_block(const char *code_in, const int length_in, char *plaintext_out,
  21. base64_decodestate *state_in);
  22. #endif /* BASE64_CDECODE_H */
  23. /*
  24. end library headers
  25. */
  26. #include "ua_base64.h"
  27. UA_Int32 UA_base64_getDecodedSize(UA_String *const base64EncodedData) {
  28. //note that base64-encoded data length is always divisible by 4
  29. UA_Int32 temp = base64EncodedData->length * 3 / 4;
  30. //subtract padding
  31. if(base64EncodedData->data[base64EncodedData->length-1] == '=') {
  32. temp--;
  33. if(base64EncodedData->data[base64EncodedData->length-2] == '=')
  34. temp--;
  35. }
  36. return temp;
  37. }
  38. UA_Int32 UA_base64_decode(UA_String *const base64EncodedData, UA_Byte *target) {
  39. if(target == UA_NULL)
  40. return UA_ERROR;
  41. base64_decodestate state;
  42. base64_init_decodestate(&state);
  43. base64_decode_block((char *)(base64EncodedData->data), base64EncodedData->length, (char *)target, &state);
  44. return UA_NO_ERROR;
  45. }
  46. /*
  47. begin library code
  48. */
  49. /*
  50. cdecoder.c - c source to a base64 decoding algorithm implementation
  51. This is part of the libb64 project, and has been placed in the public domain.
  52. For details, see http://sourceforge.net/projects/libb64
  53. */
  54. int base64_decode_value(char value_in) {
  55. static const char decoding[] =
  56. { 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3,
  57. 4, 5,
  58. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26,
  59. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
  60. static const char decoding_size = sizeof(decoding);
  61. value_in -= 43;
  62. if(value_in > decoding_size) return -1;
  63. return decoding[(int)value_in];
  64. }
  65. void base64_init_decodestate(base64_decodestate *state_in) {
  66. state_in->step = step_a;
  67. state_in->plainchar = 0;
  68. }
  69. int base64_decode_block(const char *code_in, const int length_in, char *plaintext_out,
  70. base64_decodestate *state_in) {
  71. const char *codechar = code_in;
  72. char *plainchar = plaintext_out;
  73. signed char fragment;
  74. *plainchar = state_in->plainchar;
  75. switch(state_in->step) {
  76. while(1) {
  77. case step_a:
  78. do {
  79. if(codechar == code_in+length_in) {
  80. state_in->step = step_a;
  81. state_in->plainchar = *plainchar;
  82. return plainchar - plaintext_out;
  83. }
  84. fragment = (char)base64_decode_value(*codechar++);
  85. } while(fragment < 0);
  86. *plainchar = (fragment & 0x03f) << 2;
  87. case step_b:
  88. do {
  89. if(codechar == code_in+length_in) {
  90. state_in->step = step_b;
  91. state_in->plainchar = *plainchar;
  92. return plainchar - plaintext_out;
  93. }
  94. fragment = (char)base64_decode_value(*codechar++);
  95. } while(fragment < 0);
  96. *plainchar++ |= (fragment & 0x030) >> 4;
  97. *plainchar = (fragment & 0x00f) << 4;
  98. case step_c:
  99. do {
  100. if(codechar == code_in+length_in) {
  101. state_in->step = step_c;
  102. state_in->plainchar = *plainchar;
  103. return plainchar - plaintext_out;
  104. }
  105. fragment = (char)base64_decode_value(*codechar++);
  106. } while(fragment < 0);
  107. *plainchar++ |= (fragment & 0x03c) >> 2;
  108. *plainchar = (fragment & 0x003) << 6;
  109. case step_d:
  110. do {
  111. if(codechar == code_in+length_in) {
  112. state_in->step = step_d;
  113. state_in->plainchar = *plainchar;
  114. return plainchar - plaintext_out;
  115. }
  116. fragment = (char)base64_decode_value(*codechar++);
  117. } while(fragment < 0);
  118. *plainchar++ |= (fragment & 0x03f);
  119. }
  120. }
  121. /* control should not reach here */
  122. return plainchar - plaintext_out;
  123. }