Error PackedDataContainer::pack(const Variant &p_data) {

	Vector<uint8_t> tmpdata;
	Map<String, uint32_t> string_cache;
	_pack(p_data, tmpdata, string_cache);
	datalen = tmpdata.size();
	data.resize(tmpdata.size());
	PoolVector<uint8_t>::Write w = data.write();
	copymem(w.ptr(), tmpdata.ptr(), tmpdata.size());

	return OK;
}
示例#2
0
int Exec(const TCHAR *Package, DWORD dwPackParam, DWORD dwCmdLine, const TCHAR *output, const TCHAR *args)
{
	wstring _pack((wchar_t*)Package);
	wstring packName = GetPackageName(_pack);

	setlocale(0, "Japanese");
	FILE* f = _wfopen(Package, L"rb");
	if (f)
	{
		fseek(f, 0, SEEK_END);
		size_t flength = ftell(f);
		fseek(f, 0, SEEK_SET);
		buf = (unsigned char*)malloc(flength);
		fread(buf, flength, 1, f);
		fclose(f);

		//FILE* txt = _wfopen(L"test.txt",L"wb");
		wchar_t new_name[1024];
		wsprintf(new_name, L"%s.txt", packName.c_str());
		FILE* txt = _wfopen(new_name, L"wb");

		SCENEHEADER *sce_header = (SCENEHEADER*)buf;
		PFILE_INFO string_index = (PFILE_INFO)&buf[sce_header->string_index_pair.offset];
		wchar_t* string_data = (wchar_t*)&buf[sce_header->string_data_pair.offset];
		fwrite("\xFF\xFE", 2, 1, txt);
		for (DWORD x = 0; x<sce_header->string_index_pair.count; x++)
		{
			PFILE_INFO info = &string_index[x];
			wchar_t* info_str = &string_data[info->offset];
			wchar_t* new_str = new wchar_t[info->length * 2];
			memset(new_str, 0, sizeof(wchar_t)* info->length * 2);

			decrypt_string(info_str, new_str, info->length, x);
			for (int i = 0; i<info->length; i++)
			{
				fwprintf(txt, L"%c", new_str[i]);
			}
			fwprintf(txt, L"\r\n");
			delete new_str;
		}
	}
	return 0;
}
MC2BoundingBox getBBoxForMap( uint32 mapID, uint32 listenPort ) {
   DatagramReceiver 
      receiver( MultiCastProperties::changeMapSetPort( listenPort ),
                DatagramReceiver::FINDFREEPORT );
   uint32 mapip = MultiCastProperties::getNumericIP( MODULE_TYPE_MAP, true );
   uint16 mapport = MultiCastProperties::getPort( MODULE_TYPE_MAP, true );
   uint32 mapSet = Properties::getMapSet();
   
   if ( mapSet != MAX_UINT32 ) {
      // code also exists in PacketContainer.cpp and
      // ModuleMap.cpp, move to utility function?
      IPnPort before( mapip, mapport );
      IPnPort newaddr = MultiCastProperties::
         changeMapSetAddr( IPnPort( mapip, mapport ) );

      mapip = newaddr.getIP();
      mapport = newaddr.getPort();
      mc2dbg << "[getBBoxForMap] Changed map module addr from "
             << before << " -> " << newaddr
             << " because mapSet = " << mapSet;
   }

   Packet _pack( MAX_PACKET_SIZE ); // For receiving the mapreply
   DatagramSender sock;


   const int waittime = 1000000;
   uint32 status = StringTable::NOT;
   const int maxRetries = 5;
   int nbrRetries = 0;
   while ( status != StringTable::OK && nbrRetries++ <= maxRetries ) {      
      AllMapRequestPacket reqpack( Packet::RequestID( 1 ),
                                   Packet::PacketID( 1 ),
                                   AllMapRequestPacket::BOUNDINGBOX );
      
      reqpack.setOriginIP( NetUtility::getLocalIP() );
      reqpack.setOriginPort( receiver.getPort() );
      reqpack.setResendNbr((byte) nbrRetries-1);
      
      // Send request to open TCP connection between local and mapmodule
      
      if ( ! sock.send( &reqpack, mapip, mapport ) ) {
         mc2log << error << "[getBBoxForMap] could not send "
                << " AllMapRequestPacket - retrying." << endl;
         continue; // Go another round in the loop.
      }
      
      // Receive packet with ip and port to a mapModule
      if ( ! receiver.receive( &_pack, waittime ) ) {
         mc2log << error << "[getBBoxForMap] error receiving ack - retrying."
                << endl;
         continue; // Go another round in the loop.
      }
      
      if ( _pack.getSubType() != Packet::PACKETTYPE_ALLMAPREPLY ) {
         mc2log << error << "[getBBoxForMap] Got packet with subtype "
                << _pack.getSubTypeAsString()
                << " when expecting allmapreply." << endl;
         continue; // Please try again.
      }
      
      AllMapReplyPacket* pack = static_cast<AllMapReplyPacket *>( &_pack );
      
      status = pack->getStatusCode();
      

      uint32 myMap = 0;
      while ( myMap < pack->getNbrMaps() &&
              mapID != pack->getMapID( myMap ) ){
         ++myMap;
      }

      if ( myMap != pack->getNbrMaps() ) {

         mc2dbg << "[getBBoxForMap] map "
                << prettyMapID(mapID) << " index " << myMap
                << " found " << prettyMapID( pack->getMapID( myMap ) ) << endl;
         MC2BoundingBox bbox;
         pack->setMC2BoundingBox( myMap, &bbox );

         return bbox;

      } else {
         mc2log << warn << "[getBBoxForMap] Map " << prettyMapID( mapID ) 
                << " not found in AllMapReplyPacket." << endl;
      }
   }

   return MC2BoundingBox();
}
示例#4
0
文件: fwch.c 项目: sharugupta/OpenUH
long
_fwch(
	unit	*cup,
	long	*uda,
	long	chars,
	int	mode)
{
	register int	bytsiz;
	register long	nchr;
	unsigned char	tbuf[TBUFSZB];	/* Line packing buffer */
	FILE		*fptr;

/*
 *	If positioned after an endfile, and the file does not
 *	support multiple endfiles, a write is invalid.
 */
	if (cup->uend && !cup->umultfil && !cup->uspcproc) {
		errno	= FEWRAFEN;
		return(IOERR);
	}

	nchr	= 0;

	switch (cup->ufs) {

	case FS_TEXT:
	case STD:
		fptr	= cup->ufp.std;

		/* switch the FILE structure into write mode */

#if	!defined(_LITTLE_ENDIAN) || (defined(_LITTLE_ENDIAN) && defined(__sv2))
		if ((FILE_FLAG(fptr) & (_IOWRT | _IORW)) == _IORW) {
			if (FILE_FLAG(fptr) & _IOREAD)
				(void) fseek(fptr, 0, SEEK_CUR);

			FILE_FLAG(fptr) |= _IOWRT;
		}
#endif

#if	defined(_SOLARIS) || (defined(_LITTLE_ENDIAN) && !defined(__sv2))
		while (nchr < chars) {
			register long	count;
			register int	ret;
			/* Pack chars into temp buffer and write them */
			count	= chars - nchr;
			if (count > TBUFSZB)
				count	= TBUFSZB;
#ifdef KEY /* Bug 5926 */
			count = _pack(&uda[nchr], (char *)tbuf, count,
			  terminator(&mode, nchr, count, chars));
#else /* KEY Bug 5926 */
			_pack(&uda[nchr], (char *)tbuf, count, -1);
#endif /* KEY Bug 5926 */
			ret	= fwrite(tbuf, 1, count, fptr);
			if ( ret != count || ferror(fptr) ) {
				if ( ret != count || errno == 0)
					errno	= FESTIOER;
				return(IOERR);
			}
			nchr	+= count;
		}
#else

		/* If the stream is unbuffered... */

		if (FILE_FLAG(fptr) & (_IONBF | _IOLBF)) {
			while (nchr < chars) {
				register long	count;
				register long	ret;

				/* Pack chars into temp buffer and write them */

				count	= chars - nchr;

				if (count > TBUFSZB)
					count	= TBUFSZB;

#ifdef KEY /* Bug 5926 */
			count= _pack(&uda[nchr], (char *)tbuf, count,
			  terminator(&mode, nchr, count, chars));
#else /* KEY Bug 5926 */
				_pack(&uda[nchr], (char *)tbuf, count, -1);

#endif /* KEY Bug 5926 */
				ret	= fwrite(tbuf, 1, count, fptr);

				if ( ret != count || ferror(fptr) ) {
					if ( ret != count || errno == 0)
						errno	= FESTIOER;
					return(IOERR);
				}

				nchr	+= count;
			}
		}
		else {	/* for a buffered stream... */

			while (FILE_CNT(fptr) < chars - nchr) {
				register long	count;
				register int	ret;

				count	= FILE_CNT(fptr); /* space left in buffer */
				if (count > 0) {
					/* pack data into the buffer */
					_pack(&uda[nchr], (char *)FILE_PTR(fptr),
						count, -1);
					FILE_PTR(fptr)	+= count;
					FILE_CNT(fptr)	= 0;
				}

				/*
				 * We set errno to 0 here in case the following
				 * buffer flush fails.  UNICOS 8.2 fputc (and
				 * previous) was not X/Open compliant and did
				 * not always set errno when a buffer flush
				 * completed partially due to a disk full
				 * conditon.  The zeroing of errno may be
				 * removed when we can assume that the fputc()
				 * from UNICOS and Solaris are X/Open compliant.
				 */

				errno	= 0;

				/*
				 * This fputc() will either trigger a buffer
				 * flush or cause the buffer to be allocated
				 * for the first time.
				 */

				ret	= fputc(uda[nchr + count], fptr);

				if (ret == EOF && ferror(fptr)) {
					if (errno == 0)
						errno	= FESTIOER;
					return(IOERR);
				}

				nchr	+= count + 1;
			}

			if (nchr < chars) {	/* Put data in buffer */
				_pack(&uda[nchr], (char *)FILE_PTR(fptr),
					chars - nchr, -1);

				FILE_CNT(fptr)	-= chars - nchr;
				FILE_PTR(fptr)	+= chars - nchr;
			}
		}
#endif

		if (mode == FULL) {
			register int	ret;

 			ret	= putc('\n', fptr);;

			if (ret == EOF && ferror(fptr)) {
				if (errno == 0)
					errno	= FESTIOER;
				return(IOERR);
			}
			chars++;
		}

		return(chars);

	case FS_FDC:

		/*
		 * If a logical endfile record had just been read,
		 * replace it with a physical endfile record before
		 * starting the current data record.
		 */
		if ((cup->uend == LOGICAL_ENDFILE) && !(cup->uspcproc)) {
			if (XRCALL(cup->ufp.fdc, weofrtn)cup->ufp.fdc,
				&cup->uffsw) < 0){
				errno	= cup->uffsw.sw_error;
				return(IOERR);
			}
		}

		cup->uend	= BEFORE_ENDFILE;

		if (cup->ucharset == 0) {
			register long	ret;

			ret	= XRCALL(cup->ufp.fdc, writecrtn) cup->ufp.fdc,
					WPTR2BP(uda),
					chars, &cup->uffsw, mode);

			if (ret < 0) {
				errno	= cup->uffsw.sw_error;
				return(IOERR);
			}

			return(chars);
		}

