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.

base16.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2010 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 <stdio.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ipxe/string.h>
  29. #include <ipxe/base16.h>
  30. /** @file
  31. *
  32. * Base16 encoding
  33. *
  34. */
  35. /**
  36. * Base16-encode data
  37. *
  38. * @v raw Raw data
  39. * @v len Length of raw data
  40. * @v encoded Buffer for encoded string
  41. *
  42. * The buffer must be the correct length for the encoded string. Use
  43. * something like
  44. *
  45. * char buf[ base16_encoded_len ( len ) + 1 ];
  46. *
  47. * (the +1 is for the terminating NUL) to provide a buffer of the
  48. * correct size.
  49. */
  50. void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) {
  51. const uint8_t *raw_bytes = raw;
  52. char *encoded_bytes = encoded;
  53. size_t remaining = len;
  54. /* Encode each byte */
  55. for ( ; remaining-- ; encoded_bytes += 2 ) {
  56. sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
  57. }
  58. /* Ensure terminating NUL exists even if length was zero */
  59. *encoded_bytes = '\0';
  60. DBG ( "Base16-encoded to \"%s\":\n", encoded );
  61. DBG_HDA ( 0, raw, len );
  62. assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
  63. }
  64. /**
  65. * Decode hexadecimal string
  66. *
  67. * @v encoded Encoded string
  68. * @v separator Byte separator character, or 0 for no separator
  69. * @v data Buffer
  70. * @v len Length of buffer
  71. * @ret len Length of data, or negative error
  72. */
  73. int hex_decode ( const char *encoded, char separator, void *data, size_t len ) {
  74. uint8_t *out = data;
  75. unsigned int count = 0;
  76. unsigned int sixteens;
  77. unsigned int units;
  78. while ( *encoded ) {
  79. /* Check separator, if applicable */
  80. if ( count && separator && ( ( *(encoded++) != separator ) ) )
  81. return -EINVAL;
  82. /* Extract digits. Note that either digit may be NUL,
  83. * which would be interpreted as an invalid value by
  84. * digit_value(); there is therefore no need for an
  85. * explicit end-of-string check.
  86. */
  87. sixteens = digit_value ( *(encoded++) );
  88. if ( sixteens >= 16 )
  89. return -EINVAL;
  90. units = digit_value ( *(encoded++) );
  91. if ( units >= 16 )
  92. return -EINVAL;
  93. /* Store result */
  94. if ( count < len )
  95. out[count] = ( ( sixteens << 4 ) | units );
  96. count++;
  97. }
  98. return count;
  99. }
  100. /**
  101. * Base16-decode data
  102. *
  103. * @v encoded Encoded string
  104. * @v raw Raw data
  105. * @ret len Length of raw data, or negative error
  106. *
  107. * The buffer must be large enough to contain the decoded data. Use
  108. * something like
  109. *
  110. * char buf[ base16_decoded_max_len ( encoded ) ];
  111. *
  112. * to provide a buffer of the correct size.
  113. */
  114. int base16_decode ( const char *encoded, uint8_t *raw ) {
  115. int len;
  116. len = hex_decode ( encoded, 0, raw, -1UL );
  117. if ( len < 0 )
  118. return len;
  119. DBG ( "Base16-decoded \"%s\" to:\n", encoded );
  120. DBG_HDA ( 0, raw, len );
  121. assert ( len <= ( int ) base16_decoded_max_len ( encoded ) );
  122. return len;
  123. }