Exemplo n.º 1
0
Arquivo: swap.c Projeto: xcw0579/mudOS
/*
 * Read something back in from swap.  Return the size.
 *   blockp    - a pointer to what will hold the block read in
 *   loc       - position in the swap file to read from
 */
static int
swap_in(char **  blockp, int  loc)
{
    int size;

    DEBUG_CHECK(!blockp, "blockp null in swap_in()\n");
    
    if (loc == -1)
	return 0;
    swap_seek(loc, 0);
#ifdef SWAP_USE_FD
    /* find out size */
    if (read(swap_file, &size, sizeof size) == -1)
        fatal("Couldn't read the swap file.\n");
    DEBUG_CHECK(size <= 0, "Illegal size read from swap file.\n");
    *blockp = DXALLOC(size, TAG_SWAP, "swap_in");
    if (read(swap_file, *blockp, size) == -1)
        fatal("Couldn't read the swap file.\n");
#else
    /* find out size */
    if (fread((char *) &size, sizeof size, 1, swap_file) == -1)
	fatal("Couldn't read the swap file.\n");
    DEBUG_CHECK(size <= 0, "Illegal size read from swap file.\n");
    *blockp = DXALLOC(size, TAG_SWAP, "swap_in");
    if (fread(*blockp, size, 1, swap_file) == -1)
	fatal("Couldn't read the swap file.\n");
#endif
    total_bytes_swapped -= size;
    return size;
}
Exemplo n.º 2
0
char *int_string_unlink P1(char *, str)
#endif
{
    malloc_block_t *mbt, *newmbt;

    mbt = ((malloc_block_t *)str) - 1;
    mbt->ref--;
    
    if (mbt->size == USHRT_MAX) {
	int l = strlen(str + USHRT_MAX) + USHRT_MAX; /* ouch */

	newmbt = (malloc_block_t *)DXALLOC(l + sizeof(malloc_block_t) + 1, TAG_MALLOC_STRING, desc);
	memcpy((char *)(newmbt + 1), (char *)(mbt + 1), l+1);
	newmbt->size = USHRT_MAX;
	ADD_NEW_STRING(USHRT_MAX, sizeof(malloc_block_t));
    } else {
	newmbt = (malloc_block_t *)DXALLOC(mbt->size + sizeof(malloc_block_t) + 1, TAG_MALLOC_STRING, desc);
	memcpy((char *)(newmbt + 1), (char *)(mbt + 1), mbt->size+1);
	newmbt->size = mbt->size;
	ADD_NEW_STRING(mbt->size, sizeof(malloc_block_t));
    }
    newmbt->ref = 1;
    CHECK_STRING_STATS;
    
    return (char *)(newmbt + 1);
}
Exemplo n.º 3
0
static void add_define (const char * name, int nargs, const char * exps)
{
    defn_t *p = lookup_definition(name);
    int h, len;

    /* trim off leading and trailing whitespace */
    while (uisspace(*exps)) exps++;
    for (len = strlen(exps);  len && uisspace(exps[len - 1]);  len--);
    if (*exps == '#' && *(exps + 1) == '#') {
        yyerror("'##' at start of macro definition");
        return;
    }
    if (len > 2 && *(exps + len - 2) == '#' && *(exps + len - 1) == '#') {
        yyerror("'##' at end of macro definition");
        return;
    }

    if (p) {
        if (p->flags & DEF_IS_UNDEFINED) {
            p->exps = (char *)DREALLOC(p->exps, len + 1, TAG_COMPILER, "add_define: redef");
            memcpy(p->exps, exps, len);
            p->exps[len] = 0;
            p->flags = 0;
            p->nargs = nargs;
        } else {
            if (p->flags & DEF_IS_PREDEF) {
                yyerror("Illegal to redefine predefined value.");
                return;
            }
            if (nargs != p->nargs || strcmp(exps, p->exps)) {
                char buf[200 + NSIZE];

                sprintf(buf, "redefinition of #define %s\n", name);
                yywarn(buf);

                p->exps = (char *)DREALLOC(p->exps, len + 1, TAG_COMPILER, "add_define: redef");
                memcpy(p->exps, exps, len);
                p->exps[len] = 0;
                p->nargs = nargs;
            }
#ifndef LEXER
            p->flags &= ~DEF_IS_NOT_LOCAL;
#endif
        }
    } else {
        p = ALLOCATE(defn_t, TAG_COMPILER, "add_define: def");
        p->name = (char *) DXALLOC(strlen(name) + 1, TAG_COMPILER, "add_define: def name");
        strcpy(p->name, name);
        p->exps = (char *) DXALLOC(len + 1, TAG_COMPILER, "add_define: def exps");
        memcpy(p->exps, exps, len);
        p->exps[len] = 0;
        p->flags = 0;
        p->nargs = nargs;
        h = defhash(name);
        p->next = defns[h];
        defns[h] = p;
    }
}
Exemplo n.º 4
0
void f_compress (void)
{
   unsigned char* buffer;
   unsigned char* input;
   int size;
   buffer_t* real_buffer;
   uLongf new_size;

   if (sp->type == T_STRING) {
      size = SVALUE_STRLEN(sp);
      input = (unsigned char*)sp->u.string;
   } else if (sp->type == T_BUFFER) {
      size = sp->u.buf->size;
      input = sp->u.buf->item;
   } else {
      pop_n_elems(st_num_arg);
      push_undefined();
      return ;
   }

   new_size = size;
   // Make it a little larger as specified in the docs.
   buffer = (unsigned char*)DXALLOC(size * 101 / 100 + 12, TAG_TEMPORARY, "compress");
   compress(buffer, &new_size, input, size);

   // Shrink it down.
   pop_n_elems(st_num_arg);
   real_buffer = allocate_buffer(new_size);
   write_buffer(real_buffer, 0, (char *)buffer, new_size);
   FREE(buffer);
   push_buffer(real_buffer);
}
Exemplo n.º 5
0
static void s_Enlarge() {
    int n = s_handleCount + 512;
    int first = s_handleCount;
    int last;
    int i;
    HandleData *handle = NULL; /* Make compiler happy */
    
    if (s_handleTable == NULL) {
        s_handleTable = DXALLOC((size_t)n * sizeof(HandleData));
        first = s_handleCount + 1; /* Reserve the first handle. */
    } else {
        s_handleTable = DXREALLOC(s_handleTable, (size_t)n * sizeof(HandleData));
    }
    s_handleCount = n;
    
    last = -1;
    for (i = n - 1; i >= first; --i) {
        handle = &s_handleTable[i];
        handle->data = NULL;
        handle->handleType = DXHANDLE_NONE;
        handle->nextID = last;
        handle->prevID = i - 1;
        last = i;
    }
    
    handle->prevID = -1;
    
    s_handleLists[DXHANDLE_NONE] = first;
}
Exemplo n.º 6
0
int Dx_File_ReadFile(const char *filename, unsigned char **dData, unsigned int *dSize) {
    unsigned char *data;
    unsigned int size;
    int retval;
    
    SDL_RWops *rwops = Dx_File_OpenStream(filename);
    
    if (rwops == NULL) {
        return -1;
    }
    
    size = (unsigned int)SDL_RWsize(rwops);
    
    retval = 0;
    data = DXALLOC(size);
    if (SDL_RWread(rwops, data, size, 1) < 1) {
        retval = -1;
        DXFREE(data);
    } else {
        *dData = data;
        *dSize = size;
    }
    
    SDL_RWclose(rwops);
    
    return retval;
}
Exemplo n.º 7
0
/* ------------------------------------------------------------ PUBLIC INTERFACE */
int Dx_File_EXTSetDXArchiveAlias(const char *srcName, const char *destName) {
    ArchiveAliasEntry **pEntry = &s_archiveAliases;
    
    while (*pEntry != NULL) {
        if (PL_Text_Strcmp((*pEntry)->srcArchiveName, srcName) == 0) {
            DXFREE((*pEntry)->destArchiveName);
            if (destName == NULL) {
                ArchiveAliasEntry *next = (*pEntry)->next;
                DXFREE((*pEntry)->srcArchiveName);
                DXFREE(*pEntry);
                
                *pEntry = next;
            } else {
                (*pEntry)->destArchiveName = PL_Text_Strdup(destName);
            }
            return 0;
        }
        pEntry = &(*pEntry)->next;
    }
    
    if (srcName == NULL || destName == NULL) {
        return 0;
    }
    
    *pEntry = DXALLOC(sizeof(ArchiveAliasEntry));
    (*pEntry)->srcArchiveName = PL_Text_Strdup(srcName);
    (*pEntry)->destArchiveName = PL_Text_Strdup(destName);
    (*pEntry)->next = NULL;
    return 0;
}
Exemplo n.º 8
0
void
c_anonymous(int  num_arg, int  num_local, POINTER_INT  func) {
    funptr_t *fp;
    
    fp = (funptr_t *)DXALLOC(sizeof(funptr_hdr_t) + sizeof(functional_t),
			     TAG_FUNP, "c_functional");
    fp->hdr.owner = current_object;
    add_ref( current_object, "c_functional" );
    if (num_arg & 0x10000)
	fp->hdr.type = FP_FUNCTIONAL | FP_NOT_BINDABLE;
    else
	fp->hdr.type = FP_FUNCTIONAL;
    
    current_prog->func_ref++;
    
    fp->f.functional.prog = current_prog;
    fp->f.functional.offset = func;
    fp->f.functional.num_arg = num_arg & 0xff;
    fp->f.functional.num_local = num_local;
    fp->f.functional.fio = function_index_offset;
    fp->f.functional.vio = variable_index_offset;

    fp->hdr.args = 0;

    fp->hdr.ref = 1;

    STACK_INC;
    sp->type = T_FUNCTION;
    sp->u.fp = fp;
}
Exemplo n.º 9
0
void
c_functional(int  kind, int  num_arg, POINTER_INT  func) {
    funptr_t *fp;
    
    fp = (funptr_t *)DXALLOC(sizeof(funptr_hdr_t) + sizeof(functional_t),
			     TAG_FUNP, "c_functional");
    fp->hdr.owner = current_object;
    add_ref( current_object, "c_functional" );
    fp->hdr.type = kind;
    
    current_prog->func_ref++;
    
    fp->f.functional.prog = current_prog;
    fp->f.functional.offset = func;
    fp->f.functional.num_arg = num_arg;
    fp->f.functional.num_local = 0;
    fp->f.functional.fio = function_index_offset;
    fp->f.functional.vio = variable_index_offset;

    if (sp->type == T_ARRAY) {
	fp->hdr.args = sp->u.arr;
	fp->f.functional.num_arg += sp->u.arr->size;
    } else
	fp->hdr.args = 0;
    
    fp->hdr.ref = 1;

    sp->type = T_FUNCTION;
    sp->u.fp = fp;
}
Exemplo n.º 10
0
INLINE funptr_t *
make_lfun_funp (int index, svalue_t * args)
{
    funptr_t *fp;
    int newindex;

    if (replace_program_pending(current_object))
        error("cannot bind an lfun fp to an object with a pending replace_program()\n");

    fp = (funptr_t *)DXALLOC(sizeof(funptr_hdr_t) + sizeof(local_ptr_t),
                             TAG_FUNP, "make_lfun_funp");
    fp->hdr.owner = current_object;
    add_ref( current_object, "make_lfun_funp" );
    fp->hdr.type = FP_LOCAL | FP_NOT_BINDABLE;
    
    fp->hdr.owner->prog->func_ref++;
    debug(d_flag, ("add func ref /%s: now %i\n",
                fp->hdr.owner->prog->filename,
                fp->hdr.owner->prog->func_ref));
    
    newindex = index + function_index_offset;
    if (current_object->prog->function_flags[newindex] & FUNC_ALIAS)
        newindex = current_object->prog->function_flags[newindex] & ~FUNC_ALIAS;
    fp->f.local.index = newindex;
    
    if (args->type == T_ARRAY) {
        fp->hdr.args = args->u.arr;
        args->u.arr->ref++;
    } else
        fp->hdr.args = 0;
    
    fp->hdr.ref = 1;
    return fp;
}
Exemplo n.º 11
0
char *int_new_string (int size)
#endif
{
    malloc_block_t *mbt;

#if 0
    if (!size) {
        the_null_string_blocks[0].ref++;
        ADD_NEW_STRING(0, sizeof(malloc_block_t));
        return the_null_string;
    }
#endif
    
    mbt = (malloc_block_t *)DXALLOC(size + sizeof(malloc_block_t) + 1, TAG_MALLOC_STRING, tag);
    if (size < USHRT_MAX) {
        mbt->size = size;
        ADD_NEW_STRING(size, sizeof(malloc_block_t));
    } else {
        mbt->size = USHRT_MAX;
        ADD_NEW_STRING(USHRT_MAX, sizeof(malloc_block_t));
    }
    mbt->ref = 1;
    ADD_STRING(mbt->size);
    CHECK_STRING_STATS;
    return (char *)(mbt + 1);
}
Exemplo n.º 12
0
char *int_alloc_cstring (const char * str)
#endif
{
    char *ret;

    ret = (char *)DXALLOC(strlen(str) + 1, TAG_STRING, tag);
    strcpy(ret, str);
    return ret;
}
Exemplo n.º 13
0
array_t *allocate_empty_class_by_size(int  size) {
    array_t *p;

    p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (size - 1), TAG_CLASS, "allocate_class");
    p->ref = 1;
    p->size = size;

    return p;
}
Exemplo n.º 14
0
static void add_define P3(char *, name, int, nargs, char *, exps)
{
    defn_t *p = lookup_definition(name);
    int h;

    if (p) {
	if (p->flags & DEF_IS_UNDEFINED) {
	    p->exps = (char *)DREALLOC(p->exps, strlen(exps) + 1, TAG_COMPILER, "add_define: redef");
	    strcpy(p->exps, exps);
	    p->flags = 0;
	    p->nargs = nargs;
	} else {
	    if (p->flags & DEF_IS_PREDEF) {
		yyerror("Illegal to redefine predefined value.");
		return;
	    }
	    if (nargs != p->nargs || strcmp(exps, p->exps)) {
		char buf[200 + NSIZE];
		
		sprintf(buf, "redefinition of #define %s\n", name);
		yywarn(buf);
		
		p->exps = (char *)DREALLOC(p->exps, strlen(exps) + 1, TAG_COMPILER, "add_define: redef");
		strcpy(p->exps, exps);
		p->nargs = nargs;
	    }
#ifndef LEXER
	    p->flags &= ~DEF_IS_NOT_LOCAL;
#endif
	}
    } else {
	p = ALLOCATE(defn_t, TAG_COMPILER, "add_define: def");
	p->name = (char *) DXALLOC(strlen(name) + 1, TAG_COMPILER, "add_define: def name");
	strcpy(p->name, name);
	p->exps = (char *) DXALLOC(strlen(exps) + 1, TAG_COMPILER, "add_define: def exps");
	strcpy(p->exps, exps);
	p->flags = 0;
	p->nargs = nargs;
	h = defhash(name);
	p->next = defns[h];
	defns[h] = p;
    }
}
Exemplo n.º 15
0
Arquivo: swap.c Projeto: xcw0579/mudOS
/*
 * Free up a chunk of swap space, coalescing free blocks if necessary.
 *
 * Todo - think about tradeoff of storing the free block
 * info in the swap file itself.
 */