/*
 *		Get proper byte size (might not be 8-bits if doing conversion).
 */

#if	NUMERIC_DATA_CONVERSION_ENABLED
		bytsiz	= __fndc_charsz[cup->ucharset];
#else
		bytsiz	= 8;
#endif

		do {
			register long	breq;
			register int	fulp;
			register long	ncnt;
			register long	ret;
			int		ubc;

			ncnt	= TBUFSZB;
			breq	= 0;
			ubc	= 0;

			if ((chars - nchr) > 0) {
				register long	totbits;

				if (ncnt > (chars - nchr))
					ncnt	= chars - nchr;

				if (_fdc_packc((char *)tbuf, &uda[nchr], ncnt,
					cup->ucharset) < 0) {
					return(IOERR);
				}


				totbits	= bytsiz * ncnt;	/* bit count */
				breq	= (totbits + 7) >> 3; /* 8-bit bytes */
				ubc	= (breq << 3) - totbits;
			}

			nchr	+= ncnt;

			if ((nchr >= chars) && ( mode == FULL ))
				fulp	= FULL;
			else
				fulp	= PARTIAL;

			ret	= XRCALL(cup->ufp.fdc, writertn) cup->ufp.fdc,
					CPTR2BP(tbuf),
					breq, &cup->uffsw, fulp, &ubc);

			if (ret != breq) { 	/* if an error */
				errno	= cup->uffsw.sw_error;
				return(IOERR);
			}

		} while (nchr < chars);

		return(chars);
