File: gp_wutf8.c

package info (click to toggle)
ghostscript 9.06~dfsg-2%2Bdeb8u7
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 62,484 kB
  • sloc: ansic: 440,074; python: 4,915; cpp: 3,565; sh: 2,520; tcl: 1,482; perl: 1,374; makefile: 421; lisp: 407; awk: 66; yacc: 18
file content (109 lines) | stat: -rw-r--r-- 3,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Copyright (C) 2001-2012 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


#include "windows_.h"

#ifndef WINDOWS_NO_UNICODE
int utf8_to_wchar(wchar_t *out, const char *in)
{
    unsigned int i;
    unsigned int len = 1;
    unsigned char c;

    if (out) {
        while (i = *(unsigned char *)in++) {
            if (i < 0x80) {
                *out++ = (wchar_t)i;
                len++;
            } else if ((i & 0xE0) == 0xC0) {
                i &= 0x1F;
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                *out++ = (wchar_t)i;
                len++;
            } else if ((i & 0xF0) == 0xE0) {
                i &= 0xF;
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                c = (unsigned char)*in++;
                if ((c & 0xC0) != 0x80)
                    return -1;
                i = (i<<6) | (c & 0x3f);
                *out++ = (wchar_t)i;
                len++;
            } else {
                return -1;
            }
        }
        *out = 0;
    } else {
        while (i = *(unsigned char *)in++) {
            if (i < 0x80) {
                len++;
            } else if ((i & 0xE0) == 0xC0) {
                in++;
                len += 2;
            } else if ((i & 0xF0) == 0xE0) {
                in+=2;
                len += 3;
            } else {
                return -1;
            }
        }
    }
    return len;
}

int wchar_to_utf8(char *out, const wchar_t *in)
{
    unsigned int i;
    unsigned int len = 1;

    if (out) {
        while (i = (unsigned int)*in++) {
            if (i < 0x80) {
                *out++ = (char)i;
                len++;
            } else if (i < 0x800) {
                *out++ = 0xC0 | ( i>> 6        );
                *out++ = 0x80 | ( i      & 0x3F);
                len++;
            } else /* if (i < 0x10000) */ {
                *out++ = 0xE0 | ( i>>12        );
                *out++ = 0x80 | ((i>> 6) & 0x3F);
                *out++ = 0x80 | ( i      & 0x3F);
                len++;
            }
        }
        *out = 0;
    } else {
        while (i = (unsigned int)*in++) {
            if (i < 0x80) {
                len++;
            } else if (i < 0x800) {
                len += 2;
            } else /* if (i < 0x10000) */ {
                len += 3;
            }
        }
    }
    return len;
}
#endif