static void free_swap(int  start, int  length)
{
    sw_block_t *m, *ptr, *prev;

    length += sizeof(int);	/* extend with size of hidden information */

    /*
     * Construct and insert new free block
     */
    m = (sw_block_t *) DXALLOC(sizeof(sw_block_t), TAG_SWAP, "free_swap");
    m->start = start;
    m->length = length;

    for (ptr = swap_free, prev = 0; ptr; prev = ptr, ptr = ptr->next) {
	if (start < ptr->start)
	    break;
    }
    if (!prev) {
	swap_free = m;
    } else {
	prev->next = m;
    }
    m->next = ptr;

    /*
     * Combine adjacent blocks
     */
    if (ptr && m->start + m->length == ptr->start) {
	m->length += ptr->length;
	m->next = ptr->next;
	FREE(ptr);
    }
    if (prev && prev->start + prev->length == m->start) {
	prev->length += m->length;
	prev->next = m->next;
	FREE(m);
	m = prev;
    }
    /*
     * There is an implicit infinite block at the end making life hard Can't
     * do this earlier, since m and prev could have combined, so prev must be
     * found again (or use doubly linked list, etc).
     */
    if (m->start + m->length == last_data) {
	DEBUG_CHECK(m->next, "extraneous free swap blocks!\n");
	/* find prev pointer again *sigh* */
	for (ptr = swap_free, prev = 0; ptr != m; prev = ptr, ptr = ptr->next);
	last_data = m->start;
	FREE(m);
	if (!prev)
	    swap_free = 0;
	else
	    prev->next = 0;
    }
}
Exemplo n.º 16
0
array_t *allocate_class P1(class_def_t *, cld) {
    array_t *p;
    int n = cld->size;

    p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (n - 1), TAG_CLASS, "allocate_class");
    p->ref = 1;
    p->size = n;
    while (n--)
	p->item[n] = const0;
    return p;
}
Exemplo n.º 17
0
array_t *allocate_class_by_size(int  size) {
    array_t *p;

    p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (size - 1), TAG_CLASS, "allocate_class");
    p->ref = 1;
    p->size = size;

    while (size--)
	p->item[size] = const0;

    return p;
}
Exemplo n.º 18
0
void *PL_Handle_AllocateData(int handleID, size_t dataSize) {
    HandleData *handle;
    
    if (handleID < 0 || handleID >= s_handleCount) {
        return NULL;
    }
    
    handle = &s_handleTable[handleID];
    handle->data = DXALLOC((size_t)dataSize);
    
    return handle->data;
}
Exemplo n.º 19
0
array_t *allocate_empty_class_by_size (int size) {
	array_t *p;

#ifdef CLASS_STATS
	num_classes++;
	total_class_size += sizeof(array_t) + sizeof(svalue_t) * (size - 1);
#endif
	p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (size - 1), TAG_CLASS, "allocate_class");
	p->ref = 1;
	p->size = size;

	return p;
}
Exemplo n.º 20
0
array_t *allocate_class(class_def_t *  cld, int  has_values) {
    array_t *p;
    int n = cld->size;

    p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (n - 1), TAG_CLASS, "allocate_class");
    p->ref = 1;
    p->size = n;
    if (has_values) {
	while (n--)
	    p->item[n] = *sp--;
    } else {
	while (n--)
	    p->item[n] = const0;
    }
    return p;
}
Exemplo n.º 21
0
INLINE funptr_t *
make_simul_funp (int index, svalue_t * args)
{
    funptr_t *fp;
    
    fp = (funptr_t *)DXALLOC(sizeof(funptr_hdr_t) + sizeof(simul_ptr_t),
                             TAG_FUNP, "make_simul_funp");
    fp->hdr.owner = current_object;
    add_ref( current_object, "make_simul_funp" );
    fp->hdr.type = FP_SIMUL;
    
    fp->f.simul.index = index;
    
    if (args->type == T_ARRAY) {
        fp->hdr.args = args->u.arr;
        args->u.arr->ref++;
    } else
        fp->hdr.args = 0;
    
    fp->hdr.ref = 1;
    return fp;
}
Exemplo n.º 22
0
INLINE funptr_t *
make_efun_funp (int opcode, svalue_t * args)
{
    funptr_t *fp;
    
    fp = (funptr_t *)DXALLOC(sizeof(funptr_t),
                             TAG_FUNP, "make_efun_funp");
    fp->hdr.owner = current_object;
    add_ref( current_object, "make_efun_funp" );
    fp->hdr.type = FP_EFUN;
    
    fp->f.efun.index = opcode;
    
    if (args->type == T_ARRAY) {
        fp->hdr.args = args->u.arr;
        args->u.arr->ref++;
    } else
        fp->hdr.args = 0;
    
    fp->hdr.ref = 1;
    return fp;
}
Exemplo n.º 23
0
INLINE_STATIC block_t *
alloc_new_string (const char * string, int h)
{
    block_t *b;
    int len = strlen(string);
    int size;

    if (len > max_string_length) {
        len = max_string_length;
    }
    size = sizeof(block_t) + len + 1;
    b = (block_t *) DXALLOC(size, TAG_SHARED_STRING, "alloc_new_string");
    strncpy(STRING(b), string, len);
    STRING(b)[len] = '\0';      /* strncpy doesn't put on \0 if 'from' too
                                 * long */
    SIZE(b) = (len > USHRT_MAX ? USHRT_MAX : len);
    REFS(b) = 1;
    NEXT(b) = base_table[h];
    base_table[h] = b;
    ADD_NEW_STRING(SIZE(b), sizeof(block_t));
    ADD_STRING(SIZE(b));
    return (b);
}
Exemplo n.º 24
0
INLINE funptr_t *
make_functional_funp (short num_arg, short num_local, short len, svalue_t * args, int flag)
{
    funptr_t *fp;

    if (replace_program_pending(current_object))
        error("cannot bind a functional to an object with a pending replace_program()\n");
    
    fp = (funptr_t *)DXALLOC(sizeof(funptr_hdr_t) + sizeof(functional_t), 
                             TAG_FUNP, "make_functional_funp");
    fp->hdr.owner = current_object;
    add_ref( current_object, "make_functional_funp" );
    fp->hdr.type = FP_FUNCTIONAL + flag;
    
    current_prog->func_ref++;
    debug(d_flag, ("add func ref /%s: now %i\n",
               current_prog->filename,
               current_prog->func_ref));
    
    fp->f.functional.prog = current_prog;
    fp->f.functional.offset = pc - current_prog->program;
    fp->f.functional.num_arg = num_arg;
    fp->f.functional.num_local = num_local;
    fp->f.functional.fio = function_index_offset;
    fp->f.functional.vio = variable_index_offset;
    pc += len;
    
    if (args && args->type == T_ARRAY) {
        fp->hdr.args = args->u.arr;
        args->u.arr->ref++;
        fp->f.functional.num_arg += args->u.arr->size;
    } else
        fp->hdr.args = 0;
    
    fp->hdr.ref = 1;
    return fp;
}
Exemplo n.º 25
0
static DXArchive *s_GetArchive(const char *filename) {
    /* - Check to see if this archive is already open first. */
    DXArchive *archive;
    ArchiveListEntry *entry;

    for (entry = s_archiveList; entry != NULL; entry = entry->next) {
        if (!PL_Text_Strcmp(entry->filename, filename)) {
            return entry->archive;
        }
    }
    
    /* - Since it isn't, try to load it up. */
    archive = DXA_OpenArchive(filename, s_defaultArchiveString);
    if (archive != NULL) {
        /* - On success, add to the open list. */
        entry = DXALLOC(sizeof(ArchiveListEntry));
        entry->archive = archive;
        entry->filename = PL_Text_Strdup(filename);
        entry->next = s_archiveList;
        s_archiveList = entry;
    }

    return archive;
}
Exemplo n.º 26
0
array_t *allocate_class (class_def_t * cld, int has_values) {
	array_t *p;
	int n = cld->size;
	if(!n)
		n++;

#ifdef CLASS_STATS
	num_classes++;
	total_class_size += sizeof(array_t) + sizeof(svalue_t) * (n - 1);
#endif

	p = (array_t *)DXALLOC(sizeof(array_t) + sizeof(svalue_t) * (n - 1), TAG_CLASS, "allocate_class");
	n = cld->size;
	p->ref = 1;
	p->size = n;
	if (has_values) {
		while (n--)
			p->item[n] = *sp--;
	} else {
		while (n--)
			p->item[n] = const0;
	}
	return p;
}
Exemplo n.º 27
0
LFONTSPRITE LunaFontSprite::CreateFromFile(const char *pFileName,
                                 const char *pExt, Bool IsAlpha, Uint32 Num,
                                 Bool IsSortZ,
                                 eSurfaceFormat Format) {
    char filebuf[4096];
    int file = PL_File_OpenRead(
        PL_Text_ConvertStrncpyIfNecessary(
            filebuf, -1, pFileName, g_lunaUseCharSet, 4096)
    );
    unsigned char *lfdData = NULL;
    
    do {
        int64_t fileSize = PL_File_GetSize(file);
        const LFDHeader *lfdHeader;
        LunaFontSprData *fontspr;
        int fontSprHandle;
        int fontBaseSize;
        int i;
        
        /* don't allow files beyond a certain size */
        if (file < 0 || fileSize <= 0 || fileSize > 65536) {
            break;
        }
        
        lfdData = (unsigned char *)DXALLOC((int)fileSize);
        if (PL_File_Read(file, lfdData, (int)fileSize) != (int)fileSize) {
            break;
        }
        PL_File_Close(file);
        file = -1;
        
        /* sanity checks to verify LFD data is valid */
        lfdHeader = (const LFDHeader *)lfdData;
        if (memcmp(lfdHeader, lfd_id, 4) != 0 || lfdHeader->sheetCount == 0 || lfdHeader->sheetCount > 32) {
            break;
        }
        
        fontBaseSize = sizeof(LFDHeader) + (sizeof(Uint16) * CODE_TABLE_SIZE) + (32 * lfdHeader->sheetCount);
        if (fileSize < (int64_t)(fontBaseSize + (sizeof(LFDCharEntry) * lfdHeader->fontMax))) {
            break;
        }
        
        /* create sprite handle and data */
        fontSprHandle = PL_Handle_AcquireID(DXHANDLE_LUNAFONTSPRITE);
        if (fontSprHandle < 0) {
            break;
        }
        
        fontspr = (LunaFontSprData *)PL_Handle_AllocateData(fontSprHandle, sizeof(LunaFontSprData));
        fontspr->lfdData = lfdData;
        fontspr->lfdHeader = lfdHeader;
        fontspr->lfdIndex = (const Uint16 *)(lfdData + sizeof(LFDHeader));
        fontspr->lfdCharEntries = (const LFDCharEntry *)(lfdData + fontBaseSize);
        fontspr->charMax = lfdHeader->fontMax;
        
        fontspr->sheetCount = lfdHeader->sheetCount;
        fontspr->sheetGraphs = (int *)DXALLOC(sizeof(int) * fontspr->sheetCount);
        fontspr->sheetSprites = (int *)DXALLOC(sizeof(int) * fontspr->sheetCount);
        
        fontspr->space = 0;
        
        for (i = 0; i < fontspr->sheetCount; ++i) {
            char buf[64];
            memcpy(buf, lfdData + sizeof(LFDHeader) + (sizeof(Uint16) * CODE_TABLE_SIZE) + (32 * i), 32);
            buf[32] = '\0';
            if (pExt != NULL) {
                char newBuf[2048];
                PL_Text_ConvertStrncpy(
                    newBuf, -1, buf, g_lunaUseCharSet, 2048);
                PL_Text_ConvertStrncat(
                    newBuf, -1, pExt, g_lunaUseCharSet, 2048);
                
                int surface = PL_Surface_Load(newBuf);
                fontspr->sheetGraphs[i] = PL_Surface_ToTexture(surface);
                PL_Surface_Delete(surface);
            } else {
                /* FIXME: LAG support */
                fontspr->sheetGraphs[i] = -1;
            }
            fontspr->sheetSprites[i] =
                LunaSprite::Create(Num, PRIM_VERTEX_UV1, IsSortZ);
            
            LunaSprite::AttatchTexture(fontspr->sheetSprites[i], 0, fontspr->sheetGraphs[i]);
        }
        
        return fontSprHandle;
    } while(0);
    
    PL_File_Close(file);
    if (lfdData != NULL) {
        DXFREE(lfdData);
    }
        
    return INVALID_FONTSPRITE;
}
Exemplo n.º 28
0
/*
   - regcomp - compile a regular expression into internal code
 *
 * We can't allocate space until we know how big the compiled form will be,
 * but we can't compile it (and thus know how big it is) until we've got a
 * place to put the code.  So we cheat:  we compile it twice, once with code
 * generation turned off and size counting turned on, and once "for real".
 * This also means that we don't allocate space until we are sure that the
 * thing really will compile successfully, and we never have to move the
 * code and thus invalidate pointers into it.  (Note that it has to be in
 * one piece because FREE() must be able to free it all.)
 *
 * Beware that the optimization-preparation code in here knows about some
 * of the structure of the compiled regexp.
 */
