token-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2019 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.  You may
00006   obtain a copy of the License at
00007 
00008     https://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 *) magick_attribute((__pure__));
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           break;
00122         *octets=(unsigned int) (i+1);
00123         return(unicode);
00124       }
00125     c=(int) (*text++ ^ 0x80) & 0xff;
00126     if ((c & 0xc0) != 0)
00127       break;
00128     if (unicode > 0x10FFFF)
00129       break;
00130     unicode=(unicode << 6) | c;
00131   }
00132   errno=EILSEQ;
00133   return(-1);
00134 }
00135 
00136 static inline int GetUTFCode(const char *text)
00137 {
00138   unsigned int
00139     octets;
00140 
00141   return(GetNextUTFCode(text,&octets));
00142 }
00143 
00144 static inline unsigned int GetUTFOctets(const char *text)
00145 {
00146   unsigned int
00147     octets;
00148 
00149   (void) GetNextUTFCode(text,&octets);
00150   return(octets);
00151 }
00152 
00153 static inline MagickBooleanType IsUTFSpace(int code)
00154 {
00155   if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
00156       (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
00157       (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
00158       (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
00159       (code == 0x205f) || (code == 0x3000))
00160     return(MagickTrue);
00161   return(MagickFalse);
00162 }
00163 
00164 static inline MagickBooleanType IsUTFValid(int code)
00165 {
00166   int
00167     mask;
00168 
00169   mask=(int) 0x7fffffff;
00170   if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
00171       (code != 0xfffe) && (code != 0xffff))
00172     return(MagickFalse);
00173   return(MagickTrue);
00174 }
00175 
00176 static inline MagickBooleanType IsUTFAscii(int code)
00177 {
00178   int
00179     mask;
00180 
00181   mask=(int) 0x7f;
00182   if ((code & ~mask) != 0)
00183     return(MagickFalse);
00184   return(MagickTrue);
00185 }
00186 
00187 #if defined(__cplusplus) || defined(c_plusplus)
00188 }
00189 #endif
00190 
00191 #endif

Generated on 19 Aug 2019 for MagickCore by  doxygen 1.6.1