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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <ipxe/base16.h>
  24. /** @file
  25. *
  26. * Base16 encoding
  27. *
  28. */
  29. /**
  30. * Base16-encode data
  31. *
  32. * @v raw Raw data
  33. * @v len Length of raw data
  34. * @v encoded Buffer for encoded string
  35. *
  36. * The buffer must be the correct length for the encoded string. Use
  37. * something like
  38. *
  39. * char buf[ base16_encoded_len ( len ) + 1 ];
  40. *
  41. * (the +1 is for the terminating NUL) to provide a buffer of the
  42. * correct size.
  43. */
  44. void base16_encode ( const uint8_t *raw, size_t len, char *encoded ) {
  45. const uint8_t *raw_bytes = raw;
  46. char *encoded_bytes = encoded;
  47. size_t remaining = len;
  48. for ( ; remaining-- ; encoded_bytes += 2 ) {
  49. sprintf ( encoded_bytes, "%02x", *(raw_bytes++) );
  50. }
  51. DBG ( "Base16-encoded to \"%s\":\n", encoded );
  52. DBG_HDA ( 0, raw, len );
  53. assert ( strlen ( encoded ) == base16_encoded_len ( len ) );
  54. }
  55. /**
  56. * Base16-decode data
  57. *
  58. * @v encoded Encoded string
  59. * @v raw Raw data
  60. * @ret len Length of raw data, or negative error
  61. *
  62. * The buffer must be large enough to contain the decoded data. Use
  63. * something like
  64. *
  65. * char buf[ base16_decoded_max_len ( encoded ) ];
  66. *
  67. * to provide a buffer of the correct size.
  68. */
  69. int base16_decode ( const char *encoded, uint8_t *raw ) {
  70. const char *encoded_bytes = encoded;
  71. uint8_t *raw_bytes = raw;
  72. char buf[3];
  73. char *endp;
  74. size_t len;
  75. while ( encoded_bytes[0] ) {
  76. if ( ! encoded_bytes[1] ) {
  77. DBG ( "Base16-encoded string \"%s\" has invalid "
  78. "length\n", encoded );
  79. return -EINVAL;
  80. }
  81. memcpy ( buf, encoded_bytes, 2 );
  82. buf[2] = '\0';
  83. *(raw_bytes++) = strtoul ( buf, &endp, 16 );
  84. if ( *endp != '\0' ) {
  85. DBG ( "Base16-encoded string \"%s\" has invalid "
  86. "byte \"%s\"\n", encoded, buf );
  87. return -EINVAL;
  88. }
  89. encoded_bytes += 2;
  90. }
  91. len = ( raw_bytes - raw );
  92. DBG ( "Base16-decoded \"%s\" to:\n", encoded );
  93. DBG_HDA ( 0, raw, len );
  94. assert ( len <= base16_decoded_max_len ( encoded ) );
  95. return ( len );
  96. }