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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. for ( ; remaining-- ; encoded_bytes += 2 ) {
  50. sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
  51. }
  52. DBG ( "Base16-encoded to \"%s\":\n", encoded );
  53. DBG_HDA ( 0, raw, len );
  54. assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
  55. }
  56. /**
  57. * Base16-decode data
  58. *
  59. * @v encoded Encoded string
  60. * @v raw Raw data
  61. * @ret len Length of raw data, or negative error
  62. *
  63. * The buffer must be large enough to contain the decoded data. Use
  64. * something like
  65. *
  66. * char buf[ base16_decoded_max_len ( encoded ) ];
  67. *
  68. * to provide a buffer of the correct size.
  69. */
  70. int base16_decode ( const char *encoded, uint8_t *raw ) {
  71. const char *encoded_bytes = encoded;
  72. uint8_t *raw_bytes = raw;
  73. char buf[3];
  74. char *endp;
  75. size_t len;
  76. while ( encoded_bytes[0] ) {
  77. if ( ! encoded_bytes[1] ) {
  78. DBG ( "Base16-encoded string \"%s\" has invalid "
  79. "length\n", encoded );
  80. return -EINVAL;
  81. }
  82. memcpy ( buf, encoded_bytes, 2 );
  83. buf[2] = '\0';
  84. *(raw_bytes++) = strtoul ( buf, &endp, 16 );
  85. if ( *endp != '\0' ) {
  86. DBG ( "Base16-encoded string \"%s\" has invalid "
  87. "byte \"%s\"\n", encoded, buf );
  88. return -EINVAL;
  89. }
  90. encoded_bytes += 2;
  91. }
  92. len = ( raw_bytes - raw );
  93. DBG ( "Base16-decoded \"%s\" to:\n", encoded );
  94. DBG_HDA ( 0, raw, len );
  95. assert ( len <= base16_decoded_max_len ( encoded ) );
  96. return ( len );
  97. }