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

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