]> granicus.if.org Git - imagemagick/blob - magick/token-private.h
(no commit message)
[imagemagick] / magick / token-private.h
1 /*
2   Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization
3   dedicated to making software imaging solutions freely available.
4
5   You may not use this file except in compliance with the License.
6   obtain a copy of the License at
7
8     http://www.imagemagick.org/script/license.php
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16   MagickCore private token methods.
17 */
18 #ifndef _MAGICKCORE_TOKEN_PRIVATE_H
19 #define _MAGICKCORE_TOKEN_PRIVATE_H
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 #ifndef EILSEQ
26   #define EILSEQ  ENOENT
27 #endif
28
29 #define MaxMultibyteCodes  6
30
31 typedef struct
32 {
33   long
34     code_mask,
35     code_value,
36     utf_mask,
37     utf_value;
38 } UTFInfo;
39
40 static UTFInfo
41   utf_info[MaxMultibyteCodes] =
42   {
43     { 0x80, 0x00, 0x000007f, 0x0000000 },  /* 1 byte sequence */
44     { 0xE0, 0xC0, 0x00007ff, 0x0000080 },  /* 2 byte sequence */
45     { 0xF0, 0xE0, 0x000ffff, 0x0000800 },  /* 3 byte sequence */
46     { 0xF8, 0xF0, 0x01fffff, 0x0010000 },  /* 4 byte sequence */
47     { 0xFC, 0xF8, 0x03fffff, 0x0200000 },  /* 5 byte sequence */
48     { 0xFE, 0xFC, 0x7ffffff, 0x4000000 },  /* 6 byte sequence */
49   };
50
51 static inline long GetNextUTFCode(const char *text,size_t *octets)
52 {
53   long
54     code;
55
56   register long
57     i;
58
59   register long
60     c,
61     unicode;
62
63   *octets=0;
64   if (text == (const char *) NULL)
65     {
66       errno=EINVAL;
67       return(-1);
68     }
69   code=(long) (*text++) & 0xff;
70   unicode=code;
71   for (i=0; i < MaxMultibyteCodes; i++)
72   {
73     if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
74       {
75         unicode&=utf_info[i].utf_mask;
76         if (unicode < utf_info[i].utf_value)
77           {
78             errno=EILSEQ;
79             return(-1);
80           }
81         *octets=(size_t) (i+1);
82         return(unicode);
83       }
84     c=(long) (*text++ ^ 0x80) & 0xff;
85     if ((c & 0xc0) != 0)
86       {
87         errno=EILSEQ;
88         return(-1);
89       }
90     unicode=(unicode << 6) | c;
91   }
92   errno=EILSEQ;
93   return(-1);
94 }
95
96 static long GetUTFCode(const char *text)
97 {
98   size_t
99     octets;
100
101   return(GetNextUTFCode(text,&octets));
102 }
103
104 static size_t GetUTFOctets(const char *text)
105 {
106   size_t
107     octets;
108
109   (void) GetNextUTFCode(text,&octets);
110   return(octets);
111 }
112
113 static inline MagickBooleanType IsUTFSpace(long code)
114 {
115   if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
116       (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
117       (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
118       (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
119       (code == 0x205f) || (code == 0x3000))
120     return(MagickTrue);
121   return(MagickFalse);
122 }
123
124 static inline MagickBooleanType IsUTFValid(long code)
125 {
126   long
127     mask;
128
129   mask=(long) 0x7fffffff;
130   if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
131       (code != 0xfffe) && (code != 0xffff))
132     return(MagickFalse);
133   return(MagickTrue);
134 }
135
136 static inline MagickBooleanType IsUTFAscii(long code)
137 {
138   long
139     mask;
140
141   mask=(long) 0x7f;
142   if ((code & ~mask) != 0)
143     return(MagickFalse);
144   return(MagickTrue);
145 }
146
147 #if defined(__cplusplus) || defined(c_plusplus)
148 }
149 #endif
150
151 #endif