uthex.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: uthex -- Hex/ASCII support functions
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #define _COMPONENT ACPI_COMPILER
  12. ACPI_MODULE_NAME("uthex")
  13. /* Hex to ASCII conversion table */
  14. static const char acpi_gbl_hex_to_ascii[] = {
  15. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
  16. 'E', 'F'
  17. };
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_ut_hex_to_ascii_char
  21. *
  22. * PARAMETERS: integer - Contains the hex digit
  23. * position - bit position of the digit within the
  24. * integer (multiple of 4)
  25. *
  26. * RETURN: The converted Ascii character
  27. *
  28. * DESCRIPTION: Convert a hex digit to an Ascii character
  29. *
  30. ******************************************************************************/
  31. char acpi_ut_hex_to_ascii_char(u64 integer, u32 position)
  32. {
  33. u64 index;
  34. acpi_ut_short_shift_right(integer, position, &index);
  35. return (acpi_gbl_hex_to_ascii[index & 0xF]);
  36. }
  37. /*******************************************************************************
  38. *
  39. * FUNCTION: acpi_ut_ascii_to_hex_byte
  40. *
  41. * PARAMETERS: two_ascii_chars - Pointer to two ASCII characters
  42. * return_byte - Where converted byte is returned
  43. *
  44. * RETURN: Status and converted hex byte
  45. *
  46. * DESCRIPTION: Perform ascii-to-hex translation, exactly two ASCII characters
  47. * to a single converted byte value.
  48. *
  49. ******************************************************************************/
  50. acpi_status acpi_ut_ascii_to_hex_byte(char *two_ascii_chars, u8 *return_byte)
  51. {
  52. /* Both ASCII characters must be valid hex digits */
  53. if (!isxdigit((int)two_ascii_chars[0]) ||
  54. !isxdigit((int)two_ascii_chars[1])) {
  55. return (AE_BAD_HEX_CONSTANT);
  56. }
  57. *return_byte =
  58. acpi_ut_ascii_char_to_hex(two_ascii_chars[1]) |
  59. (acpi_ut_ascii_char_to_hex(two_ascii_chars[0]) << 4);
  60. return (AE_OK);
  61. }
  62. /*******************************************************************************
  63. *
  64. * FUNCTION: acpi_ut_ascii_char_to_hex
  65. *
  66. * PARAMETERS: hex_char - Hex character in Ascii. Must be:
  67. * 0-9 or A-F or a-f
  68. *
  69. * RETURN: The binary value of the ascii/hex character
  70. *
  71. * DESCRIPTION: Perform ascii-to-hex translation
  72. *
  73. ******************************************************************************/
  74. u8 acpi_ut_ascii_char_to_hex(int hex_char)
  75. {
  76. /* Values 0-9 */
  77. if (hex_char <= '9') {
  78. return ((u8)(hex_char - '0'));
  79. }
  80. /* Upper case A-F */
  81. if (hex_char <= 'F') {
  82. return ((u8)(hex_char - 0x37));
  83. }
  84. /* Lower case a-f */
  85. return ((u8)(hex_char - 0x57));
  86. }