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

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. * 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/vsprintf.h>
  30. #include <ipxe/base16.h>
  31. /** @file
  32. *
  33. * Base16 encoding
  34. *
  35. */
  36. /**
  37. * Encode hexadecimal string (with optional byte separator character)
  38. *
  39. * @v separator Byte separator character, or 0 for no separator
  40. * @v raw Raw data
  41. * @v raw_len Length of raw data
  42. * @v data Buffer
  43. * @v len Length of buffer
  44. * @ret len Encoded length
  45. */
  46. size_t hex_encode ( char separator, const void *raw, size_t raw_len,
  47. char *data, size_t len ) {
  48. const uint8_t *bytes = raw;
  49. const char delimiter[2] = { separator, '\0' };
  50. size_t used = 0;
  51. unsigned int i;
  52. if ( len )
  53. data[0] = 0; /* Ensure that a terminating NUL exists */
  54. for ( i = 0 ; i < raw_len ; i++ ) {
  55. used += ssnprintf ( ( data + used ), ( len - used ),
  56. "%s%02x", ( used ? delimiter : "" ),
  57. bytes[i] );
  58. }
  59. return used;
  60. }
  61. /**
  62. * Decode hexadecimal string (with optional byte separator character)
  63. *
  64. * @v separator Byte separator character, or 0 for no separator
  65. * @v encoded Encoded string
  66. * @v data Buffer
  67. * @v len Length of buffer
  68. * @ret len Length of data, or negative error
  69. */
  70. int hex_decode ( char separator, const char *encoded, void *data, size_t len ) {
  71. uint8_t *out = data;
  72. unsigned int count = 0;
  73. unsigned int sixteens;
  74. unsigned int units;
  75. while ( *encoded ) {
  76. /* Check separator, if applicable */
  77. if ( count && separator && ( ( *(encoded++) != separator ) ) )
  78. return -EINVAL;
  79. /* Extract digits. Note that either digit may be NUL,
  80. * which would be interpreted as an invalid value by
  81. * digit_value(); there is therefore no need for an
  82. * explicit end-of-string check.
  83. */
  84. sixteens = digit_value ( *(encoded++) );
  85. if ( sixteens >= 16 )
  86. return -EINVAL;
  87. units = digit_value ( *(encoded++) );
  88. if ( units >= 16 )
  89. return -EINVAL;
  90. /* Store result */
  91. if ( count < len )
  92. out[count] = ( ( sixteens << 4 ) | units );
  93. count++;
  94. }
  95. return count;
  96. }