Esempio n. 1
0
File: scat.c Progetto: npe9/harvey
int
loadngc(int32_t index)
{
    static int failed;
    int32_t j;

    ngcopen();
    j = (index-1)*sizeof(NGCrec);
    grow();
    cur->type = NGC;
    cur->index = index;
    seek(ngcdb, j, 0);
    /* special case: NGC data may not be available */
    if(read(ngcdb, &cur->ngc, sizeof(NGCrec)) != sizeof(NGCrec)) {
        if(!failed) {
            fprint(2, "scat: NGC database not available\n");
            failed++;
        }
        cur->type = NONGC;
        cur->ngc.ngc = 0;
        cur->ngc.ra = 0;
        cur->ngc.dec = 0;
        cur->ngc.diam = 0;
        cur->ngc.mag = 0;
        return 0;
    }
    cur->ngc.ngc = Short(&cur->ngc.ngc);
    cur->ngc.ra = Long(&cur->ngc.ra);
    cur->ngc.dec = Long(&cur->ngc.dec);
    cur->ngc.diam = Long(&cur->ngc.diam);
    cur->ngc.mag = Short(&cur->ngc.mag);
    return 1;
}
Esempio n. 2
0
File: scat.c Progetto: npe9/harvey
int
loadabell(int32_t index)
{
    int32_t j;

    abellopen();
    j = index-1;
    grow();
    cur->type = Abell;
    cur->index = index;
    seek(abelldb, j*sizeof(Abellrec), 0);
    Eread(abelldb, "abell", &cur->abell, sizeof(Abellrec));
    cur->abell.abell = Short(&cur->abell.abell);
    if(cur->abell.abell != index) {
        fprint(2, "bad format in abell catalog\n");
        exits("abell");
    }
    cur->abell.ra = Long(&cur->abell.ra);
    cur->abell.dec = Long(&cur->abell.dec);
    cur->abell.glat = Long(&cur->abell.glat);
    cur->abell.glong = Long(&cur->abell.glong);
    cur->abell.rad = Long(&cur->abell.rad);
    cur->abell.mag10 = Short(&cur->abell.mag10);
    cur->abell.pop = Short(&cur->abell.pop);
    cur->abell.dist = Short(&cur->abell.dist);
    return 1;
}
Esempio n. 3
0
CAMLprim value caml_lex_engine(struct lexing_table *tbl, value start_state,
                               struct lexer_buffer *lexbuf)
{
  int state, base, backtrk, c;

  state = Int_val(start_state);
  if (state >= 0) {
    /* First entry */
    lexbuf->lex_last_pos = lexbuf->lex_start_pos = lexbuf->lex_curr_pos;
    lexbuf->lex_last_action = Val_int(-1);
  } else {
    /* Reentry after refill */
    state = -state - 1;
  }
  while(1) {
    /* Lookup base address or action number for current state */
    base = Short(tbl->lex_base, state);
    if (base < 0) return Val_int(-base-1);
    /* See if it's a backtrack point */
    backtrk = Short(tbl->lex_backtrk, state);
    if (backtrk >= 0) {
      lexbuf->lex_last_pos = lexbuf->lex_curr_pos;
      lexbuf->lex_last_action = Val_int(backtrk);
    }
    /* See if we need a refill */
    if (lexbuf->lex_curr_pos >= lexbuf->lex_buffer_len){
      if (lexbuf->lex_eof_reached == Val_bool (0)){
        return Val_int(-state - 1);
      }else{
        c = 256;
      }
    }else{
      /* Read next input char */
      c = Byte_u(lexbuf->lex_buffer, Long_val(lexbuf->lex_curr_pos));
      lexbuf->lex_curr_pos += 2;
    }
    /* Determine next state */
    if (Short(tbl->lex_check, base + c) == state)
      state = Short(tbl->lex_trans, base + c);
    else
      state = Short(tbl->lex_default, state);
    /* If no transition on this char, return to last backtrack point */
    if (state < 0) {
      lexbuf->lex_curr_pos = lexbuf->lex_last_pos;
      if (lexbuf->lex_last_action == Val_int(-1)) {
        caml_failwith("lexing: empty token");
      } else {
        return lexbuf->lex_last_action;
      }
    }else{
      /* Erase the EOF condition only if the EOF pseudo-character was
         consumed by the automaton (i.e. there was no backtrack above)
       */
      if (c == 256) lexbuf->lex_eof_reached = Val_bool (0);
    }
  }
}
Esempio n. 4
0
/*
 * adfGetCacheEntry
 *
 * Returns a cache entry, starting from the offset p (the index into records[])
 * This offset is updated to the end of the returned entry.
 */
