Esempio n. 1
0
static void do_load(int fd, struct psf_header *psfhdr, size_t len)
{
	int unit;
	int fontsize;
	int hastable;
	unsigned head0, head = head;

	/* test for psf first */
	if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) {
		if (psfhdr->mode > PSF_MAXMODE)
			bb_error_msg_and_die("unsupported psf file mode");
		fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256);
#if !defined(PIO_FONTX) || defined(__sparc__)
		if (fontsize != 256)
			bb_error_msg_and_die("only fontsize 256 supported");
#endif
		hastable = (psfhdr->mode & PSF_MODEHASTAB);
		unit = psfhdr->charsize;
		head0 = sizeof(struct psf_header);

		head = head0 + fontsize * unit;
		if (head > len || (!hastable && head != len))
			bb_error_msg_and_die("input file: bad length");
	} else {
		/* file with three code pages? */
		if (len == 9780) {
			head0 = 40;
			unit = 16;
		} else {
			/* bare font */
			if (len & 0377)
				bb_error_msg_and_die("input file: bad length");
			head0 = 0;
			unit = len / 256;
		}
		fontsize = 256;
		hastable = 0;
	}

	do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize);
	if (hastable)
		do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize);
}
static void loadnewfont(int fd)
{
	int unit;
	char inbuf[32768];			/* primitive */
	unsigned int inputlth, offset;

	/*
	 * We used to look at the length of the input file
	 * with stat(); now that we accept compressed files,
	 * just read the entire file.
	 */
	inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
	if (ferror(stdin))
		perror_msg_and_die("Error reading input font");
	/* use malloc/realloc in case of giant files;
	   maybe these do not occur: 16kB for the font,
	   and 16kB for the map leaves 32 unicode values
	   for each font position */
	if (!feof(stdin))
		perror_msg_and_die("Font too large");

	/* test for psf first */
	{
		struct psf_header psfhdr;
		int fontsize;
		int hastable;
		unsigned int head0, head;

		if (inputlth < sizeof(struct psf_header))
			goto no_psf;

		psfhdr = *(struct psf_header *) &inbuf[0];

		if (!PSF_MAGIC_OK(psfhdr))
			goto no_psf;

		if (psfhdr.mode > PSF_MAXMODE)
			error_msg_and_die("Unsupported psf file mode");
		fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
#if !defined( PIO_FONTX ) || defined( __sparc__ )
		if (fontsize != 256)
			error_msg_and_die("Only fontsize 256 supported");
#endif
		hastable = (psfhdr.mode & PSF_MODEHASTAB);
		unit = psfhdr.charsize;
		head0 = sizeof(struct psf_header);

		head = head0 + fontsize * unit;
		if (head > inputlth || (!hastable && head != inputlth))
			error_msg_and_die("Input file: bad length");
		do_loadfont(fd, inbuf + head0, unit, fontsize);
		if (hastable)
			do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
		return;
	}
  no_psf:

	/* file with three code pages? */
	if (inputlth == 9780) {
		offset = 40;
		unit = 16;
	} else {
		/* bare font */
		if (inputlth & 0377)
			error_msg_and_die("Bad input file size");
		offset = 0;
		unit = inputlth / 256;
	}
	do_loadfont(fd, inbuf + offset, unit, 256);
}