xref: /PHP-8.1/ext/gd/libgd/gd_io.c (revision ea97fc9c)
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 (gd_error("Putting word..."));
56 	(ctx->putC) (ctx, (unsigned char) (w >> 8));
57 	(ctx->putC) (ctx, (unsigned char) (w & 0xFF));
58 	IO_DBG (gd_error("put."));
59 }
60 
gdPutInt(int w,gdIOCtx * ctx)61 void gdPutInt (int w, gdIOCtx * ctx)
62 {
63 	IO_DBG (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 (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 
gdGetWordLSB(signed short int * result,gdIOCtx * ctx)98 int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
99 {
100 	int high = 0, low = 0;
101 	low = (ctx->getC) (ctx);
102 	if (low == EOF) {
103 		return 0;
104 	}
105 
106 	high = (ctx->getC) (ctx);
107 	if (high == EOF) {
108 		return 0;
109 	}
110 
111 	if (result) {
112 		*result = (high << 8) | low;
113 	}
114 
115 	return 1;
116 }
117 
gdGetInt(int * result,gdIOCtx * ctx)118 int gdGetInt (int *result, gdIOCtx * ctx)
119 {
120 	unsigned int r;
121 	r = (ctx->getC) (ctx);
122 	GD_IO_EOF_CHK(r);
123 	*result = r << 24;
124 
125 	r = (ctx->getC) (ctx);
126 	GD_IO_EOF_CHK(r);
127 	*result += r << 16;
128 
129 	r = (ctx->getC) (ctx);
130 	if (r == EOF) {
131 		return 0;
132 	}
133 	*result += r << 8;
134 
135 	r = (ctx->getC) (ctx);
136 	GD_IO_EOF_CHK(r);
137 	*result += r;
138 
139 	return 1;
140 }
141 
gdGetIntLSB(signed int * result,gdIOCtx * ctx)142 int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
143 {
144 	unsigned int c;
145 	unsigned int r = 0;
146 
147 	c = (ctx->getC) (ctx);
148 	if (c == EOF) {
149 		return 0;
150 	}
151 	r |= (c << 24);
152 	r >>= 8;
153 
154 	c = (ctx->getC) (ctx);
155 	if (c == EOF) {
156 		return 0;
157 	}
158 	r |= (c << 24);
159 	r >>= 8;
160 
161 	c = (ctx->getC) (ctx);
162 	if (c == EOF) {
163 		return 0;
164 	}
165 	r |= (c << 24);
166 	r >>= 8;
167 
168 	c = (ctx->getC) (ctx);
169 	if (c == EOF) {
170 		return 0;
171 	}
172 	r |= (c << 24);
173 
174 	if (result) {
175 		*result = (signed int)r;
176 	}
177 
178 	return 1;
179 }
180 
gdPutBuf(const void * buf,int size,gdIOCtx * ctx)181 int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
182 {
183 	IO_DBG (gd_error("Putting buf..."));
184 	return (ctx->putBuf) (ctx, buf, size);
185 	IO_DBG (gd_error("put."));
186 }
187 
gdGetBuf(void * buf,int size,gdIOCtx * ctx)188 int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
189 {
190 	return (ctx->getBuf) (ctx, buf, size);
191 }
192 
gdSeek(gdIOCtx * ctx,const int pos)193 int gdSeek (gdIOCtx * ctx, const int pos)
194 {
195 	IO_DBG (gd_error("Seeking..."));
196 	return ((ctx->seek) (ctx, pos));
197 	IO_DBG (gd_error("Done."));
198 }
199 
gdTell(gdIOCtx * ctx)200 long gdTell (gdIOCtx * ctx)
201 {
202 	IO_DBG (gd_error("Telling..."));
203 	return ((ctx->tell) (ctx));
204 	IO_DBG (gd_error ("told."));
205 }
206