1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <guacamole/unicode.h>
21 
22 #include <stdint.h>
23 
guac_rdp_utf16_to_utf8(const unsigned char * utf16,int length,char * utf8,int size)24 void guac_rdp_utf16_to_utf8(const unsigned char* utf16, int length,
25         char* utf8, int size) {
26 
27     int i;
28     const uint16_t* in_codepoint = (const uint16_t*) utf16;
29 
30     /* For each UTF-16 character */
31     for (i=0; i<length; i++) {
32 
33         /* Get next codepoint */
34         uint16_t codepoint = *(in_codepoint++);
35 
36         /* Save codepoint as UTF-8 */
37         int bytes_written = guac_utf8_write(codepoint, utf8, size);
38         size -= bytes_written;
39         utf8 += bytes_written;
40 
41     }
42 
43     /* Save NULL terminator */
44     *(utf8++) = 0;
45 
46 }
47 
guac_rdp_utf8_to_utf16(const unsigned char * utf8,int length,char * utf16,int size)48 void guac_rdp_utf8_to_utf16(const unsigned char* utf8, int length,
49         char* utf16, int size) {
50 
51     int i;
52     uint16_t* out_codepoint = (uint16_t*) utf16;
53 
54     /* For each UTF-8 character */
55     for (i=0; i<length; i++) {
56 
57         /* Get next codepoint */
58         int codepoint;
59         utf8 += guac_utf8_read((const char*) utf8, 4, &codepoint);
60 
61         /* Save codepoint as UTF-16 */
62         *(out_codepoint++) = codepoint;
63 
64         /* Stop if buffer full */
65         size -= 2;
66         if (size < 2)
67             break;
68 
69     }
70 
71 }
72 
73