void adfGetCacheEntry(struct bDirCacheBlock *dirc, int *p, struct CacheEntry *cEntry)
{
    int ptr;

    ptr = *p;

/*printf("p=%d\n",ptr);*/

#ifdef LITT_ENDIAN
    cEntry->header = swapLong(dirc->records+ptr);
    cEntry->size = swapLong(dirc->records+ptr+4);
    cEntry->protect = swapLong(dirc->records+ptr+8);
    cEntry->days = swapShort(dirc->records+ptr+16);
    cEntry->mins = swapShort(dirc->records+ptr+18);
    cEntry->ticks = swapShort(dirc->records+ptr+20);
#else
    cEntry->header = Long(dirc->records+ptr);
    cEntry->size = Long(dirc->records+ptr+4);
    cEntry->protect = Long(dirc->records+ptr+8);
    cEntry->days = Short(dirc->records+ptr+16);
    cEntry->mins = Short(dirc->records+ptr+18);
    cEntry->ticks = Short(dirc->records+ptr+20);
#endif
    cEntry->type =(signed char) dirc->records[ptr+22];

    cEntry->nLen = dirc->records[ptr+23];
/*    cEntry->name = (char*)calloc(1,sizeof(char)*(cEntry->nLen+1));
    if (!cEntry->name)
         return;
*/    memcpy(cEntry->name, dirc->records+ptr+24, cEntry->nLen);
    cEntry->name[(int)(cEntry->nLen)]='\0';

    cEntry->cLen = dirc->records[ptr+24+cEntry->nLen];
    if (cEntry->cLen>0) {
/*        cEntry->comm =(char*)calloc(1,sizeof(char)*(cEntry->cLen+1));
        if (!cEntry->comm) {
            free( cEntry->name ); cEntry->name=NULL;
            return;
        }
*/        memcpy(cEntry->comm,dirc->records+ptr+24+cEntry->nLen+1,cEntry->cLen);
    }
        cEntry->comm[(int)(cEntry->cLen)]='\0';
/*printf("cEntry->nLen %d cEntry->cLen %d %s\n",cEntry->nLen,cEntry->cLen,cEntry->name);*/
    *p  = ptr+24+cEntry->nLen+1+cEntry->cLen;

    /* the starting offset of each record must be even (68000 constraint) */ 
    if ((*p%2)!=0)
        *p=(*p)+1;
}
Esempio n. 5
0
// Internal function used to get the .pic header from the current file.
ILboolean iGetPicHead(PIC_HEAD *Header)
{
	if (iread(Header, sizeof(PIC_HEAD), 1) != 1)
		return IL_FALSE;

	Int(&Header->Magic);
	//iread(&Header->Version, sizeof(ILfloat), 1);  // wtf?  A float?!
	Short(&Header->Width);
	Short(&Header->Height);
	//iread(&Header->Ratio, sizeof(ILfloat), 1);
	Short(&Header->Fields);
	Short(&Header->Padding);

	return IL_TRUE;
}
Esempio n. 6
0
// do the whole hash in one call
void SpookyHashV2::Hash128(
    const void *message,
    size_t length,
    uint64_t *hash1,
    uint64_t *hash2)
{
    if (length < sc_bufSize)
    {
        Short(message, length, hash1, hash2);
        return;
    }

    uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
    uint64_t buf[sc_numVars];
    uint64_t *end;
    union
    {
        const uint8_t *p8;
        uint64_t *p64;
        size_t i;
    } u;
    size_t remainder;

    h0=h3=h6=h9  = *hash1;
    h1=h4=h7=h10 = *hash2;
    h2=h5=h8=h11 = sc_const;

    u.p8 = (const uint8_t *)message;
    end = u.p64 + (length/sc_blockSize)*sc_numVars;

    // handle all whole sc_blockSize blocks of bytes
    if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0))
    {
        while (u.p64 < end)
        {
            Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
            u.p64 += sc_numVars;
        }
    }
    else
    {
        while (u.p64 < end)
        {
            memcpy(buf, u.p64, sc_blockSize);
            Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
            u.p64 += sc_numVars;
        }
    }

    // handle the last partial block of sc_blockSize bytes
    remainder = (length - ((const uint8_t *)end-(const uint8_t *)message));
    memcpy(buf, end, remainder);
    memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder);
    ((uint8_t *)buf)[sc_blockSize-1] = remainder;

    // do some final mixing
    End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
    *hash1 = h0;
    *hash2 = h1;
}
Esempio n. 7
0
    Model3DSChunk::Model3DSChunk(std::ifstream &infile , int csend) :
      file(infile),
      chunkset_end(csend)
    {
#ifdef DEBUG3DS
      std::ostringstream t;

      t<< "Model3DSChunk::Model3DSChunk()";
      LOG.Success("c3ds", t.str());
#endif // DEBUG3DS

      begin = file.tellg();

#ifdef DEBUG3DS
      unsigned int nPos = nRead;
#endif

      id=Short() & 0x0000ffff;

      end=Int()+begin;  // Compute absolute position

#ifdef DEBUG3DS
      t.str("");
      t  << "(" << nPos << ") id = (0x" << std::hex << id << ") " << std::dec
        << "begin = (" << begin << ") "
        << "end = (" << end << ") "
        << "chunkset_end = (" << chunkset_end << ")";

      if (0==id || -1==begin || end > chunkset_end)
        LOG.Error("c3ds", t.str());
      else
        LOG.Success("c3ds", t.str());
#endif // DEBUG3DS
    }
