/* Enter here to process! */
int main(int argc, char **argv)	//STONESOUP:SOURCE_TAINT:STDIN
{
	FILE *otfil = NULL;	/* Output file pointer */
	struct BmpHdr hdr;	/* First part of the file header goes here */
	struct BmpInfoHdr *infohdr;	/* Most of the file information goes here */
	unsigned char c[3], *preamble = NULL, *img = NULL, *s;
	unsigned long rowsz, numrows, col, row;
	unsigned int i, j, len, ar[2] = { 5, 5 };
	enum engb { enum0 = 0, enum1 = 1, enum2 = 2, enum3 = 3, enum4 = 4 } EnumGibberish;
	EnumGibberish = enum4;

	if (argc < 2)
	{
		fprintf(stderr, "Not enough arguments to command line\nExpected:\n\n");
		fprintf(stderr, "%s <BMP_output_filename.bmp> < <BMP_input_filename.bmp>\n",
		"desaturate.exe");
		return(1);
	}

#if _WIN32
	_setmode(_fileno(stdin), _O_BINARY);
#endif

	if ((otfil = fopen(argv[1], "wb")) == NULL)
	{
		fprintf(stderr, "Unable to open output file because of error %d\n", errno);
		return(1);
	}

	/* Get the rest of the initial header which contains the offset to the image */
	if (fread(c, 1, 2, stdin) != 2)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to read the 'BM' identifier of %s because of error %d\n",
			"stdin", errno);
		return(1);
	}

	/* Identify the file type as a 'BM' file */
	if ((c[0] != 'B') || (c[1] != 'M'))
	{
		closeuperr(otfil, argv[1], preamble, img, "BMP file %s is unknown type, expected 'B' 'M', found '%c' '%c'\n",
			"stdin", c[0], c[1]);
		return(1);
	}

	if (fwrite("BM", 1, 2, otfil) != 2)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to write the 'BM' BMP preamble of %s because of error %d\n",
			argv[1], errno);
		return(1);
	}

	/* Get the rest of the initial header which contains the offset to the image */
	if (fread(&hdr, sizeof(hdr), 1, stdin) != 1)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to read the file header of %s because of error %d\n",
			"stdin", errno);
		return(1);
	}

	if (fwrite(&hdr, sizeof(hdr), 1, otfil) != 1)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to write the BMP header of %s because of error %d\n",
			argv[1], errno);
		return(1);
	}

	/* Check sanity of image offset */
	if (hdr.img_ofst >= hdr.fsize)
	{
		closeuperr(otfil, argv[1], preamble, img, "BMP file %s has %lu bytes before image, but file size is %u\n",
			"stdin", hdr.img_ofst, hdr.fsize);
		return(1);
	}

	/* Check sanity of image offset */
	if ((preamble = malloc(hdr.img_ofst)) == NULL)
	{
		closeuperr(otfil, argv[1], preamble, img, "BMP file %s, could not malloc %u bytes\n",
			"stdin", hdr.img_ofst);
		return(1);
	}

	/* Get the rest of the image information */
	if (fread(preamble, hdr.img_ofst - sizeof(hdr) - 2, 1, stdin) != 1)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to read the infoheader of %s because of error %d\n",
			"stdin", errno);
		return(1);
	}

	if (fwrite(preamble, hdr.img_ofst - sizeof(hdr) - 2, 1, otfil) != 1)
	{
		closeuperr(otfil, argv[1], preamble, img, "Unable to write the BMP descriptor of %s because of error %d\n",
			argv[1], errno);
		return(1);
	}

	infohdr = (struct BmpInfoHdr *)preamble;

	/* DeCompression is not yet built into this package */
	if (infohdr->compress_type != 0)
	{
		closeuperr(otfil, argv[1], preamble, img, "BMP file %s is compressed of type %u and this function does not handle compression\n",
			"stdin", infohdr->compress_type);
		return(1);
	}

	/* Also haven't tested anything other than 24 bits per pixel */
	if ((infohdr->bitsperpixel != 32) && (infohdr->bitsperpixel != 24))
	{
		closeuperr(otfil, argv[1], preamble, img, "Can only process 24 or 32 bits per pixel in BMP file %s, not %d\n",
			"stdin", infohdr->bitsperpixel);
		return(1);
	}

	/* The row length MUST be a multiple of 4 bytes */
	rowsz = (((infohdr->bitsperpixel * infohdr->wid) + 16) / 32) * 4;
	numrows = infohdr->hght < 0 ? -infohdr->hght : infohdr->hght;

	int xx = 11;
	ar[1] = hdr.fsize - hdr.img_ofst + EnumGibberish;	//STONESOUP:DATA_TYPE:ENUM
	if ((img = malloc(ar[(xx * xx) - 120])) == NULL)	//STONESOUP:DATA_FLOW:ARRAY_INDEX_NONLINEAR_EXPRESSION
	{
		closeuperr(otfil, argv[1], preamble, img, "BMP file %s, could not malloc %u bytes for image\n",
			"stdin", ar[1]);
		return(1);
	}

	// Load image by reading through file and loading into buffer
	s = img;
	len = interfile(img);	//STONESOUP:INTERACTION_POINT	//STONESOUP:CROSSOVER_POINT	//STONESOUP:TRIGGER_POINT	//STONESOUP:CONTROL_FLOW:INTERFILE

	/* Make sure we only do the 24 bits per pixel */
	if (infohdr->bitsperpixel == 24)
	{
		unsigned char p[3];	/* Byte order: Blue, Green, Red */
		unsigned char gray;
		unsigned int r, g, b;
		i = 3;	/* The number of bytes per pixel to read and write */
		long rowsz_mod = rowsz - (infohdr->wid * 3);
//printf("%d %d\n", rowsz_mod, rowsz);

		/* Loop through each row of the image */
		for (row = 0; row < numrows; row++)
		{
			/* Loop through each pixel of the image */
			for (col = 0; col < infohdr->wid; col++)
			{
				/* Inefficient, but we only get 1 pixel at a time */
				/* No buffers to overflow or keep track of */
				if (len < i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to read %u pixels from file %s, read %u\n",
						i, "stdin", len);
					return(1);
				}
				len -= i;

				/* Average all of the brightness values to obtain the grayscale value */
				r = *s++;
				g = *s++;
				b = *s++;
				gray = (r + g + b) / 3;
				/* Now store the value into each color */
				p[0] = gray;
				p[1] = gray;
				p[2] = gray;
				/* Now write the value out */
				if ((j = fwrite(p, 1, i, otfil)) != i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to write %u pixels to file %s, wrote %u\n",
						i, "stdin", j);
					return(1);
				}
			}
