2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT OOO K K EEEEE N N %
8 % T O O KKK EEE N N N %
10 % T OOO K K EEEEE N N %
13 % MagickCore Token Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/exception.h"
45 #include "MagickCore/exception-private.h"
46 #include "MagickCore/image.h"
47 #include "MagickCore/memory_.h"
48 #include "MagickCore/string_.h"
49 #include "MagickCore/string-private.h"
50 #include "MagickCore/token.h"
51 #include "MagickCore/token-private.h"
52 #include "MagickCore/utility.h"
53 #include "MagickCore/utility-private.h"
56 Typedef declaractions.
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % A c q u i r e T o k e n I n f o %
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % AcquireTokenInfo() allocates the TokenInfo structure.
89 % The format of the AcquireTokenInfo method is:
91 % TokenInfo *AcquireTokenInfo()
94 MagickExport TokenInfo *AcquireTokenInfo(void)
99 token_info=(TokenInfo *) AcquireMagickMemory(sizeof(*token_info));
100 if (token_info == (TokenInfo *) NULL)
101 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
102 token_info->signature=MagickSignature;
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % D e s t r o y T o k e n I n f o %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
120 % The format of the DestroyTokenInfo method is:
122 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
124 % A description of each parameter follows:
126 % o token_info: Specifies a pointer to an TokenInfo structure.
129 MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
132 assert(token_info != (TokenInfo *) NULL);
133 assert(token_info->signature == MagickSignature);
134 token_info->signature=(~MagickSignature);
135 token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 + G e t M a g i c k T o k e n %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % GetMagickToken() gets a token from the token stream. A token is defined as
151 % a sequence of characters delimited by whitespace (e.g. clip-path), a
152 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
153 % parenthesis (e.g. rgb(0,0,0)). GetMagickToken() also recognizes these
154 % separator characters: ':', '=', ',', and ';'.
156 % The format of the GetMagickToken method is:
158 % void GetMagickToken(const char *start,const char **end,char *token)
160 % A description of each parameter follows:
162 % o start: the start of the token sequence.
164 % o end: point to the end of the token sequence.
166 % o token: copy the token to this buffer.
169 MagickExport void GetMagickToken(const char *start,const char **end,char *token)
180 assert(start != (const char *) NULL);
181 assert(token != (char *) NULL);
183 for (p=start; *p != '\0'; )
185 while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
201 case '"': escape='"'; break;
202 case '\'': escape='\''; break;
203 case '`': escape='\''; break;
204 case '{': escape='}'; break;
205 default: escape=(*p); break;
207 for (p++; *p != '\0'; p++)
209 if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
224 if ((*p == '>') || (*p == '/'))
233 value=StringToDouble(p,&q);
235 if ((p != q) && (*p != ','))
237 for ( ; (p < q) && (*p != ','); p++)
243 if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
244 (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
249 for ( ; *p != '\0'; p++)
251 if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
252 (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
254 if ((i > 0) && (*p == '<'))
260 for (p++; *p != '\0'; p++)
263 if ((*p == ')') && (*(p-1) != '\\'))
273 if (LocaleNCompare(token,"url(",4) == 0)
279 if (token[offset] == '#')
281 i=(ssize_t) strlen(token);
282 (void) CopyMagickString(token,token+offset,MaxTextExtent);
283 token[i-offset-1]='\0';
285 while (isspace((int) ((unsigned char) *p)) != 0)
287 if (end != (const char **) NULL)
288 *end=(const char *) p;
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % G l o b E x p r e s s i o n %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % GlobExpression() returns MagickTrue if the expression matches the pattern.
304 % The format of the GlobExpression function is:
306 % MagickBooleanType GlobExpression(const char *expression,
307 % const char *pattern,const MagickBooleanType case_insensitive)
309 % A description of each parameter follows:
311 % o expression: Specifies a pointer to a text string containing a file name.
313 % o pattern: Specifies a pointer to a text string containing a pattern.
315 % o case_insensitive: set to MagickTrue to ignore the case when matching
319 MagickExport MagickBooleanType GlobExpression(const char *expression,
320 const char *pattern,const MagickBooleanType case_insensitive)
330 Return on empty pattern or '*'.
332 if (pattern == (char *) NULL)
334 if (GetUTFCode(pattern) == 0)
336 if (LocaleCompare(pattern,"*") == 0)
338 p=pattern+strlen(pattern)-1;
339 if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
348 Determine if pattern is a scene, i.e. img0001.pcd[2].
350 image_info=AcquireImageInfo();
351 (void) CopyMagickString(image_info->filename,pattern,MaxTextExtent);
352 exception=AcquireExceptionInfo();
353 (void) SetImageInfo(image_info,0,exception);
354 exception=DestroyExceptionInfo(exception);
355 if (LocaleCompare(image_info->filename,pattern) != 0)
357 image_info=DestroyImageInfo(image_info);
360 image_info=DestroyImageInfo(image_info);
363 Evaluate glob expression.
366 while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
368 if (GetUTFCode(expression) == 0)
369 if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
371 switch (GetUTFCode(pattern))
379 pattern+=GetUTFOctets(pattern);
380 while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
382 status=GlobExpression(expression,pattern,case_insensitive);
383 expression+=GetUTFOctets(expression);
385 if (status != MagickFalse)
387 while (GetUTFCode(expression) != 0)
388 expression+=GetUTFOctets(expression);
389 while (GetUTFCode(pattern) != 0)
390 pattern+=GetUTFOctets(pattern);
399 pattern+=GetUTFOctets(pattern);
402 if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
407 if (GetUTFCode(pattern) == '\\')
409 pattern+=GetUTFOctets(pattern);
410 if (GetUTFCode(pattern) == 0)
416 if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
418 c=GetUTFCode(pattern);
419 pattern+=GetUTFOctets(pattern);
420 pattern+=GetUTFOctets(pattern);
421 if (GetUTFCode(pattern) == ']')
426 if (GetUTFCode(pattern) == '\\')
428 pattern+=GetUTFOctets(pattern);
429 if (GetUTFCode(pattern) == 0)
435 if ((GetUTFCode(expression) < c) ||
436 (GetUTFCode(expression) > GetUTFCode(pattern)))
438 pattern+=GetUTFOctets(pattern);
443 if (GetUTFCode(pattern) != GetUTFCode(expression))
445 pattern+=GetUTFOctets(pattern);
448 pattern+=GetUTFOctets(pattern);
449 while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
451 if ((GetUTFCode(pattern) == '\\') &&
452 (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
453 pattern+=GetUTFOctets(pattern);
454 pattern+=GetUTFOctets(pattern);
456 if (GetUTFCode(pattern) != 0)
458 pattern+=GetUTFOctets(pattern);
459 expression+=GetUTFOctets(expression);
467 pattern+=GetUTFOctets(pattern);
468 expression+=GetUTFOctets(expression);
476 pattern+=GetUTFOctets(pattern);
477 while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
481 while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
482 (GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
483 (match != MagickFalse))
485 if (GetUTFCode(pattern) == '\\')
486 pattern+=GetUTFOctets(pattern);
487 match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
490 pattern+=GetUTFOctets(pattern);
492 if (GetUTFCode(pattern) == 0)
499 if (match != MagickFalse)
502 while ((GetUTFCode(pattern) != '}') &&
503 (GetUTFCode(pattern) != 0))
505 pattern+=GetUTFOctets(pattern);
506 if (GetUTFCode(pattern) == '\\')
508 pattern+=GetUTFOctets(pattern);
509 if (GetUTFCode(pattern) == '}')
510 pattern+=GetUTFOctets(pattern);
516 while ((GetUTFCode(pattern) != '}') &&
517 (GetUTFCode(pattern) != ',') &&
518 (GetUTFCode(pattern) != 0))
520 pattern+=GetUTFOctets(pattern);
521 if (GetUTFCode(pattern) == '\\')
523 pattern+=GetUTFOctets(pattern);
524 if ((GetUTFCode(pattern) == '}') ||
525 (GetUTFCode(pattern) == ','))
526 pattern+=GetUTFOctets(pattern);
530 if (GetUTFCode(pattern) != 0)
531 pattern+=GetUTFOctets(pattern);
537 pattern+=GetUTFOctets(pattern);
538 if (GetUTFCode(pattern) == 0)
543 if (case_insensitive != MagickFalse)
545 if (tolower((int) GetUTFCode(expression)) !=
546 tolower((int) GetUTFCode(pattern)))
553 if (GetUTFCode(expression) != GetUTFCode(pattern))
558 expression+=GetUTFOctets(expression);
559 pattern+=GetUTFOctets(pattern);
563 while (GetUTFCode(pattern) == '*')
564 pattern+=GetUTFOctets(pattern);
565 match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
566 MagickTrue : MagickFalse;
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581 % IsGlob() returns MagickTrue if the path specification contains a globbing
584 % The format of the IsGlob method is:
586 % MagickBooleanType IsGlob(const char *geometry)
588 % A description of each parameter follows:
593 MagickPrivate MagickBooleanType IsGlob(const char *path)
598 if (IsPathAccessible(path) != MagickFalse)
600 status=(strchr(path,'*') != (char *) NULL) ||
601 (strchr(path,'?') != (char *) NULL) ||
602 (strchr(path,'{') != (char *) NULL) ||
603 (strchr(path,'}') != (char *) NULL) ||
604 (strchr(path,'[') != (char *) NULL) ||
605 (strchr(path,']') != (char *) NULL) ? MagickTrue : MagickFalse;
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % T o k e n i z e r %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
621 % one at a time from a string of characters. The characters used for white
622 % space, for break characters, and for quotes can be specified. Also,
623 % characters in the string can be preceded by a specifiable escape character
624 % which removes any special meaning the character may have.
626 % Here is some terminology:
628 % o token: A single unit of information in the form of a group of
631 % o white space: Apace that gets ignored (except within quotes or when
632 % escaped), like blanks and tabs. in addition, white space terminates a
635 % o break set: One or more characters that separates non-quoted tokens.
636 % Commas are a common break character. The usage of break characters to
637 % signal the end of a token is the same as that of white space, except
638 % multiple break characters with nothing or only white space between
639 % generate a null token for each two break characters together.
641 % For example, if blank is set to be the white space and comma is set to
642 % be the break character, the line
646 % ... consists of 5 tokens:
651 % 4) "" (the null string)
654 % o Quote character: A character that, when surrounding a group of other
655 % characters, causes the group of characters to be treated as a single
656 % token, no matter how many white spaces or break characters exist in
657 % the group. Also, a token always terminates after the closing quote.
658 % For example, if ' is the quote character, blank is white space, and
659 % comma is the break character, the following string
663 % ... consists of 4 tokens:
666 % 2) " B, CD" (note the blanks & comma)
670 % The quote characters themselves do not appear in the resultant
671 % tokens. The double quotes are delimiters i use here for
672 % documentation purposes only.
674 % o Escape character: A character which itself is ignored but which
675 % causes the next character to be used as is. ^ and \ are often used
676 % as escape characters. An escape in the last position of the string
677 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
678 % and non-escape) character. For example, assume white space, break
679 % character, and quote are the same as in the above examples, and
680 % further, assume that ^ is the escape character. Then, in the string
682 % ABC, ' DEF ^' GH' I ^ J K^ L ^
684 % ... there are 7 tokens:
689 % 4) " " (a lone blank)
692 % 7) "^" (passed as is at end of line)
694 % The format of the Tokenizer method is:
696 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
697 % const size_t max_token_length,const char *line,const char *white,
698 % const char *break_set,const char *quote,const char escape,
699 % char *breaker,int *next,char *quoted)
701 % A description of each parameter follows:
703 % o flag: right now, only the low order 3 bits are used.
705 % 1 => convert non-quoted tokens to upper case
706 % 2 => convert non-quoted tokens to lower case
707 % 0 => do not convert non-quoted tokens
709 % o token: a character string containing the returned next token
711 % o max_token_length: the maximum size of "token". Characters beyond
712 % "max_token_length" are truncated.
714 % o string: the string to be parsed.
716 % o white: a string of the valid white spaces. example:
718 % char whitesp[]={" \t"};
720 % blank and tab will be valid white space.
722 % o break: a string of the valid break characters. example:
724 % char breakch[]={";,"};
726 % semicolon and comma will be valid break characters.
728 % o quote: a string of the valid quote characters. An example would be
730 % char whitesp[]={"'\"");
732 % (this causes single and double quotes to be valid) Note that a
733 % token starting with one of these characters needs the same quote
734 % character to terminate it.
740 % is unterminated, but
744 % are properly terminated. Note that different quote characters
745 % can appear on the same line; only for a given token do the quote
746 % characters have to be the same.
748 % o escape: the escape character (NOT a string ... only one
749 % allowed). Use zero if none is desired.
751 % o breaker: the break character used to terminate the current
752 % token. If the token was quoted, this will be the quote used. If
753 % the token is the last one on the line, this will be zero.
755 % o next: this variable points to the first character of the
756 % next token. it gets reset by "tokenizer" as it steps through the
757 % string. Set it to 0 upon initialization, and leave it alone
758 % after that. You can change it if you want to jump around in the
759 % string or re-parse from the beginning, but be careful.
761 % o quoted: set to True if the token was quoted and MagickFalse
762 % if not. You may need this information (for example: in C, a
763 % string with quotes around it is a character string, while one
764 % without is an identifier).
766 % o result: 0 if we haven't reached EOS (end of string), and 1
776 static ssize_t sindex(int c,const char *string)
781 for (p=string; *p != '\0'; p++)
783 return((ssize_t) (p-string));
787 static void StoreToken(TokenInfo *token_info,char *string,
788 size_t max_token_length,int c)
793 if ((token_info->offset < 0) ||
794 ((size_t) token_info->offset >= (max_token_length-1)))
796 i=token_info->offset++;
798 if (token_info->state == IN_QUOTE)
800 switch (token_info->flag & 0x03)
804 string[i]=(char) toupper(c);
809 string[i]=(char) tolower(c);
817 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
818 char *token,const size_t max_token_length,const char *line,const char *white,
819 const char *break_set,const char *quote,const char escape,char *breaker,
820 int *next,char *quoted)
830 if (line[*next] == '\0')
832 token_info->state=IN_WHITE;
833 token_info->quote=(char) MagickFalse;
834 token_info->flag=flag;
835 for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
838 i=sindex(c,break_set);
841 switch (token_info->state)
848 *breaker=break_set[i];
849 token[token_info->offset]='\0';
854 StoreToken(token_info,token,max_token_length,c);
863 switch (token_info->state)
867 token_info->state=IN_QUOTE;
868 token_info->quote=quote[i];
869 *quoted=(char) MagickTrue;
874 if (quote[i] != token_info->quote)
875 StoreToken(token_info,token,max_token_length,c);
878 token_info->state=IN_OZONE;
879 token_info->quote='\0';
887 token[token_info->offset]='\0';
896 switch (token_info->state)
903 token_info->state=IN_OZONE;
908 StoreToken(token_info,token,max_token_length,c);
914 if (c == (int) escape)
916 if (line[(*next)+1] == '\0')
919 StoreToken(token_info,token,max_token_length,c);
921 token[token_info->offset]='\0';
924 switch (token_info->state)
929 token_info->state=IN_TOKEN;
937 StoreToken(token_info,token,max_token_length,c);
942 token[token_info->offset]='\0';
948 switch (token_info->state)
951 token_info->state=IN_TOKEN;
955 StoreToken(token_info,token,max_token_length,c);
960 token[token_info->offset]='\0';
965 token[token_info->offset]='\0';