This source file includes following definitions.
- AcquireTokenInfo
- DestroyTokenInfo
- GetMagickToken
- GlobExpression
- IsGlob
- IsMagickTrue
- sindex
- StoreToken
- Tokenizer
#include "magick/studio.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/image.h"
#include "magick/memory_.h"
#include "magick/string_.h"
#include "magick/string-private.h"
#include "magick/token.h"
#include "magick/token-private.h"
#include "magick/utility.h"
struct _TokenInfo
{
int
state;
MagickStatusType
flag;
ssize_t
offset;
char
quote;
size_t
signature;
};
MagickExport TokenInfo *AcquireTokenInfo(void)
{
TokenInfo
*token_info;
token_info=(TokenInfo *) AcquireMagickMemory(sizeof(*token_info));
if (token_info == (TokenInfo *) NULL)
ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
token_info->signature=MagickSignature;
return(token_info);
}
MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
{
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
assert(token_info != (TokenInfo *) NULL);
assert(token_info->signature == MagickSignature);
token_info->signature=(~MagickSignature);
token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
return(token_info);
}
MagickExport void GetMagickToken(const char *start,const char **end,char *token)
{
double
value;
register const char
*p;
register ssize_t
i;
assert(start != (const char *) NULL);
assert(token != (char *) NULL);
i=0;
p=start;
while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
p++;
switch (*p)
{
case '\0':
break;
case '"':
case '\'':
case '`':
case '{':
{
register char
escape;
switch (*p)
{
case '"': escape='"'; break;
case '\'': escape='\''; break;
case '`': escape='\''; break;
case '{': escape='}'; break;
default: escape=(*p); break;
}
for (p++; *p != '\0'; p++)
{
if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
p++;
else
if (*p == escape)
{
p++;
break;
}
token[i++]=(*p);
}
break;
}
case '/':
{
token[i++]=(*p++);
if ((*p == '>') || (*p == '/'))
token[i++]=(*p++);
break;
}
default:
{
char
*q;
value=StringToDouble(p,&q);
(void) value;
if ((p != q) && (*p != ','))
{
for ( ; (p < q) && (*p != ','); p++)
token[i++]=(*p);
if (*p == '%')
token[i++]=(*p++);
break;
}
if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
(*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
{
token[i++]=(*p++);
break;
}
for ( ; *p != '\0'; p++)
{
if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
(*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
break;
if ((i > 0) && (*p == '<'))
break;
token[i++]=(*p);
if (*p == '>')
break;
if (*p == '(')
for (p++; *p != '\0'; p++)
{
token[i++]=(*p);
if ((*p == ')') && (*(p-1) != '\\'))
break;
}
}
break;
}
}
token[i]='\0';
if (LocaleNCompare(token,"url(",4) == 0)
{
ssize_t
offset;
offset=4;
if (token[offset] == '#')
offset++;
i=(ssize_t) strlen(token);
(void) CopyMagickString(token,token+offset,MaxTextExtent);
token[i-offset-1]='\0';
}
while (isspace((int) ((unsigned char) *p)) != 0)
p++;
if (end != (const char **) NULL)
*end=(const char *) p;
}
MagickExport MagickBooleanType GlobExpression(const char *expression,
const char *pattern,const MagickBooleanType case_insensitive)
{
MagickBooleanType
done,
match;
register const char
*p;
if (pattern == (char *) NULL)
return(MagickTrue);
if (GetUTFCode(pattern) == 0)
return(MagickTrue);
if (LocaleCompare(pattern,"*") == 0)
return(MagickTrue);
p=pattern+strlen(pattern)-1;
if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
{
ExceptionInfo
*exception;
ImageInfo
*image_info;
image_info=AcquireImageInfo();
(void) CopyMagickString(image_info->filename,pattern,MaxTextExtent);
exception=AcquireExceptionInfo();
(void) SetImageInfo(image_info,0,exception);
exception=DestroyExceptionInfo(exception);
if (LocaleCompare(image_info->filename,pattern) != 0)
{
image_info=DestroyImageInfo(image_info);
return(MagickFalse);
}
image_info=DestroyImageInfo(image_info);
}
done=MagickFalse;
while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
{
if (GetUTFCode(expression) == 0)
if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
break;
switch (GetUTFCode(pattern))
{
case '*':
{
MagickBooleanType
status;
status=MagickFalse;
pattern+=GetUTFOctets(pattern);
while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
{
status=GlobExpression(expression,pattern,case_insensitive);
expression+=GetUTFOctets(expression);
}
if (status != MagickFalse)
{
while (GetUTFCode(expression) != 0)
expression+=GetUTFOctets(expression);
while (GetUTFCode(pattern) != 0)
pattern+=GetUTFOctets(pattern);
}
break;
}
case '[':
{
int
c;
pattern+=GetUTFOctets(pattern);
for ( ; ; )
{
if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
{
done=MagickTrue;
break;
}
if (GetUTFCode(pattern) == '\\')
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == 0)
{
done=MagickTrue;
break;
}
}
if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
{
c=GetUTFCode(pattern);
pattern+=GetUTFOctets(pattern);
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == ']')
{
done=MagickTrue;
break;
}
if (GetUTFCode(pattern) == '\\')
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == 0)
{
done=MagickTrue;
break;
}
}
if ((GetUTFCode(expression) < c) ||
(GetUTFCode(expression) > GetUTFCode(pattern)))
{
pattern+=GetUTFOctets(pattern);
continue;
}
}
else
if (GetUTFCode(pattern) != GetUTFCode(expression))
{
pattern+=GetUTFOctets(pattern);
continue;
}
pattern+=GetUTFOctets(pattern);
while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
{
if ((GetUTFCode(pattern) == '\\') &&
(GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
pattern+=GetUTFOctets(pattern);
pattern+=GetUTFOctets(pattern);
}
if (GetUTFCode(pattern) != 0)
{
pattern+=GetUTFOctets(pattern);
expression+=GetUTFOctets(expression);
}
break;
}
break;
}
case '?':
{
pattern+=GetUTFOctets(pattern);
expression+=GetUTFOctets(expression);
break;
}
case '{':
{
register const char
*p;
pattern+=GetUTFOctets(pattern);
while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
{
p=expression;
match=MagickTrue;
while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
(GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
(match != MagickFalse))
{
if (GetUTFCode(pattern) == '\\')
pattern+=GetUTFOctets(pattern);
match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
MagickFalse;
p+=GetUTFOctets(p);
pattern+=GetUTFOctets(pattern);
}
if (GetUTFCode(pattern) == 0)
{
match=MagickFalse;
done=MagickTrue;
break;
}
else
if (match != MagickFalse)
{
expression=p;
while ((GetUTFCode(pattern) != '}') &&
(GetUTFCode(pattern) != 0))
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == '\\')
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == '}')
pattern+=GetUTFOctets(pattern);
}
}
}
else
{
while ((GetUTFCode(pattern) != '}') &&
(GetUTFCode(pattern) != ',') &&
(GetUTFCode(pattern) != 0))
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == '\\')
{
pattern+=GetUTFOctets(pattern);
if ((GetUTFCode(pattern) == '}') ||
(GetUTFCode(pattern) == ','))
pattern+=GetUTFOctets(pattern);
}
}
}
if (GetUTFCode(pattern) != 0)
pattern+=GetUTFOctets(pattern);
}
break;
}
case '\\':
{
pattern+=GetUTFOctets(pattern);
if (GetUTFCode(pattern) == 0)
break;
}
default:
{
if (case_insensitive != MagickFalse)
{
if (tolower((int) GetUTFCode(expression)) !=
tolower((int) GetUTFCode(pattern)))
{
done=MagickTrue;
break;
}
}
else
if (GetUTFCode(expression) != GetUTFCode(pattern))
{
done=MagickTrue;
break;
}
expression+=GetUTFOctets(expression);
pattern+=GetUTFOctets(pattern);
}
}
}
while (GetUTFCode(pattern) == '*')
pattern+=GetUTFOctets(pattern);
match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
MagickTrue : MagickFalse;
return(match);
}
MagickExport MagickBooleanType IsGlob(const char *path)
{
MagickBooleanType
status = MagickFalse;
register const char
*p;
if (IsPathAccessible(path) != MagickFalse)
return(MagickFalse);
for (p=path; *p != '\0'; p++)
{
switch (*p)
{
case '*':
case '?':
case '{':
case '}':
case '[':
case ']':
{
status=MagickTrue;
break;
}
default:
break;
}
}
return(status);
}
MagickExport MagickBooleanType IsMagickTrue(const char *value)
{
if (value == (const char *) NULL)
return(MagickFalse);
if (LocaleCompare(value,"true") == 0)
return(MagickTrue);
if (LocaleCompare(value,"on") == 0)
return(MagickTrue);
if (LocaleCompare(value,"yes") == 0)
return(MagickTrue);
if (LocaleCompare(value,"1") == 0)
return(MagickTrue);
return(MagickFalse);
}
#define IN_WHITE 0
#define IN_TOKEN 1
#define IN_QUOTE 2
#define IN_OZONE 3
static ssize_t sindex(int c,const char *string)
{
register const char
*p;
for (p=string; *p != '\0'; p++)
if (c == (int) (*p))
return((ssize_t) (p-string));
return(-1);
}
static void StoreToken(TokenInfo *token_info,char *string,
size_t max_token_length,int c)
{
register ssize_t
i;
if ((token_info->offset < 0) ||
((size_t) token_info->offset >= (max_token_length-1)))
return;
i=token_info->offset++;
string[i]=(char) c;
if (token_info->state == IN_QUOTE)
return;
switch (token_info->flag & 0x03)
{
case 1:
{
string[i]=(char) toupper(c);
break;
}
case 2:
{
string[i]=(char) tolower(c);
break;
}
default:
break;
}
}
MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
char *token,const size_t max_token_length,const char *line,const char *white,
const char *break_set,const char *quote,const char escape,char *breaker,
int *next,char *quoted)
{
int
c;
register ssize_t
i;
*breaker='\0';
*quoted='\0';
if (line[*next] == '\0')
return(1);
token_info->state=IN_WHITE;
token_info->quote=(char) MagickFalse;
token_info->flag=flag;
for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
{
c=(int) line[*next];
i=sindex(c,break_set);
if (i >= 0)
{
switch (token_info->state)
{
case IN_WHITE:
case IN_TOKEN:
case IN_OZONE:
{
(*next)++;
*breaker=break_set[i];
token[token_info->offset]='\0';
return(0);
}
case IN_QUOTE:
{
StoreToken(token_info,token,max_token_length,c);
break;
}
}
continue;
}
i=sindex(c,quote);
if (i >= 0)
{
switch (token_info->state)
{
case IN_WHITE:
{
token_info->state=IN_QUOTE;
token_info->quote=quote[i];
*quoted=(char) MagickTrue;
break;
}
case IN_QUOTE:
{
if (quote[i] != token_info->quote)
StoreToken(token_info,token,max_token_length,c);
else
{
token_info->state=IN_OZONE;
token_info->quote='\0';
}
break;
}
case IN_TOKEN:
case IN_OZONE:
{
*breaker=(char) c;
token[token_info->offset]='\0';
return(0);
}
}
continue;
}
i=sindex(c,white);
if (i >= 0)
{
switch (token_info->state)
{
case IN_WHITE:
case IN_OZONE:
break;
case IN_TOKEN:
{
token_info->state=IN_OZONE;
break;
}
case IN_QUOTE:
{
StoreToken(token_info,token,max_token_length,c);
break;
}
}
continue;
}
if (c == (int) escape)
{
if (line[(*next)+1] == '\0')
{
*breaker='\0';
StoreToken(token_info,token,max_token_length,c);
(*next)++;
token[token_info->offset]='\0';
return(0);
}
switch (token_info->state)
{
case IN_WHITE:
{
(*next)--;
token_info->state=IN_TOKEN;
break;
}
case IN_TOKEN:
case IN_QUOTE:
{
(*next)++;
c=(int) line[*next];
StoreToken(token_info,token,max_token_length,c);
break;
}
case IN_OZONE:
{
token[token_info->offset]='\0';
return(0);
}
}
continue;
}
switch (token_info->state)
{
case IN_WHITE:
{
token_info->state=IN_TOKEN;
StoreToken(token_info,token,max_token_length,c);
break;
}
case IN_TOKEN:
case IN_QUOTE:
{
StoreToken(token_info,token,max_token_length,c);
break;
}
case IN_OZONE:
{
token[token_info->offset]='\0';
return(0);
}
}
}
token[token_info->offset]='\0';
return(0);
}