summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/gui/wps_parser.c79
-rw-r--r--apps/misc.c66
-rw-r--r--apps/misc.h3
3 files changed, 103 insertions, 45 deletions
diff --git a/apps/gui/wps_parser.c b/apps/gui/wps_parser.c
index fd4db2e9de..54f9f588bf 100644
--- a/apps/gui/wps_parser.c
+++ b/apps/gui/wps_parser.c
@@ -489,7 +489,7 @@ static int parse_image_load(const char *wps_bufptr,
489 489
490 ptr++; 490 ptr++;
491 491
492 if (!(ptr = parse_list("ssdd", '|', ptr, &id, &filename, &x, &y))) 492 if (!(ptr = parse_list("ssdd", NULL, '|', ptr, &id, &filename, &x, &y)))
493 return WPS_ERROR_INVALID_PARAM; 493 return WPS_ERROR_INVALID_PARAM;
494 494
495 /* Check there is a terminating | */ 495 /* Check there is a terminating | */
@@ -540,17 +540,34 @@ static int parse_viewport(const char *wps_bufptr,
540 struct wps_token *token, 540 struct wps_token *token,
541 struct wps_data *wps_data) 541 struct wps_data *wps_data)
542{ 542{
543 (void)token; /* Kill warnings */
543 const char *ptr = wps_bufptr; 544 const char *ptr = wps_bufptr;
544 struct viewport* vp; 545 struct viewport* vp;
545 int depth; 546 int depth;
546 547 int valid = 0;
547 (void)token; /* Kill warnings */ 548 enum {
549 PL_X = 0,
550 PL_Y,
551 PL_WIDTH,
552 PL_HEIGHT,
553 PL_FONT,
554 PL_FG,
555 PL_BG,
556 };
557 int lcd_width = LCD_WIDTH, lcd_height = LCD_HEIGHT;
558#ifdef HAVE_REMOTE_LCD
559 if (wps_data->remote_wps)
560 {
561 lcd_width = LCD_REMOTE_WIDTH;
562 lcd_height = LCD_REMOTE_HEIGHT;
563 }
564#endif
548 565
549 if (*wps_bufptr != '|') 566 if (*wps_bufptr != '|')
550 return WPS_ERROR_INVALID_PARAM; /* malformed token: e.g. %Cl7 */ 567 return WPS_ERROR_INVALID_PARAM; /* malformed token: e.g. %Cl7 */
551 568
552 ptr = wps_bufptr + 1; 569 ptr = wps_bufptr + 1;
553 /* format: %V|x|y|width|height|fg_pattern|bg_pattern| */ 570 /* format: %V|x|y|width|height|font|fg_pattern|bg_pattern| */
554 571
555 if (wps_data->num_viewports >= WPS_MAX_VIEWPORTS) 572 if (wps_data->num_viewports >= WPS_MAX_VIEWPORTS)
556 return WPS_ERROR_INVALID_PARAM; 573 return WPS_ERROR_INVALID_PARAM;
@@ -573,7 +590,7 @@ static int parse_viewport(const char *wps_bufptr,
573#ifdef HAVE_LCD_COLOR 590#ifdef HAVE_LCD_COLOR
574 if (depth == 16) 591 if (depth == 16)
575 { 592 {
576 if (!(ptr = parse_list("dddddcc", '|', ptr, &vp->x, &vp->y, &vp->width, 593 if (!(ptr = parse_list("dddddcc", &valid, '|', ptr, &vp->x, &vp->y, &vp->width,
577 &vp->height, &vp->font, &vp->fg_pattern,&vp->bg_pattern))) 594 &vp->height, &vp->font, &vp->fg_pattern,&vp->bg_pattern)))
578 return WPS_ERROR_INVALID_PARAM; 595 return WPS_ERROR_INVALID_PARAM;
579 } 596 }
@@ -581,7 +598,10 @@ static int parse_viewport(const char *wps_bufptr,
581#endif 598#endif
582#if (LCD_DEPTH == 2) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2) 599#if (LCD_DEPTH == 2) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
583 if (depth == 2) { 600 if (depth == 2) {
584 if (!(ptr = parse_list("dddddgg", '|', ptr, &vp->x, &vp->y, &vp->width, 601 /* Default to black on white */
602 vp->fg_pattern = 0;
603 vp->bg_pattern = 3;
604 if (!(ptr = parse_list("dddddgg", &valid, '|', ptr, &vp->x, &vp->y, &vp->width,
585 &vp->height, &vp->font, &vp->fg_pattern, &vp->bg_pattern))) 605 &vp->height, &vp->font, &vp->fg_pattern, &vp->bg_pattern)))
586 return WPS_ERROR_INVALID_PARAM; 606 return WPS_ERROR_INVALID_PARAM;
587 } 607 }
@@ -590,8 +610,8 @@ static int parse_viewport(const char *wps_bufptr,
590#if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1) 610#if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
591 if (depth == 1) 611 if (depth == 1)
592 { 612 {
593 if (!(ptr = parse_list("ddddd", '|', ptr, &vp->x, &vp->y, &vp->width, 613 if (!(ptr = parse_list("ddddd", &valid, '|', ptr, &vp->x, &vp->y,
594 &vp->height, &vp->font))) 614 &vp->width, &vp->height, &vp->font)))
595 return WPS_ERROR_INVALID_PARAM; 615 return WPS_ERROR_INVALID_PARAM;
596 } 616 }
597 else 617 else
@@ -602,34 +622,39 @@ static int parse_viewport(const char *wps_bufptr,
602 if (*ptr != '|') 622 if (*ptr != '|')
603 return WPS_ERROR_INVALID_PARAM; 623 return WPS_ERROR_INVALID_PARAM;
604 624
625 if ((valid&(1<<PL_X)) == 0 || (valid&(1<<PL_Y)) == 0)
626 return WPS_ERROR_INVALID_PARAM;
627
628 /* fix defaults */
629 if ((valid&(1<<PL_WIDTH)) == 0)
630 vp->width = lcd_width - vp->x;
631 if ((valid&(1<<PL_HEIGHT)) == 0)
632 vp->height = lcd_height - vp->y;
633
605 /* Default to using the user font if the font was an invalid number */ 634 /* Default to using the user font if the font was an invalid number */
606 if ((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI)) 635 if (((valid&(1<<PL_FONT)) == 0) ||
636 ((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI)))
607 vp->font = FONT_UI; 637 vp->font = FONT_UI;
608 638
609 /* Validate the viewport dimensions - we know that the numbers are 639 /* Validate the viewport dimensions - we know that the numbers are
610 non-negative integers */ 640 non-negative integers */
611#ifdef HAVE_REMOTE_LCD 641 if ((vp->x >= lcd_width) ||
612 if (wps_data->remote_wps) 642 ((vp->x + vp->width) > lcd_width) ||
643 (vp->y >= lcd_height) ||
644 ((vp->y + vp->height) > lcd_height))
613 { 645 {
614 if ((vp->x >= LCD_REMOTE_WIDTH) || 646 return WPS_ERROR_INVALID_PARAM;
615 ((vp->x + vp->width) > LCD_REMOTE_WIDTH) ||
616 (vp->y >= LCD_REMOTE_HEIGHT) ||
617 ((vp->y + vp->height) > LCD_REMOTE_HEIGHT))
618 {
619 return WPS_ERROR_INVALID_PARAM;
620 }
621 } 647 }
622 else 648
623#endif 649#ifdef HAVE_LCD_COLOR
650 if (depth == 16)
624 { 651 {
625 if ((vp->x >= LCD_WIDTH) || 652 if ((valid&(1<<PL_FG)) == 0)
626 ((vp->x + vp->width) > LCD_WIDTH) || 653 vp->fg_pattern = global_settings.fg_color;
627 (vp->y >= LCD_HEIGHT) || 654 if ((valid&(1<<PL_BG)) == 0)
628 ((vp->y + vp->height) > LCD_HEIGHT)) 655 vp->fg_pattern = global_settings.bg_color;
629 {
630 return WPS_ERROR_INVALID_PARAM;
631 }
632 } 656 }
657#endif
633 658
634 wps_data->viewports[wps_data->num_viewports-1].last_line = wps_data->num_lines - 1; 659 wps_data->viewports[wps_data->num_viewports-1].last_line = wps_data->num_lines - 1;
635 660
diff --git a/apps/misc.c b/apps/misc.c
index e9f1724b1b..51e02f6a62 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -1217,7 +1217,9 @@ int hex_to_rgb(const char* hex, int* color)
1217 s - string (sets pointer to string, without copying) 1217 s - string (sets pointer to string, without copying)
1218 c - hex colour (RGB888 - e.g. ff00ff) 1218 c - hex colour (RGB888 - e.g. ff00ff)
1219 g - greyscale "colour" (0-3) 1219 g - greyscale "colour" (0-3)
1220 1220 valid_vals - if not NULL 1 is set in the bitplace if the item was read OK
1221 0 if not read.
1222 first item is LSB, (max 32 items! )
1221 sep - list separator (e.g. ',' or '|') 1223 sep - list separator (e.g. ',' or '|')
1222 str - string to parse, must be terminated by 0 or sep 1224 str - string to parse, must be terminated by 0 or sep
1223 ... - pointers to store the parsed values 1225 ... - pointers to store the parsed values
@@ -1229,25 +1231,29 @@ int hex_to_rgb(const char* hex, int* color)
1229/* '0'-'3' are ASCII 0x30 to 0x33 */ 1231/* '0'-'3' are ASCII 0x30 to 0x33 */
1230#define is0123(x) (((x) & 0xfc) == 0x30) 1232#define is0123(x) (((x) & 0xfc) == 0x30)
1231 1233
1232const char* parse_list(const char *fmt, const char sep, const char* str, ...) 1234const char* parse_list(const char *fmt, unsigned int *valid_vals,
1235 const char sep, const char* str, ...)
1233{ 1236{
1234 va_list ap; 1237 va_list ap;
1235 const char* p = str; 1238 const char* p = str, *f = fmt;
1236 const char** s; 1239 const char** s;
1237 int* d; 1240 int* d;
1241 bool valid;
1242 int i=0;
1238 1243
1239 va_start(ap, str); 1244 va_start(ap, str);
1240 1245 if (valid_vals)
1246 *valid_vals = 0;
1241 while (*fmt) 1247 while (*fmt)
1242 { 1248 {
1243 /* Check for separator, if we're not at the start */ 1249 /* Check for separator, if we're not at the start */
1244 if (p != str) 1250 if (f != fmt)
1245 { 1251 {
1246 if (*p != sep) 1252 if (*p != sep)
1247 goto err; 1253 goto err;
1248 p++; 1254 p++;
1249 } 1255 }
1250 1256 valid = false;
1251 switch (*fmt++) 1257 switch (*fmt++)
1252 { 1258 {
1253 case 's': /* string - return a pointer to it (not a copy) */ 1259 case 's': /* string - return a pointer to it (not a copy) */
@@ -1256,18 +1262,25 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1256 *s = p; 1262 *s = p;
1257 while (*p && *p != sep) 1263 while (*p && *p != sep)
1258 p++; 1264 p++;
1259 1265 valid = (*s[0]!=sep);
1260 break; 1266 break;
1261 1267
1262 case 'd': /* int */ 1268 case 'd': /* int */
1263 d = va_arg(ap, int*); 1269 d = va_arg(ap, int*);
1264 if (!isdigit(*p)) 1270 if (!isdigit(*p))
1265 goto err; 1271 {
1266 1272 if (!valid_vals)
1267 *d = *p++ - '0'; 1273 goto err;
1268 1274 while (*p && *p != sep)
1269 while (isdigit(*p)) 1275 p++;
1270 *d = (*d * 10) + (*p++ - '0'); 1276 }
1277 else
1278 {
1279 *d = *p++ - '0';
1280 while (isdigit(*p))
1281 *d = (*d * 10) + (*p++ - '0');
1282 valid = true;
1283 }
1271 1284
1272 break; 1285 break;
1273 1286
@@ -1276,9 +1289,17 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1276 d = va_arg(ap, int*); 1289 d = va_arg(ap, int*);
1277 1290
1278 if (hex_to_rgb(p, d) < 0) 1291 if (hex_to_rgb(p, d) < 0)
1279 goto err; 1292 {
1280 1293 if (!valid_vals)
1281 p += 6; 1294 goto err;
1295 while (*p && *p != sep)
1296 p++;
1297 }
1298 else
1299 {
1300 p += 6;
1301 valid = true;
1302 }
1282 1303
1283 break; 1304 break;
1284#endif 1305#endif
@@ -1288,9 +1309,17 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1288 d = va_arg(ap, int*); 1309 d = va_arg(ap, int*);
1289 1310
1290 if (is0123(*p)) 1311 if (is0123(*p))
1312 {
1291 *d = *p++ - '0'; 1313 *d = *p++ - '0';
1292 else 1314 valid = true;
1315 }
1316 else if (!valid_vals)
1293 goto err; 1317 goto err;
1318 else
1319 {
1320 while (*p && *p != sep)
1321 p++;
1322 }
1294 1323
1295 break; 1324 break;
1296#endif 1325#endif
@@ -1299,6 +1328,9 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1299 goto err; 1328 goto err;
1300 break; 1329 break;
1301 } 1330 }
1331 if (valid_vals && valid)
1332 *valid_vals |= (1<<i);
1333 i++;
1302 } 1334 }
1303 1335
1304 va_end(ap); 1336 va_end(ap);
diff --git a/apps/misc.h b/apps/misc.h
index 4d0226ae51..8dc61920c8 100644
--- a/apps/misc.h
+++ b/apps/misc.h
@@ -129,6 +129,7 @@ bool dir_exists(const char *path);
129char *strip_extension(char* buffer, int buffer_size, const char *filename); 129char *strip_extension(char* buffer, int buffer_size, const char *filename);
130 130
131/* A simplified scanf */ 131/* A simplified scanf */
132const char* parse_list(const char *fmt, const char sep, const char* str, ...); 132const char* parse_list(const char *fmt, unsigned int *valid_vals,
133 const char sep, const char* str, ...);
133 134
134#endif /* MISC_H */ 135#endif /* MISC_H */