int main(int argc, char **argv) { const char *ifname, *ofname, *itname, *otname; FILE *ifil, *ofil, *itab, *otab; int psftype, charsize, fontlen, hastable, notable; int i; int width = 8, bytewidth, height; char *inbuf, *fontbuf; int inbuflth, fontbuflth; set_progname(argv[0]); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); if (argc == 2 && !strcmp(argv[1], "-V")) print_version_and_exit(); ifil = ofil = itab = otab = NULL; ifname = ofname = itname = otname = NULL; fontbuf = NULL; notable = 0; if (!strcmp(get_progname(), "psfaddtable")) { /* Do not send binary data to stdout without explicit "-" */ if (argc != 4) { char *u = _("Usage:\n\t%s infont intable outfont\n"); fprintf(stderr, u, get_progname()); exit(EX_USAGE); } ifname = argv[1]; itname = argv[2]; ofname = argv[3]; } else if (!strcmp(get_progname(), "psfgettable")) { if (argc < 2 || argc > 3) { char *u = _("Usage:\n\t%s infont [outtable]\n"); fprintf(stderr, u, get_progname()); exit(EX_USAGE); } ifname = argv[1]; otname = (argc == 3) ? argv[2] : "-"; } else if (!strcmp(get_progname(), "psfstriptable")) { /* Do not send binary data to stdout without explicit "-" */ if (argc != 3) { char *u = _("Usage:\n\t%s infont outfont\n"); fprintf(stderr, u, get_progname()); exit(EX_USAGE); } ifname = argv[1]; ofname = argv[2]; notable = 1; } else { for (i = 1; i < argc; i++) { if ((!strcmp(argv[i], "-i") || !strcmp(argv[i], "-if")) && i < argc - 1) ifname = argv[++i]; else if ((!strcmp(argv[i], "-o") || !strcmp(argv[i], "-of")) && i < argc - 1) ofname = argv[++i]; else if (!strcmp(argv[i], "-it") && i < argc - 1) itname = argv[++i]; else if (!strcmp(argv[i], "-ot") && i < argc - 1) otname = argv[++i]; else if (!strcmp(argv[i], "-nt")) notable = 1; else break; } if (i < argc || argc <= 1) { char *u = _("Usage:\n\t%s [-i infont] [-o outfont] " "[-it intable] [-ot outtable] [-nt]\n"); fprintf(stderr, u, get_progname()); exit(EX_USAGE); } } if (!ifname) ifname = "-"; if (!strcmp(ifname, "-")) ifil = stdin; else { ifil = fopen(ifname, "r"); if (!ifil) { perror(ifname); exit(EX_NOINPUT); } } if (!itname) /* nothing */; else if (!strcmp(itname, "-")) itab = stdin; else { itab = fopen(itname, "r"); if (!itab) { perror(itname); exit(EX_NOINPUT); } } /* Refuse ifil == itab == stdin ? Perhaps not. */ if (!ofname) /* nothing */; else if (!strcmp(ofname, "-")) ofil = stdout; else { ofil = fopen(ofname, "w"); if (!ofil) { perror(ofname); exit(EX_CANTCREAT); } } if (!otname) /* nothing */; else if (!strcmp(otname, "-")) otab = stdout; else { otab = fopen(otname, "w"); if (!otab) { perror(otname); exit(EX_CANTCREAT); } } if (readpsffont(ifil, &inbuf, &inbuflth, &fontbuf, &fontbuflth, &width, &fontlen, 0, itab ? NULL : &uclistheads) == -1) { char *u = _("%s: Bad magic number on %s\n"); fprintf(stderr, u, get_progname(), ifname); exit(EX_DATAERR); } fclose(ifil); charsize = fontbuflth / fontlen; bytewidth = (width + 7) / 8; if (!bytewidth) bytewidth = 1; height = charsize / bytewidth; hastable = (uclistheads != NULL); if (PSF1_MAGIC_OK((unsigned char *)inbuf)) { psftype = 1; } else if (PSF2_MAGIC_OK((unsigned char *)inbuf)) { psftype = 2; } else { char *u = _("%s: psf file with unknown magic\n"); fprintf(stderr, u, get_progname()); exit(EX_DATAERR); } if (itab) { read_itable(itab, fontlen, &uclistheads); fclose(itab); } if (otab) { struct unicode_list *ul; struct unicode_seq *us; const char *sep; if (!hastable) { char *u = _("%s: input font does not have an index\n"); fprintf(stderr, u, get_progname()); exit(EX_DATAERR); } fprintf(otab, "#\n# Character table extracted from font %s\n#\n", ifname); for (i = 0; i < fontlen; i++) { fprintf(otab, "0x%03x\t", i); sep = ""; ul = uclistheads[i].next; while (ul) { us = ul->seq; while (us) { fprintf(otab, "%sU+%04x", sep, us->uc); us = us->next; sep = ", "; } ul = ul->next; sep = " "; } fprintf(otab, "\n"); } fclose(otab); } if (ofil) { writepsffont(ofil, fontbuf, width, height, fontlen, psftype, notable ? NULL : uclistheads); fclose(ofil); } return EX_OK; }
static void do_load(int fd, unsigned char *buffer, size_t len) { int height; int width = 8; int charsize; int fontsize = 256; int has_table = 0; unsigned char *font = buffer; unsigned char *table; if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) { if (psf1h(buffer)->mode > PSF1_MAXMODE) bb_error_msg_and_die("unsupported psf file mode"); if (psf1h(buffer)->mode & PSF1_MODE512) fontsize = 512; if (psf1h(buffer)->mode & PSF1_MODEHASTAB) has_table = 1; height = charsize = psf1h(buffer)->charsize; font += sizeof(struct psf1_header); } else #if ENABLE_FEATURE_LOADFONT_PSF2 if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) { if (psf2h(buffer)->version > PSF2_MAXVERSION) bb_error_msg_and_die("unsupported psf file version"); fontsize = psf2h(buffer)->length; if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE) has_table = 2; charsize = psf2h(buffer)->charsize; height = psf2h(buffer)->height; width = psf2h(buffer)->width; font += psf2h(buffer)->headersize; } else #endif #if ENABLE_FEATURE_LOADFONT_RAW if (len == 9780) { /* file with three code pages? */ charsize = height = 16; font += 40; } else if ((len & 0377) == 0) { /* bare font */ charsize = height = len / 256; } else #endif { bb_error_msg_and_die("input file: bad length or unsupported font type"); } #if !defined(PIO_FONTX) || defined(__sparc__) if (fontsize != 256) bb_error_msg_and_die("only fontsize 256 supported"); #endif table = font + fontsize * charsize; buffer += len; if (table > buffer || (!has_table && table != buffer)) bb_error_msg_and_die("input file: bad length"); do_loadfont(fd, font, height, width, charsize, fontsize); if (has_table) do_loadtable(fd, table, buffer - table, fontsize, has_table - 1); }