1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* io_dp.c
*
* Implements the dynamic pointer interface.
*
* Based on GD.pm code by Lincoln Stein for interfacing to libgd.
* Added support for reading as well as support for 'tell' and 'seek'.
*
* As will all I/O modules, most functions are for local use only (called
* via function pointers in the I/O context).
*
* gdDPExtractData is the exception to this: it will return the pointer to
* the internal data, and reset the internal storage.
*
* Written/Modified 1999, Philip Warner.
*
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "gd.h"
#include "gdhelpers.h"
#define TRUE 1
#define FALSE 0
/* this is used for creating images in main memory*/
typedef struct dpStruct {
void* data;
int logicalSize;
int realSize;
int dataGood;
int pos;
} dynamicPtr;
typedef struct dpIOCtx {
gdIOCtx ctx;
dynamicPtr *dp;
} dpIOCtx;
typedef struct dpIOCtx *dpIOCtxPtr;
/* these functions operate on in-memory dynamic pointers */
static int allocDynamic (dynamicPtr* dp,int initialSize, void *data);
static int appendDynamic (dynamicPtr* dp, const void* src, int size);
static int gdReallocDynamic (dynamicPtr* dp, int required);
static int trimDynamic (dynamicPtr* dp);
static void gdFreeDynamicCtx(struct gdIOCtx* ctx);
static dynamicPtr* newDynamic(int initialSize, void *data);
static int dynamicPutbuf( struct gdIOCtx*, const void *, int );
static void dynamicPutchar( struct gdIOCtx*, int a );
static int dynamicGetbuf( gdIOCtxPtr ctx, void *buf, int len);
static int dynamicGetchar( gdIOCtxPtr ctx );
static int dynamicSeek(struct gdIOCtx*, const int);
static long dynamicTell(struct gdIOCtx*);
/* return data as a dynamic pointer */
gdIOCtx* gdNewDynamicCtx (int initialSize, void *data) {
dpIOCtx *ctx;
dynamicPtr* dp;
ctx = (dpIOCtx*) gdMalloc(sizeof(dpIOCtx));
if (ctx == NULL) {
return NULL;
}
dp = newDynamic(initialSize, data);
if (!dp) {
gdFree(ctx);
return NULL;
};
ctx->dp = dp;
ctx->ctx.getC = dynamicGetchar;
ctx->ctx.putC = dynamicPutchar;
ctx->ctx.getBuf = dynamicGetbuf;
ctx->ctx.putBuf = dynamicPutbuf;
ctx->ctx.seek = dynamicSeek;
ctx->ctx.tell = dynamicTell;
ctx->ctx.free = gdFreeDynamicCtx;
return (gdIOCtx*)ctx;
}
void* gdDPExtractData(struct gdIOCtx* ctx, int *size)
{
dynamicPtr *dp;
dpIOCtx *dctx;
void *data;
dctx = (dpIOCtx*) ctx;
dp = dctx->dp;
/* clean up the data block and return it */
if (dp->dataGood) {
trimDynamic(dp);
*size = dp->logicalSize;
data = dp->data;
} else {
*size = 0;
data = NULL;
if (dp->data != NULL) {
gdFree(dp->data);
}
}
dp->data = NULL;
dp->realSize=0;
dp->logicalSize=0;
return data;
}
static
void gdFreeDynamicCtx(struct gdIOCtx* ctx)
{
dynamicPtr *dp;
dpIOCtx *dctx;
dctx = (dpIOCtx*) ctx;
dp = dctx->dp;
gdFree(ctx);
/* clean up the data block and return it */
if (dp->data != NULL) {
gdFree(dp->data);
dp->data = NULL;
}
dp->realSize=0;
dp->logicalSize=0;
gdFree(dp);
}
static long dynamicTell(struct gdIOCtx* ctx)
{
dpIOCtx *dctx;
dctx = (dpIOCtx*) ctx;
return (dctx->dp->pos);
}
static int dynamicSeek(struct gdIOCtx* ctx, const int pos)
{
int bytesNeeded;
dynamicPtr *dp;
dpIOCtx *dctx;
dctx = (dpIOCtx*) ctx;
dp = dctx->dp;
if (!dp->dataGood) return FALSE;
bytesNeeded = pos;
if (bytesNeeded > dp->realSize) {
if (dp->realSize >= INT_MAX/2 ||
!gdReallocDynamic(dp,dp->realSize*2)) {
dp->dataGood = FALSE;
return FALSE;
}
}
/* if we get here, we can be sure that we have enough bytes
to copy safely */
/* Extend the logical size if we seek beyond EOF. */
if (pos > dp->logicalSize) {
dp->logicalSize = pos;
};
dp->pos = pos;
return TRUE;
}
/* return data as a dynamic pointer */
static dynamicPtr* newDynamic (int initialSize, void *data) {
dynamicPtr* dp;
dp = (dynamicPtr*) gdMalloc(sizeof(dynamicPtr));
if (dp == NULL) {
return NULL;
}
if (!allocDynamic(dp,initialSize, data))
return NULL;
dp->pos = 0;
return dp;
}
static int
dynamicPutbuf( struct gdIOCtx* ctx, const void *buf, int size )
{
dpIOCtx *dctx;
dctx = (dpIOCtx*) ctx;
appendDynamic(dctx->dp,buf,size);
if (dctx->dp->dataGood) {
return size;
} else {
return -1;
};
}
static void
dynamicPutchar( struct gdIOCtx* ctx, int a )
{
unsigned char b;
dpIOCtxPtr dctx;
b = a;
dctx = (dpIOCtxPtr) ctx;
appendDynamic(dctx->dp,&b,1);
}
static int
dynamicGetbuf( gdIOCtxPtr ctx, void *buf, int len)
{
int rlen, remain;
dpIOCtxPtr dctx;
dynamicPtr* dp;
dctx = (dpIOCtxPtr) ctx;
dp = dctx->dp;
remain = dp->logicalSize - dp->pos;
if (remain >= len) {
rlen = len;
} else {
if (remain == 0) {
return EOF;
}
rlen = remain;
}
memcpy(buf, (void*)((char*)dp->data + dp->pos), rlen);
dp->pos += rlen;
return rlen;
}
static int
dynamicGetchar( gdIOCtxPtr ctx )
{
unsigned char b;
int rv;
rv = dynamicGetbuf(ctx, &b, 1);
if (rv != 1) {
return EOF;
} else {
return b ;/* (b & 0xff); */
}
}
/* *********************************************************************
*
* InitDynamic - Return a dynamically resizable void*
*
* *********************************************************************
*/
static int
allocDynamic (dynamicPtr* dp,int initialSize, void *data) {
if (data == NULL) {
dp->logicalSize = 0;
dp->dataGood = FALSE;
dp->data = gdMalloc(initialSize);
} else {
dp->logicalSize = initialSize;
dp->dataGood = TRUE;
dp->data = data;
}
if (dp->data !=NULL) {
dp->realSize = initialSize;
dp->dataGood = TRUE;
dp->pos = 0;
return TRUE;
} else {
dp->realSize = 0;
return FALSE;
}
}
/* append bytes to the end of a dynamic pointer */
static int
appendDynamic (dynamicPtr* dp, const void* src, int size) {
int bytesNeeded;
char* tmp;
if (!dp->dataGood) return FALSE;
/* bytesNeeded = dp->logicalSize + size; */
bytesNeeded = dp->pos + size;
if (bytesNeeded > dp->realSize) {
if (bytesNeeded >= INT_MAX/2 ||
!gdReallocDynamic(dp,bytesNeeded*2)) {
dp->dataGood = FALSE;
return FALSE;
}
}
/* if we get here, we can be sure that we have enough bytes
to copy safely */
/*printf("Mem OK Size: %d, Pos: %d\n", dp->realSize, dp->pos); */
tmp = (char*)dp->data;
memcpy((void*)(tmp+(dp->pos)),src,size);
dp->pos += size;
if (dp->pos > dp->logicalSize) {
dp->logicalSize = dp->pos;
};
return TRUE;
}
/* grow (or shrink) dynamic pointer */
static int
gdReallocDynamic (dynamicPtr* dp, int required) {
void* newPtr;
/* First try gdRealloc(). If that doesn't work, make a new
memory block and copy. */
if ( (newPtr = gdRealloc(dp->data,required)) ) {
dp->realSize = required;
dp->data = newPtr;
return TRUE;
}
/* create a new pointer */
newPtr = gdMalloc(required);
if (!newPtr) {
dp->dataGood = FALSE;
return FALSE;
}
/* copy the old data into it */
memcpy(newPtr,dp->data,dp->logicalSize);
gdFree(dp->data);
dp->data = newPtr;
dp->realSize = required;
return TRUE;
}
/* trim pointer so that its real and logical sizes match */
static int
trimDynamic (dynamicPtr* dp) {
return gdReallocDynamic(dp,dp->logicalSize);
}
|