Example #1
0
static int ReadStateChunk(FILE *st, SFORMAT *sf, int size)
{
 SFORMAT *tmp;
 int temp;
 temp=ftell(st);

 while(ftell(st)<temp+size)
 {
  uint32 tsize;
  char toa[4];
  if(fread(toa,1,4,st)<=0)
   return 0;

  read32le(&tsize,st);

  if((tmp=CheckS(sf,tsize,toa)))
  {
   fread((uint8 *)tmp->v,1,tmp->s&(~RLSB),st);

   #ifndef LSB_FIRST
   if(tmp->s&RLSB)
    FlipByteOrder(tmp->v,tmp->s&(~RLSB));
   #endif
  }
  else
   fseek(st,tsize,SEEK_CUR);
 } // while(...)
 return 1;
}
Example #2
0
static int ReadStateChunk(MEMFILE *st, SFORMAT *sf, int size)
{
 //if(scan_chunks)
 //  return mem_fseek(st,size,SEEK_CUR) == 0;

 SFORMAT *tmp;
 int temp;
 temp=mem_ftell(st);

 while(mem_ftell(st)<temp+size)
 {
  uint32 tsize;
  char toa[4];
  if(mem_fread(toa,1,4,st)<=0)
   return 0;

  mem_read32le(&tsize,st);

  if((tmp=CheckS(sf,tsize,toa)))
  {
   mem_fread((uint8 *)tmp->v,1,tmp->s&(~RLSB),st);

   #ifndef LSB_FIRST
   if(tmp->s&RLSB)
    FlipByteOrder(tmp->v,tmp->s&(~RLSB));
   #endif
  }
  else
  {
   mem_fseek(st,tsize,SEEK_CUR);
   printf("ReadStateChunk: sect \"%c%c%c%c\" not handled\n", toa[0], toa[1], toa[2], toa[3]);
  }
 } // while(...)
 return 1;
}
Example #3
0
static int SubWrite(FILE *st, SFORMAT *sf)
{
 uint32 acc=0;

 while(sf->v)
 {
  if(sf->s==~0)    /* Link to another struct.  */
  {
   uint32 tmp;

   if(!(tmp=SubWrite(st,(SFORMAT *)sf->v)))
    return(0);
   acc+=tmp;
   sf++;
   continue;
  }

  acc+=8;      /* Description + size */
  acc+=sf->s&(~RLSB);

  if(st)      /* Are we writing or calculating the size of this block? */
  {
   fwrite(sf->desc,1,4,st);
   write32le(sf->s&(~RLSB),st);

   #ifndef LSB_FIRST
   if(sf->s&RLSB)
    FlipByteOrder(sf->v,sf->s&(~RLSB));
   #endif

   fwrite((uint8 *)sf->v,1,sf->s&(~RLSB),st);
   /* Now restore the original byte order. */
   #ifndef LSB_FIRST
   if(sf->s&RLSB)
    FlipByteOrder(sf->v,sf->s&(~RLSB));
   #endif
  }
  sf++;
 }

 return(acc);
}
Example #4
0
static bool ReadStateChunk(EMUFILE* is, const SFORMAT *sf, int size)
{
	const SFORMAT *tmp = NULL;
	const SFORMAT *guessSF = NULL;
	int temp = is->ftell();

	while(is->ftell()<temp+size)
	{
		u32 sz, count;

		char toa[4];
		is->fread(toa,4);
		if(is->fail())
			return false;

		if(!read32le(&sz,is)) return false;
		if(!read32le(&count,is)) return false;

		if((tmp=CheckS(guessSF,sf,sz,count,toa)))
		{
#ifdef MSB_FIRST
         if(sz == 1)
         {
            //special case: read a huge byte array
            is->fread((char *)tmp->v,count);
         } else {
            for(unsigned int i=0;i<count;i++)
            {
               is->fread((char *)tmp->v + i*sz,sz);
               FlipByteOrder((u8*)tmp->v + i*sz,sz);
            }
         }
#else
         // no need to ever loop one at a time if not flipping byte order
         is->fread((char *)tmp->v,sz*count);
#endif
			guessSF = tmp + 1;
		}
		else
		{
			is->fseek(sz*count,SEEK_CUR);
			guessSF = NULL;
		}
	} // while(...)
	return true;
}
static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
{
   int temp = st->loc;

   while (st->loc < (temp + size))
   {
      uint32_t recorded_size;	// In bytes
      uint8_t toa[1 + 256];	// Don't change to char unless cast toa[0] to unsigned to smem_read() and other places.

      if(smem_read(st, toa, 1) != 1)
      {
         puts("Unexpected EOF");
         return(0);
      }

      if(smem_read(st, toa + 1, toa[0]) != toa[0])
      {
         puts("Unexpected EOF?");
         return 0;
      }

      toa[1 + toa[0]] = 0;

      smem_read32le(st, &recorded_size);

      SFORMAT *tmp = FindSF((char*)toa + 1, sf);

      if(tmp)
      {
         uint32_t expected_size = tmp->size;	// In bytes

         if(recorded_size != expected_size)
         {
            printf("Variable in save state wrong size: %s.  Need: %d, got: %d\n", toa + 1, expected_size, recorded_size);
            if(smem_seek(st, recorded_size, SEEK_CUR) < 0)
            {
               puts("Seek error");
               return(0);
            }
         }
         else
         {
            smem_read(st, (uint8_t *)tmp->v, expected_size);

            if(tmp->flags & MDFNSTATE_BOOL)
            {
               // Converting downwards is necessary for the case of sizeof(bool) > 1
               for(int32_t bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--)
               {
                  ((bool *)tmp->v)[bool_monster] = ((uint8_t *)tmp->v)[bool_monster];
               }
            }
#ifdef MSB_FIRST
            if(tmp->flags & MDFNSTATE_RLSB64)
               Endian_A64_LE_to_NE(tmp->v, expected_size / sizeof(uint64_t));
            else if(tmp->flags & MDFNSTATE_RLSB32)
               Endian_A32_LE_to_NE(tmp->v, expected_size / sizeof(uint32_t));
            else if(tmp->flags & MDFNSTATE_RLSB16)
               Endian_A16_LE_to_NE(tmp->v, expected_size / sizeof(uint16_t));
            else if(tmp->flags & RLSB)
               FlipByteOrder((uint8_t*)tmp->v, expected_size);
#endif
         }
      }
      else
      {
         printf("Unknown variable in save state: %s\n", toa + 1);
         if(smem_seek(st, recorded_size, SEEK_CUR) < 0)
         {
            puts("Seek error");
            return(0);
         }
      }
   } // while(...)

   assert(st->loc == (temp + size));
   return 1;
}
static bool SubWrite(StateMem *st, SFORMAT *sf, const char *name_prefix = NULL)
{
   while(sf->size || sf->name)	// Size can sometimes be zero, so also check for the text name.  These two should both be zero only at the end of a struct.
   {
      if(!sf->size || !sf->v)
      {
         sf++;
         continue;
      }

      if(sf->size == (uint32_t)~0)		/* Link to another struct.	*/
      {
         if(!SubWrite(st, (SFORMAT *)sf->v, name_prefix))
            return(0);

         sf++;
         continue;
      }

      int32_t bytesize = sf->size;

      char nameo[1 + 256];
      int slen;

      slen = snprintf(nameo + 1, 256, "%s%s", name_prefix ? name_prefix : "", sf->name);
      nameo[0] = slen;

      if(slen >= 255)
      {
         printf("Warning:  state variable name possibly too long: %s %s %s %d\n", sf->name, name_prefix, nameo, slen);
         slen = 255;
      }

      smem_write(st, nameo, 1 + nameo[0]);
      smem_write32le(st, bytesize);

#ifdef MSB_FIRST
      /* Flip the byte order... */
      if(sf->flags & MDFNSTATE_BOOL)
      {

      }
      else if(sf->flags & MDFNSTATE_RLSB64)
         Endian_A64_Swap(sf->v, bytesize / sizeof(uint64_t));
      else if(sf->flags & MDFNSTATE_RLSB32)
         Endian_A32_Swap(sf->v, bytesize / sizeof(uint32_t));
      else if(sf->flags & MDFNSTATE_RLSB16)
         Endian_A16_Swap(sf->v, bytesize / sizeof(uint16_t));
      else if(sf->flags & RLSB)
         FlipByteOrder((uint8_t*)sf->v, bytesize);
#endif

      // Special case for the evil bool type, to convert bool to 1-byte elements.
      // Don't do it if we're only saving the raw data.
      if(sf->flags & MDFNSTATE_BOOL)
      {
         for(int32_t bool_monster = 0; bool_monster < bytesize; bool_monster++)
         {
            uint8_t tmp_bool = ((bool *)sf->v)[bool_monster];
            //printf("Bool write: %.31s\n", sf->name);
            smem_write(st, &tmp_bool, 1);
         }
      }
      else
         smem_write(st, (uint8_t *)sf->v, bytesize);

#ifdef MSB_FIRST
      /* Now restore the original byte order. */
      if(sf->flags & MDFNSTATE_BOOL)
      {

      }
      else if(sf->flags & MDFNSTATE_RLSB64)
         Endian_A64_LE_to_NE(sf->v, bytesize / sizeof(uint64_t));
      else if(sf->flags & MDFNSTATE_RLSB32)
         Endian_A32_LE_to_NE(sf->v, bytesize / sizeof(uint32_t));
      else if(sf->flags & MDFNSTATE_RLSB16)
         Endian_A16_LE_to_NE(sf->v, bytesize / sizeof(uint16_t));
      else if(sf->flags & RLSB)
         FlipByteOrder((uint8_t*)sf->v, bytesize);
#endif
      sf++; 
   }

   return true;
}
Example #7
0
static int SubWrite(EMUFILE* os, const SFORMAT *sf)
{
	uint32 acc=0;

#ifdef DEBUG
	std::set<std::string> keyset;
#endif

	const SFORMAT* temp = sf;
	while(temp->v) {
		const SFORMAT* seek = sf;
		while(seek->v && seek != temp) {
			if(!strcmp(seek->desc,temp->desc)) {
				printf("ERROR! duplicated chunk name: %s\n", temp->desc);
			}
			seek++;
		}
		temp++;
	}

	while(sf->v)
	{
		//not supported right now
		//if(sf->size==~0)		//Link to another struct
		//{
		//	uint32 tmp;

		//	if(!(tmp=SubWrite(os,(SFORMAT *)sf->v)))
		//		return(0);
		//	acc+=tmp;
		//	sf++;
		//	continue;
		//}

		int count = sf->count;
		int size = sf->size;

        //add size of current node to the accumulator
		acc += 4 + sizeof(sf->size) + sizeof(sf->count);
		acc += count * size;

		if(os)			//Are we writing or calculating the size of this block?
		{
			os->fwrite(sf->desc,4);
			write32le(sf->size,os);
			write32le(sf->count,os);

			#ifdef DEBUG
			//make sure we dont dup any keys
			if(keyset.find(sf->desc) != keyset.end())
			{
				printf("duplicate save key!\n");
				assert(false);
			}
			keyset.insert(sf->desc);
			#endif


#ifdef MSB_FIRST
         if(size == 1) {
            //special case: write a huge byte array
            os->fwrite((char *)sf->v,count);
         } else {
            for(int i=0;i<count;i++) {
               FlipByteOrder((u8*)sf->v + i*size, size);
               os->fwrite((char*)sf->v + i*size,size);
               //Now restore the original byte order.
               FlipByteOrder((u8*)sf->v + i*size, size);
            }
         }
#else
         // no need to ever loop one at a time if not flipping byte order
         os->fwrite((char *)sf->v,size*count);
#endif
		}
		sf++;
	}

	return(acc);
}
int CheckForNewIPAddress(unsigned int *LocalAddr) {
    IPECHO_MESSAGE Msg ;
    int Socket ;
    fd_set SocketArray ;
    int Result, ResponseLength ;
    struct sockaddr_in sin;
    int Tries = 10 ;
    unsigned int EchoServerIPAddress ;
    struct timeval tv = {0,0}  ;
    int BytesSent ;

    //if ((EchoServerIPAddress = FetchIPAddress(ECHO_SERVER_TO_USE)) == 0)  {
    	if ((EchoServerIPAddress = FetchIPAddress(TZO_ECHO_SERVER)) == 0)  {
    	printf("115 EchoServerIPAddress = %d \n",EchoServerIPAddress);
        printf("Unable to contact TZO Echo Servers\n") ;
        return(-1) ;
    }
    printf("119 EchoServerIPAddress = %d \n",EchoServerIPAddress);

    if ((Socket = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("Unable to create a socket\n") ;
        return(-1) ;
    }

// test
    // EchoServerIPAddress = 0x401BA664;--->"No resonce from server\n"
     
    //  EchoServerIPAddress = 0x64A61B40 ;
     
    sin.sin_family = AF_INET ;
    sin.sin_addr.s_addr = EchoServerIPAddress ;
    
    sin.sin_port = htons(IPECHO_PORT) ;

    Msg.ID = IPECHO_ID_CLIENT ;
    Msg.Request = IPECHO_REQUEST_ECHO ;
    Msg.IPAddress = *LocalAddr ;
    Msg.Signature = CalcIPEchoSignature(&Msg) ;

    ResponseLength = 0 ;
    
    printf("LocalAddr =%d \n",Msg.IPAddress);

    if ((BytesSent = sendto(Socket, (unsigned char *)&Msg, sizeof(Msg), 0, (const struct sockaddr *)&sin, sizeof(sin))) < 0) {
    	 //sin.sin_addr.s_addr =0x64A61B40 ;//0x401BA664;
    	 //if ((BytesSent = sendto(Socket, (unsigned char *)&Msg, sizeof(Msg), 0, (const struct sockaddr *)&sin, sizeof(sin))) < 0) {
        printf("Unable to sendto socket\n") ;
        close(Socket) ;
        
        
        return(-1) ;
    }

    while (Tries--)  {
        tv.tv_sec = 0 ;
        tv.tv_usec = 0 ;
        FD_ZERO(&SocketArray) ;
        FD_SET(Socket, &SocketArray) ;
        tv.tv_sec = 1 ;
        tv.tv_usec = 0 ;
        Result = select(Socket+1, &SocketArray, NULL, NULL, &tv) ;

        if (Result)
            break ;
        sleep(1) ;
        }

    if (!Result) {
        printf("No resonce from server\n") ;
	    close(Socket) ;
        return(-1) ;
    }

    if ((ResponseLength = recvfrom(Socket, (unsigned char *)&Msg, 16, 0, 0, 0)) <= 0) {
        printf("Bad responce from server\n") ;
        close(Socket) ;
        return(-1) ;
    }

    close(Socket) ;

    if (ResponseLength == sizeof(IPECHO_MESSAGE)) {
        *LocalAddr = FlipByteOrder(Msg.IPAddress) ;
        return 1 ;
    }
printf("strange server response, are you connecting to the right server?\n") ;
  return(-1) ;
}
Example #9
0
void Endian_V_NE_to_LE(void *src, uint32 bytesize)
{
 #ifdef MSB_FIRST
 FlipByteOrder((uint8 *)src, bytesize);
 #endif
}