Esempio n. 8
0
 ZZ pow(long long b) const {
     ZZ x=Short(1),y=*this;
     while(b > 0){
         if(b%2 == 1)
             x *= y;
         y *= y; // squaring the base
         b /= 2;
     }
     return x;
 }
Esempio n. 9
0
File: scat.c Progetto: npe9/harvey
int
loadsao(int index)
{
    if(index<=0 || index>NSAO)
        return 0;
    saoopen();
    grow();
    cur->type = SAO;
    cur->index = index;
    seek(saodb, (index-1)*sizeof(SAOrec), 0);
    Eread(saodb, "sao", &cur->sao, sizeof(SAOrec));
    cur->sao.ra = Long(&cur->sao.ra);
    cur->sao.dec = Long(&cur->sao.dec);
    cur->sao.dra = Long(&cur->sao.dra);
    cur->sao.ddec = Long(&cur->sao.ddec);
    cur->sao.mag = Short(&cur->sao.mag);
    cur->sao.mpg = Short(&cur->sao.mpg);
    cur->sao.hd = Long(&cur->sao.hd);
    return 1;
}
Esempio n. 10
0
ILboolean iGetOS2Head(OS2_HEAD * const Header)
{
	if (iread(Header, sizeof(OS2_HEAD), 1) != 1)
		return IL_FALSE;

	UShort(&Header->bfType);
	UInt(&Header->biSize);
	Short(&Header->xHotspot);
	Short(&Header->yHotspot);
	UInt(&Header->DataOff);
	UInt(&Header->cbFix);

	//2003-09-01 changed to UShort according to MSDN
	UShort(&Header->cx);
	UShort(&Header->cy);
	UShort(&Header->cPlanes);
	UShort(&Header->cBitCount);

	iseek((ILint)Header->cbFix - 12, IL_SEEK_CUR);  // Skip rest of header, if any.

	return IL_TRUE;
}
Esempio n. 11
0
File: scat.c Progetto: npe9/harvey
void
mopen(void)
{
    int i;

    if(mindexdb == 0) {
        mindexdb = eopen("mindex");
        Eread(mindexdb, "mindex", mindex, sizeof mindex);
        close(mindexdb);
        for(i=0; i<NMrec; i++)
            mindex[i].ngc = Short(&mindex[i].ngc);
    }
}
Esempio n. 12
0
File: scat.c Progetto: npe9/harvey
void
constelopen(void)
{
    int i;

    if(condb == 0) {
        condb = eopen("con");
        conindexdb = eopen("conindex");
        Eread(conindexdb, "conindex", conindex, sizeof conindex);
        close(conindexdb);
        for(i=0; i<Ncon+1; i++)
            conindex[i] = Short((int16_t*)&conindex[i]);
    }
}
Esempio n. 13
0
// report the hash for the concatenation of all message fragments so far
void SpookyHashV2::Final(uint64_t *hash1, uint64_t *hash2) const
{
    // init the variables
    if (m_length < sc_bufSize)
    {
        *hash1 = m_state[0];
        *hash2 = m_state[1];
        Short( m_data, m_length, hash1, hash2);
        return;
    }

    uint64_t buf[2*sc_numVars];
    memcpy(buf, m_data, sizeof(buf));
    uint64_t *data = buf;
    uint8_t remainder = m_remainder;

    uint64_t h0 = m_state[0];
    uint64_t h1 = m_state[1];
    uint64_t h2 = m_state[2];
    uint64_t h3 = m_state[3];
    uint64_t h4 = m_state[4];
    uint64_t h5 = m_state[5];
    uint64_t h6 = m_state[6];
    uint64_t h7 = m_state[7];
    uint64_t h8 = m_state[8];
    uint64_t h9 = m_state[9];
    uint64_t h10 = m_state[10];
    uint64_t h11 = m_state[11];

    if (remainder >= sc_blockSize)
    {
        // m_data can contain two blocks; handle any whole first block
        Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
        data += sc_numVars;
        remainder -= sc_blockSize;
    }

    // mix in the last partial block, and the length mod sc_blockSize
    memset(&((uint8_t *)data)[remainder], 0, (sc_blockSize-remainder));

    ((uint8_t *)data)[sc_blockSize-1] = remainder;

    // do some final mixing
    End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);

    *hash1 = h0;
    *hash2 = h1;
}
Esempio n. 14
0
// report the hash for the concatenation of all message fragments so far
void SpookyHash::Final(uint64 *hash1, uint64 *hash2)
{
    // init the variables
    if (m_length < sc_bufSize)
    {
        Short( m_data, m_length, hash1, hash2);
        return;
    }
    
    const uint64 *data = (const uint64 *)m_data;
    uint8 remainder = m_remainder;
    
    uint64 h0 = m_state[0];
    uint64 h1 = m_state[1];
    uint64 h2 = m_state[2];
    uint64 h3 = m_state[3];
    uint64 h4 = m_state[4];
    uint64 h5 = m_state[5];
    uint64 h6 = m_state[6];
    uint64 h7 = m_state[7];
    uint64 h8 = m_state[8];
    uint64 h9 = m_state[9];
    uint64 h10 = m_state[10];
    uint64 h11 = m_state[11];

    if (remainder >= sc_blockSize)
    {
        // m_data can contain two blocks; handle any whole first block
        Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
	data += sc_numVars;
	remainder -= sc_blockSize;
    }

    // mix in the last partial block, and the length mod sc_blockSize
    memset(&((uint8 *)data)[remainder], 0, (sc_blockSize-remainder));

    ((uint8 *)data)[sc_blockSize-1] = remainder;
    Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
    
    // do some final mixing
    End(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);

    *hash1 = h0;
    *hash2 = h1;
}
Esempio n. 15
0
    void
