2008-03-12 01:17:25 -07:00
|
|
|
/*
|
2007-10-28 14:15:15 -07:00
|
|
|
* io.c
|
|
|
|
*
|
|
|
|
* Implements the simple I/O 'helper' routines.
|
2007-10-28 15:04:36 -07:00
|
|
|
*
|
2007-10-28 14:15:15 -07:00
|
|
|
* Not really essential, but these routines were used extensively in GD,
|
|
|
|
* so they were moved here. They also make IOCtx calls look better...
|
2007-10-28 15:04:36 -07:00
|
|
|
*
|
2007-10-28 14:15:15 -07:00
|
|
|
* Written (or, at least, moved) 1999, Philip Warner.
|
2006-04-05 08:42:56 -07:00
|
|
|
*/
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2006-04-05 08:44:56 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-10-28 16:09:09 -07:00
|
|
|
# include "config.h"
|
2006-04-05 08:44:56 -07:00
|
|
|
#endif
|
|
|
|
|
2006-04-05 08:35:53 -07:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "gd.h"
|
|
|
|
|
|
|
|
/* Use this for commenting out debug-print statements. */
|
|
|
|
/* Just use the first '#define' to allow all the prints... */
|
|
|
|
/*#define IO_DBG(s) (s) */
|
|
|
|
#define IO_DBG(s)
|
|
|
|
|
2007-10-07 12:41:52 -07:00
|
|
|
#define GD_IO_EOF_CHK(r) \
|
2007-10-28 14:15:15 -07:00
|
|
|
if(r == EOF) { \
|
2007-10-07 12:41:52 -07:00
|
|
|
return 0; \
|
|
|
|
}
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
void gdPutC(const unsigned char c, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
(ctx->putC)(ctx, c);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
void gdPutWord (int w, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
IO_DBG(printf("Putting word...\n"));
|
|
|
|
(ctx->putC)(ctx, (unsigned char)(w >> 8));
|
|
|
|
(ctx->putC)(ctx, (unsigned char)(w & 0xFF));
|
|
|
|
IO_DBG(printf("put.\n"));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
void gdPutInt (int w, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
IO_DBG(printf("Putting int...\n"));
|
|
|
|
(ctx->putC)(ctx, (unsigned char) (w >> 24));
|
|
|
|
(ctx->putC)(ctx, (unsigned char) ((w >> 16) & 0xFF));
|
|
|
|
(ctx->putC)(ctx, (unsigned char) ((w >> 8) & 0xFF));
|
|
|
|
(ctx->putC)(ctx, (unsigned char) (w & 0xFF));
|
|
|
|
IO_DBG(printf("put.\n"));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdGetC(gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
return ((ctx->getC)(ctx));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdGetByte(int *result, gdIOCtx *ctx)
|
|
|
|
{
|
|
|
|
int r;
|
2008-03-12 01:17:25 -07:00
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
*result = r;
|
2006-04-05 08:35:53 -07:00
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
return 1;
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdGetWord(int *result, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = r << 8;
|
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result += r;
|
|
|
|
|
|
|
|
return 1;
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2008-03-12 01:17:25 -07:00
|
|
|
int gdGetWordLSB(signed short int *result, gdIOCtx *ctx)
|
2007-10-09 04:13:56 -07:00
|
|
|
{
|
2013-02-14 22:39:38 -08:00
|
|
|
int high = 0, low = 0;
|
2008-05-31 12:51:14 -07:00
|
|
|
low = (ctx->getC) (ctx);
|
|
|
|
if (low == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
high = (ctx->getC) (ctx);
|
|
|
|
if (high == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
if (result) {
|
2007-10-09 04:13:56 -07:00
|
|
|
*result = (high << 8) | low;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdGetInt(int *result, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2018-01-25 20:02:32 -08:00
|
|
|
unsigned int r;
|
2007-10-28 14:15:15 -07:00
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = r << 24;
|
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result += r << 16;
|
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result += r << 8;
|
|
|
|
|
|
|
|
r = (ctx->getC)(ctx);
|
|
|
|
if(r == EOF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result += r;
|
|
|
|
|
|
|
|
return 1;
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2008-03-12 01:17:25 -07:00
|
|
|
int gdGetIntLSB(signed int *result, gdIOCtx *ctx)
|
2007-10-09 04:13:56 -07:00
|
|
|
{
|
2018-01-25 20:18:56 -08:00
|
|
|
unsigned int c;
|
2008-05-31 12:51:14 -07:00
|
|
|
unsigned int r = 0;
|
2007-10-28 14:15:15 -07:00
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
c = (ctx->getC) (ctx);
|
|
|
|
if (c == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
r |= (c << 24);
|
|
|
|
r >>= 8;
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
c = (ctx->getC) (ctx);
|
|
|
|
if (c == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
r |= (c << 24);
|
|
|
|
r >>= 8;
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
c = (ctx->getC) (ctx);
|
|
|
|
if (c == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
r |= (c << 24);
|
|
|
|
r >>= 8;
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
c = (ctx->getC) (ctx);
|
|
|
|
if (c == EOF) {
|
2013-04-03 05:23:11 -07:00
|
|
|
return 0;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
r |= (c << 24);
|
|
|
|
|
2008-05-31 12:51:14 -07:00
|
|
|
if (result) {
|
|
|
|
*result = (signed int)r;
|
2007-10-09 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdPutBuf(const void *buf, int size, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
IO_DBG(printf("Putting buf...\n"));
|
|
|
|
return (ctx->putBuf)(ctx, buf, size);
|
|
|
|
IO_DBG(printf("put.\n"));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdGetBuf(void *buf, int size, gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
return (ctx->getBuf)(ctx, buf, size);
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
int gdSeek(gdIOCtx *ctx, const int pos)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
IO_DBG(printf("Seeking...\n"));
|
|
|
|
return ((ctx->seek)(ctx, pos));
|
|
|
|
IO_DBG(printf("Done.\n"));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|
|
|
|
|
2007-10-28 14:15:15 -07:00
|
|
|
long gdTell(gdIOCtx *ctx)
|
2006-04-05 08:35:53 -07:00
|
|
|
{
|
2007-10-28 14:15:15 -07:00
|
|
|
IO_DBG(printf("Telling...\n"));
|
|
|
|
return ((ctx->tell)(ctx));
|
|
|
|
IO_DBG(printf("told.\n"));
|
2006-04-05 08:35:53 -07:00
|
|
|
}
|