summaryrefslogtreecommitdiff
path: root/apps/gui/wps_parser.c
diff options
context:
space:
mode:
authorNicolas Pennequin <nicolas.pennequin@free.fr>2007-04-15 02:59:34 +0000
committerNicolas Pennequin <nicolas.pennequin@free.fr>2007-04-15 02:59:34 +0000
commit2e1169bddaa424ae5eef36e2ec12e2a3cb0adc68 (patch)
tree6967092524e64d2365034073efef820b41d31a2d /apps/gui/wps_parser.c
parentac946416060c19eb6cfe9da3d1885baee329df30 (diff)
downloadrockbox-2e1169bddaa424ae5eef36e2ec12e2a3cb0adc68.tar.gz
rockbox-2e1169bddaa424ae5eef36e2ec12e2a3cb0adc68.zip
* Make the WPS parser close open conditionals on new sublines and comments as well as new lines.
* Make the displaying code check for invalid conditional constructs in order to avoid some rare cases of infinite looping. * Make the WPS parser check that it doesn't read more strings than it can. * Increase the string buffer size (from 512 to 1024, to accomodate the TextBox WPS which uses a lot of unicode characters). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13162 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/gui/wps_parser.c')
-rw-r--r--apps/gui/wps_parser.c64
1 files changed, 40 insertions, 24 deletions
diff --git a/apps/gui/wps_parser.c b/apps/gui/wps_parser.c
index e72079ce71..2c7a000367 100644
--- a/apps/gui/wps_parser.c
+++ b/apps/gui/wps_parser.c
@@ -270,12 +270,29 @@ static int skip_end_of_line(const char *wps_bufptr)
270} 270}
271 271
272/* Starts a new subline in the current line during parsing */ 272/* Starts a new subline in the current line during parsing */
273static void wps_start_new_subline(struct wps_data *data) { 273static void wps_start_new_subline(struct wps_data *data)
274{
274 data->num_sublines++; 275 data->num_sublines++;
275 data->sublines[data->num_sublines].first_token_idx = data->num_tokens; 276 data->sublines[data->num_sublines].first_token_idx = data->num_tokens;
276 data->lines[data->num_lines].num_sublines++; 277 data->lines[data->num_lines].num_sublines++;
277} 278}
278 279
280static void close_conditionals(struct wps_data *data, int n)
281{
282 int i;
283 for (i = 0; i < n; i++)
284 {
285 data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_END;
286 if (lastcond[level])
287 data->tokens[lastcond[level]].value.i = data->num_tokens;
288
289 lastcond[level] = 0;
290 data->num_tokens++;
291 data->tokens[condindex[level]].value.i = numoptions[level];
292 level--;
293 }
294}
295
279#ifdef HAVE_LCD_BITMAP 296#ifdef HAVE_LCD_BITMAP
280 297
281static int parse_statusbar_enable(const char *wps_bufptr, 298static int parse_statusbar_enable(const char *wps_bufptr,
@@ -638,6 +655,7 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
638 return false; 655 return false;
639 656
640 char *current_string = data->string_buffer; 657 char *current_string = data->string_buffer;
658 int stringbuf_used = 0;
641 659
642 while(*wps_bufptr && data->num_tokens < WPS_MAX_TOKENS - 1 660 while(*wps_bufptr && data->num_tokens < WPS_MAX_TOKENS - 1
643 && data->num_lines < WPS_MAX_LINES) 661 && data->num_lines < WPS_MAX_LINES)
@@ -652,6 +670,9 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
652 670
653 /* Alternating sublines separator */ 671 /* Alternating sublines separator */
654 case ';': 672 case ';':
673 if (level >= 0)
674 close_conditionals(data, level + 1);
675
655 if (data->num_sublines+1 < WPS_MAX_SUBLINES) 676 if (data->num_sublines+1 < WPS_MAX_SUBLINES)
656 wps_start_new_subline(data); 677 wps_start_new_subline(data);
657 else 678 else
@@ -670,17 +691,7 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
670 if (level < 0) /* not in a conditional, ignore the char */ 691 if (level < 0) /* not in a conditional, ignore the char */
671 break; 692 break;
672 693
673condlistend: /* close a conditional. sometimes we want to close them even when 694 close_conditionals(data, 1);
674 we don't have a closing token, e.g. at the end of a line. */
675
676 data->tokens[data->num_tokens].type = WPS_TOKEN_CONDITIONAL_END;
677 if (lastcond[level])
678 data->tokens[lastcond[level]].value.i = data->num_tokens;
679
680 lastcond[level] = 0;
681 data->num_tokens++;
682 data->tokens[condindex[level]].value.i = numoptions[level];
683 level--;
684 break; 695 break;
685 696
686 /* Conditional list option */ 697 /* Conditional list option */
@@ -699,19 +710,17 @@ condlistend: /* close a conditional. sometimes we want to close them even when
699 710
700 /* Comment */ 711 /* Comment */
701 case '#': 712 case '#':
713 if (level >= 0)
714 close_conditionals(data, level + 1);
715
702 wps_bufptr += skip_end_of_line(wps_bufptr); 716 wps_bufptr += skip_end_of_line(wps_bufptr);
703 break; 717 break;
704 718
705 /* End of this line */ 719 /* End of this line */
706 case '\n': 720 case '\n':
707 if (level >= 0) 721 if (level >= 0)
708 { 722 close_conditionals(data, level + 1);
709 /* We have unclosed conditionals, so we 723
710 close them before adding the EOL token */
711 wps_bufptr--;
712 goto condlistend;
713 break;
714 }
715 wps_start_new_subline(data); 724 wps_start_new_subline(data);
716 data->num_lines++; /* Start a new line */ 725 data->num_lines++; /* Start a new line */
717 726
@@ -726,29 +735,36 @@ condlistend: /* close a conditional. sometimes we want to close them even when
726 735
727 /* String */ 736 /* String */
728 default: 737 default:
729 if (data->num_strings < WPS_MAX_STRINGS) 738 if (data->num_strings < WPS_MAX_STRINGS
739 && stringbuf_used < STRING_BUFFER_SIZE - 1)
730 { 740 {
731 data->tokens[data->num_tokens].type = WPS_TOKEN_STRING; 741 data->tokens[data->num_tokens].type = WPS_TOKEN_STRING;
732 data->strings[data->num_strings] = current_string; 742 data->strings[data->num_strings] = current_string;
733 data->tokens[data->num_tokens].value.i = data->num_strings++; 743 data->tokens[data->num_tokens].value.i = data->num_strings;
734 data->num_tokens++; 744 data->num_tokens++;
735 745
736 /* Copy the first byte */ 746 /* Copy the first byte */
737 *current_string++ = *(wps_bufptr - 1); 747 *current_string++ = *(wps_bufptr - 1);
748 stringbuf_used++;
738 749
739 /* continue until we hit something that ends the string */ 750 /* continue until we hit something that ends the string
751 or we run out of memory */
740 while(wps_bufptr && *wps_bufptr != '#' && 752 while(wps_bufptr && *wps_bufptr != '#' &&
741 *wps_bufptr != '%' && *wps_bufptr != ';' && 753 *wps_bufptr != '%' && *wps_bufptr != ';' &&
742 *wps_bufptr != '<' && *wps_bufptr != '>' && 754 *wps_bufptr != '<' && *wps_bufptr != '>' &&
743 *wps_bufptr != '|' && *wps_bufptr != '\n') 755 *wps_bufptr != '|' && *wps_bufptr != '\n' &&
756 stringbuf_used < STRING_BUFFER_SIZE - 1)
744 { 757 {
745 *current_string++ = *wps_bufptr++; 758 *current_string++ = *wps_bufptr++;
759 stringbuf_used++;
746 } 760 }
747 761
748 /* null terminate the string */ 762 /* null terminate the string */
749 *current_string++ = '\0'; 763 *current_string++ = '\0';
750 } 764 stringbuf_used++;
751 765
766 data->num_strings++;
767 }
752 break; 768 break;
753 } 769 }
754 } 770 }