void ReadSegment( InputStream *t, uint8 *dest, long numBytes ) { if ( numBytes > 0 ) { unsigned long pos = t->pos; unsigned char *ptr; #ifdef ENABLE_NON_RAM_STREAM if ( t->ReadToRamFunc != NULL ) { /* prior to the USE_PRE_CACHING option the test was (t->privateBase == NULL) */ int ioCode = t->ReadToRamFunc( t->nonRamID, dest, pos, numBytes ); tsi_Assert( t->mem, ioCode >= 0, T2K_EXT_IO_CALLBACK_ERR ); } else { ptr = &t->privateBase[pos]; memcpy( dest, ptr, (unsigned long)numBytes ); } #else ptr = &t->privateBase[pos]; memcpy( dest, ptr, (unsigned long)numBytes ); #endif pos = pos + numBytes; if ( pos > t->maxPos ) { tsi_Assert( t->mem, pos <= t->maxPos, T2K_BAD_FONT ); } t->pos = pos; } }
/* * Description: Allocates a chunk of memory, and returns a pointer to it. * How used: Just call with the size in bytes. * Side Effects: None. * Return value: A pointer to the memory. */ void *tsi_AllocMem( register tsiMemObject *t, size_t size ) { register tt_int32 i, maxPointers; register unsigned char *p = NULL; register tt_uint32 *plong; register void **base; tsi_Assert( t, t != NULL, T2K_ERR_NULL_MEM ); /* tsi_ValidateMemory( t ); */ /* We bound max memory chunk by 32Mb (similarly to what we do in definition of CLIENT_MALLOC() in config.h. We duplicate check here because it also protects from integer overflow. Note: size_t is supposed to be unsigned but better be safe there are reports that it is actually signed in some gcc version */ if (size >= 0 && size < 0x2000000) { p = (unsigned char *) CLIENT_MALLOC(headerSize + size + tailSize); } tsi_Assert( t, p != NULL, T2K_ERR_MEM_MALLOC_FAILED ); #ifdef TRACK_RAM t->totRAM += headerSize + size + tailSize; if ( t->totRAM > t->maxRAM ) t->maxRAM = t->totRAM; #endif plong = (tt_uint32 *)p; plong[0] = MAGIC3; plong[1] = (tt_uint32)size; p[headerSize + size] = (unsigned char)MAGIC4; p[headerSize + size+1] = (unsigned char)MAGIC5; tsi_Assert( t, t->numPointers < t->maxPointers, T2K_ERR_MEM_TOO_MANY_PTRS ); base = t->base; maxPointers = t->maxPointers; for ( i = 0; i < maxPointers; i++ ) { if ( base[i] == NULL ) { base[i] = p; t->numPointers++; break; /*****/ } } tsi_Assert( t, i < maxPointers, T2K_ERR_MEM_BAD_LOGIC ); #ifdef ZAP_MEMORY { for ( i = 0; i < size; i++ ) { ((char *)p)[i+headerSize] = 0x5a; } } #endif return (p+headerSize); /*****/ }
uint8 ReadUnsignedByteSlow( InputStream *t ) { unsigned long pos = t->pos; uint8 byte; register unsigned char *ptr; #ifdef ENABLE_NON_RAM_STREAM unsigned char base[1]; if ( t->privateBase == NULL ) { int ioCode; ptr = base; ioCode = t->ReadToRamFunc( t->nonRamID, ptr, pos, 1 ); tsi_Assert( t->mem, ioCode >= 0, T2K_EXT_IO_CALLBACK_ERR ); } else { ptr = &t->privateBase[pos]; } #else ptr = &t->privateBase[pos]; #endif assert( pos < t->maxPos ); pos = pos + 1; byte = *ptr; t->pos = pos; return byte; /*****/ }
void tsi_DeleteMemhandler( tsiMemObject *t ) { #ifdef OLD tsi_Assert( t, t->stamp1 == MAGIC1 && t->stamp2 == MAGIC2 , T2K_ERR_BAD_MEM_STAMP ); tsi_Assert( t, t->numPointers == 0, T2K_ERR_MEM_LEAK ); /* Check for dangling pointers */ #endif assert( t->stamp1 == MAGIC1 && t->stamp2 == MAGIC2 ); assert( t->numPointers == 0 ); /* Check for dangling pointers */ #ifdef TRACK_RAM t->totRAM -= (sizeof( tsiMemObject ) + sizeof( void *) * t->maxPointers); printf("********************\n" ); printf("t->totRAM = %d\n", t->totRAM ); printf("t->maxRAM = %d\n", t->maxRAM ); printf("********************\n" ); #endif CLIENT_FREE( t->base ); CLIENT_FREE( t ); }
void tsi_ValidatePointer( register tsiMemObject *t, void *pIn ) { int err; char *p = (char *) pIn; tt_uint32 size; register tt_uint32 *plong; tsi_Assert( t, t != NULL, T2K_ERR_NULL_MEM ); p -= headerSize; plong = (tt_uint32 *)p; err = ( plong[0] != MAGIC3 ); if ( err == 0 ) { size = plong[1]; err |= ( ((unsigned char *)p)[headerSize + size] != MAGIC4 ); err |= ( ((unsigned char *)p)[headerSize + size + 1] != MAGIC5 ); } if ( err != 0 ) { printf("trouble\n"); } tsi_Assert( t, err == 0, T2K_ERR_MEM_INVALID_PTR ); }
void tsi_ValidateMemory( register tsiMemObject *t ) { register tt_int32 i, maxPointers; register void **base; assert( false ); tsi_Assert( t, t != NULL, T2K_ERR_NULL_MEM ); base = t->base; maxPointers = t->maxPointers; for ( i = 0; i < maxPointers; i++ ) { if ( base[i] != NULL ) { tsi_ValidatePointer( t, (char *)base[i] + headerSize ); } } }
int32 ReadInt32( InputStream *t ) { register unsigned char *ptr = t->privateBase; unsigned long pos = t->pos; register uint32 lword; #ifdef ENABLE_NON_RAM_STREAM unsigned long delta; unsigned char base[4]; if ( ptr != NULL ) { /* ptr == t->privateBase */ delta = pos; /* ptr == &t->privateBase[pos] */ #ifdef USE_PRE_CACHING if ( t->ReadToRamFunc != NULL ) { EnsureWeHaveDataInT2KInputStream( t, 4 ); /* Combine additions into one step wo we never have a pointer out of bounds. */ delta -= t->cachePosition; /* ptr = &t->privateBase[pos - t->cachePosition]; */ } #endif ptr += delta; } else { int ioCode; ptr = base; ioCode = t->ReadToRamFunc( t->nonRamID, ptr, pos, 4 ); tsi_Assert( t->mem, ioCode >= 0, T2K_EXT_IO_CALLBACK_ERR ); } #else ptr += pos; #endif pos = pos + 4; assert( pos <= t->maxPos ); t->pos = pos; lword = *ptr++; lword <<= 8; lword |= *ptr++; lword <<= 8; lword |= *ptr++; lword <<= 8; lword |= *ptr; return (int32)lword; /*****/ }
/* * Description: reallocs the memory the pointer "p" points at. * How used: Call with the pointer to the memory, received from ag_AllocMem. * It is OK to call this with a NULL pointer. * Side Effects: None. * Return value: None. */ void *tsi_ReAllocMem( register tsiMemObject *t, void *pIn, size_t size2 ) { register tt_int32 i, maxPointers; register void **base; register tt_uint32 *plong; unsigned char *p = (unsigned char *) pIn; tt_uint32 size1; if ( p != NULL ) { p -= headerSize; plong = (tt_uint32 *)p; tsi_Assert( t, plong[0] == MAGIC3, T2K_ERR_BAD_MEM_STAMP ); size1 = plong[1]; tsi_Assert( t, ((unsigned char *)p)[headerSize + size1] == MAGIC4, T2K_ERR_BAD_MEM_STAMP ); tsi_Assert( t, ((unsigned char *)p)[headerSize + size1 + 1] == MAGIC5, T2K_ERR_BAD_MEM_STAMP ); base = t->base; maxPointers = t->maxPointers; tsi_Assert( t, t->numPointers > 0 && t->numPointers <= maxPointers, T2K_ERR_BAD_PTR_COUNT ); for ( i = 0; i < maxPointers; i++ ) { if ( base[i] == p ) { base[i] = CLIENT_REALLOC( p, headerSize + size2 + tailSize ); #ifdef TRACK_RAM t->totRAM -= size1; t->totRAM += size2; if ( t->totRAM > t->maxRAM ) t->maxRAM = t->totRAM; #endif p = (unsigned char *) base[i]; tsi_Assert( t, p != NULL, T2K_ERR_MEM_REALLOC_FAILED ); plong = (tt_uint32 *)p; tsi_Assert( t, plong[0] == MAGIC3, T2K_ERR_BAD_MEM_STAMP ); plong[1] = size2; p[headerSize + size2] = (unsigned char)MAGIC4; p[headerSize + size2+1] = (unsigned char)MAGIC5; break; /*****/ } } tsi_Assert( t, i < t->maxPointers, T2K_ERR_MEM_BAD_PTR ); return (p+headerSize); /*****/ } return NULL; /*****/ }
/* * Description: Free the memory the pointer "p" points at. * How used: Call with the pointer to the memory, received from ag_AllocMem. * It is OK to call this with a NULL pointer. * Side Effects: None. * Return value: None. */ void tsi_DeAllocMem( register tsiMemObject *t, void *pIn ) { register tt_int32 i, maxPointers; register void **base; register tt_uint32 *plong; char *p = (char *) pIn; tt_uint32 size; tsi_Assert( t, t != NULL, T2K_ERR_NULL_MEM ); if ( p != NULL ) { p -= headerSize; plong = (tt_uint32 *)p; tsi_Assert( t, plong[0] == MAGIC3, T2K_ERR_BAD_MEM_STAMP ); size = plong[1]; tsi_Assert( t, ((unsigned char *)p)[headerSize + size] == MAGIC4, T2K_ERR_BAD_MEM_STAMP ); tsi_Assert( t, ((unsigned char *)p)[headerSize + size + 1] == MAGIC5, T2K_ERR_BAD_MEM_STAMP ); #ifdef ZAP_MEMORY { for ( i = 0; i < size; i++ ) { ((char *)pIn)[i] = 0xa5; } } #endif base = t->base; maxPointers = t->maxPointers; tsi_Assert( t, t->numPointers <= maxPointers, T2K_ERR_MEM_TOO_MANY_PTRS ); for ( i = 0; i < maxPointers; i++ ) { if ( base[i] == p ) { base[i] = NULL; t->numPointers--; break; /*****/ } } tsi_Assert( t, i < t->maxPointers, T2K_ERR_MEM_BAD_PTR ); CLIENT_FREE(p); #ifdef TRACK_RAM t->totRAM -= (headerSize+size+tailSize); #endif } }
void PreLoadT2KInputStream( InputStream *t, long requestedByteCount ) { if ( t->ReadToRamFunc != NULL ) { long byteCount; int ioCode; assert( t->ReadToRamFunc != NULL ); byteCount = PRE_CACHE_SIZE; if ( requestedByteCount < PRE_CACHE_SIZE ) { byteCount = requestedByteCount; } t->bytesLeftToPreLoad = requestedByteCount - byteCount; t->privateBase = t->cacheBase; ioCode = t->ReadToRamFunc( t->nonRamID, t->privateBase, t->pos, byteCount ); tsi_Assert( t->mem, ioCode >= 0, T2K_EXT_IO_CALLBACK_ERR ); t->cachePosition = t->pos; t->cacheCount = (unsigned long)byteCount; } }
unsigned char *GetEntireStreamIntoMemory( InputStream *stream ) { #ifdef MAYBE_SOON if ( stream->privateBase != NULL && stream->ReadToRamFunc == NULL ) { ; /* OK */ } if ( stream->privateBase == NULL && stream->ReadToRamFunc != NULL ) { int ioCode; t->constructorType = 1; t->privateBase = tsi_AllocMem( stream->mem, t->maxPos ); ioCode = t->ReadToRamFunc( t->nonRamID, t->privateBase, 0, t->maxPos ); tsi_Assert( t->mem, ioCode >= 0, T2K_EXT_IO_CALLBACK_ERR ); } else { assert( false ); } #else assert( stream->privateBase != NULL ); /* Only used for Type 1, does not work for non-RAM fonts */ #ifdef ENABLE_NON_RAM_STREAM assert( stream->ReadToRamFunc == NULL ); #endif #endif return &stream->privateBase[stream->posZero]; /*****/ }
void *tsi_ReAllocArray(register tsiMemObject *t, void *pIn, size_t size, size_t n) { tsi_Assert(t, SAFE_TO_ALLOC(size, n), T2K_ERR_MEM_MALLOC_FAILED); return tsi_ReAllocMem(t, pIn, size*n); }