xref: /PHP-5.3/ext/gd/libgd/gd_io.c (revision 75d36222)
1 
2 
3 /*
4    * io.c
5    *
6    * Implements the simple I/O 'helper' routines.
7    *
8    * Not really essential, but these routines were used extensively in GD,
9    * so they were moved here. They also make IOCtx calls look better...
10    *
11    * Written (or, at least, moved) 1999, Philip Warner.
12    *
13  */
14 
15 #include <math.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include "gd.h"
19 
20 /* Use this for commenting out debug-print statements. */
21 /* Just use the first '#define' to allow all the prints... */
22 /*#define IO_DBG(s) (s) */
23 #define IO_DBG(s)
24 
25 
26 #define GD_IO_EOF_CHK(r)	\
27 	if (r == EOF) {		\
28 		return 0;	\
29 	}			\
30 
31 /*
32  * Write out a word to the I/O context pointer
33  */
Putword(int w,gdIOCtx * ctx)34 void Putword (int w, gdIOCtx * ctx)
35 {
36 	unsigned char buf[2];
37 
38 	buf[0] = w & 0xff;
39 	buf[1] = (w / 256) & 0xff;
40 	(ctx->putBuf) (ctx, (char *) buf, 2);
41 }
42 
Putchar(int c,gdIOCtx * ctx)43 void Putchar (int c, gdIOCtx * ctx)
44 {
45 	(ctx->putC) (ctx, c & 0xff);
46 }
47 
gdPutC(const unsigned char c,gdIOCtx * ctx)48 void gdPutC (const unsigned char c, gdIOCtx * ctx)
49 {
50 	(ctx->putC) (ctx, c);
51 }
52 
gdPutWord(int w,gdIOCtx * ctx)53 void gdPutWord (int w, gdIOCtx * ctx)
54 {
55 	IO_DBG (php_gd_error("Putting word..."));
56 	(ctx->putC) (ctx, (unsigned char) (w >> 8));
57 	(ctx->putC) (ctx, (unsigned char) (w & 0xFF));
58 	IO_DBG (php_gd_error("put."));
59 }
60 
gdPutInt(int w,gdIOCtx * ctx)61 void gdPutInt (int w, gdIOCtx * ctx)
62 {
63 	IO_DBG (php_gd_error("Putting int..."));
64 	(ctx->putC) (ctx, (unsigned char) (w >> 24));
65 	(ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
66 	(ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
67 	(ctx->putC) (ctx, (unsigned char) (w & 0xFF));
68 	IO_DBG (php_gd_error("put."));
69 }
70 
gdGetC(gdIOCtx * ctx)71 int gdGetC (gdIOCtx * ctx)
72 {
73 	return ((ctx->getC) (ctx));
74 }
75 
gdGetByte(int * result,gdIOCtx * ctx)76 int gdGetByte (int *result, gdIOCtx * ctx)
77 {
78 	int r;
79 	r = (ctx->getC) (ctx);
80 	GD_IO_EOF_CHK(r);
81 	*result = r;
82 	return 1;
83 }
84 
gdGetWord(int * result,gdIOCtx * ctx)85 int gdGetWord (int *result, gdIOCtx * ctx)
86 {
87 	int r;
88 	r = (ctx->getC) (ctx);
89 	GD_IO_EOF_CHK(r);
90 	*result = r << 8;
91 	r = (ctx->getC) (ctx);
92 	GD_IO_EOF_CHK(r);
93 	*result += r;
94 	return 1;
95 }
96 
97 
gdGetInt(int * result,gdIOCtx * ctx)98 int gdGetInt (int *result, gdIOCtx * ctx)
99 {
100 	int r;
101 	r = (ctx->getC) (ctx);
102 	GD_IO_EOF_CHK(r);
103 	*result = r << 24;
104 
105 	r = (ctx->getC) (ctx);
106 	GD_IO_EOF_CHK(r);
107 	*result += r << 16;
108 
109 	r = (ctx->getC) (ctx);
110 	if (r == EOF) {
111 		return 0;
112 	}
113 	*result += r << 8;
114 
115 	r = (ctx->getC) (ctx);
116 	GD_IO_EOF_CHK(r);
117 	*result += r;
118 
119 	return 1;
120 }
121 
gdPutBuf(const void * buf,int size,gdIOCtx * ctx)122 int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
123 {
124 	IO_DBG (php_gd_error("Putting buf..."));
125 	return (ctx->putBuf) (ctx, buf, size);
126 	IO_DBG (php_gd_error("put."));
127 }
128 
gdGetBuf(void * buf,int size,gdIOCtx * ctx)129 int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
130 {
131 	return (ctx->getBuf) (ctx, buf, size);
132 }
133 
gdSeek(gdIOCtx * ctx,const int pos)134 int gdSeek (gdIOCtx * ctx, const int pos)
135 {
136 	IO_DBG (php_gd_error("Seeking..."));
137 	return ((ctx->seek) (ctx, pos));
138 	IO_DBG (php_gd_error("Done."));
139 }
140 
gdTell(gdIOCtx * ctx)141 long gdTell (gdIOCtx * ctx)
142 {
143 	IO_DBG (php_gd_error("Telling..."));
144 	return ((ctx->tell) (ctx));
145 	IO_DBG (php_gd_error ("told."));
146 }
147