//printf("%d %d\n", row, col);

			/* Finish out the row */
			for (col = 0; col < rowsz_mod; col++)
			{
//printf("%d\n", col);
				if (len < 1)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to read 1 pixel in row %u from file %s, read %u\n", row, "stdin", j);
					return(1);
				}
				len--;

				if (fwrite(s++, 1, 1, otfil) != 1)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to write 1 pixel to file %s, wrote none\n",
						"stdin");
					return(1);
				}
			}
//closeuperr(otfil, NULL, "\n");
//return(1);
		}
	}
	else if (infohdr->bitsperpixel == 32)
	{
		unsigned char p[4];	// Byte order: Blue, Green, Red, Alpha
		unsigned char gray;
		unsigned int r, g, b;
		i = 4;

		for (row = 0; row < numrows; row++)
		{
			for (col = 0; col < rowsz; col += i)
			{
				if (len < i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to read %u pixels from file %s, read %u\n",
						i, "stdin", len);
					return(1);
				}
				len -= i;
				r = *s++;
				g = *s++;
				b = *s++;
				gray = (r + g + b) / 3;
				p[0] = gray;
				p[1] = gray;
				p[2] = gray;
				p[3] = *s++;
				// Now write the value out
				if ((j = fwrite(p, 1, i, otfil)) != i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to write %d pixels to file %s, wrote %d\n",
						i, "stdin", j);
					return(1);
				}
			}
		}
	}
