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