swapEndian( unsigned char *buf, int type )
{
    int i,j;
    int p;

    i=0;
    p=0;

    if (type>MAX_SWTYPE || type<0)
        adfEnv.eFct("SwapEndian: type do not exist");

    while( swapTable[type][i]!=0 ) {
        for(j=0; j<swapTable[type][i]; j++) {
            switch( swapTable[type][i+1] ) {
            case SW_LONG:
                *(unsigned long*)(buf+p)=Long(buf+p);
                p+=4;
                break;
            case SW_SHORT:
                *(unsigned short*)(buf+p)=Short(buf+p);
                p+=2;
                break;
            case SW_CHAR:
                p++;
                break;
            default:
                ;
            }
        }
    i+=2;
    }
    if (p!=swapTable[type][i+1]) 
        (*adfEnv.wFct)("Warning: Endian Swapping length");		/* BV */
    

}
Esempio n. 16
0
int CFileLoader::Write()
{
	if ( !IsLoaded() )
		return ERROR_FILE_NOT_FOUND;

	if ( !m_data.vStarts.size() || !m_data.vEnemy.size() || !m_data.vLevel.size() )
		return ERROR_NOINTERFACE;

	int err = 0;
	std::vector<BYTE> file = m_File.vFile;

	DWORD systemData = 0, levelData = 0, enemyData = 0, occupiedSpace = 0, totalSpace = 0;
	
	// first, apply modifications to levels
	for(int i = 0; i < MAX_LEVELS; ++i)
		if ( m_vEditors[i]->Changed() )
			m_vEditors[i]->Apply();

	if ( !GetMemoryStatus(systemData, levelData, enemyData, occupiedSpace, totalSpace) || occupiedSpace >= totalSpace 
		|| file.size() < NES_PTR_EOF )
		return ERROR_NOT_ENOUGH_MEMORY;

	const DWORD DELTA_PTR = 0x8010;

	std::vector<DWORD> vLevels, vEnemies;

	m_ptr.ptrLevelStarts = NES_PTR_START - DELTA_PTR;
	CopyMemory(&file[NES_PTR_START], &m_data.vStarts[0], m_data.vStarts.size());

	m_ptr.ptrLevels[0] = m_ptr.ptrLevelStarts + m_data.vStarts.size();
	m_ptr.ptrLevels[1] = m_ptr.ptrLevels[0] + MAX_LEVELS;

	// write levels banks
	DWORD ptr = m_ptr.ptrLevels[1] + MAX_LEVELS;
	int c = m_data.vLevel.size();
	for(int i = 0; i < c; ++i)
	{
		vLevels.push_back(ptr);

		PBYTE pArray = NULL;
		DWORD dwSize = 0;
		if ( m_data.vLevel[i]->MakeByteArray(&pArray, &dwSize) &&
			ptr + dwSize + DELTA_PTR < NES_PTR_EOF )
		{
			CopyMemory(&file[ptr + DELTA_PTR], pArray, dwSize);
			ptr += dwSize;
			delete[] pArray;
		}
		else
			return ERROR_NOT_ENOUGH_MEMORY;
	}

	// write level pointers
	for(int i = 0; i < MAX_LEVELS; ++i)
	{
		file[m_ptr.ptrLevels[1] + DELTA_PTR + i] = HIBYTE(LOWORD(vLevels[m_game.nLevel[i] - 1]));
		file[m_ptr.ptrLevels[0] + DELTA_PTR + i] = LOBYTE(LOWORD(vLevels[m_game.nLevel[i] - 1]));
	}

	// now update place of enemies data
	// note, that we already checked, that we have enough space to store all data

	// enemies pointers arrays
	DWORD dwEnmPtrsSize = MAX_LEVELS * 2 + ( MAX_LEVELS / 10 ) * 4;
	DWORD eptr = max(ptr, m_ptr.ptrEnemies[0]);

	if ( eptr + dwEnmPtrsSize + enemyData + DELTA_PTR > NES_PTR_EOF )	// move data backward
		eptr = NES_PTR_EOF - DELTA_PTR - enemyData - dwEnmPtrsSize;
	
	if ( eptr < ptr ) // cant rewrite level data, but it shall be always false
		return ERROR_NOT_ENOUGH_MEMORY;
	
	if ( ptr < eptr )	// fill free space with 'ff'
		for(DWORD p = ptr + DELTA_PTR; p < eptr + DELTA_PTR; ++p)
			file[p] = 0xFF;
	
	m_ptr.ptrEnemies[0] = eptr;
	m_ptr.ptrEnemies[1] = m_ptr.ptrEnemies[0] + MAX_LEVELS / 10;
	m_ptr.ptrEnemies[2] = m_ptr.ptrEnemies[1] + MAX_LEVELS / 10;
	m_ptr.ptrEnemies[3] = m_ptr.ptrEnemies[2] + MAX_LEVELS / 10;
	eptr = m_ptr.ptrEnemies[3] + MAX_LEVELS / 10;
	ptr = eptr + MAX_LEVELS * 2;
	// now 'eptr' points to start of array ptrs, 
	// and 'ptr' points to data
	
	c = m_data.vEnemy.size();
	for(int i = 0; i < c; ++i)
	{
		vEnemies.push_back(ptr);
		
		PBYTE pArray = NULL;
		DWORD dwSize = 0;
		if ( m_data.vEnemy[i]->MakeByteArray(&pArray, &dwSize) &&
			ptr + dwSize < NES_PTR_EOF )
		{
			CopyMemory(&file[ptr + DELTA_PTR], pArray, dwSize);
			delete[] pArray;

			ptr += dwSize;
		}
		else
			return ERROR_NOT_ENOUGH_MEMORY;
	}

	for(; ptr < NES_PTR_EOF - DELTA_PTR; ++ptr)
		file[ptr + DELTA_PTR] = 0xFF;

	// write enemies pointers
	for(int lv = 0; lv < MAX_LEVELS / 10; ++lv)
	{
		DWORD levelPtrHi = eptr + 20 * lv, levelPtrLo = eptr + 20 * lv + 10;
		file[m_ptr.ptrEnemies[0] + lv + DELTA_PTR] = HIBYTE(LOWORD(levelPtrHi));
		file[m_ptr.ptrEnemies[1] + lv + DELTA_PTR] = LOBYTE(LOWORD(levelPtrHi));
		file[m_ptr.ptrEnemies[2] + lv + DELTA_PTR] = HIBYTE(LOWORD(levelPtrLo));
		file[m_ptr.ptrEnemies[3] + lv + DELTA_PTR] = LOBYTE(LOWORD(levelPtrLo));

		for(int i = 0; i < 10; ++i)
		{
			file[levelPtrHi + DELTA_PTR + i] = HIBYTE(LOWORD(vEnemies[m_game.nEnemy[10 * lv + i] - 1]));
			file[levelPtrLo + DELTA_PTR + i] = LOBYTE(LOWORD(vEnemies[m_game.nEnemy[10 * lv + i] - 1]));
		}
	}

	// write from copy to a file array
	m_File.vFile = file;

	// write main pointers

	Short(NES_PTR_LEVEL_STARTS, LOWORD(m_ptr.ptrLevelStarts));
	Short(NES_PTR_LEVELS1, LOWORD(m_ptr.ptrLevels[0]));
	Short(NES_PTR_LEVELS2, LOWORD(m_ptr.ptrLevels[1]));
	Short(NES_PTR_ENEMY1, LOWORD(m_ptr.ptrEnemies[0]));
	Short(NES_PTR_ENEMY2, LOWORD(m_ptr.ptrEnemies[1]));
	Short(NES_PTR_ENEMY3, LOWORD(m_ptr.ptrEnemies[2]));
	Short(NES_PTR_ENEMY4, LOWORD(m_ptr.ptrEnemies[3]));

	return err;
}
Esempio n. 17
0
CAMLprim value caml_parse_engine(struct parser_tables *tables,
                                 struct parser_env *env, value cmd, value arg)
{
  int state;
  mlsize_t sp, asp;
  int errflag;
  int n, n1, n2, m, state1;

