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