regexp *regcomp (unsigned char * exp,
        int excompat)       /* \( \) operators like in unix ex */
{
    register regexp *r;
    register unsigned char *scan;
    register char *longest;
    register int len;
    int flags;
    short *exp2, *dest, c;

    if (!exp)
        FAIL("NULL argument\n");

    exp2 = (short *)
        DXALLOC((strlen((char *)exp) + 1) * (sizeof(short[8]) / sizeof(char[8])),
                TAG_TEMPORARY, "regcomp: 1");
    for (scan = exp, dest = exp2; (c = *scan++);) {
        switch (c) {
            case '(':
            case ')':
                *dest++ = excompat ? c : c | SPECIAL;
                break;
            case '.':
            case '*':
            case '+':
            case '?':
            case '|':
            case '$':
            case '^':
            case '[':
            case ']':
                *dest++ = c | SPECIAL;
                break;
            case '\\':
                switch (c = *scan++) {
                    case 0:
                        FREE(exp2);
                        FAIL("Regular expression cannot end with '\\'.  Use \"\\\\\".\n");
                        break;
                    case '(':
                    case ')':
                        *dest++ = excompat ? c | SPECIAL : c;
                        break;
                    case '<':
                    case '>':
                        *dest++ = c | SPECIAL;
                        break;
                    case '{':
                    case '}':
                        FREE(exp2);
                        FAIL("sorry, unimplemented operator\n");
                    case 'b':
                        *dest++ = '\b';
                        break;
                    case 't':
                        *dest++ = '\t';
                        break;
                    case 'r':
                        *dest++ = '\r';
                        break;
                    default:
                        *dest++ = c;
                }
                break;
            default:
                *dest++ = c;
        }
    }
    *dest = 0;
    /* First pass: determine size, legality. */
    regparse = exp2;
    regnpar = 1;
    regsize = 0L;
    regcode = &regdummy;
    regc((char) MAGIC);
    if (reg(0, &flags) == (char *) NULL) {
        FREE(exp2);
        return ((regexp *) NULL);
    }

    /* Small enough for pointer-storage convention? */
    if (regsize >= 32767L)      /* Probably could be 65535L. */
    {
        FREE(exp2);
        FAIL("regexp too big\n");
    }

    /* Allocate space. */
    r = (regexp *) DXALLOC(sizeof(regexp) + (unsigned) regsize,
            TAG_TEMPORARY, "regcomp: 2");
    if (r == (regexp *) NULL) {
        FREE(exp2);
        FAIL("out of space\n");
    }

    /* Second pass: emit code. */
    regparse = exp2;
    regnpar = 1;
    regcode = (char *)(r->program);
    regc((char) MAGIC);
    if (reg(0, &flags) == NULL) {
        FREE(exp2);
        FREE(r);
        return ((regexp *) NULL);
    }

    /* Dig out information for optimizations. */
    r->regstart = '\0';         /* Worst-case defaults. */
    r->reganch = 0;
    r->regmust = NULL;
    r->regmlen = 0;
    scan = (unsigned char *)(r->program + 1);   /* First BRANCH. */
    if (OP(regnext((char *)scan)) == END) {     /* Only one top-level choice. */
        scan = OPERAND(scan);

        /* Starting-point info. */
        if (OP(scan) == EXACTLY)
            r->regstart = *OPERAND(scan);
        else if (OP(scan) == BOL)
            r->reganch++;

        /*
         * If there's something expensive in the r.e., find the longest
         * literal string that must appear and make it the regmust.  Resolve
         * ties in favor of later strings, since the regstart check works
         * with the beginning of the r.e. and avoiding duplication
         * strengthens checking.  Not a strong reason, but sufficient in the
         * absence of others.
         */
        if (flags & SPSTART) {
            longest = NULL;
            len = 0;
            for (; scan != NULL; scan = (unsigned char *)regnext((char *)scan)) {
                char *tmp = (char *)OPERAND(scan);
                int tlen;
                if (OP(scan) == EXACTLY && (tlen = strlen(tmp)) >= len) {
                    longest = tmp;
                    len = tlen;
                }
            }
            r->regmust = longest;
            r->regmlen = len;
        }
    }
    FREE((char *) exp2);
    return (r);
}
Exemplo n.º 29
0
void f_uncompress (void)
{
   z_stream* compressed;
   unsigned char compress_buf[COMPRESS_BUF_SIZE];
   unsigned char* output_data = NULL;
   int len;
   int pos;
   buffer_t* buffer;
   int ret;

   if (sp->type == T_BUFFER) {
      buffer = sp->u.buf;
   } else {
      pop_n_elems(st_num_arg);
      push_undefined();
      return ;
   }

   compressed = (z_stream *)
            DXALLOC(sizeof(z_stream), TAG_INTERACTIVE,
                    "start_compression");
   compressed->next_in = buffer->item;
   compressed->avail_in = buffer->size;
   compressed->next_out = compress_buf;
   compressed->avail_out = COMPRESS_BUF_SIZE;
   compressed->zalloc = zlib_alloc;
   compressed->zfree = zlib_free;
   compressed->opaque = NULL;

   if (inflateInit(compressed) != Z_OK) {
      FREE(compressed);
      pop_n_elems(st_num_arg);
      push_undefined();
      return ;
   }

   len = 0;
   output_data = NULL;
   do {
      ret = inflate(compressed, 0);
      if (ret == Z_OK) {
         pos = len;
         len += COMPRESS_BUF_SIZE - compressed->avail_out;
         if (!output_data) {
            output_data = (unsigned char*)DXALLOC(len, TAG_TEMPORARY, "uncompress");
         } else {
            output_data = REALLOC(output_data, len);
         }
         memcpy(output_data + pos, compress_buf, len - pos);
         compressed->next_out = compress_buf;
         compressed->avail_out = COMPRESS_BUF_SIZE;
      }
   } while (ret == Z_OK);

   inflateEnd(compressed);

   pop_n_elems(st_num_arg);

   if (ret == Z_STREAM_END) {
      buffer = allocate_buffer(len);
      memcpy(buffer->item, output_data, len);
      FREE(output_data);
      push_buffer(buffer);
   } else {
      push_undefined();
   }
}