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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 tmp;
  53. for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
  54. tmp = ( ( raw_bytes[ bit / 8 ] << ( bit % 8 ) ) |
  55. ( raw_bytes[ bit / 8 + 1 ] >> ( 8 - ( bit % 8 ) ) ) );
  56. tmp = ( ( tmp >> 2 ) & 0x3f );
  57. *(encoded_bytes++) = base64[tmp];
  58. }
  59. for ( ; ( bit % 8 ) != 0 ; bit += 6 )
  60. *(encoded_bytes++) = '=';
  61. *(encoded_bytes++) = '\0';
  62. DBG ( "Base64-encoded to \"%s\":\n", encoded );
  63. DBG_HDA ( 0, raw, len );
  64. assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
  65. }
  66. /**
  67. * Base64-decode string
  68. *
  69. * @v encoded Encoded string
  70. * @v raw Raw data
  71. * @ret len Length of raw data, or negative error
  72. *
  73. * The buffer must be large enough to contain the decoded data. Use
  74. * something like
  75. *
  76. * char buf[ base64_decoded_max_len ( encoded ) ];
  77. *
  78. * to provide a buffer of the correct size.
  79. */
  80. int base64_decode ( const char *encoded, uint8_t *raw ) {
  81. const uint8_t *encoded_bytes = ( ( const uint8_t * ) encoded );
  82. uint8_t *raw_bytes = ( ( uint8_t * ) raw );
  83. uint8_t encoded_byte;
  84. char *match;
  85. int decoded;
  86. unsigned int bit = 0;
  87. unsigned int pad_count = 0;
  88. size_t len;
  89. /* Zero the raw data */
  90. memset ( raw, 0, base64_decoded_max_len ( encoded ) );
  91. /* Decode string */
  92. while ( ( encoded_byte = *(encoded_bytes++) ) ) {
  93. /* Ignore whitespace characters */
  94. if ( isspace ( encoded_byte ) )
  95. continue;
  96. /* Process pad characters */
  97. if ( encoded_byte == '=' ) {
  98. if ( pad_count >= 2 ) {
  99. DBG ( "Base64-encoded string \"%s\" has too "
  100. "many pad characters\n", encoded );
  101. return -EINVAL;
  102. }
  103. pad_count++;
  104. bit -= 2; /* unused_bits = ( 2 * pad_count ) */
  105. continue;
  106. }
  107. if ( pad_count ) {
  108. DBG ( "Base64-encoded string \"%s\" has invalid pad "
  109. "sequence\n", encoded );
  110. return -EINVAL;
  111. }
  112. /* Process normal characters */
  113. match = strchr ( base64, encoded_byte );
  114. if ( ! match ) {
  115. DBG ( "Base64-encoded string \"%s\" contains invalid "
  116. "character '%c'\n", encoded, encoded_byte );
  117. return -EINVAL;
  118. }
  119. decoded = ( match - base64 );
  120. /* Add to raw data */
  121. decoded <<= 2;
  122. raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
  123. raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
  124. bit += 6;
  125. }
  126. /* Check that we decoded a whole number of bytes */
  127. if ( ( bit % 8 ) != 0 ) {
  128. DBG ( "Base64-encoded string \"%s\" has invalid bit length "
  129. "%d\n", encoded, bit );
  130. return -EINVAL;
  131. }
  132. len = ( bit / 8 );
  133. DBG ( "Base64-decoded \"%s\" to:\n", encoded );
  134. DBG_HDA ( 0, raw, len );
  135. assert ( len <= base64_decoded_max_len ( encoded ) );
  136. /* Return length in bytes */
  137. return ( len );
  138. }