/*
 *	unsupported structure if not TEXT/STD, or FDC
 */
	default:
		errno	= FEINTFST;
		return(IOERR);
	}
}
uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {

	switch (p_data.get_type()) {

		case Variant::STRING: {

			String s = p_data;
			if (string_cache.has(s)) {
				return string_cache[s];
			}

			string_cache[s] = tmpdata.size();

		}; //fallthrough
		case Variant::NIL:
		case Variant::BOOL:
		case Variant::INT:
		case Variant::REAL:
		case Variant::VECTOR2:
		case Variant::RECT2:
		case Variant::VECTOR3:
		case Variant::TRANSFORM2D:
		case Variant::PLANE:
		case Variant::QUAT:
		case Variant::AABB:
		case Variant::BASIS:
		case Variant::TRANSFORM:
		case Variant::POOL_BYTE_ARRAY:
		case Variant::POOL_INT_ARRAY:
		case Variant::POOL_REAL_ARRAY:
		case Variant::POOL_STRING_ARRAY:
		case Variant::POOL_VECTOR2_ARRAY:
		case Variant::POOL_VECTOR3_ARRAY:
		case Variant::POOL_COLOR_ARRAY:
		case Variant::NODE_PATH: {

			uint32_t pos = tmpdata.size();
			int len;
			encode_variant(p_data, NULL, len);
			tmpdata.resize(tmpdata.size() + len);
			encode_variant(p_data, &tmpdata[pos], len);
			return pos;

		} break;
		// misc types
		case Variant::_RID:
		case Variant::OBJECT: {

			return _pack(Variant(), tmpdata, string_cache);
		} break;
		case Variant::DICTIONARY: {

			Dictionary d = p_data;
			//size is known, use sort
			uint32_t pos = tmpdata.size();
			int len = d.size();
			tmpdata.resize(tmpdata.size() + len * 12 + 8);
			encode_uint32(TYPE_DICT, &tmpdata[pos + 0]);
			encode_uint32(len, &tmpdata[pos + 4]);

			List<Variant> keys;
			d.get_key_list(&keys);
			List<DictKey> sortk;

			for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {

				DictKey dk;
				dk.hash = E->get().hash();
				dk.key = E->get();
				sortk.push_back(dk);
			}

			sortk.sort();

			int idx = 0;
			for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {

				encode_uint32(E->get().hash, &tmpdata[pos + 8 + idx * 12 + 0]);
				uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
				encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 4]);
				ofs = _pack(d[E->get().key], tmpdata, string_cache);
				encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 8]);
				idx++;
			}

			return pos;

		} break;
		case Variant::ARRAY: {

			Array a = p_data;
			//size is known, use sort
			uint32_t pos = tmpdata.size();
			int len = a.size();
			tmpdata.resize(tmpdata.size() + len * 4 + 8);
			encode_uint32(TYPE_ARRAY, &tmpdata[pos + 0]);
			encode_uint32(len, &tmpdata[pos + 4]);

			for (int i = 0; i < len; i++) {

				uint32_t ofs = _pack(a[i], tmpdata, string_cache);
				encode_uint32(ofs, &tmpdata[pos + 8 + i * 4]);
			}

			return pos;

		} break;

		default: {}
	}

	return OK;
}
/**
 * @brief Encode a vfr_hud struct
 *
 * @param system_id ID of this system
 * @param component_id ID of this component (e.g. 200 for IMU)
 * @param msg The MAVLink message to compress the data into
 * @param vfr_hud C-struct to read the message contents from
 */
