GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_utility_unicode_to_utf8.c Lines: 34 34 100.0 %
Date: 2024-12-05 08:52:37 Branches: 10 10 100.0 %

Line Branch Exec Source
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
70
128
UINT  _gx_utility_unicode_to_utf8(ULONG unicode, GX_UBYTE *return_utf8_str, UINT *return_utf8_size)
71
{
72
128
    if (unicode < 0x0080)
73
    {
74
1
        *return_utf8_size = 1;
75
1
        return_utf8_str[0] = (GX_UBYTE)unicode;
76
    }
77
127
    else if (unicode < 0x0800)
78
    {
79
1
        *return_utf8_size = 2;
80
1
        return_utf8_str[0] = (GX_UBYTE)(0xC0 + (GX_UBYTE)((unicode & 0x7C0) >> 6));
81
1
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
82
    }
83
126
    else if (unicode < 0x10000)
84
    {
85
121
        *return_utf8_size = 3;
86
121
        return_utf8_str[0] = (GX_UBYTE)(0xE0 + (GX_UBYTE)((unicode & 0xF000) >> 12));
87
121
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
88
121
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
89
    }
90
5
    else if (unicode < 0x200000)
91
    {
92
2
        *return_utf8_size = 4;
93
2
        return_utf8_str[0] = (GX_UBYTE)(0xF0 + (GX_UBYTE)((unicode & 0x1C0000) >> 18));
94
2
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
95
2
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
96
2
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
97
    }
98
3
    else if (unicode < 0x4000000)
99
    {
100
2
        *return_utf8_size = 5;
101
2
        return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x3000000) >> 24));
102
2
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
103
2
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
104
2
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
105
2
        return_utf8_str[4] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
106
    }
107
    else
108
    {
109
1
        *return_utf8_size = 6;
110
1
        return_utf8_str[0] = (GX_UBYTE)(0xFC + (GX_UBYTE)((unicode & 0x40000000) >> 30));
111
1
        return_utf8_str[1] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000000) >> 24));
112
1
        return_utf8_str[2] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0000) >> 18));
113
1
        return_utf8_str[3] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0x3F000) >> 12));
114
1
        return_utf8_str[4] = (GX_UBYTE)(0x80 + (GX_UBYTE)((unicode & 0xFC0) >> 6));
115
1
        return_utf8_str[5] = (GX_UBYTE)(0x80 + (unicode & 0x3F));
116
    }
117
118
128
    return GX_SUCCESS;
119
}
120
#endif /* GX_UTF8_SUPPORT */
121