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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 raw_len Length of raw data
  42. * @v data Buffer
  43. * @v len Length of buffer
  44. * @ret len Encoded length
  45. */
  46. size_t base64_encode ( const void *raw, size_t raw_len, char *data,
  47. size_t len ) {
  48. const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
  49. size_t raw_bit_len = ( 8 * raw_len );
  50. size_t used = 0;
  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, used++ ) {
  56. byte = ( bit / 8 );
  57. shift = ( bit % 8 );
  58. tmp = ( raw_bytes[byte] << shift );
  59. if ( ( byte + 1 ) < raw_len )
  60. tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
  61. tmp = ( ( tmp >> 2 ) & 0x3f );
  62. if ( used < len )
  63. data[used] = base64[tmp];
  64. }
  65. for ( ; ( bit % 8 ) != 0 ; bit += 6, used++ ) {
  66. if ( used < len )
  67. data[used] = '=';
  68. }
  69. if ( used < len )
  70. data[used] = '\0';
  71. if ( len )
  72. data[ len - 1 ] = '\0'; /* Ensure terminator exists */
  73. return used;
  74. }
  75. /**
  76. * Base64-decode string
  77. *
  78. * @v encoded Encoded string
  79. * @v data Buffer
  80. * @v len Length of buffer
  81. * @ret len Length of data, or negative error
  82. */
  83. int base64_decode ( const char *encoded, void *data, size_t len ) {
  84. const char *in = encoded;
  85. uint8_t *out = data;
  86. uint8_t in_char;
  87. char *match;
  88. int in_bits;
  89. unsigned int bit = 0;
  90. unsigned int pad_count = 0;
  91. size_t offset;
  92. /* Zero the output buffer */
  93. memset ( data, 0, len );
  94. /* Decode string */
  95. while ( ( in_char = *(in++) ) ) {
  96. /* Ignore whitespace characters */
  97. if ( isspace ( in_char ) )
  98. continue;
  99. /* Process pad characters */
  100. if ( in_char == '=' ) {
  101. if ( pad_count >= 2 ) {
  102. DBG ( "Base64-encoded string \"%s\" has too "
  103. "many pad characters\n", encoded );
  104. return -EINVAL;
  105. }
  106. pad_count++;
  107. bit -= 2; /* unused_bits = ( 2 * pad_count ) */
  108. continue;
  109. }
  110. if ( pad_count ) {
  111. DBG ( "Base64-encoded string \"%s\" has invalid pad "
  112. "sequence\n", encoded );
  113. return -EINVAL;
  114. }
  115. /* Process normal characters */
  116. match = strchr ( base64, in_char );
  117. if ( ! match ) {
  118. DBG ( "Base64-encoded string \"%s\" contains invalid "
  119. "character '%c'\n", encoded, in_char );
  120. return -EINVAL;
  121. }
  122. in_bits = ( match - base64 );
  123. /* Add to raw data */
  124. in_bits <<= 2;
  125. offset = ( bit / 8 );
  126. if ( offset < len )
  127. out[offset] |= ( in_bits >> ( bit % 8 ) );
  128. offset++;
  129. if ( offset < len )
  130. out[offset] |= ( in_bits << ( 8 - ( bit % 8 ) ) );
  131. bit += 6;
  132. }
  133. /* Check that we decoded a whole number of bytes */
  134. if ( ( bit % 8 ) != 0 ) {
  135. DBG ( "Base64-encoded string \"%s\" has invalid bit length "
  136. "%d\n", encoded, bit );
  137. return -EINVAL;
  138. }
  139. /* Return length in bytes */
  140. return ( bit / 8 );
  141. }