|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
* io_ss.c
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Implements the Source/Sink interface.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* As will all I/O modules, most functions are for local use only (called
|
|
Packit |
ed3af9 |
* via function pointers in the I/O context).
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* The Source/Sink model is the primary 'user' interface for alternate data
|
|
Packit |
ed3af9 |
* sources; the IOCtx interface is intended (at least in version 1.5) to be
|
|
Packit |
ed3af9 |
* used internally until it settles down a bit.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* This module just layers the Source/Sink interface on top of the IOCtx; no
|
|
Packit |
ed3af9 |
* support is provided for tell/seek, so GD2 writing is not possible, and
|
|
Packit |
ed3af9 |
* retrieving parts of GD2 files is also not possible.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* A new SS context does not need to be created with both a Source and a Sink.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
* Written/Modified 1999, Philip Warner.
|
|
Packit |
ed3af9 |
*
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
ed3af9 |
# include "config.h"
|
|
Packit |
ed3af9 |
#endif
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
#include <math.h>
|
|
Packit |
ed3af9 |
#include <string.h>
|
|
Packit |
ed3af9 |
#include <stdlib.h>
|
|
Packit |
ed3af9 |
#include "gd.h"
|
|
Packit |
ed3af9 |
#include "gdhelpers.h"
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/* this is used for creating images in main memory */
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
typedef struct ssIOCtx {
|
|
Packit |
ed3af9 |
gdIOCtx ctx;
|
|
Packit |
ed3af9 |
gdSourcePtr src;
|
|
Packit |
ed3af9 |
gdSinkPtr snk;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
ssIOCtx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
typedef struct ssIOCtx *ssIOCtxPtr;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int sourceGetbuf(gdIOCtx *, void *, int);
|
|
Packit |
ed3af9 |
static int sourceGetchar(gdIOCtx *ctx);
|
|
Packit |
ed3af9 |
static int sinkPutbuf(gdIOCtx *ctx, const void *buf, int size);
|
|
Packit |
ed3af9 |
static void sinkPutchar(gdIOCtx *ctx, int a);
|
|
Packit |
ed3af9 |
static void gdFreeSsCtx(gdIOCtx *ctx);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
Function: gdNewSSCtx
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
Return data as a dynamic pointer.
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
BGD_DECLARE(gdIOCtx *) gdNewSSCtx(gdSourcePtr src, gdSinkPtr snk)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
ssIOCtxPtr ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx = (ssIOCtxPtr)gdMalloc(sizeof (ssIOCtx));
|
|
Packit |
ed3af9 |
if (ctx == NULL) {
|
|
Packit |
ed3af9 |
return NULL;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->src = src;
|
|
Packit |
ed3af9 |
ctx->snk = snk;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.getC = sourceGetchar;
|
|
Packit |
ed3af9 |
ctx->ctx.getBuf = sourceGetbuf;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.putC = sinkPutchar;
|
|
Packit |
ed3af9 |
ctx->ctx.putBuf = sinkPutbuf;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.tell = NULL;
|
|
Packit |
ed3af9 |
ctx->ctx.seek = NULL;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
ctx->ctx.gd_free = gdFreeSsCtx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
return (gdIOCtx *)ctx;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static void gdFreeSsCtx(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
gdFree(ctx);
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int sourceGetbuf(gdIOCtx *ctx, void *buf, int size)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
ssIOCtx *lctx;
|
|
Packit |
ed3af9 |
int res;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
lctx = (ssIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
res = ((lctx->src->source)(lctx->src->context, buf, size));
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
/*
|
|
Packit |
ed3af9 |
* Translate the return values from the Source object:
|
|
Packit |
ed3af9 |
* 0 is EOF, -1 is error
|
|
Packit |
ed3af9 |
*/
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (res == 0) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
} else if (res < 0) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
} else {
|
|
Packit |
ed3af9 |
return res;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int sourceGetchar(gdIOCtx *ctx)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
int res;
|
|
Packit |
ed3af9 |
unsigned char buf;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
res = sourceGetbuf(ctx, &buf, 1);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (res == 1) {
|
|
Packit |
ed3af9 |
return buf;
|
|
Packit |
ed3af9 |
} else {
|
|
Packit |
ed3af9 |
return EOF;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static int sinkPutbuf(gdIOCtx *ctx, const void *buf, int size)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
ssIOCtxPtr lctx;
|
|
Packit |
ed3af9 |
int res;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
lctx = (ssIOCtx *)ctx;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
res = (lctx->snk->sink)(lctx->snk->context, buf, size);
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
if (res <= 0) {
|
|
Packit |
ed3af9 |
return 0;
|
|
Packit |
ed3af9 |
} else {
|
|
Packit |
ed3af9 |
return res;
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
}
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
static void sinkPutchar(gdIOCtx *ctx, int a)
|
|
Packit |
ed3af9 |
{
|
|
Packit |
ed3af9 |
unsigned char b;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
b = a;
|
|
Packit |
ed3af9 |
|
|
Packit |
ed3af9 |
sinkPutbuf(ctx, &b, 1);
|
|
Packit |
ed3af9 |
}
|