1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Utility                                                             */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_utility_string_length_check                     PORTABLE C      */
35 /*                                                           6.1.12       */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function checks if a NULL-terminated C string reaches its end  */
43 /*    before specified length exceeds.                                    */
44 /*                                                                        */
45 /*    On success the actual length of C string is written back to UINT    */
46 /*    variable pointed by string_length_ptr (if not NULL).                */
47 /*    Otherwise the variable keeps untouched.                             */
48 /*                                                                        */
49 /*    Note NULL terminator is not counted in string length                */
50 /*    (same as C strlen).                                                 */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    string                                Pointer to string             */
55 /*    string_length_ptr                     Pointer to UINT to receive    */
56 /*                                            the string length           */
57 /*    max_string_length                     Max string length             */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    returns success if the string length was less than the max length,  */
62 /*    else it returns error                                               */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    None                                                                */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    USBX Components                                                     */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            resulting in version 6.1.12 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_utility_string_length_check(UCHAR * string,UINT * string_length_ptr,UINT max_string_length)83 UINT  _ux_utility_string_length_check(UCHAR *string, UINT *string_length_ptr, UINT max_string_length)
84 {
85 
86 UINT    string_length;
87 
88 
89     if (string == UX_NULL)
90         return(UX_ERROR);
91 
92     string_length = 0;
93 
94     while (1)
95     {
96 
97         if (string[string_length] == '\0')
98             break;
99 
100         string_length++;
101         if (string_length > max_string_length)
102         {
103 
104             /* Error trap. */
105             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_UTILITY, UX_ERROR);
106 
107             return(UX_ERROR);
108         }
109     }
110 
111     if (string_length_ptr)
112         *string_length_ptr = string_length;
113 
114     return(UX_SUCCESS);
115 }
116 
117