  switch(Int_val(cmd)) {

  case START:
    state = 0;
    sp = Int_val(env->sp);
    errflag = 0;

  loop:
    n = Short(tables->defred, state);
    if (n != 0) goto reduce;
    if (Int_val(env->curr_char) >= 0) goto testshift;
    SAVE;
    return READ_TOKEN;
                                /* The ML code calls the lexer and updates */
                                /* symb_start and symb_end */
  case TOKEN_READ:
    RESTORE;
    if (Is_block(arg)) {
      env->curr_char = Field(tables->transl_block, Tag_val(arg));
      caml_modify(&env->lval, Field(arg, 0));
    } else {
      env->curr_char = Field(tables->transl_const, Int_val(arg));
      caml_modify(&env->lval, Val_long(0));
    }
    if (caml_parser_trace) print_token(tables, state, arg);

  testshift:
    n1 = Short(tables->sindex, state);
    n2 = n1 + Int_val(env->curr_char);
    if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
        Short(tables->check, n2) == Int_val(env->curr_char)) goto shift;
    n1 = Short(tables->rindex, state);
    n2 = n1 + Int_val(env->curr_char);
    if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
        Short(tables->check, n2) == Int_val(env->curr_char)) {
      n = Short(tables->table, n2);
      goto reduce;
    }
    if (errflag > 0) goto recover;
    SAVE;
    return CALL_ERROR_FUNCTION;
                                /* The ML code calls the error function */
  case ERROR_DETECTED:
    RESTORE;
  recover:
    if (errflag < 3) {
      errflag = 3;
      while (1) {
        state1 = Int_val(Field(env->s_stack, sp));
        n1 = Short(tables->sindex, state1);
        n2 = n1 + ERRCODE;
        if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
            Short(tables->check, n2) == ERRCODE) {
          if (caml_parser_trace)
#ifdef _KERNEL
            printf("Recovering in state %d\n", state1);
#else
            fprintf(stderr, "Recovering in state %d\n", state1);
#endif
          goto shift_recover;
        } else {
          if (caml_parser_trace){
#ifdef _KERNEL
            printf("Discarding state %d\n", state1);
#else
            fprintf(stderr, "Discarding state %d\n", state1);
#endif
          }
          if (sp <= Int_val(env->stackbase)) {
            if (caml_parser_trace){
#ifdef _KERNEL
              printf("No more states to discard\n");
#else
              fprintf(stderr, "No more states to discard\n");
#endif
            }
            return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */
          }
          sp--;
        }
      }
    } else {
      if (Int_val(env->curr_char) == 0)
        return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */
#ifdef _KERNEL
      if (caml_parser_trace) printf("Discarding last token read\n");
#else
      if (caml_parser_trace) fprintf(stderr, "Discarding last token read\n");
#endif
      env->curr_char = Val_int(-1);
      goto loop;
    }

  shift:
    env->curr_char = Val_int(-1);
    if (errflag > 0) errflag--;
  shift_recover:
    if (caml_parser_trace)
