MagickCore  6.9.12-67
Convert, Edit, Or Compose Bitmap Images
 All Data Structures
token.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % TTTTT OOO K K EEEEE N N %
7 % T O O K K E NN N %
8 % T O O KKK EEE N N N %
9 % T O O K K E N NN %
10 % T OOO K K EEEEE N N %
11 % %
12 % %
13 % MagickCore Token Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % January 1993 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
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. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 
40 /*
41  Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/exception.h"
45 #include "magick/exception-private.h"
46 #include "magick/image.h"
47 #include "magick/image-private.h"
48 #include "magick/locale-private.h"
49 #include "magick/memory_.h"
50 #include "magick/string_.h"
51 #include "magick/string-private.h"
52 #include "magick/token.h"
53 #include "magick/token-private.h"
54 #include "magick/utility.h"
55 
56 /*
57  Typedef declaractions.
58 */
59 struct _TokenInfo
60 {
61  int
62  state;
63 
64  MagickStatusType
65  flag;
66 
67  ssize_t
68  offset;
69 
70  char
71  quote;
72 
73  size_t
74  signature;
75 };
76 
77 /*
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % %
80 % %
81 % %
82 % A c q u i r e T o k e n I n f o %
83 % %
84 % %
85 % %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %
88 % AcquireTokenInfo() allocates the TokenInfo structure.
89 %
90 % The format of the AcquireTokenInfo method is:
91 %
92 % TokenInfo *AcquireTokenInfo()
93 %
94 */
95 MagickExport TokenInfo *AcquireTokenInfo(void)
96 {
97  TokenInfo
98  *token_info;
99 
100  token_info=(TokenInfo *) AcquireMagickMemory(sizeof(*token_info));
101  if (token_info == (TokenInfo *) NULL)
102  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
103  token_info->signature=MagickCoreSignature;
104  return(token_info);
105 }
106 
107 /*
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % %
110 % %
111 % %
112 % D e s t r o y T o k e n I n f o %
113 % %
114 % %
115 % %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 %
118 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
119 % structure.
120 %
121 % The format of the DestroyTokenInfo method is:
122 %
123 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
124 %
125 % A description of each parameter follows:
126 %
127 % o token_info: Specifies a pointer to an TokenInfo structure.
128 %
129 */
130 MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
131 {
132  assert(token_info != (TokenInfo *) NULL);
133  assert(token_info->signature == MagickCoreSignature);
134  if (IsEventLogging() != MagickFalse)
135  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
136  token_info->signature=(~MagickCoreSignature);
137  token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
138  return(token_info);
139 }
140 
141 /*
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % %
144 % %
145 % %
146 + G e t N e x t T o k e n %
147 % %
148 % %
149 % %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 %
152 % GetNextToken() gets a token from the token stream. A token is defined as
153 % a sequence of characters delimited by whitespace (e.g. clip-path), a
154 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
155 % parenthesis (e.g. rgb(0,0,0)). GetNextToken() also recognizes these
156 % separator characters: ':', '=', ',', and ';'. GetNextToken() returns the
157 % length of the consumed token.
158 %
159 % The format of the GetNextToken method is:
160 %
161 % size_t GetNextToken(const char *magick_restrict start,
162 % const char **magick_restrict end,const size_t extent,
163 % char *magick_restrict token)
164 %
165 % A description of each parameter follows:
166 %
167 % o start: the start of the token sequence.
168 %
169 % o end: point to the end of the token sequence.
170 %
171 % o extent: maximum extent of the token.
172 %
173 % o token: copy the token to this buffer.
174 %
175 */
176 MagickExport magick_hot_spot size_t GetNextToken(
177  const char *magick_restrict start,const char **magick_restrict end,
178  const size_t extent,char *magick_restrict token)
179 {
180  double
181  value;
182 
183  char
184  *magick_restrict q;
185 
186  const char
187  *magick_restrict p;
188 
189  ssize_t
190  i;
191 
192  assert(start != (const char *) NULL);
193  assert(token != (char *) NULL);
194  i=0;
195  p=start;
196  while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
197  p++;
198  switch (*p)
199  {
200  case '\0':
201  break;
202  case '"':
203  case '\'':
204  case '`':
205  case '{':
206  {
207  char
208  escape;
209 
210  switch (*p)
211  {
212  case '"': escape='"'; break;
213  case '\'': escape='\''; break;
214  case '`': escape='\''; break;
215  case '{': escape='}'; break;
216  default: escape=(*p); break;
217  }
218  for (p++; *p != '\0'; p++)
219  {
220  if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
221  p++;
222  else
223  if (*p == escape)
224  {
225  p++;
226  break;
227  }
228  if (i < (ssize_t) (extent-1))
229  token[i++]=(*p);
230  if ((size_t) (p-start) >= (extent-1))
231  break;
232  }
233  break;
234  }
235  case '/':
236  {
237  if (i < (ssize_t) (extent-1))
238  token[i++]=(*p);
239  p++;
240  if ((*p == '>') || (*p == '/'))
241  {
242  if (i < (ssize_t) (extent-1))
243  token[i++]=(*p);
244  p++;
245  }
246  break;
247  }
248  default:
249  {
250  char
251  *q;
252 
253  value=StringToDouble(p,&q);
254  (void) value;
255  if ((p != q) && (*p != ','))
256  {
257  for ( ; (p < q) && (*p != ','); p++)
258  {
259  if (i < (ssize_t) (extent-1))
260  token[i++]=(*p);
261  if ((size_t) (p-start) >= (extent-1))
262  break;
263  }
264  if (*p == '%')
265  {
266  if (i < (ssize_t) (extent-1))
267  token[i++]=(*p);
268  p++;
269  }
270  break;
271  }
272  if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
273  (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
274  {
275  if (i < (ssize_t) (extent-1))
276  token[i++]=(*p);
277  p++;
278  break;
279  }
280  for ( ; *p != '\0'; p++)
281  {
282  if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
283  (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
284  break;
285  if ((i > 0) && (*p == '<'))
286  break;
287  if (i < (ssize_t) (extent-1))
288  token[i++]=(*p);
289  if (*p == '>')
290  break;
291  if (*p == '(')
292  {
293  for (p++; *p != '\0'; p++)
294  {
295  if (i < (ssize_t) (extent-1))
296  token[i++]=(*p);
297  if ((*p == ')') && (*(p-1) != '\\'))
298  break;
299  if ((size_t) (p-start) >= (extent-1))
300  break;
301  }
302  if (*p == '\0')
303  break;
304  }
305  if ((size_t) (p-start) >= (extent-1))
306  break;
307  }
308  break;
309  }
310  }
311  token[i]='\0';
312  if (LocaleNCompare(token,"url(#",5) == 0)
313  {
314  q=strrchr(token,')');
315  if (q != (char *) NULL)
316  {
317  *q='\0';
318  (void) memmove(token,token+5,(size_t) (q-token-4));
319  }
320  }
321  while (isspace((int) ((unsigned char) *p)) != 0)
322  p++;
323  if (end != (const char **) NULL)
324  *end=(const char *) p;
325  return(p-start+1);
326 }
327 
328 /*
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % %
331 % %
332 % %
333 % G l o b E x p r e s s i o n %
334 % %
335 % %
336 % %
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 %
339 % GlobExpression() returns MagickTrue if the expression matches the pattern.
340 %
341 % The format of the GlobExpression function is:
342 %
343 % MagickBooleanType GlobExpression(const char *magick_restrict expression,
344 % const char *magick_restrict pattern,
345 % const MagickBooleanType case_insensitive)
346 %
347 % A description of each parameter follows:
348 %
349 % o expression: Specifies a pointer to a text string containing a file name.
350 %
351 % o pattern: Specifies a pointer to a text string containing a pattern.
352 %
353 % o case_insensitive: set to MagickTrue to ignore the case when matching
354 % an expression.
355 %
356 */
357 MagickExport MagickBooleanType GlobExpression(
358  const char *magick_restrict expression,const char *magick_restrict pattern,
359  const MagickBooleanType case_insensitive)
360 {
361  char
362  path[MagickPathExtent];
363 
364  MagickBooleanType
365  done,
366  match;
367 
368  /*
369  Return on empty pattern or '*'.
370  */
371  if (pattern == (char *) NULL)
372  return(MagickTrue);
373  if (GetUTFCode(pattern) == 0)
374  return(MagickTrue);
375  if (LocaleCompare(pattern,"*") == 0)
376  return(MagickTrue);
377  GetPathComponent(pattern,SubimagePath,path);
378  if (*path != '\0')
379  return(MagickFalse);
380  /*
381  Evaluate glob expression.
382  */
383  done=MagickFalse;
384  while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
385  {
386  if (GetUTFCode(expression) == 0)
387  if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
388  break;
389  switch (GetUTFCode(pattern))
390  {
391  case '*':
392  {
393  MagickBooleanType
394  status;
395 
396  status=MagickFalse;
397  while (GetUTFCode(pattern) == '*')
398  pattern+=GetUTFOctets(pattern);
399  while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
400  {
401  status=GlobExpression(expression,pattern,case_insensitive);
402  expression+=GetUTFOctets(expression);
403  }
404  if (status != MagickFalse)
405  {
406  while (GetUTFCode(expression) != 0)
407  expression+=GetUTFOctets(expression);
408  while (GetUTFCode(pattern) != 0)
409  pattern+=GetUTFOctets(pattern);
410  }
411  break;
412  }
413  case '[':
414  {
415  int
416  c;
417 
418  pattern+=GetUTFOctets(pattern);
419  for ( ; ; )
420  {
421  if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
422  {
423  done=MagickTrue;
424  break;
425  }
426  if (GetUTFCode(pattern) == '\\')
427  {
428  pattern+=GetUTFOctets(pattern);
429  if (GetUTFCode(pattern) == 0)
430  {
431  done=MagickTrue;
432  break;
433  }
434  }
435  if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
436  {
437  c=GetUTFCode(pattern);
438  pattern+=GetUTFOctets(pattern);
439  pattern+=GetUTFOctets(pattern);
440  if (GetUTFCode(pattern) == ']')
441  {
442  done=MagickTrue;
443  break;
444  }
445  if (GetUTFCode(pattern) == '\\')
446  {
447  pattern+=GetUTFOctets(pattern);
448  if (GetUTFCode(pattern) == 0)
449  {
450  done=MagickTrue;
451  break;
452  }
453  }
454  if ((GetUTFCode(expression) < c) ||
455  (GetUTFCode(expression) > GetUTFCode(pattern)))
456  {
457  pattern+=GetUTFOctets(pattern);
458  continue;
459  }
460  }
461  else
462  if (GetUTFCode(pattern) != GetUTFCode(expression))
463  {
464  pattern+=GetUTFOctets(pattern);
465  continue;
466  }
467  pattern+=GetUTFOctets(pattern);
468  while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
469  {
470  if ((GetUTFCode(pattern) == '\\') &&
471  (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
472  pattern+=GetUTFOctets(pattern);
473  pattern+=GetUTFOctets(pattern);
474  }
475  if (GetUTFCode(pattern) != 0)
476  {
477  pattern+=GetUTFOctets(pattern);
478  expression+=GetUTFOctets(expression);
479  }
480  break;
481  }
482  break;
483  }
484  case '?':
485  {
486  pattern+=GetUTFOctets(pattern);
487  expression+=GetUTFOctets(expression);
488  break;
489  }
490  case '{':
491  {
492  char
493  *target;
494 
495  char
496  *p;
497 
498  target=AcquireString(pattern);
499  p=target;
500  pattern++;
501  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
502  {
503  *p++=(*pattern++);
504  if ((GetUTFCode(pattern) == ',') || (GetUTFCode(pattern) == '}'))
505  {
506  *p='\0';
507  match=GlobExpression(expression,target,case_insensitive);
508  if (match != MagickFalse)
509  {
510  expression+=MagickMin(strlen(expression),strlen(target));
511  break;
512  }
513  p=target;
514  pattern+=GetUTFOctets(pattern);
515  }
516  }
517  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
518  pattern+=GetUTFOctets(pattern);
519  if (GetUTFCode(pattern) != 0)
520  pattern+=GetUTFOctets(pattern);
521  target=DestroyString(target);
522  break;
523  }
524  case '\\':
525  {
526  pattern+=GetUTFOctets(pattern);
527  if (GetUTFCode(pattern) == 0)
528  break;
529  }
530  default:
531  {
532  if (case_insensitive != MagickFalse)
533  {
534  if (LocaleToLowercase((int) GetUTFCode(expression)) != LocaleToLowercase((int) GetUTFCode(pattern)))
535  {
536  done=MagickTrue;
537  break;
538  }
539  }
540  else
541  if (GetUTFCode(expression) != GetUTFCode(pattern))
542  {
543  done=MagickTrue;
544  break;
545  }
546  expression+=GetUTFOctets(expression);
547  pattern+=GetUTFOctets(pattern);
548  }
549  }
550  }
551  while (GetUTFCode(pattern) == '*')
552  pattern+=GetUTFOctets(pattern);
553  match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
554  MagickTrue : MagickFalse;
555  return(match);
556 }
557 
558 /*
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % %
561 % %
562 % %
563 + I s G l o b %
564 % %
565 % %
566 % %
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 %
569 % IsGlob() returns MagickTrue if the path specification contains a globbing
570 % pattern.
571 %
572 % The format of the IsGlob method is:
573 %
574 % MagickBooleanType IsGlob(const char *geometry)
575 %
576 % A description of each parameter follows:
577 %
578 % o path: the path.
579 %
580 */
581 MagickExport MagickBooleanType IsGlob(const char *path)
582 {
583  MagickBooleanType
584  status = MagickFalse;
585 
586  const char
587  *p;
588 
589  if (IsPathAccessible(path) != MagickFalse)
590  return(MagickFalse);
591  for (p=path; *p != '\0'; p++)
592  {
593  switch (*p)
594  {
595  case '*':
596  case '?':
597  case '{':
598  case '}':
599  case '[':
600  case ']':
601  {
602  status=MagickTrue;
603  break;
604  }
605  default:
606  break;
607  }
608  }
609  return(status);
610 }
611 
612 /*
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % %
615 % %
616 % %
617 % I s M a g i c k T r u e %
618 % %
619 % %
620 % %
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 %
623 % IsMagickTrue() returns MagickTrue if the value is "true", "on", "yes" or
624 % "1".
625 %
626 % The format of the IsMagickTrue method is:
627 %
628 % MagickBooleanType IsMagickTrue(const char *value)
629 %
630 % A description of each parameter follows:
631 %
632 % o option: either MagickTrue or MagickFalse depending on the value
633 % parameter.
634 %
635 % o value: Specifies a pointer to a character array.
636 %
637 */
638 MagickExport MagickBooleanType IsMagickTrue(const char *value)
639 {
640  if (value == (const char *) NULL)
641  return(MagickFalse);
642  if (LocaleCompare(value,"true") == 0)
643  return(MagickTrue);
644  if (LocaleCompare(value,"on") == 0)
645  return(MagickTrue);
646  if (LocaleCompare(value,"yes") == 0)
647  return(MagickTrue);
648  if (LocaleCompare(value,"1") == 0)
649  return(MagickTrue);
650  return(MagickFalse);
651 }
652 
653 /*
654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % %
656 % %
657 % %
658 % T o k e n i z e r %
659 % %
660 % %
661 % %
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 %
664 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
665 % one at a time from a string of characters. The characters used for white
666 % space, for break characters, and for quotes can be specified. Also,
667 % characters in the string can be preceded by a specifiable escape character
668 % which removes any special meaning the character may have.
669 %
670 % Here is some terminology:
671 %
672 % o token: A single unit of information in the form of a group of
673 % characters.
674 %
675 % o white space: Apace that gets ignored (except within quotes or when
676 % escaped), like blanks and tabs. in addition, white space terminates a
677 % non-quoted token.
678 %
679 % o break set: One or more characters that separates non-quoted tokens.
680 % Commas are a common break character. The usage of break characters to
681 % signal the end of a token is the same as that of white space, except
682 % multiple break characters with nothing or only white space between
683 % generate a null token for each two break characters together.
684 %
685 % For example, if blank is set to be the white space and comma is set to
686 % be the break character, the line
687 %
688 % A, B, C , , DEF
689 %
690 % ... consists of 5 tokens:
691 %
692 % 1) "A"
693 % 2) "B"
694 % 3) "C"
695 % 4) "" (the null string)
696 % 5) "DEF"
697 %
698 % o Quote character: A character that, when surrounding a group of other
699 % characters, causes the group of characters to be treated as a single
700 % token, no matter how many white spaces or break characters exist in
701 % the group. Also, a token always terminates after the closing quote.
702 % For example, if ' is the quote character, blank is white space, and
703 % comma is the break character, the following string
704 %
705 % A, ' B, CD'EF GHI
706 %
707 % ... consists of 4 tokens:
708 %
709 % 1) "A"
710 % 2) " B, CD" (note the blanks & comma)
711 % 3) "EF"
712 % 4) "GHI"
713 %
714 % The quote characters themselves do not appear in the resultant
715 % tokens. The double quotes are delimiters i use here for
716 % documentation purposes only.
717 %
718 % o Escape character: A character which itself is ignored but which
719 % causes the next character to be used as is. ^ and \ are often used
720 % as escape characters. An escape in the last position of the string
721 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
722 % and non-escape) character. For example, assume white space, break
723 % character, and quote are the same as in the above examples, and
724 % further, assume that ^ is the escape character. Then, in the string
725 %
726 % ABC, ' DEF ^' GH' I ^ J K^ L ^
727 %
728 % ... there are 7 tokens:
729 %
730 % 1) "ABC"
731 % 2) " DEF ' GH"
732 % 3) "I"
733 % 4) " " (a lone blank)
734 % 5) "J"
735 % 6) "K L"
736 % 7) "^" (passed as is at end of line)
737 %
738 % The format of the Tokenizer method is:
739 %
740 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
741 % const size_t max_token_length,const char *line,const char *white,
742 % const char *break_set,const char *quote,const char escape,
743 % char *breaker,int *next,char *quoted)
744 %
745 % A description of each parameter follows:
746 %
747 % o flag: right now, only the low order 3 bits are used.
748 %
749 % 1 => convert non-quoted tokens to upper case
750 % 2 => convert non-quoted tokens to lower case
751 % 0 => do not convert non-quoted tokens
752 %
753 % o token: a character string containing the returned next token
754 %
755 % o max_token_length: the maximum size of "token". Characters beyond
756 % "max_token_length" are truncated.
757 %
758 % o string: the string to be parsed.
759 %
760 % o white: a string of the valid white spaces. example:
761 %
762 % char whitesp[]={" \t"};
763 %
764 % blank and tab will be valid white space.
765 %
766 % o break: a string of the valid break characters. example:
767 %
768 % char breakch[]={";,"};
769 %
770 % semicolon and comma will be valid break characters.
771 %
772 % o quote: a string of the valid quote characters. An example would be
773 %
774 % char whitesp[]={"'\"");
775 %
776 % (this causes single and double quotes to be valid) Note that a
777 % token starting with one of these characters needs the same quote
778 % character to terminate it.
779 %
780 % for example:
781 %
782 % "ABC '
783 %
784 % is unterminated, but
785 %
786 % "DEF" and 'GHI'
787 %
788 % are properly terminated. Note that different quote characters
789 % can appear on the same line; only for a given token do the quote
790 % characters have to be the same.
791 %
792 % o escape: the escape character (NOT a string ... only one
793 % allowed). Use zero if none is desired.
794 %
795 % o breaker: the break character used to terminate the current
796 % token. If the token was quoted, this will be the quote used. If
797 % the token is the last one on the line, this will be zero.
798 %
799 % o next: this variable points to the first character of the
800 % next token. it gets reset by "tokenizer" as it steps through the
801 % string. Set it to 0 upon initialization, and leave it alone
802 % after that. You can change it if you want to jump around in the
803 % string or re-parse from the beginning, but be careful.
804 %
805 % o quoted: set to True if the token was quoted and MagickFalse
806 % if not. You may need this information (for example: in C, a
807 % string with quotes around it is a character string, while one
808 % without is an identifier).
809 %
810 % o result: 0 if we haven't reached EOS (end of string), and 1
811 % if we have.
812 %
813 */
814 
815 #define IN_WHITE 0
816 #define IN_TOKEN 1
817 #define IN_QUOTE 2
818 #define IN_OZONE 3
819 
820 static ssize_t sindex(int c,const char *string)
821 {
822  const char
823  *p;
824 
825  for (p=string; *p != '\0'; p++)
826  if (c == (int) (*p))
827  return((ssize_t) (p-string));
828  return(-1);
829 }
830 
831 static void StoreToken(TokenInfo *token_info,char *string,
832  size_t max_token_length,int c)
833 {
834  ssize_t
835  i;
836 
837  if ((token_info->offset < 0) ||
838  ((size_t) token_info->offset >= (max_token_length-1)))
839  return;
840  i=token_info->offset++;
841  string[i]=(char) c;
842  if (token_info->state == IN_QUOTE)
843  return;
844  switch (token_info->flag & 0x03)
845  {
846  case 1:
847  {
848  string[i]=(char) LocaleToUppercase(c);
849  break;
850  }
851  case 2:
852  {
853  string[i]=(char) LocaleToLowercase(c);
854  break;
855  }
856  default:
857  break;
858  }
859 }
860 
861 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
862  char *token,const size_t max_token_length,const char *line,const char *white,
863  const char *break_set,const char *quote,const char escape,char *breaker,
864  int *next,char *quoted)
865 {
866  int
867  c;
868 
869  ssize_t
870  i;
871 
872  *breaker='\0';
873  *quoted='\0';
874  if (line[*next] == '\0')
875  return(1);
876  token_info->state=IN_WHITE;
877  token_info->quote=(char) MagickFalse;
878  token_info->flag=flag;
879  for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
880  {
881  c=(int) line[*next];
882  i=sindex(c,break_set);
883  if (i >= 0)
884  {
885  switch (token_info->state)
886  {
887  case IN_WHITE:
888  case IN_TOKEN:
889  case IN_OZONE:
890  {
891  (*next)++;
892  *breaker=break_set[i];
893  token[token_info->offset]='\0';
894  return(0);
895  }
896  case IN_QUOTE:
897  {
898  StoreToken(token_info,token,max_token_length,c);
899  break;
900  }
901  }
902  continue;
903  }
904  i=sindex(c,quote);
905  if (i >= 0)
906  {
907  switch (token_info->state)
908  {
909  case IN_WHITE:
910  {
911  token_info->state=IN_QUOTE;
912  token_info->quote=quote[i];
913  *quoted=(char) MagickTrue;
914  break;
915  }
916  case IN_QUOTE:
917  {
918  if (quote[i] != token_info->quote)
919  StoreToken(token_info,token,max_token_length,c);
920  else
921  {
922  token_info->state=IN_OZONE;
923  token_info->quote='\0';
924  }
925  break;
926  }
927  case IN_TOKEN:
928  case IN_OZONE:
929  {
930  *breaker=(char) c;
931  token[token_info->offset]='\0';
932  return(0);
933  }
934  }
935  continue;
936  }
937  i=sindex(c,white);
938  if (i >= 0)
939  {
940  switch (token_info->state)
941  {
942  case IN_WHITE:
943  case IN_OZONE:
944  break;
945  case IN_TOKEN:
946  {
947  token_info->state=IN_OZONE;
948  break;
949  }
950  case IN_QUOTE:
951  {
952  StoreToken(token_info,token,max_token_length,c);
953  break;
954  }
955  }
956  continue;
957  }
958  if (c == (int) escape)
959  {
960  if (line[(*next)+1] == '\0')
961  {
962  *breaker='\0';
963  StoreToken(token_info,token,max_token_length,c);
964  (*next)++;
965  token[token_info->offset]='\0';
966  return(0);
967  }
968  switch (token_info->state)
969  {
970  case IN_WHITE:
971  {
972  (*next)--;
973  token_info->state=IN_TOKEN;
974  break;
975  }
976  case IN_TOKEN:
977  case IN_QUOTE:
978  {
979  (*next)++;
980  c=(int) line[*next];
981  StoreToken(token_info,token,max_token_length,c);
982  break;
983  }
984  case IN_OZONE:
985  {
986  token[token_info->offset]='\0';
987  return(0);
988  }
989  }
990  continue;
991  }
992  switch (token_info->state)
993  {
994  case IN_WHITE:
995  {
996  token_info->state=IN_TOKEN;
997  StoreToken(token_info,token,max_token_length,c);
998  break;
999  }
1000  case IN_TOKEN:
1001  case IN_QUOTE:
1002  {
1003  StoreToken(token_info,token,max_token_length,c);
1004  break;
1005  }
1006  case IN_OZONE:
1007  {
1008  token[token_info->offset]='\0';
1009  return(0);
1010  }
1011  }
1012  }
1013  token[token_info->offset]='\0';
1014  return(0);
1015 }