1 /*  Part of SWI-Prolog
2 
3     Author:        Jan Wielemaker
4     E-mail:        J.Wielemaker@vu.nl
5     WWW:           http://www.swi-prolog.org
6     Copyright (c)  2000-2011, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <stdio.h>
36 #include "utf8.h"
37 
38 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 UTF-8 Decoding, based on http://www.cl.cam.ac.uk/~mgk25/unicode.html
40 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
41 
42 #define CONT(i) ((in[i]&0xc0) == 0x80)
43 #define VAL(i, s) ((in[i]&0x3f) << s)
44 
45 char *
sgml__utf8_get_char(const char * in,int * chr)46 sgml__utf8_get_char(const char *in, int *chr)
47 { 					/* 2-byte, 0x80-0x7ff */
48   if ( (in[0]&0xe0) == 0xc0 && CONT(1) )
49   { *chr = ((in[0]&0x1f) << 6)|VAL(1,0);
50     return (char *)in+2;
51   }
52 					/* 3-byte, 0x800-0xffff */
53   if ( (in[0]&0xf0) == 0xe0 && CONT(1) && CONT(2) )
54   { *chr = ((in[0]&0xf) << 12)|VAL(1,6)|VAL(2,0);
55     return (char *)in+3;
56   }
57 					/* 4-byte, 0x10000-0x1FFFFF */
58   if ( (in[0]&0xf8) == 0xf0 && CONT(1) && CONT(2) && CONT(3) )
59   { *chr = ((in[0]&0x7) << 18)|VAL(1,12)|VAL(2,6)|VAL(3,0);
60     return (char *)in+4;
61   }
62 					/* 5-byte, 0x200000-0x3FFFFFF */
63   if ( (in[0]&0xfc) == 0xf8 && CONT(1) && CONT(2) && CONT(3) && CONT(4) )
64   { *chr = ((in[0]&0x3) << 24)|VAL(1,18)|VAL(2,12)|VAL(3,6)|VAL(4,0);
65     return (char *)in+5;
66   }
67 					/* 6-byte, 0x400000-0x7FFFFFF */
68   if ( (in[0]&0xfe) == 0xfc && CONT(1) && CONT(2) && CONT(3) && CONT(4) && CONT(5) )
69   { *chr = ((in[0]&0x1) << 30)|VAL(1,24)|VAL(2,18)|VAL(3,12)|VAL(4,6)|VAL(5,0);
70     return (char *)in+4;
71   }
72 
73   *chr = *in;
74 
75   return (char *)in+1;
76 }
77 
78 
79 char *
sgml_utf8_put_char(char * out,int chr)80 sgml_utf8_put_char(char *out, int chr)
81 { if ( chr < 0x80 )
82   { *out++ = chr;
83   } else if ( chr < 0x800 )
84   { *out++ = 0xc0|((chr>>6)&0x1f);
85     *out++ = 0x80|(chr&0x3f);
86   } else if ( chr < 0x10000 )
87   { *out++ = 0xe0|((chr>>12)&0x0f);
88     *out++ = 0x80|((chr>>6)&0x3f);
89     *out++ = 0x80|(chr&0x3f);
90   } else if ( chr < 0x200000 )
91   { *out++ = 0xf0|((chr>>18)&0x07);
92     *out++ = 0x80|((chr>>12)&0x3f);
93     *out++ = 0x80|((chr>>6)&0x3f);
94     *out++ = 0x80|(chr&0x3f);
95   } else if ( chr < 0x4000000 )
96   { *out++ = 0xf8|((chr>>24)&0x03);
97     *out++ = 0x80|((chr>>18)&0x3f);
98     *out++ = 0x80|((chr>>12)&0x3f);
99     *out++ = 0x80|((chr>>6)&0x3f);
100     *out++ = 0x80|(chr&0x3f);
101   } else if ( (unsigned)chr < 0x80000000 )
102   { *out++ = 0xfc|((chr>>30)&0x01);
103     *out++ = 0x80|((chr>>24)&0x3f);
104     *out++ = 0x80|((chr>>18)&0x3f);
105     *out++ = 0x80|((chr>>12)&0x3f);
106     *out++ = 0x80|((chr>>6)&0x3f);
107     *out++ = 0x80|(chr&0x3f);
108   }
109 
110   return out;
111 }
112 
113 
114 size_t
sgml_utf8_strlen(const char * s,size_t len)115 sgml_utf8_strlen(const char *s, size_t len)
116 { const char *e = &s[len];
117   unsigned int l = 0;
118 
119   while(s<e)
120   { int chr;
121 
122     s = utf8_get_char(s, &chr);
123     l++;
124   }
125 
126   return l;
127 }
128