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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <ipxe/base64.h>
  30. /** @file
  31. *
  32. * Base64 encoding
  33. *
  34. */
  35. static const char base64[64] =
  36. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  37. /**
  38. * Base64-encode data
  39. *
  40. * @v raw Raw data
  41. * @v len Length of raw data
  42. * @v encoded Buffer for encoded string
  43. *
  44. * The buffer must be the correct length for the encoded string. Use
  45. * something like
  46. *
  47. * char buf[ base64_encoded_len ( len ) + 1 ];
  48. *
  49. * (the +1 is for the terminating NUL) to provide a buffer of the
  50. * correct size.
  51. */
  52. void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
  53. const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
  54. uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
  55. size_t raw_bit_len = ( 8 * len );
  56. unsigned int bit;
  57. unsigned int byte;
  58. unsigned int shift;
  59. unsigned int tmp;
  60. for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
  61. byte = ( bit / 8 );
  62. shift = ( bit % 8 );
  63. tmp = ( raw_bytes[byte] << shift );
  64. if ( ( byte + 1 ) < len )
  65. tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
  66. tmp = ( ( tmp >> 2 ) & 0x3f );
  67. *(encoded_bytes++) = base64[tmp];
  68. }
  69. for ( ; ( bit % 8 ) != 0 ; bit += 6 )
  70. *(encoded_bytes++) = '=';
  71. *(encoded_bytes++) = '\0';
  72. DBG ( "Base64-encoded to \"%s\":\n", encoded );
  73. DBG_HDA ( 0, raw, len );
  74. assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
  75. }
  76. /**
  77. * Base64-decode string
  78. *
  79. * @v encoded Encoded string
  80. * @v raw Raw data
  81. * @ret len Length of raw data, or negative error
  82. *
  83. * The buffer must be large enough to contain the decoded data. Use
  84. * something like
  85. *
  86. * char buf[ base64_decoded_max_len ( encoded ) ];
  87. *
  88. * to provide a buffer of the correct size.
  89. */
  90. int base64_decode ( const char *encoded, uint8_t *raw ) {
  91. const uint8_t *encoded_bytes = ( ( const uint8_t * ) encoded );
  92. uint8_t *raw_bytes = ( ( uint8_t * ) raw );
  93. uint8_t encoded_byte;
  94. char *match;
  95. int decoded;
  96. unsigned int bit = 0;
  97. unsigned int pad_count = 0;
  98. size_t len;
  99. /* Zero the raw data */
  100. memset ( raw, 0, base64_decoded_max_len ( encoded ) );
  101. /* Decode string */
  102. while ( ( encoded_byte = *(encoded_bytes++) ) ) {
  103. /* Ignore whitespace characters */
  104. if ( isspace ( encoded_byte ) )
  105. continue;
  106. /* Process pad characters */
  107. if ( encoded_byte == '=' ) {
  108. if ( pad_count >= 2 ) {
  109. DBG ( "Base64-encoded string \"%s\" has too "
  110. "many pad characters\n", encoded );
  111. return -EINVAL;
  112. }
  113. pad_count++;
  114. bit -= 2; /* unused_bits = ( 2 * pad_count ) */
  115. continue;
  116. }
  117. if ( pad_count ) {
  118. DBG ( "Base64-encoded string \"%s\" has invalid pad "
  119. "sequence\n", encoded );
  120. return -EINVAL;
  121. }
  122. /* Process normal characters */
  123. match = strchr ( base64, encoded_byte );
  124. if ( ! match ) {
  125. DBG ( "Base64-encoded string \"%s\" contains invalid "
  126. "character '%c'\n", encoded, encoded_byte );
  127. return -EINVAL;
  128. }
  129. decoded = ( match - base64 );
  130. /* Add to raw data */
  131. decoded <<= 2;
  132. raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
  133. raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
  134. bit += 6;
  135. }
  136. /* Check that we decoded a whole number of bytes */
  137. if ( ( bit % 8 ) != 0 ) {
  138. DBG ( "Base64-encoded string \"%s\" has invalid bit length "
  139. "%d\n", encoded, bit );
  140. return -EINVAL;
  141. }
  142. len = ( bit / 8 );
  143. DBG ( "Base64-decoded \"%s\" to:\n", encoded );
  144. DBG_HDA ( 0, raw, len );
  145. assert ( len <= base64_decoded_max_len ( encoded ) );
  146. /* Return length in bytes */
  147. return ( len );
  148. }