Пример #1
0
static bool WiiLoad (s32 connection)
	{
	char wiiloadVersion[2];
	unsigned char header[9];
	size_t buffsize;

	wiiload.status = WIILOAD_RECEIVING;
			
	printopt ("WiiLoad begin");
	
	NetRead(connection, header, 8, 250);

	if (memcmp(header, "HAXX", 4) != 0) // unsupported protocol
		{
		printopt ("unsupported protocol");
		return false;
		}
		
	LWP_SetThreadPriority (networkthread, PRIO_RCV);

	wiiloadVersion[0] = header[4];
	wiiloadVersion[1] = header[5];

	NetRead(connection, (u8 *) &buffsize, 4, 250);

	if (header[4] > 0 || header[5] > 4)
		{
		printopt ("compressed file !");
		NetRead(connection, (u8*) &uncfilesize, 4, 250); // Compressed protocol, read another 4 bytes
		}
	
	/*
	buff = (u8 *) malloc(buffsize);
	if (!buff)
		{
		printopt ("Not enough memory.");
		return false;
		}
	*/
	
	printopt ("Receiving file (%s)...", incommingIP);

	u32 done = 0;
	u32 blocksize = 4096;
	
	u8 *buff = malloc (blocksize);

	int retries = 10;
	int result;
	int isZip = 0;
	
	FILE *f;
	char path[300];
	sprintf (path, "%s/%s", tpath, WIILOAD_TMPFILE);
	f = fopen (path, "wb");

	do
		{
		if (blocksize > buffsize - done)
			blocksize = buffsize - done;

		result = net_read(connection, buff, blocksize);
		if (result <= 0)
			{
			--retries;
			usleep (100000); // lets wait 10 msec
			}
		else
			{
			if (f) fwrite (buff, 1, result, f);
			
			retries = 10;
			
			if (done == 0 && (buff[0] == 'P' && buff[1] == 'K' && buff[2] == 0x03 && buff[3] == 0x04))
				{
				Debug ("This is a ZIP file");
				isZip = 1;
				}
				
			done += result;
			}

		if (retries == 0)
			{
			free (buff);
			buff = 0;
			buffsize = 0;
			if (f) fclose(f);
			printopt ("Transfer failed.");
			return false;
			}

		//printopt ("%d of %d bytes (r %d)", done, buffsize, retries);
		printopt ("!%d%c", (done * 100) / buffsize, 37);
		//fflush(stdout);
		} 
	while (done < buffsize);
	
	if (f) fclose(f);
	
	if (done != buffsize)
		{
		wiiload.status = WIILOAD_IDLE;
		//free (buff);
		//buffsize = 0;
		//buff = 0;
		printopt("Filesize doesn't match.");
		return false;
		}

	// These are the arguments....
	char temp[1024];
	int ret = NetRead(connection, (u8 *) temp, 1023, 250);
	temp[ret] = 0;
	
	//Debug_hexdump (temp, ret);
	
	if (ret > 2 && temp[ret - 1] == '\0' && temp[ret - 2] == '\0') // Check if it is really an arg
		{
		wiiload.args = malloc (ret);
		if (wiiload.args)
			{
			memcpy (wiiload.args, temp, ret);

			// convert arguments in ; separated items
			int i;
			for (i = 0; i < ret; i++)
				{
				if (wiiload.args[i] == '\0' && wiiload.args[i+1] != '\0')
					wiiload.args[i] = ';';
				}

			wiiload.argl = ret;
			}
		}

	temp[ret] = 0;

	printopt("Filename %s (%d)", temp, ret);
	strcpy (wiiload.filename, temp);
	
	wiiload.status = WIILOAD_IDLE;
	UncompressData (wiiloadVersion, isZip);
	
	free (buff);
	
	LWP_SetThreadPriority (networkthread, PRIO_IDLE);
		
	return true;
	}
