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 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Utility (Utility)                                                   */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 /* Include necessary system files.  */
24 
25 #include "gx_api.h"
26 #include "gx_utility.h"
27 
28 /**************************************************************************/
29 /*                                                                        */
30 /*  FUNCTION                                               RELEASE        */
31 /*                                                                        */
32 /*    _gx_utility_utf8_string_character_get               PORTABLE C      */
33 /*                                                           6.1          */
34 /*  AUTHOR                                                                */
35 /*                                                                        */
36 /*    Kenneth Maxwell, Microsoft Corporation                              */
37 /*                                                                        */
38 /*  DESCRIPTION                                                           */
39 /*                                                                        */
40 /*    This function convert a unicode to a utf8.                          */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    unicode                               Unicode to convert            */
45 /*    return_utf8_str                       Returned utf8 string          */
46 /*    return_utf8_size                      utf8 string size              */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    status                                Completion status             */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*                                                                        */
55 /*  CALLED BY                                                             */
56 /*                                                                        */
57 /*    Application Code                                                    */
58 /*    GUIX Internal Code                                                  */
59 /*                                                                        */
60 /*  RELEASE HISTORY                                                       */
61 /*                                                                        */
62 /*    DATE              NAME                      DESCRIPTION             */
63 /*                                                                        */
64 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
65 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
66 /*                                            resulting in version 6.1    */
67 /*                                                                        */
68 /**************************************************************************/
69 #ifdef GX_UTF8_SUPPORT
_gx_utility_unicode_to_utf8(ULONG unicode,GX_UBYTE * return_utf8_str,UINT * return_utf8_size)70 UINT  _gx_utility_unicode_to_utf8(ULONG unicode, GX_UBYTE *return_utf8_str, UINT *return_utf8_size)
71 {
72     if (unicode < 0x0080)
73     {
74         *return_utf8_size = 1;
75         return_utf8_str[0] = (GX_UBYTE)unicode;
76     }
77     else if (unicode < 0x0800)
78     {
79         *return_utf8_size = 2;
80         return_utf8_str[0] = (GX_UBYTE)(0xC0 + (GX_UBYTE)((unicode & 0x7C0) >> 6));
81         return_utf8_str[1] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
82     }
83     else if (unicode < 0x10000)
84     {
85         *return_utf8_size = 3;
86         return_utf8_str[0] = (GX_UBYTE)(0xE0 + (GX_UBYTE)((unicode & 0xF000) >> 12));
87         return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
88         return_utf8_str[2] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
89     }
90     else if (unicode < 0x200000)
91     {
92         *return_utf8_size = 4;
93         return_utf8_str[0] = (GX_UBYTE)(0xF0 + (GX_UBYTE)((unicode & 0x1C0000) >> 18));
94         return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
95         return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
96         return_utf8_str[3] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
97     }
98     else if (unicode < 0x4000000)
99     {
100         *return_utf8_size = 5;
101         return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x3000000) >> 24));
102         return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
103         return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
104         return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
105         return_utf8_str[4] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
106     }
107     else
108     {
109         *return_utf8_size = 6;
110         return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x40000000) >> 30));
111         return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000000) >> 24));
112         return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
113         return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
114         return_utf8_str[4] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
115         return_utf8_str[5] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
116     }
117 
118     return GX_SUCCESS;
119 }
120 #endif /* GX_UTF8_SUPPORT */
121 
122