MagickCore  6.7.7
token-private.h
Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004 
00005   You may not use this file except in compliance with the License.
00006   obtain a copy of the License at
00007 
00008     http://www.imagemagick.org/script/license.php
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private token methods.
00017 */
00018 #ifndef _MAGICKCORE_TOKEN_PRIVATE_H
00019 #define _MAGICKCORE_TOKEN_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #ifndef EILSEQ
00026   #define EILSEQ  ENOENT
00027 #endif
00028 
00029 #define MaxMultibyteCodes  6
00030 
00031 extern MagickPrivate MagickBooleanType
00032   IsGlob(const char *);
00033 
00034 typedef struct
00035 {
00036   int
00037     code_mask,
00038     code_value,
00039     utf_mask,
00040     utf_value;
00041 } UTFInfo;
00042 
00043 static UTFInfo
00044   utf_info[MaxMultibyteCodes] =
00045   {
00046     { 0x80, 0x00, 0x000007f, 0x0000000 },  /* 1 byte sequence */
00047     { 0xE0, 0xC0, 0x00007ff, 0x0000080 },  /* 2 byte sequence */
00048     { 0xF0, 0xE0, 0x000ffff, 0x0000800 },  /* 3 byte sequence */
00049     { 0xF8, 0xF0, 0x01fffff, 0x0010000 },  /* 4 byte sequence */
00050     { 0xFC, 0xF8, 0x03fffff, 0x0200000 },  /* 5 byte sequence */
00051     { 0xFE, 0xFC, 0x7ffffff, 0x4000000 },  /* 6 byte sequence */
00052   };
00053 
00054 static inline unsigned char *ConvertLatin1ToUTF8(const unsigned char *content)
00055 {
00056   int
00057     c;
00058 
00059   register const unsigned char
00060     *p;
00061 
00062   register unsigned char
00063     *q;
00064 
00065   size_t
00066     length;
00067 
00068   unsigned char
00069     *utf8;
00070 
00071   length=0;
00072   for (p=content; *p != '\0'; p++)
00073     length+=(*p & 0x80) != 0 ? 2 : 1;
00074   utf8=(unsigned char *) NULL;
00075   if (~length >= 1)
00076     utf8=(unsigned char *) AcquireQuantumMemory(length+1UL,sizeof(*utf8));
00077   if (utf8 == (unsigned char *) NULL)
00078     return((unsigned char *) NULL);
00079   q=utf8;
00080   for (p=content; *p != '\0'; p++)
00081   {
00082     c=(*p);
00083     if ((c & 0x80) == 0)
00084       *q++=(unsigned char) c;
00085     else
00086       {
00087         *q++=(unsigned char) (0xc0 | ((c >> 6) & 0x3f));
00088         *q++=(unsigned char) (0x80 | (c & 0x3f));
00089       }
00090   }
00091   *q='\0';
00092   return(utf8);
00093 }
00094 
00095 static inline int GetNextUTFCode(const char *text,unsigned int *octets)
00096 {
00097   int
00098     code;
00099 
00100   register ssize_t
00101     i;
00102 
00103   register int
00104     c,
00105     unicode;
00106 
00107   *octets=1;
00108   if (text == (const char *) NULL)
00109     {
00110       errno=EINVAL;
00111       return(-1);
00112     }
00113   code=(int) (*text++) & 0xff;
00114   unicode=code;
00115   for (i=0; i < MaxMultibyteCodes; i++)
00116   {
00117     if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
00118       {
00119         unicode&=utf_info[i].utf_mask;
00120         if (unicode < utf_info[i].utf_value)
00121           {
00122             errno=EILSEQ;
00123             return(-1);
00124           }
00125         *octets=(unsigned int) (i+1);
00126         return(unicode);
00127       }
00128     c=(int) (*text++ ^ 0x80) & 0xff;
00129     if ((c & 0xc0) != 0)
00130       {
00131         errno=EILSEQ;
00132         return(-1);
00133       }
00134     unicode=(unicode << 6) | c;
00135   }
00136   errno=EILSEQ;
00137   return(-1);
00138 }
00139 
00140 static inline int GetUTFCode(const char *text)
00141 {
00142   unsigned int
00143     octets;
00144 
00145   return(GetNextUTFCode(text,&octets));
00146 }
00147 
00148 static inline unsigned int GetUTFOctets(const char *text)
00149 {
00150   unsigned int
00151     octets;
00152 
00153   (void) GetNextUTFCode(text,&octets);
00154   return(octets);
00155 }
00156 
00157 static inline MagickBooleanType IsUTFSpace(int code)
00158 {
00159   if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
00160       (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
00161       (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
00162       (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
00163       (code == 0x205f) || (code == 0x3000))
00164     return(MagickTrue);
00165   return(MagickFalse);
00166 }
00167 
00168 static inline MagickBooleanType IsUTFValid(int code)
00169 {
00170   int
00171     mask;
00172 
00173   mask=(int) 0x7fffffff;
00174   if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
00175       (code != 0xfffe) && (code != 0xffff))
00176     return(MagickFalse);
00177   return(MagickTrue);
00178 }
00179 
00180 static inline MagickBooleanType IsUTFAscii(int code)
00181 {
00182   int
00183     mask;
00184 
00185   mask=(int) 0x7f;
00186   if ((code & ~mask) != 0)
00187     return(MagickFalse);
00188   return(MagickTrue);
00189 }
00190 
00191 #if defined(__cplusplus) || defined(c_plusplus)
00192 }
00193 #endif
00194 
00195 #endif