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 : */
34 : void Putword (int w, gdIOCtx * ctx)
35 0 : {
36 : unsigned char buf[2];
37 :
38 0 : buf[0] = w & 0xff;
39 0 : buf[1] = (w / 256) & 0xff;
40 0 : (ctx->putBuf) (ctx, (char *) buf, 2);
41 0 : }
42 :
43 : void Putchar (int c, gdIOCtx * ctx)
44 0 : {
45 0 : (ctx->putC) (ctx, c & 0xff);
46 0 : }
47 :
48 : void gdPutC (const unsigned char c, gdIOCtx * ctx)
49 97165 : {
50 97165 : (ctx->putC) (ctx, c);
51 97165 : }
52 :
53 : void gdPutWord (int w, gdIOCtx * ctx)
54 67 : {
55 : IO_DBG (php_gd_error("Putting word..."));
56 67 : (ctx->putC) (ctx, (unsigned char) (w >> 8));
57 67 : (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
58 : IO_DBG (php_gd_error("put."));
59 67 : }
60 :
61 : void gdPutInt (int w, gdIOCtx * ctx)
62 72372 : {
63 : IO_DBG (php_gd_error("Putting int..."));
64 72372 : (ctx->putC) (ctx, (unsigned char) (w >> 24));
65 72372 : (ctx->putC) (ctx, (unsigned char) ((w >> 16) & 0xFF));
66 72372 : (ctx->putC) (ctx, (unsigned char) ((w >> 8) & 0xFF));
67 72372 : (ctx->putC) (ctx, (unsigned char) (w & 0xFF));
68 : IO_DBG (php_gd_error("put."));
69 72372 : }
70 :
71 : int gdGetC (gdIOCtx * ctx)
72 71217 : {
73 71217 : return ((ctx->getC) (ctx));
74 : }
75 :
76 : int gdGetByte (int *result, gdIOCtx * ctx)
77 2058 : {
78 : int r;
79 2058 : r = (ctx->getC) (ctx);
80 2058 : GD_IO_EOF_CHK(r);
81 2058 : *result = r;
82 2058 : return 1;
83 : }
84 :
85 : int gdGetWord (int *result, gdIOCtx * ctx)
86 63 : {
87 : int r;
88 63 : r = (ctx->getC) (ctx);
89 63 : GD_IO_EOF_CHK(r);
90 63 : *result = r << 8;
91 63 : r = (ctx->getC) (ctx);
92 63 : GD_IO_EOF_CHK(r);
93 63 : *result += r;
94 63 : return 1;
95 : }
96 :
97 :
98 : int gdGetInt (int *result, gdIOCtx * ctx)
99 71670 : {
100 : int r;
101 71670 : r = (ctx->getC) (ctx);
102 71670 : GD_IO_EOF_CHK(r);
103 71670 : *result = r << 24;
104 :
105 71670 : r = (ctx->getC) (ctx);
106 71670 : GD_IO_EOF_CHK(r);
107 71670 : *result += r << 16;
108 :
109 71670 : r = (ctx->getC) (ctx);
110 71670 : if (r == EOF) {
111 0 : return 0;
112 : }
113 71670 : *result += r << 8;
114 :
115 71670 : r = (ctx->getC) (ctx);
116 71670 : GD_IO_EOF_CHK(r);
117 71670 : *result += r;
118 :
119 71670 : return 1;
120 : }
121 :
122 : int gdPutBuf (const void *buf, int size, gdIOCtx * ctx)
123 1831 : {
124 : IO_DBG (php_gd_error("Putting buf..."));
125 1831 : return (ctx->putBuf) (ctx, buf, size);
126 : IO_DBG (php_gd_error("put."));
127 : }
128 :
129 : int gdGetBuf (void *buf, int size, gdIOCtx * ctx)
130 3738 : {
131 3738 : return (ctx->getBuf) (ctx, buf, size);
132 : }
133 :
134 : int gdSeek (gdIOCtx * ctx, const int pos)
135 1 : {
136 : IO_DBG (php_gd_error("Seeking..."));
137 1 : return ((ctx->seek) (ctx, pos));
138 : IO_DBG (php_gd_error("Done."));
139 : }
140 :
141 : long gdTell (gdIOCtx * ctx)
142 1 : {
143 : IO_DBG (php_gd_error("Telling..."));
144 1 : return ((ctx->tell) (ctx));
145 : IO_DBG (php_gd_error ("told."));
146 : }
|