#ifdef _KERNEL
      printf("State %d: shift to state %d\n",
              state, Short(tables->table, n2));
#else
      fprintf(stderr, "State %d: shift to state %d\n",
              state, Short(tables->table, n2));
#endif
    state = Short(tables->table, n2);
    sp++;
    if (sp < Long_val(env->stacksize)) goto push;
    SAVE;
    return GROW_STACKS_1;
                                 /* The ML code resizes the stacks */
  case STACKS_GROWN_1:
    RESTORE;
  push:
    Field(env->s_stack, sp) = Val_int(state);
    caml_modify(&Field(env->v_stack, sp), env->lval);
    Store_field (env->symb_start_stack, sp, env->symb_start);
    Store_field (env->symb_end_stack, sp, env->symb_end);
    goto loop;

  reduce:
    if (caml_parser_trace)
#ifdef _KERNEL
      printf("State %d: reduce by rule %d\n", state, n);
#else
      fprintf(stderr, "State %d: reduce by rule %d\n", state, n);
#endif
    m = Short(tables->len, n);
    env->asp = Val_int(sp);
    env->rule_number = Val_int(n);
    env->rule_len = Val_int(m);
    sp = sp - m + 1;
    m = Short(tables->lhs, n);
    state1 = Int_val(Field(env->s_stack, sp - 1));
    n1 = Short(tables->gindex, m);
    n2 = n1 + state1;
    if (n1 != 0 && n2 >= 0 && n2 <= Int_val(tables->tablesize) &&
        Short(tables->check, n2) == state1) {
      state = Short(tables->table, n2);
    } else {
      state = Short(tables->dgoto, m);
    }
    if (sp < Long_val(env->stacksize)) goto semantic_action;
    SAVE;
    return GROW_STACKS_2;
                                /* The ML code resizes the stacks */
  case STACKS_GROWN_2:
    RESTORE;
  semantic_action:
    SAVE;
    return COMPUTE_SEMANTIC_ACTION;
                                /* The ML code calls the semantic action */
  case SEMANTIC_ACTION_COMPUTED:
    RESTORE;
    Field(env->s_stack, sp) = Val_int(state);
    caml_modify(&Field(env->v_stack, sp), arg);
    asp = Int_val(env->asp);
    Store_field (env->symb_end_stack, sp, Field(env->symb_end_stack, asp));
    if (sp > asp) {
      /* This is an epsilon production. Take symb_start equal to symb_end. */
      Store_field (env->symb_start_stack, sp, Field(env->symb_end_stack, asp));
    }
    goto loop;

  default:                      /* Should not happen */
    Assert(0);
    return RAISE_PARSE_ERROR;   /* Keeps gcc -Wall happy */
  }

}
Esempio n. 18
0
int CFileLoader::Read()
{
	if ( !m_File.vFile.size() )
		return ERROR_FILE_NOT_FOUND;

	Flush();

	const DWORD DELTA_PTR = 0x8010;
	int err = 0;
	std::map<DWORD, DWORD> mLevels, mEnemies;

	// get main pointers
	m_ptr.ptrLevelStarts = Short(NES_PTR_LEVEL_STARTS);
	m_ptr.ptrLevels[0] = Short(NES_PTR_LEVELS1);
	m_ptr.ptrLevels[1] = Short(NES_PTR_LEVELS2);
	m_ptr.ptrEnemies[0] = Short(NES_PTR_ENEMY1);
	m_ptr.ptrEnemies[1] = Short(NES_PTR_ENEMY2);
	m_ptr.ptrEnemies[2] = Short(NES_PTR_ENEMY3);
	m_ptr.ptrEnemies[3] = Short(NES_PTR_ENEMY4);

	// load levels
	if ( m_ptr.ptrLevels[1] - m_ptr.ptrLevels[0] != MAX_LEVELS 
		|| m_ptr.ptrLevelStarts + DELTA_PTR + MAX_LEVELS / 10 > m_File.vFile.size() )
	{
		Flush();
		return ERROR_FILE_CORRUPT;
	}

	m_data.vStarts.insert(m_data.vStarts.end(), 
		m_File.vFile.begin() + (m_ptr.ptrLevelStarts + DELTA_PTR), 
		m_File.vFile.begin() + (m_ptr.ptrLevelStarts + DELTA_PTR + MAX_LEVELS / 10));

	for(int i = 0; i < MAX_LEVELS; ++i)
	{
		DWORD levelPtr = 
			MAKEWORD(
				m_File.vFile[i + m_ptr.ptrLevels[0] + DELTA_PTR],
				m_File.vFile[i + m_ptr.ptrLevels[1] + DELTA_PTR]
				);

		if ( levelPtr + DELTA_PTR >= m_File.vFile.size() )
		{
			err = ERROR_FILE_CORRUPT;
			break;
		}

		if ( mLevels.find(levelPtr) == mLevels.end() )
		{
			CNesLevel * plv = new CNesLevel();
			plv->LoadLevel(&m_File.vFile[levelPtr + DELTA_PTR]);
			m_data.vLevel.push_back(plv);
			mLevels.insert(std::make_pair(levelPtr, m_data.vLevel.size()));
		}

		m_game.nLevel[i] = LOBYTE(LOWORD(mLevels.find(levelPtr)->second));
	}

	DWORD ptrsList[MAX_LEVELS] = { 0 };

	for(int lv = 0; lv < MAX_LEVELS / 10; ++lv)
	{
		DWORD levelPtrHi = 
			MAKEWORD(
				m_File.vFile[lv + m_ptr.ptrEnemies[1] + DELTA_PTR],
				m_File.vFile[lv + m_ptr.ptrEnemies[0] + DELTA_PTR]
			),
			  levelPtrLo = 
			MAKEWORD(
				m_File.vFile[lv + m_ptr.ptrEnemies[3] + DELTA_PTR],
				m_File.vFile[lv + m_ptr.ptrEnemies[2] + DELTA_PTR]
			);

		if ( levelPtrHi + DELTA_PTR >= m_File.vFile.size() || levelPtrLo + DELTA_PTR >= m_File.vFile.size() )
		{
			err = ERROR_FILE_CORRUPT;
			break;
		}

		for(int i = 0; i < 10; ++i)
		{
			DWORD levelPtr = 
				MAKEWORD(
					m_File.vFile[i + levelPtrLo + DELTA_PTR],
					m_File.vFile[i + levelPtrHi + DELTA_PTR]
				);

			if ( levelPtr + DELTA_PTR >= m_File.vFile.size() )
				continue;

			if ( mEnemies.find(levelPtr) == mEnemies.end() )
			{
				CNesEnemyData * ped = new CNesEnemyData;
				m_data.vEnemy.push_back(ped);
				mEnemies.insert(std::make_pair(levelPtr, m_data.vEnemy.size()));
			}

			m_game.nEnemy[10 * lv + i] = LOBYTE(LOWORD(mEnemies[levelPtr]));
		}
	}

	if ( !err )
	{
		// due to enemies data has no special marker for end data, 
		// we shall calculate sizes manualy, and load enemies after 'for'
		// also note that std::map is ALREADY sorted by the key value,
		// so sizes of enemies data will be actually calculated between neighbors arrays
		for(std::map<DWORD, DWORD>::iterator m = mEnemies.begin(); m != mEnemies.end(); ++m)
		{
			std::map<DWORD, DWORD>::iterator next = m;
			next++;

			if ( next != mEnemies.end() )
			{
				m_data.vEnemy[m->second - 1]->LoadData( &m_File.vFile[m->first + DELTA_PTR], 
					next->first - m->first);
			}
			else
			{
				// try to find 'FF' marker, or EOF data
				DWORD lastPtr = m->first + DELTA_PTR, eofPtr = min(NES_PTR_EOF, m_File.vFile.size());
				while(lastPtr < eofPtr && 0xFF != m_File.vFile[lastPtr])
					lastPtr++;

				m_data.vEnemy[m->second - 1]->LoadData( &m_File.vFile[m->first + DELTA_PTR],
					( lastPtr - DELTA_PTR ) - m->first - 1);
			}
		}

		// now load editors
		for(int i = 0; i < MAX_LEVELS; ++i)
		{
			CNesEditor * pEditor = new CNesEditor(
				*m_data.vLevel[m_game.nLevel[i] - 1], *m_data.vEnemy[m_game.nEnemy[i] - 1],
				*this, i
				);
			m_vEditors.push_back(pEditor);
		}

		m_game.fLoaded = TRUE;
	}
	else if ( err )
		Flush();

	return err;
}