fbsplash: reinstate drawing of over/undersized images; shrink

Based on the patch by Nuno Lucas <ntlucas@gmail.com>

function                                             old     new   delta
fbsplash_main                                        938     864     -74

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 3bf16ab..5974006 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -84,7 +84,7 @@
 	// map the device in memory
 	G.addr = mmap(NULL,
 			G.scr_var.xres * G.scr_var.yres
-			* BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/ ,
+			* BYTES_PER_PIXEL /*(G.scr_var.bits_per_pixel / 8)*/,
 			PROT_WRITE, MAP_SHARED, fbfd, 0);
 	if (G.addr == MAP_FAILED)
 		bb_perror_msg_and_die("mmap");
@@ -121,7 +121,7 @@
 	// vertical lines
 	ptr1 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx) * BYTES_PER_PIXEL);
 	ptr2 = (DATA*)(G.addr + (G.nbar_posy * G.scr_var.xres + G.nbar_posx + G.nbar_width - 1) * BYTES_PER_PIXEL);
-	cnt = G.nbar_height - 1 /* HUH?!  G.nbar_posy + G.nbar_height - 1 - G.nbar_posy*/;
+	cnt = G.nbar_height - 1;
 	do {
 		*ptr1 = thispix; ptr1 += G.scr_var.xres;
 		*ptr2 = thispix; ptr2 += G.scr_var.xres;
@@ -216,70 +216,69 @@
  */
 static void fb_drawimage(void)
 {
-	char *head, *ptr;
 	FILE *theme_file;
+	char *read_ptr;
 	unsigned char *pixline;
 	unsigned i, j, width, height, line_size;
 
-	if (LONE_DASH(G.image_filename))
+	if (LONE_DASH(G.image_filename)) {
 		theme_file = stdin;
-	else {
+	} else {
 		int fd = open_zipped(G.image_filename);
 		if (fd < 0)
 			bb_simple_perror_msg_and_die(G.image_filename);
 		theme_file = xfdopen_for_read(fd);
 	}
-	head = xmalloc(256);
 
-	/* parse ppm header
-	 * - A ppm image’s magic number is the two characters "P6".
+	/* Parse ppm header:
+	 * - Magic: two characters "P6".
 	 * - Whitespace (blanks, TABs, CRs, LFs).
 	 * - A width, formatted as ASCII characters in decimal.
 	 * - Whitespace.
-	 * - A height, again in ASCII decimal.
+	 * - A height, ASCII decimal.
 	 * - Whitespace.
-	 * - The maximum color value (Maxval), again in ASCII decimal. Must be
-	 *   less than 65536.
+	 * - The maximum color value, ASCII decimal, in 0..65535
 	 * - Newline or other single whitespace character.
+	 *   (we support newline only)
 	 * - A raster of Width * Height pixels in triplets of rgb
-	 *   in pure binary by 1 (or not implemented 2) bytes.
+	 *   in pure binary by 1 or 2 bytes. (we support only 1 byte)
 	 */
+#define concat_buf bb_common_bufsiz1
+	read_ptr = concat_buf;
 	while (1) {
-		if (fgets(head, 256, theme_file) == NULL
-			/* do not overrun the buffer */
-			|| strlen(bb_common_bufsiz1) >= sizeof(bb_common_bufsiz1) - 256)
+		int w, h, max_color_val;
+		int rem = concat_buf + sizeof(concat_buf) - read_ptr;
+		if (rem < 2
+		 || fgets(read_ptr, rem, theme_file) == NULL
+		) {
 			bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
-
-		ptr = memchr(skip_whitespace(head), '#', 256);
-		if (ptr != NULL)
-			*ptr = 0; /* ignore comments */
-		strcat(bb_common_bufsiz1, head);
-		// width, height, max_color_val
-		if (sscanf(bb_common_bufsiz1, "P6 %u %u %u", &width, &height, &i) == 3
-			&& i <= 255)
+		}
+		read_ptr = strchrnul(read_ptr, '#');
+		*read_ptr = '\0'; /* ignore #comments */
+		if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
+		 && max_color_val <= 255
+		) {
+			width = w; /* w is on stack, width may be in register */
+			height = h;
 			break;
-		/* If we do not find a signature throughout the whole file then
-		   we will diagnose this via EOF on read in the head of the loop.  */
+		}
 	}
 
-	if (ENABLE_FEATURE_CLEAN_UP)
-		free(head);
-	if (width != G.scr_var.xres || height != G.scr_var.yres)
-		bb_error_msg_and_die("PPM %dx%d does not match screen %dx%d",
-							 width, height, G.scr_var.xres, G.scr_var.yres);
 	line_size = width*3;
+	pixline = xmalloc(line_size);
+
 	if (width > G.scr_var.xres)
 		width = G.scr_var.xres;
 	if (height > G.scr_var.yres)
 		height = G.scr_var.yres;
-
-	pixline = xmalloc(line_size);
 	for (j = 0; j < height; j++) {
-		unsigned char *pixel = pixline;
-		DATA *src = (DATA *)(G.addr + j * G.scr_fix.line_length);
+		unsigned char *pixel;
+		DATA *src;
 
 		if (fread(pixline, 1, line_size, theme_file) != line_size)
 			bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
+		pixel = pixline;
+		src = (DATA *)(G.addr + j * G.scr_fix.line_length);
 		for (i = 0; i < width; i++) {
 			unsigned thispix;
 			thispix = (((unsigned)pixel[0] << 8) & 0xf800)
@@ -289,8 +288,7 @@
 			pixel += 3;
 		}
 	}
-	if (ENABLE_FEATURE_CLEAN_UP)
-		free(pixline);
+	free(pixline);
 	fclose(theme_file);
 }
 
@@ -312,7 +310,7 @@
 	char *token[2];
 	parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
 	while (config_read(parser, token, 2, 2, "#=",
-				    (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
+				(PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
 		unsigned val = xatoi_u(token[1]);
 		int i = index_in_strings(param_names, token[0]);
 		if (i < 0)