uint16_t _encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_vfr_hud_t* vfr_hud)
{
	return _pack(system_id, component_id, msg, vfr_hud->airspeed, vfr_hud->groundspeed, vfr_hud->heading, vfr_hud->throttle, vfr_hud->alt, vfr_hud->climb);
}
TCPSocket*
ModuleMap::getMapLoadingSocket( uint32 mapID,
                                uint32 loadMapRequestType,
                                const char* handshake,
                                byte zoomlevel,
                                MapSafeVector* loadedMaps,
                                uint32* mapVersion,
                                uint32* generatorVersion )
{
   uint32 mapVersionToUse = MAX_UINT32;
   uint32 generatorVersionToUse = MAX_UINT32;
   if ( mapVersion != NULL && generatorVersion != NULL ) {
      // Set the versions to use to the ones sent in
      mapVersionToUse = *mapVersion;
      generatorVersionToUse = *generatorVersion;
      // Set the ones sent in to MAX_UINT32 to detect errors
      *mapVersion = MAX_UINT32;
      *generatorVersion = MAX_UINT32;
   }
   
   DatagramReceiver receiver(8000, DatagramReceiver::FINDFREEPORT);
   
   mc2dbg4 << here << " localport " << receiver.getPort() << endl;

   uint32 mapip = MultiCastProperties::getNumericIP( MODULE_TYPE_MAP, true );
   uint16 mapport = MultiCastProperties::getPort( MODULE_TYPE_MAP, true );

   // check map set
   uint32 mapSet = Properties::getMapSet();
   if (mapSet != MAX_UINT32) {
      // code also exists in PacketContainer.cpp, move to utility function?
      mc2dbg4 << "[ModuleMap] going to change IP and port due to mapSet being set. "
                "mapSet: " << mapSet << ", IP before: " << prettyPrintIP(mapip)
             << ", port before: " << mapport << endl;
      mapip = mapip + (mapSet << 8);
      mapport = mapport | (mapSet << 13);
      mc2dbg4 << "[ModuleMap] changed IP and port. IP now: " << prettyPrintIP(mapip)
             << ", port now: " << mapport << endl;
   }
   
   uint32 status = StringTable::NOT;
   int maxRetries = 10;
   int nbrRetries = 0;

   Packet _pack(65536); // For receiving the mapreply
   DatagramSender sock;

   const int originalwaittime    =  2500000; // Wait 2.5 seconds.
   const int mapnotfoundwaittime =  2500000; // Wait 2.5 seconds.
   int waittime = originalwaittime;

   TCPSocket* TCPsock = NULL;
   
   while ( status != StringTable::OK && nbrRetries++ <= maxRetries ) {
      if ( nbrRetries > 1 ) {
         mc2log << info << here << " Retrying " << nbrRetries - 1 
                << " of " << maxRetries << endl;
      }
      if(loadedMaps != NULL)
         loadedMaps->jobThreadIsAlive(); // reset Jthread timeout clock.
      MapRequestPacket reqpack( uint16(1),  // reqID
                                uint16(1),  // PacketID
                                byte(loadMapRequestType), // Type of module
                                mapID,      // mapID
                                zoomlevel,  // Well, zoomlevel.
                                mapVersionToUse,
                                generatorVersionToUse
                                ); 
      reqpack.setOriginIP( NetUtility::getLocalIP() );
      reqpack.setOriginPort( receiver.getPort() );
      reqpack.setResendNbr((byte) nbrRetries-1);
      
      // Send request to open TCP connection between local and mapmodule
      
      if (!(sock.send(&reqpack, mapip, mapport))) {
         mc2log << error << here << " could not send - retrying"
                << endl;
         continue; // Go another round in the loop.
      }
      mc2dbg4 << "After send!" << endl;
      
      // Receive packet with ip and port to a mapModule
      if (!(receiver.receive(&_pack, waittime))) {
         mc2log << error << here << " error receiving ack - retrying"
                << endl;
         waittime = originalwaittime;
         continue; // Go another round in the loop.
      }

      bool timeout = false;
      while ( _pack.getSubType() == Packet::PACKETTYPE_ACKNOWLEDGE &&
              ! timeout ) {
         AcknowledgeRequestReplyPacket* ackPack =
            static_cast<AcknowledgeRequestReplyPacket*>(&_pack);
         uint32 packtime =
            ((AcknowledgeRequestReplyPacket*)&_pack)->getETA() * 1000;
         mc2log << info << "Got ack with " << packtime << " us delay" << endl;
         if ( ackPack->getStatus() != StringTable::OK ) {
            return NULL;
         }
         timeout = !receiver.receive(&_pack, packtime);         
      }

      if ( timeout ) {
         mc2log << error << "Got timeout after receiving ack-pack" << endl;
         continue; // Go around again
      }

      if ( _pack.getSubType() != Packet::PACKETTYPE_MAPREPLY ) {
         mc2log << error << "Got packet with subtype "
                << _pack.getSubTypeAsString()
                << " when expecting loadmapreply" << endl;
         continue; // Please try again.
      }
      
      MapReplyPacket* pack = (MapReplyPacket *)&_pack;
      
      status = pack->getStatus();
      
      if ( status != StringTable::OK || pack->getMapID() != mapID ) {
         mc2log << warn << "Got status \""
                << StringTable::getString(
                   StringTable::stringCode(pack->getStatus()),
                   StringTable::ENGLISH)
                << "\"-retrying. Wanted map = "
                << mapID
                << ", got reply for map = " << pack->getMapID() << endl;
         if ( status == StringTable::MAPNOTFOUND ) {
            // Wait longer if map not found ( loading? )
            waittime = mapnotfoundwaittime; 
         }
         if ( mapID != pack->getMapID() ) {
            status = StringTable::NOT; // Keep the loop running.
         }
         continue; // Go another round in the loop.
      }

      if ( mapVersion != NULL && generatorVersion != NULL ) {
         // Set the versions so that we know what we are caching.
         mc2dbg8 << "[ModuleMap]: Version from packet "
                 << hex << pack->getMapVersion() << ":"
                 << pack->getGeneratorVersion() << dec << endl;
         *mapVersion       = pack->getMapVersion();
         *generatorVersion = pack->getGeneratorVersion();

         // Check if new map is needed.
         if ( ! pack->newMapNeeded(mapVersionToUse,
                                   generatorVersionToUse) ) {
            // Same version. Use the cached map.
            return NULL;
         } 
      }
      
      TCPsock = new TCPSocket;      
      mc2dbg4 << here << " opening socket" << endl;
      TCPsock->open();
      
      uint32 ip = pack->getReplyIP();
      uint16 port = pack->getReplyPort();
      
      if ( ! TCPsock->connect(ip, port ) ) {
         mc2log << error << "Couldn't connect to " << ip
                << ":" << port << " - retrying" << endl;
         // Set status to not ok.
         status = StringTable::NOT;
         TCPsock->close();
         delete TCPsock;
         TCPsock = NULL;                  
         continue; // Please try again.
      }
      
      // Handshaking
      mc2dbg4 << "Starting handshake" << endl;
      int length = strlen(handshake) + 1;
      if ( TCPsock->writeAll( (byte*)handshake, length ) != length ) {
         mc2log << error << here << " handshake failed " << endl;
         status = StringTable::NOT;
         TCPsock->close();
         delete TCPsock;
         TCPsock = NULL;
         continue; // Please try again.
      } else {
         mc2dbg4 << "done" << endl;
      }
   }
   return TCPsock; // Should be NULL if we failed
}