/*
	else if (infohdr->bitsperpixel == 16)
	{
		int i = 2, j;
		unsigned char p[2];	// Nibble order: Green, Blue, Alpha, Red
		unsigned char blue, grn, red, alpha;
		unsigned char gray;

		for (row = 0; row < numrows; row++)
		{
			for (col = 0; col < rowsz; col += i)
			{
				if ((j = fread(p, 1, i, stdin)) != i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to read %d pixels from file %s, read %d\n",
						i, "stdin", j);
					return(1);
				}
				if (feof(stdin))
				{
					closeuperr(otfil, argv[1], preamble, img, "Ran out of pixels early in file %s, expected %d, found %d\n",
						"stdin", numrows * rowsz, (row * rowsz) + col);
					return(1);
				}
				blue = (p[0] >> 4) & 0xF;
				grn = p[0] & 0xF;
				alpha = (p[1] >> 4) & 0xF;
				red = p[1] & 0xF;
				gray = (blue + grn + red) / 3;
				p[0] = (gray << 4) + gray;
				p[1] = (alpha << 4) + gray;
				// Now write the value out
				if ((j = fwrite(p, 1, i, otfil)) != i)
				{
					closeuperr(otfil, argv[1], preamble, img, "Expected to write %d pixels to file %s, wrote %d\n",
						i, "stdin", j);
					return(1);
				}
			}
			if (col != rowsz)
			{
				// Finish out the row
				for (col -= i; col < rowsz; col++)
				{
					if (fread(p, 1, 1, stdin) != 1)
					{
						closeuperr(otfil, argv[1], preamble, img, "Expected to read 1 pixel from file %s, read none\n",
							"stdin");
						return(1);
					}
					if (feof(stdin))
					{
						closeuperr(otfil, argv[1], preamble, img, "Ran out of pixels early in file %s, expected %d, found %d\n",
							"stdin", numrows * rowsz, (row * rowsz) + col);
						return(1);
					}
					// Now write the value out
					if (fwrite(p, 1, 1, otfil) != 1)
					{
						closeuperr(otfil, argv[1], preamble, img, "Expected to write 1 pixel to file %s, wrote none\n",
							"stdin");
						return(1);
					}
				}
			}
		}
	}
*/

	/* Write out the residual file which will be the same */
		if (len > 0)
		{
			if ((j = fwrite(s, 1, len, otfil)) != len)
			{
				closeuperr(otfil, argv[1], preamble, img, "Expected to write %d pixels to file %s, wrote %d\n",
					len, "stdin", j);
				return(1);
			}
		}

	free(preamble);

	if (fclose(otfil) != 0)
	{
		fprintf(stderr, "Unable to close output file because of error %d\n", errno);
		fclose(stdin);
		return(1);
	}

	return(0);
}
Example #2
0
int Passone(int locctr,int li,int ol)
{
    int i=0,start=locctr,ln,n,j=0,f=0,s=0,add;
    char c[25];
    FILE *fl;
    fl=fopen("symtab.txt","w");
    fclose(fl);
    for(i=0;i<li-1;i++)
    {
        l[i].address=locctr;
        if(strcmp(l[i].com,"START")==0)
             locctr+=0;
        else if(strcmp(l[i].com,"WORD")==0)
            locctr+=3;
        else if(strcmp(l[i].com,"HEX")==0)
        {
            ln=strlen(l[i].op);
            locctr+=ln;
        }
        else if(strcmp(l[i].com,"RESB")==0)
        {
            n=atoi(l[i].op);
            locctr+=n;
        }
        else if(strcmp(l[i].com,"RESW")==0)
        {
            n=atoi(l[i].op);
            locctr+=n*3;
        }
        else
            locctr+=3;
       // printf("%d %s %s %s\n",l[i].lno,l[i].label,l[i].com,l[i].op);

    }

    for(i=0;i<li-1;i++)
    {
        //
        if(strcmp(l[i].com,"START")!=0 && strcmp(l[i].com,"END")!=0 && strcmp(l[i].com,"RESW")!=0 && strcmp(l[i].com,"RESB")!=0 && strcmp(l[i].com,"HEX")!=0 && strcmp(l[i].com,"RSUB")!=0 && strcmp(l[i].com,"WORD")!=0)
        {
            for(j=0;j<ol;j++)
            {
                if(strcmp(l[i].com,o[j].mnem)==0 )
                {
                    f=0;
                    break;
                }
                else
                {
                    f=1;
                }
            }
            if(f==1)
            {
                printf("\nINVALID MNEMONIC %s in line %d",l[i].com,l[i].lno);
                getch();
                exit(1);
            }
        }
        fl=fopen("symtab.txt","r");
        while(!feof(fl))
        {
            fscanf(fl,"%s ",c);
            fscanf(fl,"%d\n",&add);
            if(strcmp(l[i].label,c)==0)
            {
                s=1;
                break;
            }
        }
        fclose(fl);
        if(s==0 && strcmp(l[i].label,".")!=0)
        {
            fl=fopen("symtab.txt","a");
            fprintf(fl,"%s ",l[i].label);
            fprintf(fl,"%X\n",l[i].address);
            fclose(fl);
        }
        else if(s==1)
        {
            printf("\nDuplicate symbols %s in line",l[i].label,l[i].lno);
            getch();
            exit(1);
        }
        s=0;
    }
    interfile(li);
    passtwo(li,ol);
    return locctr;
}