aboutsummaryrefslogtreecommitdiff
path: root/console-tools/loadfont.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-10 15:08:44 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-10 15:08:44 +0000
commite8a0788b249cbac5bf5b2aa2d81bb8f6b29a7a4b (patch)
treefcdf3d51b6d60986b634c693d71355867bca82ff /console-tools/loadfont.c
parentd4fea900bdb92d7bba71348a40cb00b6748a8ecc (diff)
downloadbusybox-w32-e8a0788b249cbac5bf5b2aa2d81bb8f6b29a7a4b.tar.gz
busybox-w32-e8a0788b249cbac5bf5b2aa2d81bb8f6b29a7a4b.tar.bz2
busybox-w32-e8a0788b249cbac5bf5b2aa2d81bb8f6b29a7a4b.zip
moved biggest stack buffers to malloc space, or made their size configurable
(8k of shell line edit buffer is an overkill) # make ARCH=i386 bloatcheck function old new delta read_line_input 3933 3967 +34 ifaddrlist 348 345 -3 do_loadfont 208 191 -17 edit_file 840 819 -21 .rodata 129112 129080 -32 uncompress 1305 1268 -37 loadfont_main 566 495 -71 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/6 up/down: 34/-181) Total: -147 bytes
Diffstat (limited to 'console-tools/loadfont.c')
-rw-r--r--console-tools/loadfont.c77
1 files changed, 38 insertions, 39 deletions
diff --git a/console-tools/loadfont.c b/console-tools/loadfont.c
index 88d7a0401..b046d40e3 100644
--- a/console-tools/loadfont.c
+++ b/console-tools/loadfont.c
@@ -21,43 +21,27 @@ enum {
21}; 21};
22 22
23struct psf_header { 23struct psf_header {
24 unsigned char magic1, magic2; /* Magic number */ 24 unsigned char magic1, magic2; /* Magic number */
25 unsigned char mode; /* PSF font mode */ 25 unsigned char mode; /* PSF font mode */
26 unsigned char charsize; /* Character size */ 26 unsigned char charsize; /* Character size */
27}; 27};
28 28
29#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2) 29#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
30 30
31static void loadnewfont(int fd);
32
33int loadfont_main(int argc, char **argv);
34int loadfont_main(int argc, char **argv)
35{
36 int fd;
37
38 if (argc != 1)
39 bb_show_usage();
40
41 fd = xopen(CURRENT_VC, O_RDWR);
42 loadnewfont(fd);
43
44 return EXIT_SUCCESS;
45}
46
47static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize) 31static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
48{ 32{
49 char buf[16384]; 33 char *buf;
50 int i; 34 int i;
51 35
52 memset(buf, 0, sizeof(buf));
53
54 if (unit < 1 || unit > 32) 36 if (unit < 1 || unit > 32)
55 bb_error_msg_and_die("bad character size %d", unit); 37 bb_error_msg_and_die("bad character size %d", unit);
56 38
39 buf = xzalloc(16 * 1024);
40 /*memset(buf, 0, 16 * 1024);*/
57 for (i = 0; i < fontsize; i++) 41 for (i = 0; i < fontsize; i++)
58 memcpy(buf + (32 * i), inbuf + (unit * i), unit); 42 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
59 43
60#if defined( PIO_FONTX ) && !defined( __sparc__ ) 44#if defined(PIO_FONTX) && !defined(__sparc__)
61 { 45 {
62 struct consolefontdesc cfd; 46 struct consolefontdesc cfd;
63 47
@@ -66,12 +50,14 @@ static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
66 cfd.chardata = buf; 50 cfd.chardata = buf;
67 51
68 if (ioctl(fd, PIO_FONTX, &cfd) == 0) 52 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
69 return; /* success */ 53 goto ret; /* success */
70 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)"); 54 bb_perror_msg("PIO_FONTX ioctl (will try PIO_FONT)");
71 } 55 }
72#endif 56#endif
73 if (ioctl(fd, PIO_FONT, buf)) 57 if (ioctl(fd, PIO_FONT, buf))
74 bb_perror_msg_and_die("PIO_FONT ioctl error"); 58 bb_perror_msg_and_die("PIO_FONT ioctl");
59 ret:
60 free(buf);
75} 61}
76 62
77static void 63static void
@@ -124,31 +110,30 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
124 110
125static void loadnewfont(int fd) 111static void loadnewfont(int fd)
126{ 112{
113 enum { INBUF_SIZE = 32*1024 + 1 };
114
127 int unit; 115 int unit;
128 unsigned char inbuf[32768]; /* primitive */ 116 unsigned inputlth, offset;
129 unsigned int inputlth, offset; 117 /* Was on stack, but 32k is a bit too much: */
118 unsigned char *inbuf = xmalloc(INBUF_SIZE);
130 119
131 /* 120 /*
132 * We used to look at the length of the input file 121 * We used to look at the length of the input file
133 * with stat(); now that we accept compressed files, 122 * with stat(); now that we accept compressed files,
134 * just read the entire file. 123 * just read the entire file.
135 */ 124 */
136 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin); 125 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
137 if (ferror(stdin)) 126 if (inputlth < 0)
138 bb_perror_msg_and_die("error reading input font"); 127 bb_perror_msg_and_die("error reading input font");
139 /* use malloc/realloc in case of giant files; 128 if (inputlth >= INBUF_SIZE)
140 maybe these do not occur: 16kB for the font, 129 bb_error_msg_and_die("font too large");
141 and 16kB for the map leaves 32 unicode values
142 for each font position */
143 if (!feof(stdin))
144 bb_perror_msg_and_die("font too large");
145 130
146 /* test for psf first */ 131 /* test for psf first */
147 { 132 {
148 struct psf_header psfhdr; 133 struct psf_header psfhdr;
149 int fontsize; 134 int fontsize;
150 int hastable; 135 int hastable;
151 unsigned int head0, head; 136 unsigned head0, head;
152 137
153 if (inputlth < sizeof(struct psf_header)) 138 if (inputlth < sizeof(struct psf_header))
154 goto no_psf; 139 goto no_psf;
@@ -161,7 +146,7 @@ static void loadnewfont(int fd)
161 if (psfhdr.mode > PSF_MAXMODE) 146 if (psfhdr.mode > PSF_MAXMODE)
162 bb_error_msg_and_die("unsupported psf file mode"); 147 bb_error_msg_and_die("unsupported psf file mode");
163 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256); 148 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
164#if !defined( PIO_FONTX ) || defined( __sparc__ ) 149#if !defined(PIO_FONTX) || defined(__sparc__)
165 if (fontsize != 256) 150 if (fontsize != 256)
166 bb_error_msg_and_die("only fontsize 256 supported"); 151 bb_error_msg_and_die("only fontsize 256 supported");
167#endif 152#endif
@@ -177,8 +162,8 @@ static void loadnewfont(int fd)
177 do_loadtable(fd, inbuf + head, inputlth - head, fontsize); 162 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
178 return; 163 return;
179 } 164 }
180 no_psf:
181 165
166 no_psf:
182 /* file with three code pages? */ 167 /* file with three code pages? */
183 if (inputlth == 9780) { 168 if (inputlth == 9780) {
184 offset = 40; 169 offset = 40;
@@ -192,3 +177,17 @@ static void loadnewfont(int fd)
192 } 177 }
193 do_loadfont(fd, inbuf + offset, unit, 256); 178 do_loadfont(fd, inbuf + offset, unit, 256);
194} 179}
180
181int loadfont_main(int argc, char **argv);
182int loadfont_main(int argc, char **argv)
183{
184 int fd;
185
186 if (argc != 1)
187 bb_show_usage();
188
189 fd = xopen(CURRENT_VC, O_RDWR);
190 loadnewfont(fd);
191
192 return EXIT_SUCCESS;
193}