utascii.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /******************************************************************************
  2. *
  3. * Module Name: utascii - Utility ascii functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. /*******************************************************************************
  45. *
  46. * FUNCTION: acpi_ut_valid_nameseg
  47. *
  48. * PARAMETERS: name - The name or table signature to be examined.
  49. * Four characters, does not have to be a
  50. * NULL terminated string.
  51. *
  52. * RETURN: TRUE if signature is has 4 valid ACPI characters
  53. *
  54. * DESCRIPTION: Validate an ACPI table signature.
  55. *
  56. ******************************************************************************/
  57. u8 acpi_ut_valid_nameseg(char *name)
  58. {
  59. u32 i;
  60. /* Validate each character in the signature */
  61. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  62. if (!acpi_ut_valid_name_char(name[i], i)) {
  63. return (FALSE);
  64. }
  65. }
  66. return (TRUE);
  67. }
  68. /*******************************************************************************
  69. *
  70. * FUNCTION: acpi_ut_valid_name_char
  71. *
  72. * PARAMETERS: char - The character to be examined
  73. * position - Byte position (0-3)
  74. *
  75. * RETURN: TRUE if the character is valid, FALSE otherwise
  76. *
  77. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  78. * 1) Upper case alpha
  79. * 2) numeric
  80. * 3) underscore
  81. *
  82. * We allow a '!' as the last character because of the ASF! table
  83. *
  84. ******************************************************************************/
  85. u8 acpi_ut_valid_name_char(char character, u32 position)
  86. {
  87. if (!((character >= 'A' && character <= 'Z') ||
  88. (character >= '0' && character <= '9') || (character == '_'))) {
  89. /* Allow a '!' in the last position */
  90. if (character == '!' && position == 3) {
  91. return (TRUE);
  92. }
  93. return (FALSE);
  94. }
  95. return (TRUE);
  96. }
  97. /*******************************************************************************
  98. *
  99. * FUNCTION: acpi_ut_check_and_repair_ascii
  100. *
  101. * PARAMETERS: name - Ascii string
  102. * count - Number of characters to check
  103. *
  104. * RETURN: None
  105. *
  106. * DESCRIPTION: Ensure that the requested number of characters are printable
  107. * Ascii characters. Sets non-printable and null chars to <space>.
  108. *
  109. ******************************************************************************/
  110. void acpi_ut_check_and_repair_ascii(u8 *name, char *repaired_name, u32 count)
  111. {
  112. u32 i;
  113. for (i = 0; i < count; i++) {
  114. repaired_name[i] = (char)name[i];
  115. if (!name[i]) {
  116. return;
  117. }
  118. if (!isprint(name[i])) {
  119. repaired_name[i] = ' ';
  120. }
  121. }
  122. }