Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /*******************************************************************************
0003  *
0004  * Module Name: utnonansi - Non-ansi C library functions
0005  *
0006  ******************************************************************************/
0007 
0008 #include <acpi/acpi.h>
0009 #include "accommon.h"
0010 
0011 #define _COMPONENT          ACPI_UTILITIES
0012 ACPI_MODULE_NAME("utnonansi")
0013 
0014 /*
0015  * Non-ANSI C library functions - strlwr, strupr, stricmp, and "safe"
0016  * string functions.
0017  */
0018 /*******************************************************************************
0019  *
0020  * FUNCTION:    acpi_ut_strlwr (strlwr)
0021  *
0022  * PARAMETERS:  src_string      - The source string to convert
0023  *
0024  * RETURN:      None
0025  *
0026  * DESCRIPTION: Convert a string to lowercase
0027  *
0028  ******************************************************************************/
0029 void acpi_ut_strlwr(char *src_string)
0030 {
0031     char *string;
0032 
0033     ACPI_FUNCTION_ENTRY();
0034 
0035     if (!src_string) {
0036         return;
0037     }
0038 
0039     /* Walk entire string, lowercasing the letters */
0040 
0041     for (string = src_string; *string; string++) {
0042         *string = (char)tolower((int)*string);
0043     }
0044 }
0045 
0046 /*******************************************************************************
0047  *
0048  * FUNCTION:    acpi_ut_strupr (strupr)
0049  *
0050  * PARAMETERS:  src_string      - The source string to convert
0051  *
0052  * RETURN:      None
0053  *
0054  * DESCRIPTION: Convert a string to uppercase
0055  *
0056  ******************************************************************************/
0057 
0058 void acpi_ut_strupr(char *src_string)
0059 {
0060     char *string;
0061 
0062     ACPI_FUNCTION_ENTRY();
0063 
0064     if (!src_string) {
0065         return;
0066     }
0067 
0068     /* Walk entire string, uppercasing the letters */
0069 
0070     for (string = src_string; *string; string++) {
0071         *string = (char)toupper((int)*string);
0072     }
0073 }
0074 
0075 /******************************************************************************
0076  *
0077  * FUNCTION:    acpi_ut_stricmp (stricmp)
0078  *
0079  * PARAMETERS:  string1             - first string to compare
0080  *              string2             - second string to compare
0081  *
0082  * RETURN:      int that signifies string relationship. Zero means strings
0083  *              are equal.
0084  *
0085  * DESCRIPTION: Case-insensitive string compare. Implementation of the
0086  *              non-ANSI stricmp function.
0087  *
0088  ******************************************************************************/
0089 
0090 int acpi_ut_stricmp(char *string1, char *string2)
0091 {
0092     int c1;
0093     int c2;
0094 
0095     do {
0096         c1 = tolower((int)*string1);
0097         c2 = tolower((int)*string2);
0098 
0099         string1++;
0100         string2++;
0101     }
0102     while ((c1 == c2) && (c1));
0103 
0104     return (c1 - c2);
0105 }
0106 
0107 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION) || defined (ACPI_DEBUG_OUTPUT)
0108 /*******************************************************************************
0109  *
0110  * FUNCTION:    acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat
0111  *
0112  * PARAMETERS:  Adds a "DestSize" parameter to each of the standard string
0113  *              functions. This is the size of the Destination buffer.
0114  *
0115  * RETURN:      TRUE if the operation would overflow the destination buffer.
0116  *
0117  * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
0118  *              the result of the operation will not overflow the output string
0119  *              buffer.
0120  *
0121  * NOTE:        These functions are typically only helpful for processing
0122  *              user input and command lines. For most ACPICA code, the
0123  *              required buffer length is precisely calculated before buffer
0124  *              allocation, so the use of these functions is unnecessary.
0125  *
0126  ******************************************************************************/
0127 
0128 u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source)
0129 {
0130 
0131     if (strlen(source) >= dest_size) {
0132         return (TRUE);
0133     }
0134 
0135     strcpy(dest, source);
0136     return (FALSE);
0137 }
0138 
0139 u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source)
0140 {
0141 
0142     if ((strlen(dest) + strlen(source)) >= dest_size) {
0143         return (TRUE);
0144     }
0145 
0146     strcat(dest, source);
0147     return (FALSE);
0148 }
0149 
0150 u8
0151 acpi_ut_safe_strncat(char *dest,
0152              acpi_size dest_size,
0153              char *source, acpi_size max_transfer_length)
0154 {
0155     acpi_size actual_transfer_length;
0156 
0157     actual_transfer_length = ACPI_MIN(max_transfer_length, strlen(source));
0158 
0159     if ((strlen(dest) + actual_transfer_length) >= dest_size) {
0160         return (TRUE);
0161     }
0162 
0163     strncat(dest, source, max_transfer_length);
0164     return (FALSE);
0165 }
0166 
0167 void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
0168 {
0169     /* Always terminate destination string */
0170 
0171     strncpy(dest, source, dest_size);
0172     dest[dest_size - 1] = 0;
0173 }
0174 
0175 #endif