Пример #2
0
const u8 * NetReceiver::ReceiveData()
{
	if(connection < 0)
		return NULL;

	if(filebuffer)
		free(filebuffer);
	filebuffer = NULL;

	filebuffer = (u8 *) malloc(filesize);
	if(!filebuffer)
	{
		ShowError(tr("Not enough memory."));
		return NULL;
	}

	StartProgress(tr("Receiving file..."));

	u32 done = 0;
	u32 blocksize = 5*1024;
	char tmptxt[200];
	snprintf(tmptxt, sizeof(tmptxt), "Incomming from: %s", incommingIP);

	int retries = 5;

	do
	{
		if(ProgressWindow::Instance()->IsCanceled())
		{
			FreeData();
			StopProgress();
			ShowError(tr("Transfer cancelled."));
			return NULL;
		}

		if (blocksize > filesize - done)
			blocksize = filesize - done;

		ShowProgress(done, filesize, tmptxt);

		int result = network_read(connection, filebuffer+done, blocksize);
		if(result < 0)
		{
			--retries;
			if(retries == 0)
			{
				FreeData();
				StopProgress();
				ShowError(tr("Transfer failed."));
				return NULL;
			}
		}
		if(!result)
		{
			--retries;
			if(!retries)
				break;
		}

		done += result;

	} while(done < filesize);

	// finish up the progress
	FinishProgress(filesize);

	StopProgress();

	if(done != filesize)
	{
		FreeData();
		ShowError(tr("Filesize doesn't match."));
	}

	char temp[50];
	network_read(connection, (u8 *) temp, 49);
	temp[49] = 0;

	snprintf(FileName, sizeof(FileName), "%s", temp);

	if(UncompressData() == NULL)
	{
		FreeData();
		ShowError(tr("Could not decompress the file."));
	}

	return filebuffer;
}
Пример #3
0
static bool GeckoLoad (void)
	{
	int compressed = 0;
	int ret;
	char wiiloadVersion[2];
	unsigned char header[16];
	size_t buffsize;
	u8 *buff;
	
	memset (header, 0, sizeof (header));
	ret = GeckoRead(EXI_CHANNEL_1, header, 16, 1000);
	if (ret < 16)
		return false;

	if (memcmp(header, "HAXX", 4) != 0) // unsupported protocol
		{
		usb_flush (EXI_CHANNEL_1);
		return false;
		}
		
	LWP_SetThreadPriority (geckothread, PRIO_RCV);

	wiiloadVersion[0] = header[4];
	wiiloadVersion[1] = header[5];
	
	memcpy ((u8 *) &buffsize, &header[8], 4);
	if (header[4] > 0 || header[5] > 4)
		{
		//printopt ("compressed file !");
		compressed = 1;
		memcpy ((u8 *) &uncfilesize, &header[12], 4);
		}
	
	buff = NULL;

	buff = (u8 *) malloc(buffsize);
	if (!buff)
		{
		usb_flush (EXI_CHANNEL_1);
		printopt ("Not enough memory.");
		return false;
		}

	LWP_SetThreadPriority(geckothread, 64);
	//printopt ("Receiving file (%s)...", incommingIP);

	u32 done = 0;
	u32 blocksize = 1024*16;
	int result;
	
	if (!compressed) // if the file isn't compressed, 4 bytes of file are in the header vect...
		{
		memcpy ((u8 *) buff, &header[12], 4);
		done += 4;
		}

	do
		{
		if (blocksize > buffsize - done)
			blocksize = buffsize - done;

		result = GeckoRead(EXI_CHANNEL_1, buff+done, blocksize, 1000);
		if (result > 0)
			done += result;
		else
			break;

		printopt ("!%d%c", (done * 100) / buffsize, 37);
		} 
	while (done < buffsize);
	
	LWP_SetThreadPriority(geckothread, 8);
	
	if (done != buffsize)
		{
		wiiload.status = WIILOAD_IDLE;
		free (buff);
		buffsize = 0;
		buff = 0;
		printopt("Filesize doesn't match.");
		return false;
		}

	// These are the arguments....
	char temp[1024];
	ret = GeckoRead(EXI_CHANNEL_1, (u8 *) temp, 1023, 1000);
	
	temp[ret] = 0;
	
	if (ret > 2 && temp[ret - 1] == '\0' && temp[ret - 2] == '\0') // Check if it is really an arg
		{
		wiiload.args = malloc (ret);
		if (wiiload.args)
			{
			memcpy (wiiload.args, temp, ret);
			
			// convert arguments in ; separated items
			int i;
			for (i = 0; i < ret; i++)
				{
				if (wiiload.args[i] == '\0' && wiiload.args[i+1] != '\0')
					wiiload.args[i] = ';';
				}

			wiiload.argl = ret;
			}
		}
	temp[ret] = 0;

	printopt("Filename %s (%d)", temp, ret);
	strcpy (wiiload.filename, temp);
	
	int isZip = 0;
	if (buff[0] == 'P' && buff[1] == 'K' && buff[2] == 0x03 && buff[3] == 0x04)
		isZip = 1;

	// to keep compatibility with wiiload over network, we must store the file on device...
	char path[256];
	sprintf (path, "%s/%s", tpath, WIILOAD_TMPFILE);

	//mt_Lock();
	ret = fsop_WriteFile (path, buff, buffsize);
	//mt_Unlock();
	
	free (buff);
	buff = NULL;
	
	if (ret)
		{
		UncompressData (wiiloadVersion, isZip);
		}
		
	free (buff);
	
	LWP_SetThreadPriority (geckothread, PRIO_IDLE);
	
	return true;
	}