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_to_unicode                       PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function converts a ascii string to a unicode string.          */
44 /*                                                                        */
45 /*    Note:                                                               */
46 /*    The unicode string length (including NULL-terminator) is limited by */
47 /*    length in a byte, so max ascii string length must be no more than   */
48 /*    254 (NULL-terminator excluded). Only first 254 characters           */
49 /*    are converted if the string is too long.                            */
50 /*    The buffer of destination must have enough space for result, at     */
51 /*    least 1 + (strlen(source) + 1) * 2 bytes.                           */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    source                                Ascii String                  */
56 /*    destination                           Unicode String                */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    none                                                                */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _ux_utility_string_length_check       Check and return C string     */
65 /*                                          length                        */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    USBX Components                                                     */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_ux_utility_string_to_unicode(UCHAR * source,UCHAR * destination)80 VOID  _ux_utility_string_to_unicode(UCHAR *source, UCHAR *destination)
81 {
82 
83 UINT   string_length;
84 
85     /* Get the ascii string length, when there is error length is not modified so max length is used.  */
86     string_length = 254;
87     _ux_utility_string_length_check(source, &string_length, 254);
88 
89     /* Set the length of the string as the first byte of the unicode string.
90        The length is casted as a byte since Unicode strings cannot be more than 255 chars.  */
91     *destination++ = (UCHAR)(string_length + 1);
92 
93     while(string_length--)
94     {
95         /* First character is from the source.  */
96         *destination++ = *source++;
97 
98         /* Second character of unicode word is 0.  */
99         *destination++ = 0;
100     }
101 
102     /* Finish with a 0.  */
103     *destination++ = 0;
104 
105     /* Finish with a 0.  */
106     *destination++ = 0;
107 
108     /* We are done.  */
109     return;
110 }
111 
112