MagickCore  6.9.10
Convert, Edit, Or Compose Bitmap Images
token-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2019 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  https://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  int
38 } UTFInfo;
39 
40 static UTFInfo
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 unsigned char *ConvertLatin1ToUTF8(const unsigned char *content)
52 {
53  register const unsigned char
54  *p;
55 
56  register unsigned char
57  *q;
58 
59  size_t
60  length;
61 
62  unsigned char
63  *utf8;
64 
65  unsigned int
66  c;
67 
68  length=0;
69  for (p=content; *p != '\0'; p++)
70  length+=(*p & 0x80) != 0 ? 2 : 1;
71  utf8=(unsigned char *) NULL;
72  if (~length >= 1)
73  utf8=(unsigned char *) AcquireQuantumMemory(length+1UL,sizeof(*utf8));
74  if (utf8 == (unsigned char *) NULL)
75  return((unsigned char *) NULL);
76  q=utf8;
77  for (p=content; *p != '\0'; p++)
78  {
79  c=(*p);
80  if ((c & 0x80) == 0)
81  *q++=c;
82  else
83  {
84  *q++=0xc0 | ((c >> 6) & 0x3f);
85  *q++=0x80 | (c & 0x3f);
86  }
87  }
88  *q='\0';
89  return(utf8);
90 }
91 
92 static inline int GetNextUTFCode(const char *text,unsigned int *octets)
93 {
94  int
95  code;
96 
97  register ssize_t
98  i;
99 
100  register int
101  c,
102  unicode;
103 
104  *octets=1;
105  if (text == (const char *) NULL)
106  {
107  errno=EINVAL;
108  return(-1);
109  }
110  code=(int) (*text++) & 0xff;
111  unicode=code;
112  for (i=0; i < MaxMultibyteCodes; i++)
113  {
114  if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
115  {
116  unicode&=utf_info[i].utf_mask;
117  if (unicode < utf_info[i].utf_value)
118  break;
119  *octets=(unsigned int) (i+1);
120  return(unicode);
121  }
122  c=(int) (*text++ ^ 0x80) & 0xff;
123  if ((c & 0xc0) != 0)
124  break;
125  if (unicode > 0x10FFFF)
126  break;
127  unicode=(unicode << 6) | c;
128  }
129  errno=EILSEQ;
130  return(-1);
131 }
132 
133 static inline int GetUTFCode(const char *text)
134 {
135  unsigned int
136  octets;
137 
138  return(GetNextUTFCode(text,&octets));
139 }
140 
141 static inline unsigned int GetUTFOctets(const char *text)
142 {
143  unsigned int
144  octets;
145 
146  (void) GetNextUTFCode(text,&octets);
147  return(octets);
148 }
149 
150 static inline MagickBooleanType IsUTFSpace(int code)
151 {
152  if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
153  (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
154  (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
155  (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
156  (code == 0x205f) || (code == 0x3000))
157  return(MagickTrue);
158  return(MagickFalse);
159 }
160 
161 static inline MagickBooleanType IsUTFValid(int code)
162 {
163  int
164  mask;
165 
166  mask=(int) 0x7fffffff;
167  if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
168  (code != 0xfffe) && (code != 0xffff))
169  return(MagickFalse);
170  return(MagickTrue);
171 }
172 
173 static inline MagickBooleanType IsUTFAscii(int code)
174 {
175  int
176  mask;
177 
178  mask=(int) 0x7f;
179  if ((code & ~mask) != 0)
180  return(MagickFalse);
181  return(MagickTrue);
182 }
183 
184 #if defined(__cplusplus) || defined(c_plusplus)
185 }
186 #endif
187 
188 #endif
IsUTFSpace
static MagickBooleanType IsUTFSpace(int code)
Definition: token-private.h:150
IsUTFValid
static MagickBooleanType IsUTFValid(int code)
Definition: token-private.h:161
UTFInfo::code_mask
int code_mask
Definition: token-private.h:34
UTFInfo::code_value
int code_value
Definition: token-private.h:35
UTFInfo
Definition: token-private.h:31
MagickTrue
@ MagickTrue
Definition: magick-type.h:194
GetNextUTFCode
static int GetNextUTFCode(const char *text, unsigned int *octets)
Definition: token-private.h:92
MaxMultibyteCodes
#define MaxMultibyteCodes
Definition: token-private.h:29
UTFInfo::utf_value
int utf_value
Definition: token-private.h:37
MagickFalse
@ MagickFalse
Definition: magick-type.h:193
GetUTFCode
static int GetUTFCode(const char *text)
Definition: token-private.h:133
ConvertLatin1ToUTF8
static unsigned char * ConvertLatin1ToUTF8(const unsigned char *content)
Definition: token-private.h:51
utf_info
static UTFInfo utf_info[MaxMultibyteCodes]
Definition: token-private.h:41
GetUTFOctets
static unsigned int GetUTFOctets(const char *text)
Definition: token-private.h:141
MagickBooleanType
MagickBooleanType
Definition: magick-type.h:191
UTFInfo::utf_mask
int utf_mask
Definition: token-private.h:36
IsUTFAscii
static MagickBooleanType IsUTFAscii(int code)
Definition: token-private.h:173
EILSEQ
#define EILSEQ
Definition: token-private.h:26
AcquireQuantumMemory
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:544