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

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