Bug Summary

File:x2pmp.c
Location:line 250, column 3
Description:Result of 'calloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'char'

Annotated Source Code

1/* x2pmp.c: Translate xwd window dump format into PMP format for the
2 * IBM 3812 PagePrinter.
3 */
4#include <stdio.h>
5#include <X11/Xlib.h>
6#include <X11/XWDFile.h>
7#include <X11/Xfuncs.h>
8#include <errno(*__error()).h>
9#include <string.h>
10
11#include "pmp.h"
12#include "xpr.h"
13
14#define max_(a, b)((a) > (b) ? (a) : (b)) ((a) > (b) ? (a) : (b))
15#define min_(a, b)((a) < (b) ? (a) : (b)) ((a) < (b) ? (a) : (b))
16#define abs_(a)((a) < 0 ? -(a) : (a)) ((a) < 0 ? -(a) : (a))
17
18
19/* Local prototypes */
20static unsigned char *magnification_table(int scale);
21static int bits_set(int n);
22static void leave(const char *s) _X_NORETURN__attribute((noreturn));
23static void p_move_abs(FILE *p, int x, int y);
24static void p_save_cursor(FILE *p, int reg);
25static void p_restore_cursor(FILE *p, int reg);
26static void p_set_orientation(FILE *p, enum orientation orient);
27static void p_bitmap(
28 FILE *p,
29 unsigned int h, int w,
30 unsigned long buflen,
31 unsigned char *buf);
32
33static int plane = 0;
34#define FONT_HEIGHT40 40
35#define FONT_HEIGHT_PIXELS(40*75/240) (FONT_HEIGHT40*75/PPI240)
36#define FONT_WIDTH24 24
37
38void x2pmp(FILE *in, FILE *out,
39 int scale,
40 int p_width, int p_length, int x_pos, int y_pos, /* in pels (units of PPI) */
41 char *head, char *foot,
42 enum orientation orient,
43 int invert)
44{
45 unsigned char *buffer, *win_name;
46 unsigned int win_name_size, width, height, ncolors;
47 unsigned int buffer_size, one_plane_size, byte_width, fixed_width;
48 int no_of_bits;
49 unsigned long swaptest = 1;
50 XWDFileHeader header;
51
52 /* Read header from file */
53 if (fread((char *)&header, sizeof(header), 1, in) != 1) {
54 if (feof(in))
55 return;
56 else
57 leave("fread");
58 }
59 if (*(char *) &swaptest)
60 _swaplong((char *) &header, sizeof(header));
61
62 if (header.file_version != XWD_FILE_VERSION7) {
63 fprintf(stderr__stderrp,"%s: file format version %d, not %d\n", progname,
64 (int)header.file_version, XWD_FILE_VERSION7);
65 }
66
67 win_name_size = abs_(header.header_size - sizeof(header))((header.header_size - sizeof(header)) < 0 ? -(header.header_size
- sizeof(header)) : (header.header_size - sizeof(header)))
;
68 if ((win_name = (unsigned char *)
69 calloc(win_name_size, (unsigned) sizeof(char))) == NULL((void*)0))
70 leave("Can't calloc window name storage.");
71
72 /* Read window name from file */
73 if (fread((char *) win_name, sizeof(char), (int) win_name_size, in) !=
74 win_name_size)
75 leave("Unable to read window name from dump file.");
76 DEBUG(>= 1)if (debug >= 1)
77 fprintf(stderr__stderrp,"win_name =%s\n", win_name);
78
79 width = header.pixmap_width;
80 height = header.pixmap_height;
81 fixed_width = 8 * (byte_width = header.bytes_per_line);
82 one_plane_size = byte_width * height;
83 buffer_size = one_plane_size *
84 ((header.pixmap_format == ZPixmap2)? header.pixmap_depth: 1);
85
86 /* Determine orientation and scale if not specified */
87 if (orient == UNSPECIFIED)
88 orient = (fixed_width <= height)? PORTRAIT: LANDSCAPE;
89 if (scale <= 0) {
90 int real_height = height;
91 if (head) real_height += FONT_HEIGHT_PIXELS(40*75/240) << 1;
92 if (foot) real_height += FONT_HEIGHT_PIXELS(40*75/240) << 1;
93 switch(orient) {
94 default:
95 case PORTRAIT:
96 case UPSIDE_DOWN:
97 scale = min_((p_width - 2*x_pos) / fixed_width,(((p_width - 2*x_pos) / fixed_width) < ((p_length - 2*y_pos
) / real_height) ? ((p_width - 2*x_pos) / fixed_width) : ((p_length
- 2*y_pos) / real_height))
98 (p_length - 2*y_pos) / real_height)(((p_width - 2*x_pos) / fixed_width) < ((p_length - 2*y_pos
) / real_height) ? ((p_width - 2*x_pos) / fixed_width) : ((p_length
- 2*y_pos) / real_height))
;
99 break;
100 case LANDSCAPE:
101 case LANDSCAPE_LEFT:
102 scale = min_((p_length - 2*y_pos) / fixed_width,(((p_length - 2*y_pos) / fixed_width) < ((p_width - 2*x_pos
) / real_height) ? ((p_length - 2*y_pos) / fixed_width) : ((p_width
- 2*x_pos) / real_height))
103 (p_width - 2*x_pos) / real_height)(((p_length - 2*y_pos) / fixed_width) < ((p_width - 2*x_pos
) / real_height) ? ((p_length - 2*y_pos) / fixed_width) : ((p_width
- 2*x_pos) / real_height))
;
104 break;
105 }
106 if (scale <= 0)
107 leave("PixMap doesn't fit on page.");
108 else DEBUG(>1)if (debug >1)
109 fprintf(stderr__stderrp, "scaling by %d to yield %d x %d image\n",
110 scale, fixed_width*scale, height*scale);
111 }
112
113 ncolors = header.ncolors;
114 if (ncolors) {
115 int i;
116 XColor *colors = (XColor *)malloc((unsigned) (header.ncolors * sizeof(XColor)));
117
118 if (fread((char *)colors, sizeof(XColor), ncolors, in) != ncolors)
119 leave("Unable to read colormap from dump file.");
120
121 if (*(char *) &swaptest) {
122 for (i = 0; i < ncolors; i++) {
123 _swaplong((char *) &colors[i].pixel, (long)sizeof(long));
124 _swapshort((char *) &colors[i].red, (long) (3 * sizeof(short)));
125 }
126 }
127 if (ncolors == 2 && INTENSITY(&colors[0])(30L*(int)(&colors[0])->red + 59L*(int)(&colors[0]
)->green + 11L*(int)(&colors[0])->blue)
> INTENSITY(&colors[1])(30L*(int)(&colors[1])->red + 59L*(int)(&colors[1]
)->green + 11L*(int)(&colors[1])->blue)
)
128 invert = !invert;
129 free( colors );
130 }
131
132 invert = !invert; /* 3812 puts ink (i.e. black) on 1-bits */
133
134 if ((buffer = (unsigned char *) calloc(buffer_size, 1)) == NULL((void*)0))
135 leave("Can't calloc data buffer.");
136 bzero((char *) buffer, (int) buffer_size)__builtin___memset_chk ((char *) buffer, 0, (int) buffer_size
, __builtin_object_size ((char *) buffer, 0))
;
137
138 /* Read bitmap from file */
139 if (fread((char *) buffer, sizeof(char), (int) buffer_size, in)
140 != buffer_size)
141 leave("Unable to read pixmap from dump file.");
142
143 if (header.bitmap_bit_order == LSBFirst0)
144 {
145 unsigned char bitswap[256], *bp;
146 int c;
147 for(c = 256; c--;) {
148 bitswap[c] = ((c & 01) << 7) + ((c & 02) << 5) + ((c & 04) << 3) +
149 ((c & 010) << 1) + ((c & 020) >> 1) + ((c & 040) >> 3) +
150 ((c & 0100) >> 5) + ((c & 0200) >> 7);
151 if (invert)
152 bitswap[c] = ~bitswap[c];
153 }
154 /* Here's where we do the bitswapping. */
155 for(bp = buffer+buffer_size; bp-- > buffer;)
156 *bp = bitswap[*bp];
157 }
158 else if (invert) {
159 unsigned char *bp;
160 for(bp = buffer+buffer_size; bp-- > buffer;)
161 *bp = ~*bp;
162 }
163
164 /* we don't want the last bits up to the byte/word alignment set */
165 if ((no_of_bits = fixed_width - width)) {
166 int i, j, mask = ~bits_set(no_of_bits % 8);
167 for(i = 0; i < height; i++) {
168 unsigned char *s = buffer + (i+1) * byte_width ;
169
170 for(j = no_of_bits / 8; j--;)
171 *--s = 0;
172 *--s &= mask;
173 }
174 }
175
176 DEBUG(>= 1)if (debug >= 1)
177 fprintf(stderr__stderrp,"read %d bytes for a %d (%d bytes) x %d image\n",
178 buffer_size, (int) width, byte_width, (int) height);
179 /* Scale the bitmap */
180 if (scale > 1) {
181 unsigned char *tbl = magnification_table(scale);
182 unsigned char *scale_buf;
183 int i, j, k;
184
185 if ((scale_buf = (unsigned char *)
186 calloc((unsigned) (buffer_size *= scale*scale), sizeof(char)))
187 == NULL((void*)0))
188 leave("Can't calloc scaled buffer.");
189 for(i = 0; i < height; i++) {
190 unsigned char *src, *ss;
191 src = buffer + i * byte_width ;
192 ss = scale_buf + i * scale * scale * byte_width;
193 for(j = 0; j < byte_width; j++) {
194 unsigned char *dst = ss+j*scale;
195 unsigned char *expansion = tbl+scale*src[j];
196 for(k = 0; k < scale; k++, dst += byte_width*scale) {
197 memmove((char *) dst, (char *) expansion, scale)__builtin___memmove_chk ((char *) dst, (char *) expansion, scale
, __builtin_object_size ((char *) dst, 0))
;
198 }
199 }
200 }
201 free((char *) buffer);
202 free((char *) tbl);
203 buffer = scale_buf;
204 byte_width *= scale;
205 width *= scale;
206 fixed_width *= scale;
207 height *= scale;
208 one_plane_size *= scale*scale;
209 }
210 DEBUG(==3)if (debug ==3) {
211 int i, j, k;
212 unsigned char *s;
213
214 fprintf(stderr__stderrp, "dumping %d x %d grid\n", fixed_width, height);
215 for(i = 0; i < height; i++) {
216 s = buffer + i * byte_width ;
217 for(j = 0; j < byte_width; j++)
218 for(k = 8; k--;)
219 (void) putc((s[j] & 1<<k)? '*': '-', stderr__stderrp);
220 (void) putc('\n', stderr__stderrp);
221 }
222 }
223 p_set_orientation(out, orient);
224 p_restore_cursor(out, 0);
225 p_save_cursor(out, 3);
226 if (head != NULL((void*)0)) {
227 p_move_abs( out, x_pos + (width - strlen(foot)*FONT_WIDTH24) >> 1,
228 y_pos - FONT_HEIGHT40 );
229 fprintf(out, "%s\n", head);
230 }
231 if (foot != NULL((void*)0)) {
232 p_move_abs( out, x_pos + (width - strlen(foot)*FONT_WIDTH24) >> 1,
233 y_pos + height + (FONT_HEIGHT40 << 1) );
234 fprintf(out, "%s\n", foot);
235 }
236 p_move_abs(out, x_pos, y_pos);
237 p_bitmap(out, height, fixed_width, (unsigned long) one_plane_size,
238 buffer + plane * one_plane_size);
239 free((char *) win_name);
240 free((char *) buffer);
241}
242
243static
244unsigned char *magnification_table(int scale)
245{
246 unsigned char *tbl;
247 int c;
248
249 if ((tbl = (unsigned char *)
250 calloc((unsigned) (scale*256), sizeof(char))) == NULL((void*)0))
Result of 'calloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'char'
251 leave("Can't calloc magnification table.");
252 bzero((char *) tbl, scale*256)__builtin___memset_chk ((char *) tbl, 0, scale*256, __builtin_object_size
((char *) tbl, 0))
;
253 for(c = 256; c--;) {
254 int b = c, bit;
255 unsigned char *entry = tbl+c*scale;
256
257 while (b) {
258 int i, last, mask;
259 bit = 1;
260 mask = b;
261 while (! (mask & 1)) {
262 bit++;
263 mask = mask >> 1;
264 }
265 last = scale*(bit-1);
266 for(i = scale*bit; i-- > last ;)
267 entry[(scale - 1) - i / 8] |= 1 << (i % 8);
268 b &= ~(1 << bit-1);
269 }
270 }
271 return tbl;
272}
273
274/* returns 2^n-1, i.e. a number with bits n-1 through 0 set.
275 * (zero for n == 0) */
276static
277int bits_set(int n)
278{
279 int ans = 0;
280 while(n--)
281 ans |= 1 << n;
282 return ans;
283}
284
285static
286void leave(const char *s)
287{
288 fprintf(stderr__stderrp, "\n%s: ", progname);
289 if (errno(*__error()) != 0)
290 perror(s);
291 else
292 fprintf(stderr__stderrp, "%s", s);
293 fprintf(stderr__stderrp, "\n");
294 exit(EXIT_FAILURE1);
295}
296
297/* move to coordinates x, y (in pels) */
298static
299void p_move_abs(FILE *p, int x, int y)
300{
301 if (x >= 0) {
302 PMP(p, 3){ fprintf(p, "\033[C"); { (void) putc(((3) & 0xFF), (p));
(void) putc((((3) & 0xFF00) >>8), (p)); }; }
;
303 (void) putc('\340', p);
304 p_wput(x, p){ (void) putc((((x) & 0xFF00) >>8), (p)); (void) putc
(((x) & 0xFF), (p)); }
;
305 }
306 if (y >= 0) {
307 PMP(p, 3){ fprintf(p, "\033[C"); { (void) putc(((3) & 0xFF), (p));
(void) putc((((3) & 0xFF00) >>8), (p)); }; }
;
308 (void) putc('\341', p);
309 p_wput(y, p){ (void) putc((((y) & 0xFF00) >>8), (p)); (void) putc
(((y) & 0xFF), (p)); }
;
310 }
311}
312
313/* save current cursor position into (printer) register reg */
314static
315void p_save_cursor(FILE *p, int reg)
316{
317 PMP(p, 1){ fprintf(p, "\033[C"); { (void) putc(((1) & 0xFF), (p));
(void) putc((((1) & 0xFF00) >>8), (p)); }; }
;
318 (void) putc(reg + '\200', p);
319}
320
321/* restore current cursor position from (printer) register reg */
322static
323void p_restore_cursor(FILE *p, int reg)
324{
325 PMP(p, 1){ fprintf(p, "\033[C"); { (void) putc(((1) & 0xFF), (p));
(void) putc((((1) & 0xFF00) >>8), (p)); }; }
;
326 (void) putc(reg + '\220', p);
327}
328
329/* set the page orientation to orient (see pmp.h) */
330static
331void p_set_orientation(FILE *p, enum orientation orient)
332{
333 PMP(p, 2){ fprintf(p, "\033[C"); { (void) putc(((2) & 0xFF), (p));
(void) putc((((2) & 0xFF00) >>8), (p)); }; }
;
334 fprintf(p, "\322%c", (int) orient);
335}
336
337/* generate bitmap */
338static
339void p_bitmap(
340 FILE *p,
341 unsigned int h, int w,
342 unsigned long buflen,
343 unsigned char *buf)
344{
345 PMP(p, 9){ fprintf(p, "\033[C"); { (void) putc(((9) & 0xFF), (p));
(void) putc((((9) & 0xFF00) >>8), (p)); }; }
;
346 (void) fwrite("\365\0", 1, 2, p);
347 puthl2(h, p){ (void) putc(((((h)) & 0xFF00) >>8), (p)); (void) putc
((((h)) & 0xFF), (p)); }
;
348 puthl2(w, p){ (void) putc(((((w)) & 0xFF00) >>8), (p)); (void) putc
((((w)) & 0xFF), (p)); }
;
349 puthl3(buflen, p){ (void) putc(((((buflen)) & 0xFF0000) >>16), (p));
(void) putc(((((buflen)) & 0xFF00) >>8), (p)); (void
) putc((((buflen)) & 0xFF), (p)); }
;
350
351 while(buflen) {
352 int len;
353
354 len = min(buflen, MAX_VECTOR_LEN)((buflen) < (65535)? (buflen) : (65535));
355 PMP(p, len){ fprintf(p, "\033[C"); { (void) putc(((len) & 0xFF), (p)
); (void) putc((((len) & 0xFF00) >>8), (p)); }; }
;
356 (void) fwrite((char *) buf, 1, len, p);
357 buf += len;
358 buflen -= len;
359 }
360 (void) fflush(p);
361}