1 /*
2  * io.c
3  *
4  * Implements the simple I/O 'helper' routines.
5  *
6  * Not really essential, but these routines were used extensively in GD,
7  * so they were moved here. They also make IOCtx calls look better...
8  *
9  * Written (or, at least, moved) 1999, Philip Warner.
10  */
11 
12 #ifdef HAVE_CONFIG_H
13 #	include "config.h"
14 #endif
15 
16 #include <math.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include "gd.h"
20 
21 /* Use this for commenting out debug-print statements. */
22 /* Just use the first '#define' to allow all the prints... */
23 /*#define IO_DBG(s) (s) */
24 #define IO_DBG(s)
25 
26 #define GD_IO_EOF_CHK(r) \
27 	if(r == EOF) { \
28 		return 0; \
29 	}
30 
gdPutC(const unsigned char c,gdIOCtx * ctx)31 void gdPutC(const unsigned char c, gdIOCtx *ctx)
32 {
33 	(ctx->putC)(ctx, c);
34 }
35 
gdPutWord(int w,gdIOCtx * ctx)36 void gdPutWord (int w, gdIOCtx *ctx)
37 {
38 	IO_DBG(printf("Putting word...\n"));
39 	(ctx->putC)(ctx, (unsigned char)(w >> 8));
40 	(ctx->putC)(ctx, (unsigned char)(w & 0xFF));
41 	IO_DBG(printf("put.\n"));
42 }
43 
gdPutInt(int w,gdIOCtx * ctx)44 void gdPutInt (int w, gdIOCtx *ctx)
45 {
46 	IO_DBG(printf("Putting int...\n"));
47 	(ctx->putC)(ctx, (unsigned char) (w >> 24));
48 	(ctx->putC)(ctx, (unsigned char) ((w >> 16) & 0xFF));
49 	(ctx->putC)(ctx, (unsigned char) ((w >> 8) & 0xFF));
50 	(ctx->putC)(ctx, (unsigned char) (w & 0xFF));
51 	IO_DBG(printf("put.\n"));
52 }
53 
gdGetC(gdIOCtx * ctx)54 int gdGetC(gdIOCtx *ctx)
55 {
56 	return ((ctx->getC)(ctx));
57 }
58 
gdGetByte(int * result,gdIOCtx * ctx)59 int gdGetByte(int *result, gdIOCtx *ctx)
60 {
61 	int r;
62 
63 	r = (ctx->getC)(ctx);
64 	if(r == EOF) {
65 		return 0;
66 	}
67 
68 	*result = r;
69 
70 	return 1;
71 }
72 
gdGetWord(int * result,gdIOCtx * ctx)73 int gdGetWord(int *result, gdIOCtx *ctx)
74 {
75 	int r;
76 
77 	r = (ctx->getC)(ctx);
78 	if(r == EOF) {
79 		return 0;
80 	}
81 
82 	*result = r << 8;
83 
84 	r = (ctx->getC)(ctx);
85 	if(r == EOF) {
86 		return 0;
87 	}
88 
89 	*result += r;
90 
91 	return 1;
92 }
93 
gdGetWordLSB(signed short int * result,gdIOCtx * ctx)94 int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
95 {
96 	int high = 0, low = 0;
97 	low = (ctx->getC) (ctx);
98 	if (low == EOF) {
99 		return 0;
100 	}
101 
102 	high = (ctx->getC) (ctx);
103 	if (high == EOF) {
104 		return 0;
105 	}
106 
107 	if (result) {
108 		*result = (high << 8) | low;
109 	}
110 
111 	return 1;
112 }
113 
gdGetInt(int * result,gdIOCtx * ctx)114 int gdGetInt(int *result, gdIOCtx *ctx)
115 {
116 	unsigned int r;
117 
118 	r = (ctx->getC)(ctx);
119 	if(r == EOF) {
120 		return 0;
121 	}
122 
123 	*result = r << 24;
124 
125 	r = (ctx->getC)(ctx);
126 	if(r == EOF) {
127 		return 0;
128 	}
129 
130 	*result += r << 16;
131 
132 	r = (ctx->getC)(ctx);
133 	if(r == EOF) {
134 		return 0;
135 	}
136 
137 	*result += r << 8;
138 
139 	r = (ctx->getC)(ctx);
140 	if(r == EOF) {
141 		return 0;
142 	}
143 
144 	*result += r;
145 
146 	return 1;
147 }
148 
gdGetIntLSB(signed int * result,gdIOCtx * ctx)149 int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
150 {
151 	unsigned int c;
152 	unsigned int r = 0;
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 	r >>= 8;
174 
175 	c = (ctx->getC) (ctx);
176 	if (c == EOF) {
177 		return 0;
178 	}
179 	r |= (c << 24);
180 
181 	if (result) {
182 		*result = (signed int)r;
183 	}
184 
185 	return 1;
186 }
187 
gdPutBuf(const void * buf,int size,gdIOCtx * ctx)188 int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
189 {
190 	IO_DBG(printf("Putting buf...\n"));
191 	return (ctx->putBuf)(ctx, buf, size);
192 	IO_DBG(printf("put.\n"));
193 }
194 
gdGetBuf(void * buf,int size,gdIOCtx * ctx)195 int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
196 {
197 	return (ctx->getBuf)(ctx, buf, size);
198 }
199 
gdSeek(gdIOCtx * ctx,const int pos)200 int gdSeek(gdIOCtx *ctx, const int pos)
201 {
202 	IO_DBG(printf("Seeking...\n"));
203 	return ((ctx->seek)(ctx, pos));
204 	IO_DBG(printf("Done.\n"));
205 }
206 
gdTell(gdIOCtx * ctx)207 long gdTell(gdIOCtx *ctx)
208 {
209 	IO_DBG(printf("Telling...\n"));
210 	return ((ctx->tell)(ctx));
211 	IO_DBG(printf("told.\n"));
212 }
213