utascii.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utascii - Utility ascii functions
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. /*******************************************************************************
  12. *
  13. * FUNCTION: acpi_ut_valid_nameseg
  14. *
  15. * PARAMETERS: name - The name or table signature to be examined.
  16. * Four characters, does not have to be a
  17. * NULL terminated string.
  18. *
  19. * RETURN: TRUE if signature is has 4 valid ACPI characters
  20. *
  21. * DESCRIPTION: Validate an ACPI table signature.
  22. *
  23. ******************************************************************************/
  24. u8 acpi_ut_valid_nameseg(char *name)
  25. {
  26. u32 i;
  27. /* Validate each character in the signature */
  28. for (i = 0; i < ACPI_NAMESEG_SIZE; i++) {
  29. if (!acpi_ut_valid_name_char(name[i], i)) {
  30. return (FALSE);
  31. }
  32. }
  33. return (TRUE);
  34. }
  35. /*******************************************************************************
  36. *
  37. * FUNCTION: acpi_ut_valid_name_char
  38. *
  39. * PARAMETERS: char - The character to be examined
  40. * position - Byte position (0-3)
  41. *
  42. * RETURN: TRUE if the character is valid, FALSE otherwise
  43. *
  44. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  45. * 1) Upper case alpha
  46. * 2) numeric
  47. * 3) underscore
  48. *
  49. * We allow a '!' as the last character because of the ASF! table
  50. *
  51. ******************************************************************************/
  52. u8 acpi_ut_valid_name_char(char character, u32 position)
  53. {
  54. if (!((character >= 'A' && character <= 'Z') ||
  55. (character >= '0' && character <= '9') || (character == '_'))) {
  56. /* Allow a '!' in the last position */
  57. if (character == '!' && position == 3) {
  58. return (TRUE);
  59. }
  60. return (FALSE);
  61. }
  62. return (TRUE);
  63. }
  64. /*******************************************************************************
  65. *
  66. * FUNCTION: acpi_ut_check_and_repair_ascii
  67. *
  68. * PARAMETERS: name - Ascii string
  69. * count - Number of characters to check
  70. *
  71. * RETURN: None
  72. *
  73. * DESCRIPTION: Ensure that the requested number of characters are printable
  74. * Ascii characters. Sets non-printable and null chars to <space>.
  75. *
  76. ******************************************************************************/
  77. void acpi_ut_check_and_repair_ascii(u8 *name, char *repaired_name, u32 count)
  78. {
  79. u32 i;
  80. for (i = 0; i < count; i++) {
  81. repaired_name[i] = (char)name[i];
  82. if (!name[i]) {
  83. return;
  84. }
  85. if (!isprint(name[i])) {
  86. repaired_name[i] = ' ';
  87. }
  88. }
  89. }