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.

base64.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <ipxe/base64.h>
  26. /** @file
  27. *
  28. * Base64 encoding
  29. *
  30. */
  31. static const char base64[64] =
  32. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  33. /**
  34. * Base64-encode data
  35. *
  36. * @v raw Raw data
  37. * @v len Length of raw data
  38. * @v encoded Buffer for encoded string
  39. *
  40. * The buffer must be the correct length for the encoded string. Use
  41. * something like
  42. *
  43. * char buf[ base64_encoded_len ( len ) + 1 ];
  44. *
  45. * (the +1 is for the terminating NUL) to provide a buffer of the
  46. * correct size.
  47. */
  48. void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
  49. const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
  50. uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
  51. size_t raw_bit_len = ( 8 * len );
  52. unsigned int bit;
  53. unsigned int byte;
  54. unsigned int shift;
  55. unsigned int tmp;
  56. for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
  57. byte = ( bit / 8 );
  58. shift = ( bit % 8 );
  59. tmp = ( raw_bytes[byte] << shift );
  60. if ( ( byte + 1 ) < len )
  61. tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
  62. tmp = ( ( tmp >> 2 ) & 0x3f );
  63. *(encoded_bytes++) = base64[tmp];
  64. }
  65. for ( ; ( bit % 8 ) != 0 ; bit += 6 )
  66. *(encoded_bytes++) = '=';
  67. *(encoded_bytes++) = '\0';
  68. DBG ( "Base64-encoded to \"%s\":\n", encoded );
  69. DBG_HDA ( 0, raw, len );
  70. assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
  71. }
  72. /**
  73. * Base64-decode string
  74. *
  75. * @v encoded Encoded string
  76. * @v raw Raw data
  77. * @ret len Length of raw data, or negative error
  78. *
  79. * The buffer must be large enough to contain the decoded data. Use
  80. * something like
  81. *
  82. * char buf[ base64_decoded_max_len ( encoded ) ];
  83. *
  84. * to provide a buffer of the correct size.
  85. */
  86. int base64_decode ( const char *encoded, uint8_t *raw ) {
  87. const uint8_t *encoded_bytes = ( ( const uint8_t * ) encoded );
  88. uint8_t *raw_bytes = ( ( uint8_t * ) raw );
  89. uint8_t encoded_byte;
  90. char *match;
  91. int decoded;
  92. unsigned int bit = 0;
  93. unsigned int pad_count = 0;
  94. size_t len;
  95. /* Zero the raw data */
  96. memset ( raw, 0, base64_decoded_max_len ( encoded ) );
  97. /* Decode string */
  98. while ( ( encoded_byte = *(encoded_bytes++) ) ) {
  99. /* Ignore whitespace characters */
  100. if ( isspace ( encoded_byte ) )
  101. continue;
  102. /* Process pad characters */
  103. if ( encoded_byte == '=' ) {
  104. if ( pad_count >= 2 ) {
  105. DBG ( "Base64-encoded string \"%s\" has too "
  106. "many pad characters\n", encoded );
  107. return -EINVAL;
  108. }
  109. pad_count++;
  110. bit -= 2; /* unused_bits = ( 2 * pad_count ) */
  111. continue;
  112. }
  113. if ( pad_count ) {
  114. DBG ( "Base64-encoded string \"%s\" has invalid pad "
  115. "sequence\n", encoded );
  116. return -EINVAL;
  117. }
  118. /* Process normal characters */
  119. match = strchr ( base64, encoded_byte );
  120. if ( ! match ) {
  121. DBG ( "Base64-encoded string \"%s\" contains invalid "
  122. "character '%c'\n", encoded, encoded_byte );
  123. return -EINVAL;
  124. }
  125. decoded = ( match - base64 );
  126. /* Add to raw data */
  127. decoded <<= 2;
  128. raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
  129. raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
  130. bit += 6;
  131. }
  132. /* Check that we decoded a whole number of bytes */
  133. if ( ( bit % 8 ) != 0 ) {
  134. DBG ( "Base64-encoded string \"%s\" has invalid bit length "
  135. "%d\n", encoded, bit );
  136. return -EINVAL;
  137. }
  138. len = ( bit / 8 );
  139. DBG ( "Base64-decoded \"%s\" to:\n", encoded );
  140. DBG_HDA ( 0, raw, len );
  141. assert ( len <= base64_decoded_max_len ( encoded ) );
  142. /* Return length in bytes */
  143. return ( len );
  144. }