1 /* ----------------------------------------------------------------------------
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
3 * Description: LiteOS USB Driver Config Data Stream
4 * Author: Yannik Li
5 * Create: 2021-02-21
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * --------------------------------------------------------------------------- */
28 /* ----------------------------------------------------------------------------
29 * Notice of Export Control Law
30 * ===============================================
31 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34 * applicable export control laws and regulations.
35 * --------------------------------------------------------------------------- */
36
37 #include <poll.h>
38 #include "usbd_config.h"
39 #include "implementation/global_implementation.h"
40 #include "usb_string.h"
41
42 #ifdef __cplusplus
43 #if __cplusplus
44 //extern "C" {
45 #endif /* __cplusplus */
46 #endif /* __cplusplus */
47
utf8_to_utf16le(const char * src,uint8_t * target,uint32_t len)48 int utf8_to_utf16le(const char *src, uint8_t *target, uint32_t len)
49 {
50 int count = 0;
51 uint8_t c;
52 uint16_t wc;
53
54 while (len != 0 && (c = (uint8_t)*src++) != 0)
55 {
56 if ((c & 0x80) == 0)
57 { /* 0xxxxxxx => 000000000xxxxxxx */
58 wc = c;
59 }
60 else if ((c & 0xe0) == 0xc0)
61 { /* 110yyyyy 10xxxxxx => 00000yyyyyxxxxxx */
62 wc = (c & 0x1f) << 6;
63
64 c = (uint8_t)*src++;
65 if ((c & 0xc0) != 0x80)
66 goto err;
67 c &= 0x3f;
68 wc |= c;
69 }
70 else if ((c & 0xf0) == 0xe0)
71 { /* 1110zzzz 10yyyyyy 10xxxxxx => zzzzyyyyyyxxxxxx */
72 wc = (c & 0x0f) << 12;
73
74 c = (uint8_t)*src++;
75 if ((c & 0xc0) != 0x80)
76 goto err;
77 c &= 0x3f;
78 wc |= c << 6;
79
80 c = (uint8_t)*src++;
81 if ((c & 0xc0) != 0x80)
82 goto err;
83 c &= 0x3f;
84 wc |= c;
85
86 /* don't support surrogates */
87 if (0xd800 <= wc && wc <= 0xdfff)
88 {
89 goto err;
90 }
91 }
92 else
93 { /* 4 bytes sequence is not supported */
94 goto err;
95 }
96
97 *target++ = (uint8_t)wc;
98 *target++ = (uint8_t)(wc >> 8);
99 count++;
100 len--;
101 }
102 return count;
103
104 err:
105 return -1;
106 }
107
108 #ifdef __cplusplus
109 #if __cplusplus
110 //}
111 #endif /* __cplusplus */
112 #endif /* __cplusplus */
113