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

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