Exemplo n.º 1
0
/* AM 980919 updated */
int osd_fchecksum (const char *game, const char *filename, unsigned int *length, unsigned int *sum)
{
	char name[256];
	int indx;
	struct stat stat_buffer;
	int found = 0;
	const char *gamename = game;

	/* Support "-romdir" yuck. */
	if (alternate_name)
		gamename = alternate_name;

	for (indx=0;indx<rompathc && !found; ++indx) {
		const char* dir_name = rompathv[indx];

		if (!found) {
			sprintf(name,"%s/%s",dir_name,gamename);
			if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
				sprintf(name,"%s/%s/%s",dir_name,gamename,filename);
				if (checksum_file(name,0,length,sum)==0) {
					found = 1;
				}
			}
		}

		if (!found) {
			/* try with a .zip extension */
			sprintf(name,"%s/%s.zip", dir_name, gamename);
			if (cache_stat(name,&stat_buffer)==0) {
				if (checksum_zipped_file (name, filename, length, sum)==0) {
					if (errorlog)
						fprintf(errorlog,"Using (osd_fchecksum) zip file for %s\n", filename);
					found = 1;
				}
			}
		}

		if (!found) {
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf(name,"%s/%s.zif",dir_name,gamename);
			if (cache_stat(name,&stat_buffer)==0) {
				sprintf(name,"%s/%s.zif/%s",dir_name,gamename,filename);
				if (checksum_file(name,0,length,sum)==0) {
					found = 1;
				}
			}
		}
	}

	if (!found)
		return -1;

	return 0;
}
Exemplo n.º 2
0
/* AM 980919 updated */
int osd_fchecksum (const char *game, const char *filename, unsigned int *length, unsigned int *sum)
{
	char name[256];
	int found = 0;
	const char *gamename = game;

	if (!found) {
		/* try with a .zip extension */
		sprintf(name,"%s.zip\0", gamename);
		/*if (cache_stat(name)==0)*/ {
			sprintf(name,"%sroms/%s.zip\0", gp2x_path_mame, gamename);
			if (checksum_zipped_file (name, filename, length, sum)==0) {
				found = 1;
			}
		}
	}
	if (!found) {
		if (cache_stat((char *)gamename)==0) {
			sprintf(name,"%sroms/%s/%s\0",gp2x_path_mame, gamename, filename);
			if (checksum_file(name,0,length,sum)==0) {
				found = 1;
			}
		}
	}


	if (!found)
		return -1;

	return 0;
}
Exemplo n.º 3
0
Arquivo: psort.c Projeto: BWK/os161
static
void
genkeys(void)
{
	long seedspace[numprocs];
	int i;

	/* Create the file. */
	docreate(PATH_KEYS);

	/* Generate random seeds for each subprocess. */
	srandom(randomseed);
	for (i=0; i<numprocs; i++) {
		seedspace[i] = random();
	}

	/* Do it. */
	seeds = seedspace;
	doforkall("Initialization", genkeys_sub);
	seeds = NULL;

	/* Cross-check the size of the output. */
	if (getsize(PATH_KEYS) != correctsize) {
		complainx("%s: file is wrong size", PATH_KEYS);
		exit(1);
	}

	/* Checksum the output. */
	checksum = checksum_file(PATH_KEYS);
	complainx("Checksum of unsorted keys: %ld", checksum);
}
int CommonBigDigest(int argc, char *const *argv)
{

    plan_tests(kTestTestCount);

    char *testpath = "/Volumes/Data/Users/murf/Downloads/Zin_12A130_AppleInternal_038-2423-191.dmg";
    checksum_file(testpath, kCCDigestSHA1);

    return 0;
}
Exemplo n.º 5
0
Arquivo: psort.c Projeto: BWK/os161
static
void
sort(void)
{
	unsigned long sortedsum;
	int i, j;

	/* Step 1. Toss into bins. */
	doforkall("Tossing", bin);
	checksize_bins();
	complainx("Done tossing into bins.");

	/* Step 2: Sort the bins. */
	doforkall("Sorting", sortbins);
	checksize_bins();
	complainx("Done sorting the bins.");

	/* Step 3: Merge corresponding bins. */
	doforkall("Merging", mergebins);
	checksize_merge();
	complainx("Done merging the bins.");

	/* Step 3a: delete the bins */
	for (i=0; i<numprocs; i++) {
		for (j=0; j<numprocs; j++) {
			doremove(binname(i, j));
		}
	}

	/* Step 4: assemble output file */
	docreate(PATH_SORTED);
	doforkall("Final assembly", assemble);
	if (getsize(PATH_SORTED) != correctsize) {
		complainx("%s: file is wrong size", PATH_SORTED);
		exit(1);
	}

	/* Step 4a: delete the merged bins */
	for (i=0; i<numprocs; i++) {
		doremove(mergedname(i));
	}

	/* Step 5: Checksum the result. */
	sortedsum = checksum_file(PATH_SORTED);
	complainx("Checksum of sorted keys: %ld", sortedsum);

	if (sortedsum != checksum) {
		complainx("Sums do not match");
		exit(1);
	}
}
Exemplo n.º 6
0
static int
has_checksum_changed(void)
{
    int changed = 0;
    for (int i = 0; i < file_info_u; ++i) {
        struct file_info *cur = &file_infos[i];
        unsigned char checksum_buf[1024];
        if (checksum_file(cur->path, checksum_buf, sizeof(checksum_buf)) < 0) {
            changed = 1;
            printf("File '%s' no longer exists\n", cur->path);
        } else if (strcmp(checksum_buf, cur->summ) != 0) {
            changed = 1;
            printf("File '%s' is changed\n", cur->path);
        }
    }
    return changed;
}
Exemplo n.º 7
0
/**************************************
Return RET_NOK on error
**************************************/
ret_code_t parse_incoming_data(context_t * context, Uint32 command, Uint32 command_size, char * data)
{
	char * value = NULL;
	char * fullname;
	char * elements[512];
	char * cksum;
	int i;
	char * user_name;
	char * password;

	if( !context_get_connected(context) && ( command != CMD_REQ_LOGIN  && command != CMD_REQ_FILE) ) {
		werr(LOGUSER,"Request from not authenticated client, close connection");
		return RET_NOK;
	}

	switch(command) {
	case CMD_REQ_LOGIN:
		wlog(LOGDEBUG,"Received CMD_REQ_LOGIN");
		user_name = _strsep(&data,NETWORK_DELIMITER);
		password = _strsep(&data,NETWORK_DELIMITER);

		if(entry_read_string(PASSWD_TABLE, user_name, &value, PASSWD_KEY_PASSWORD,NULL) == RET_NOK) {
			return RET_NOK;
		}
		if( strcmp(value, password) != 0) {
			free(value);
			werr(LOGUSER,"Wrong login for %s",user_name);
			// send answer
			network_send_command(context, CMD_SEND_LOGIN_NOK, 0, NULL, false);
			// force client disconnection
			return RET_NOK;
		} else {
			free(value);

			if( context_set_username(context, user_name) == RET_NOK ) {
				return RET_NOK;
			}
			context_set_connected(context, true);

			// send answer
			network_send_command(context, CMD_SEND_LOGIN_OK, 0, NULL, false);
			wlog(LOGUSER,"Login successful for user %s",context->user_name);
		}
		break;
	case CMD_REQ_CHARACTER_LIST :
		wlog(LOGDEBUG,"Received CMD_REQ_CHARACTER_LIST");
		character_send_list(context);
		wlog(LOGDEBUG,"character list sent");
		break;
	case CMD_REQ_FILE :
		i = 0;
		elements[i] = _strsep(&data,NETWORK_DELIMITER);
		while(elements[i]) {
			i++;
			elements[i] = _strsep(&data,NETWORK_DELIMITER);
		}

		if(elements[0]==NULL || elements[1]==NULL) {
			werr(LOGDEV,"Received erroneous CMD_REQ_FILE");
			break;
		}
		wlog(LOGDEBUG,"Received CMD_REQ_FILE for %s",elements[0]);
		/* compare checksum */
		fullname = strconcat(base_directory,"/",elements[0],NULL);

		cksum = checksum_file(fullname);
		free(fullname);

		if( cksum == NULL) {
			werr(LOGUSER,"Required file %s doesn't exists",elements[0]);
			break;
		}

		if( strcmp(elements[1],cksum) == 0 ) {
			wlog(LOGDEBUG,"Client has already newest %s file",elements[0]);
			free(cksum);
			break;
		}
		free(cksum);

		network_send_file(context,elements[0]);
		wlog(LOGDEBUG,"File %s sent",elements[0]);
		break;
	case CMD_REQ_USER_CHARACTER_LIST :
		wlog(LOGDEBUG,"Received CMD_REQ_USER_CHARACTER_LIST");
		character_user_send_list(context);
		wlog(LOGDEBUG,"user %s's character list sent",context->user_name);
		break;
	case CMD_REQ_START :
		if( context->in_game == false ) {
			context->id = strdup(data);
			context->in_game = true;
			context_update_from_file(context);
			context_spread(context);
			context_request_other_context(context);
		}
		wlog(LOGDEBUG,"Received CMD_REQ_START for %s /%s",context->user_name,context->id);
		break;
	case CMD_REQ_STOP :
		wlog(LOGDEBUG,"Received CMD_REQ_STOP for %s /%s",context->user_name,context->id);
		if( context->in_game == true ) {
			context->in_game = false;
			if( context->map ) {
				free(context->map);
			}
			context->map = NULL;
			if( context->prev_map ) {
				free(context->prev_map);
			}
			context->prev_map = NULL;
			if( context->id ) {
				free(context->id);
			}
			context->id = NULL;
			context_spread(context);
		}
		break;
	case CMD_REQ_ACTION :
		i = 0;
		elements[i] = NULL;
		elements[i] = _strsep(&data,NETWORK_DELIMITER);
		while(elements[i]) {
			i++;
			elements[i] = _strsep(&data,NETWORK_DELIMITER);
		}
		elements[i+1] = NULL;

		wlog(LOGDEBUG,"Received CMD_REQ_ACTION %s from %s /%s",elements[0],context->user_name,context->character_name);

		action_execute(context,elements[0],&elements[1]);
		break;
	default:
		werr(LOGDEV,"Unknown request %d from client",command);
		return RET_NOK;
	}

	return RET_OK;
}
Exemplo n.º 8
0
/* AM 980919 update */
void *osd_fopen (const char *game, const char *filename, int filetype, int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	int indx;
	struct stat stat_buffer;
	FakeFileHandle *f;
	int pathc;
	char **pathv;


	f = (FakeFileHandle *) malloc(sizeof (FakeFileHandle));
	if( !f )
	{
		logerror("osd_fopen: failed to mallocFakeFileHandle!\n");
        return 0;
	}
	memset (f, 0, sizeof (FakeFileHandle));

	gamename = (char *) game;

	/* Support "-romdir" yuck. */
	if( alternate_name )
	{
		LOG(("osd_fopen: -romdir overrides '%s' by '%s'\n", gamename, alternate_name));
        gamename = alternate_name;
	}

	switch( filetype )
	{
	case OSD_FILETYPE_ROM:
	case OSD_FILETYPE_SAMPLE:

		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}

		if( filetype == OSD_FILETYPE_SAMPLE )
		{
			LOG(("osd_fopen: using samplepath\n"));
            pathc = samplepathc;
            pathv = samplepathv;
        }
		else
		{
			LOG(("osd_fopen: using rompath\n"));
            pathc = rompathc;
            pathv = rompathv;
		}

		for( indx = 0; indx < pathc && !found; ++indx )
		{
			const char *dir_name = pathv[indx];

			if( !found )
			{
				sprintf (name, "%s/%s", dir_name, gamename);
				LOG(("Trying %s\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s/%s", dir_name, gamename, filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip extension */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s file\n", name));
                if( cache_stat (name, &stat_buffer) == 0 )
				{
					if( load_zipped_file (name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s directory\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s.zip/%s", dir_name, gamename, filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}
		}

		break;

#ifdef MESS
	case OSD_FILETYPE_IMAGE_R:

		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
        else
		{
			LOG(("osd_fopen: using rompath\n"));
            pathc = rompathc;
            pathv = rompathv;
		}

		LOG(("Open IMAGE_R '%s' for %s\n", filename, game));
        for( indx = 0; indx < pathc && !found; ++indx )
		{
			const char *dir_name = pathv[indx];

			/* this section allows exact path from .cfg */
			if( !found )
			{
				sprintf(name,"%s",dir_name);
				if( cache_stat(name,&stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf(name,"%s/%s",dir_name,filename);
					if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file (name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen(name,"rb");
						found = f->file!=0;
					}
				}
			}

			if( !found )
			{
				sprintf (name, "%s/%s", dir_name, gamename);
				LOG(("Trying %s directory\n", name));
                if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
				{
					sprintf (name, "%s/%s/%s", dir_name, gamename, filename);
					LOG(("Trying %s file\n", name));
                    if( filetype == OSD_FILETYPE_ROM )
					{
						if( checksum_file(name, &f->data, &f->length, &f->crc) == 0 )
						{
							f->type = kRAMFile;
							f->offset = 0;
							found = 1;
						}
					}
					else
					{
						f->type = kPlainFile;
						f->file = fopen (name, "rb");
						found = f->file != 0;
					}
				}
			}

			/* Zip cart support for MESS */
			if( !found && filetype == OSD_FILETYPE_IMAGE_R )
			{
				char *extension = strrchr (name, '.');    /* find extension */
				if( extension )
					strcpy (extension, ".zip");
				else
					strcat (name, ".zip");
				LOG(("Trying %s file\n", name));
				if( cache_stat(name, &stat_buffer) == 0 )
				{
					if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}

			if( !found )
			{
				/* try with a .zip extension */
				sprintf (name, "%s/%s.zip", dir_name, gamename);
				LOG(("Trying %s file\n", name));
				if( cache_stat(name, &stat_buffer) == 0 )
				{
					if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file for %s\n", filename));
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
    	}
    break; /* end of IMAGE_R */

	case OSD_FILETYPE_IMAGE_RW:
		{
			static char *write_modes[] = {"rb","wb","r+b","r+b","w+b"};
            char file[256];
			char *extension;

			LOG(("Open IMAGE_RW '%s' for %s mode '%s'\n", filename, game, write_modes[_write]));
			strcpy (file, filename);

			do
            {
			/* 29-05-00 Lee Ward: Reversed the search order. */
            for (indx=rompathc-1; indx>=0; --indx)
			{
				const char *dir_name = rompathv[indx];

					/* Exact path support */

					/* 29-05-00 Lee Ward: Changed the search order to prevent new files
					   being created in the application root as default */

					if( !found )
					{
						sprintf (name, "%s/%s", dir_name, gamename);
						LOG(("Trying %s directory\n", name));
						if( cache_stat(name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf (name, "%s/%s/%s", dir_name, gamename, file);
							LOG(("Trying %s file\n", name));
                                                        f->file = fopen (name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                                                         }
                                                 }
					}

					/* Steph - Zip disk images support for MESS */
					if( !found && !_write )
					{
						extension = strrchr (name, '.');    /* find extension */
						/* add .zip for zipfile */
						if( extension )
							strcpy(extension, ".zip");
						else
							strcat(extension, ".zip");
						LOG(("Trying %s file\n", name));
						if( cache_stat(name, &stat_buffer) == 0 )
						{
							if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
							{
								LOG(("Using (osd_fopen) zip file for %s\n", filename));
								f->type = kZippedFile;
								f->offset = 0;
								f->crc = crc32(0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if (!found)
					{
						sprintf(name, "%s", dir_name);
						LOG(("Trying %s directory\n", name));
						if( cache_stat(name,&stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf(name,"%s/%s", dir_name, file);
							LOG(("Trying %s file\n", name));
                                                        f->file = fopen(name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                                                         }
						}
					}

                    if( !found && !_write )
                    {
                        extension = strrchr (name, '.');    /* find extension */
                        /* add .zip for zipfile */
                        if( extension )
                            strcpy(extension, ".zip");
                        else
                            strcat(extension, ".zip");
						LOG(("Trying %s file\n", name));
						if( cache_stat(name, &stat_buffer) == 0 )
                        {
							if( load_zipped_file(name, filename, &f->data, &f->length) == 0 )
                            {
                                LOG(("Using (osd_fopen) zip file for %s\n", filename));
                                f->type = kZippedFile;
                                f->offset = 0;
								f->crc = crc32(0L, f->data, f->length);
                                found = 1;
                            }
                        }
                    }

					if( !found && !_write )
					{
						/* try with a .zip extension */
						sprintf (name, "%s/%s.zip", dir_name, gamename);
						LOG(("Trying %s file\n", name));
						if( cache_stat (name, &stat_buffer) == 0 )
						{
							if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
							{
								LOG(("Using (osd_fopen) zip file for %s\n", filename));
								f->type = kZippedFile;
								f->offset = 0;
								f->crc = crc32 (0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if( !found )
					{
						/* try with a .zip directory (if ZipMagic is installed) */
						sprintf (name, "%s/%s.zip", dir_name, gamename);
						LOG(("Trying %s ZipMagic directory\n", name));
						if( cache_stat (name, &stat_buffer) == 0 && (stat_buffer.st_mode & S_IFDIR) )
						{
							sprintf (name, "%s/%s.zip/%s", dir_name, gamename, file);
							LOG(("Trying %s\n", name));
							f->file = fopen (name, write_modes[_write]);
							found = f->file != 0;
							if( !found && _write == 3 )
							{
								f->file = fopen(name, write_modes[4]);
								found = f->file != 0;
                            }
                        }
					}
					if( found )
						LOG(("IMAGE_RW %s FOUND in %s!\n", file, name));
				}

				extension = strrchr (file, '.');
				if( extension )
					*extension = '\0';
			} while( !found && extension );
		}
		break;
#endif	/* MESS */


	case OSD_FILETYPE_NVRAM:
		if( !found )
		{
			sprintf (name, "%s/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.nv", nvdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}
		break;

	case OSD_FILETYPE_HIGHSCORE:
		if( mame_highscore_enabled () )
		{
			if( !found )
			{
				sprintf (name, "%s/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}

			if( !found )
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf (name, "%s.zip/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}

			if( !found )
			{
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf (name, "%s.zif/%s.hi", hidir, gamename);
				f->type = kPlainFile;
				f->file = fopen (name, _write ? "wb" : "rb");
				found = f->file != 0;
			}
		}
		break;

    case OSD_FILETYPE_CONFIG:
		sprintf (name, "%s/%s.cfg", cfgdir, gamename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;

		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.cfg", cfgdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.cfg", cfgdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}
		break;

	case OSD_FILETYPE_INPUTLOG:
		sprintf (name, "%s/%s.inp", inpdir, gamename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;

        if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.cfg", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.cfg", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
        }

		if( !_write )
		{
			char file[256];
			sprintf (file, "%s.inp", gamename);
            sprintf (name, "%s/%s.zip", inpdir, gamename);
			LOG(("Trying %s in %s\n", file, name));
            if( cache_stat (name, &stat_buffer) == 0 )
			{
				if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
				{
					LOG(("Using (osd_fopen) zip file %s for %s\n", name, file));
					f->type = kZippedFile;
					f->offset = 0;
					found = 1;
				}
			}
		}

        break;

	case OSD_FILETYPE_STATE:
		sprintf (name, "%s/%s.sta", stadir, gamename);
		f->file = fopen (name, _write ? "wb" : "rb");
		found = !(f->file == 0);
		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.sta", stadir, gamename);
			f->file = fopen (name, _write ? "wb" : "rb");
			found = !(f->file == 0);
		}
		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.sta", stadir, gamename);
			f->file = fopen (name, _write ? "wb" : "rb");
			found = !(f->file == 0);
		}
		break;

	case OSD_FILETYPE_ARTWORK:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		sprintf (name, "%s/%s", artworkdir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		if( !found )
		{
			/* try with a .zip directory (if ZipMagic is installed) */
			sprintf (name, "%s.zip/%s.png", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
		}

		if( !found )
		{
			/* try with a .zif directory (if ZipFolders is installed) */
			sprintf (name, "%s.zif/%s.png", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen (name, _write ? "wb" : "rb");
			found = f->file != 0;
        }

		if( !found )
		{
			char file[256], *extension;
			sprintf(file, "%s", filename);
            sprintf(name, "%s/%s", artworkdir, filename);
            extension = strrchr(name, '.');
			if( extension )
				strcpy (extension, ".zip");
			else
				strcat (name, ".zip");
			LOG(("Trying %s in %s\n", file, name));
            if( cache_stat (name, &stat_buffer) == 0 )
			{
				if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
				{
					LOG(("Using (osd_fopen) zip file %s\n", name));
					f->type = kZippedFile;
					f->offset = 0;
					found = 1;
				}
			}
			if( !found )
			{
				sprintf(name, "%s/%s.zip", artworkdir, game);
				LOG(("Trying %s in %s\n", file, name));
				if( cache_stat (name, &stat_buffer) == 0 )
				{
					if( load_zipped_file (name, file, &f->data, &f->length) == 0 )
					{
						LOG(("Using (osd_fopen) zip file %s\n", name));
						f->type = kZippedFile;
						f->offset = 0;
						found = 1;
					}
				}
            }
        }
        break;

	case OSD_FILETYPE_MEMCARD:
		sprintf (name, "%s/%s", memcarddir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		break;

	case OSD_FILETYPE_SCREENSHOT:
		/* only for writing */
		if( !_write )
		{
			logerror("osd_fopen: type %02x read not supported\n",filetype);
			break;
		}

		sprintf (name, "%s/%s.png", screenshotdir, filename);
		f->type = kPlainFile;
		f->file = fopen (name, _write ? "wb" : "rb");
		found = f->file != 0;
		break;

	case OSD_FILETYPE_HIGHSCORE_DB:
	case OSD_FILETYPE_HISTORY:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (filename, _write ? "w" : "r");
		found = f->file != 0;
        break;

	/* Steph */
	case OSD_FILETYPE_CHEAT:
		sprintf (name, "%s/%s", cheatdir, filename);
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (filename, _write ? "a" : "r");
		found = f->file != 0;
        break;

	case OSD_FILETYPE_LANGUAGE:
		/* only for reading */
		if( _write )
		{
			logerror("osd_fopen: type %02x write not supported\n",filetype);
            break;
		}
		sprintf (name, "%s.lng", filename);
		f->type = kPlainFile;
		/* open as ASCII files, not binary like the others */
		f->file = fopen (name, _write ? "w" : "r");
		found = f->file != 0;
logerror("fopen %s = %08x\n",name,(int)f->file);
        break;

	default:
		logerror("osd_fopen(): unknown filetype %02x\n",filetype);
	}

	if( !found )
	{
		free(f);
		return 0;
	}

	return f;
}
Exemplo n.º 9
0
void *osd_fopen(const char *game,const char *filename,int filetype,int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	FakeFileHandle *f;

	f = (FakeFileHandle *)gp2x_malloc(sizeof(FakeFileHandle));
	if (!f)
		return 0;
	fast_memset(f,0,sizeof(FakeFileHandle));

	gamename = (char *)game;

	switch (filetype)
	{
		case OSD_FILETYPE_ROM:

			/* only for reading */
			if (_write)
				break;

			if (!found) {
				/* try with a .zip extension */
				sprintf(name,"%s.zip\0", gamename);
				/*if (cache_stat(name)==0)*/ {
					sprintf(name,"%sroms/%s.zip\0", gp2x_path_mame, gamename);
					if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
			if (!found) {
				if (cache_stat(gamename)==0) {
					sprintf(name,"%sroms/%s/%s\0",gp2x_path_mame, gamename, filename);
					if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
						f->type = kRAMFile;
						f->offset = 0;
						found = 1;
					}
				}
			}


			break;
		case OSD_FILETYPE_SAMPLE:
			if (!found) {
				/* try with a .zip extension */
				sprintf(name,"%s.zip\0", gamename);
				/*if (cache_stat_sample(name)==0)*/ {
					sprintf(name,"%ssamples/%s.zip\0", gp2x_path_mame, gamename);
					if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
						f->type = kZippedFile;
						f->offset = 0;
						f->crc = crc32 (0L, f->data, f->length);
						found = 1;
					}
				}
			}
			if (!found) {
				sprintf(name,"%ssamples/%s/%s\0",gp2x_path_mame, gamename, filename);
				if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
					f->type = kRAMFile;
					f->offset = 0;
					found = 1;
				}
			}
			break;
		case OSD_FILETYPE_HIGHSCORE:
			if (!found) {
				sprintf(name,"%shi/%s.hi", gp2x_path_mame, gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}
			break;
		case OSD_FILETYPE_CONFIG:
			break;
		case OSD_FILETYPE_INPUTLOG:
			break;
		case OSD_FILETYPE_STATE:
			break;
		case OSD_FILETYPE_ARTWORK:
			break;
	}

	if (!found) {
		gp2x_free(f);
		return 0;
	}

	return f;
}
Exemplo n.º 10
0
/* AM 980919 update */
void *osd_fopen(const char *game,const char *filename,int filetype,int _write)
{
	char name[256];
	char *gamename;
	int found = 0;
	int  indx;
	struct stat stat_buffer;
	FakeFileHandle *f;
	int pathc;
	char** pathv;
   #ifdef MESS
		char file[MAXPATHL];
		char *extension;
   #endif

	f = (FakeFileHandle *)malloc(sizeof(FakeFileHandle));
	if (!f)
		return 0;
    memset(f,0,sizeof(FakeFileHandle));

	gamename = (char *)game;

	/* Support "-romdir" yuck. */
	if (alternate_name)
		gamename = alternate_name;

	switch (filetype)
	{
		case OSD_FILETYPE_ROM:
		case OSD_FILETYPE_SAMPLE:
#ifdef MESS
      case OSD_FILETYPE_ROM_CART:
#endif

      /* only for reading */
			if (_write)
				break;

         if (filetype==OSD_FILETYPE_ROM
#ifdef MESS
         	|| OSD_FILETYPE_ROM_CART
#endif
			)
			{
				pathc = rompathc;
				pathv = rompathv;
			} else {
				pathc = samplepathc;
				pathv = samplepathv;
			}

         for (indx=0;indx<pathc && !found; ++indx) {
				const char* dir_name = pathv[indx];

				if (!found) {
					sprintf(name,"%s/%s",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
						sprintf(name,"%s/%s/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}

#ifdef MESS
				/* Zip cart support for MESS */
				if (!found && filetype == OSD_FILETYPE_ROM_CART)
				{
					extension = strrchr(name, '.');		/* find extension       */
					if (extension) *extension = '\0';	/* drop extension       */
					sprintf(name,"%s.zip", name);		/* add .zip for zipfile */
					if (cache_stat(name,&stat_buffer)==0) {
						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
							if (errorlog)
								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
							f->type = kZippedFile;
							f->offset = 0;
							f->crc = crc32 (0L, f->data, f->length);
							found = 1;
						}
					}
				}

#endif


				if (!found) {
					/* try with a .zip extension */
					sprintf(name,"%s/%s.zip", dir_name, gamename);
					if (cache_stat(name,&stat_buffer)==0) {
						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
							if (errorlog)
								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
							f->type = kZippedFile;
							f->offset = 0;
							f->crc = crc32 (0L, f->data, f->length);
							found = 1;
						}
					}
				}

				if (!found) {
					/* try with a .zip directory (if ZipMagic is installed) */
					sprintf(name,"%s/%s.zip",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
						sprintf(name,"%s/%s.zip/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}

/* ZipFolders support disabled for rom and sample load.
   There is no reason to keep it because now zip files are fully supported. */
#if 0
				if (!found) {
					/* try with a .zif directory (if ZipFolders is installed) */
					sprintf(name,"%s/%s.zif",dir_name,gamename);
					if (cache_stat(name,&stat_buffer)==0) {
						sprintf(name,"%s/%s.zif/%s",dir_name,gamename,filename);
						if (filetype==OSD_FILETYPE_ROM)	{
							if (checksum_file (name, &f->data, &f->length, &f->crc)==0) {
								f->type = kRAMFile;
								f->offset = 0;
								found = 1;
							}
						}
						else {
							f->type = kPlainFile;
							f->file = fopen(name,"rb");
							found = f->file!=0;
						}
					}
				}
#endif
         }
			break;
#ifdef MESS
      case OSD_FILETYPE_IMAGE:

			if(errorlog) fprintf(errorlog,"Open IMAGE '%s' for %s\n", filename, game);
            strcpy(file, filename);

			do {
				for (indx=0; indx < rompathc && !found; ++indx)
            {
					const char* dir_name = rompathv[indx];

					if (!found) {
						sprintf(name, "%s/%s", dir_name, gamename);
						if(errorlog) fprintf(errorlog,"Trying %s\n", name);
						if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
                            sprintf(name,"%s/%s/%s", dir_name, gamename, file);
							f->file = fopen(name,_write ? "r+b" : "rb");
							found = f->file!=0;
						}
					}

 /******************************************************/
 				/* Zip IMAGE support for MESS */
 				if (filetype == OSD_FILETYPE_IMAGE && !_write) {
 					extension = strrchr(name, '.');		/* find extension       */
 					if (extension) *extension = '\0';	/* drop extension       */
 					sprintf(name,"%s.zip", name);		/* add .zip for zipfile */
 					if (cache_stat(name,&stat_buffer)==0) {
 						if (load_zipped_file(name, filename, &f->data, &f->length)==0) {
 							if (errorlog)
 								fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
 							f->type = kZippedFile;
 							f->offset = 0;
 							f->crc = crc32 (0L, f->data, f->length);
 							found = 1;
 						}
 					}
 				}

/******************************************************/

					if (!found && !_write) {
						/* try with a .zip extension */
						sprintf(name, "%s/%s.zip", dir_name, gamename);
						if (errorlog) fprintf(errorlog,"Trying %s\n", name);
                  if (cache_stat(name,&stat_buffer)==0) {
							if (load_zipped_file(name, file, &f->data, &f->length)==0) {
								if (errorlog) fprintf(errorlog,"Using (osd_fopen) zip file for %s\n", filename);
								f->type = kZippedFile;
								f->offset = 0;
                                f->crc = crc32 (0L, f->data, f->length);
								found = 1;
							}
						}
					}

					if (!found) {
						/* try with a .zip directory (if ZipMagic is installed) */
						sprintf(name, "%s/%s.zip", dir_name, gamename);
						if (errorlog) fprintf(errorlog,"Trying %s\n", name);
                  if (cache_stat(name,&stat_buffer)==0 && (stat_buffer.st_mode & S_IFDIR)) {
							sprintf(name,"%s/%s.zip/%s",dir_name,gamename,file);
							f->file = fopen(name,_write ? "r+b" : "rb");
                            found = f->file!=0;
						}
					}
               if (found)
               {
               if (errorlog) fprintf(errorlog,"IMAGE %s FOUND in %s!\n",file,name);
               }

				}

                extension = strrchr(file, '.');
				if (extension) *extension = '\0';

			} while (!found && extension);
	      break;

#endif


		case OSD_FILETYPE_HIGHSCORE:
			if (mame_highscore_enabled()) {
				if (!found) {
					sprintf(name,"%s/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}

				if (!found) {
					/* try with a .zip directory (if ZipMagic is installed) */
					sprintf(name,"%s.zip/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}

				if (!found) {
					/* try with a .zif directory (if ZipFolders is installed) */
					sprintf(name,"%s.zif/%s.hi",hidir,gamename);
					f->type = kPlainFile;
					f->file = fopen(name,_write ? "wb" : "rb");
					found = f->file!=0;
				}
			}
			break;
		case OSD_FILETYPE_CONFIG:
			sprintf(name,"%s/%s.cfg",cfgdir,gamename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;

			if (!found) {
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf(name,"%s.zip/%s.cfg",cfgdir,gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}

			if (!found) {
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf(name,"%s.zif/%s.cfg",cfgdir,gamename);
				f->type = kPlainFile;
				f->file = fopen(name,_write ? "wb" : "rb");
				found = f->file!=0;
			}
			break;
		case OSD_FILETYPE_INPUTLOG:
			sprintf(name,"%s/%s.inp", inpdir, gamename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_STATE:
			sprintf(name,"%s/%s.sta",stadir,gamename);
			f->file = fopen(name,_write ? "wb" : "rb");
			found = !(f->file == 0);
			if (!found)
			{
				/* try with a .zip directory (if ZipMagic is installed) */
				sprintf(name,"%s.zip/%s.sta",stadir,gamename);
				f->file = fopen(name,_write ? "wb" : "rb");
				found = !(f->file == 0);
            }
			if (!found)
			{
				/* try with a .zif directory (if ZipFolders is installed) */
				sprintf(name,"%s.zif/%s.sta",stadir,gamename);
				f->file = fopen(name,_write ? "wb" : "rb");
				found = !(f->file == 0);
            }
			break;
		case OSD_FILETYPE_ARTWORK:
			/* only for reading */
			if (_write)
				break;

			sprintf(name,"%s/%s", artworkdir, filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_MEMCARD:
			sprintf(name, "%s/%s",memcarddir,filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
		case OSD_FILETYPE_SCREENSHOT:
			/* only for writing */
			if (!_write)
				break;

			sprintf(name,"%s/%s.png", screenshotdir, filename);
			f->type = kPlainFile;
			f->file = fopen(name,_write ? "wb" : "rb");
			found = f->file!=0;
			break;
	}

	if (!found) {
		free(f);
		return 0;
	}

	return f;
}
Exemplo n.º 11
0
int get_file(int conn, char *pathname, int file_size, int checksum,
	     int mode, int encrypt)
{
  int fd, n_written, code;
  int found_checksum;
  char buf[BUFSIZ];

  memset(buf, '\0', sizeof(buf));

  if (!have_authorization)
    {
      send_int(conn, MR_PERM);
      return 1;
    }
  if (setuid(uid) < 0)
    {
      com_err(whoami, errno, "Unable to setuid to %d\n", uid);
      exit(1);
    }

  /* unlink old file */
  if (!config_lookup("noclobber"))
    unlink(pathname);
  /* open file descriptor */
  fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
  if (fd == -1)
    {
      code = errno;
      com_err(whoami, errno, "creating file %s (get_file)", pathname);
      send_int(conn, code);
      return 1;
    }

  /* check to see if we've got the disk space */
  n_written = 0;
  while (n_written < file_size)
    {
      int n_wrote;
      n_wrote = write(fd, buf, sizeof(buf));
      if (n_wrote == -1)
	{
	  code = errno;
	  com_err(whoami, code, "verifying free disk space for %s (get_file)",
		  pathname);
	  send_int(conn, code);

	  /* do all we can to free the space */
	  unlink(pathname);
	  ftruncate(fd, 0);
	  close(fd);
	  return 1;
	}
      n_written += n_wrote;
    }

  lseek(fd, 0, SEEK_SET);
  send_ok(conn);

  if (encrypt)
    {
#ifdef HAVE_KRB4
      des_key_sched(session, sched);
      memcpy(ivec, session, sizeof(ivec));
#else
      /* The session key only gets stored if auth happens in krb4 to
         begin with. If you don't have krb4, you can't possibly be
         coming up with a valid session key. */
      return MR_NO_KRB4;
#endif
    }

  n_written = 0;
  while (n_written < file_size)
    {
      int n_got = get_block(conn, fd, file_size - n_written, encrypt);

      if (n_got == -1)
	{
	  /* get_block has already printed a message */
	  unlink(pathname);
	  return 1;
	}
      n_written += n_got;
      if (n_written != file_size)
	send_ok(conn);
    }

  fsync(fd);
  ftruncate(fd, file_size);
  fsync(fd);
  close(fd);

  /* validate checksum */
  found_checksum = checksum_file(pathname);
  if (checksum != found_checksum)
    {
      code = MR_MISSINGFILE;
      com_err(whoami, code, ": expected = %d, found = %d",
	      checksum, found_checksum);
      send_int(conn, code);
      return 1;
    }

  send_ok(conn);
  return 0;
}
Exemplo n.º 12
0
/* checksum a file and send the response to the client.
 *
 * return FALSE if the connection failed, TRUE otherwise
 */
BOOL
ss_processfile( HANDLE hpipe,
                long lVersion,
                LPSTR pAbsName,         /* absolute name of file (to open) */
                LPSTR pRelName,         /* relative name (to report) */
                BOOL bChecksum
                )
{
        HANDLE hfile;           /* file handle from CreateFile() */
        DWORD sum, size;
        FILETIME ft;

        hfile = CreateFile(pAbsName, GENERIC_READ, FILE_SHARE_READ,
                        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        if (hfile == INVALID_HANDLE_VALUE) {
                /* We can't read the file, but we can still probably report some
                   properties because FindFirst / FindNext must have found it
                */

                WIN32_FIND_DATA finddata;
                HANDLE hFind;


                hFind = FindFirstFile(pAbsName, &finddata);
                if (hFind!=INVALID_HANDLE_VALUE){
                    FindClose(hFind);
		    Log_Write(hlogErrors, "Cannot read file %s", pAbsName);
                    /* report that we could not read the file */
                    return(ss_sendnewresp( hpipe, lVersion, SSRESP_CANTOPEN
                                         , finddata.nFileSizeLow, 0
                                         , finddata.ftLastWriteTime.dwLowDateTime
                                         , finddata.ftLastWriteTime.dwHighDateTime
                                         , pRelName));
                } else {
                    /* report that this file is cracked */
		    Log_Write(hlogErrors, "Cannot find file %s", pAbsName);
                    return(ss_sendnewresp( hpipe, lVersion, SSRESP_ERROR
                                         , GetLastError(), 0, 0, 0, pRelName));

                }

        } else {
                size = GetFileSize(hfile, NULL);
                if (!GetFileTime(hfile, NULL, NULL, &ft)) {
                        ft.dwLowDateTime = 0;
                        ft.dwHighDateTime = 0;
                }

                CloseHandle(hfile);
                if (bChecksum) {
                        LONG err;
                        sum = checksum_file(pAbsName, &err);
                        if (err!=0) {
                                return(ss_sendnewresp( hpipe, lVersion, SSRESP_ERROR
                                                      , GetLastError(),  0, 0, 0, pRelName));
                        }
                }
                else sum = 0;           /* no checksum wanted */

                return (ss_sendnewresp( hpipe, lVersion, SSRESP_FILE
                                      , size, sum
                                      , ft.dwLowDateTime, ft.dwHighDateTime
                                      , pRelName));
        }
}/* ss_processfile */
Exemplo n.º 13
0
static mame_file *generic_fopen(int pathtype, const char *gamename, const char *filename, const char* hash, UINT32 flags)
{
	static const char *access_modes[] = { "rb", "rb", "wb", "r+b" };
	const char *extension = get_extension_for_filetype(pathtype);
	int pathcount = osd_get_path_count(pathtype);
	int pathindex, pathstart, pathstop, pathinc;
	mame_file file, *newfile;
	char tempname[256];

	LOG(("generic_fopen(%d, %s, %s, %s, %X)\n", pathc, gamename, filename, extension, flags));

	/* reset the file handle */
	memset(&file, 0, sizeof(file));

	/* check for incompatible flags */
	if ((flags & FILEFLAG_OPENWRITE) && (flags & FILEFLAG_HASH))
		fprintf(stderr, "Can't use HASH option with WRITE option in generic_fopen!\n");

	/* determine start/stop based on reverse search flag */
	if (!(flags & FILEFLAG_REVERSE_SEARCH))
	{
		pathstart = 0;
		pathstop = pathcount;
		pathinc = 1;
	}
	else
	{
		pathstart = pathcount - 1;
		pathstop = -1;
		pathinc = -1;
	}

	/* loop over paths */
	for (pathindex = pathstart; pathindex != pathstop; pathindex += pathinc)
	{
		char name[1024];

		/* ----------------- STEP 1: OPEN THE FILE RAW -------------------- */

		/* first look for path/gamename as a directory */
		compose_path(name, gamename, NULL, NULL);
		LOG(("Trying %s\n", name));

		/* if the directory exists, proceed */
		if (*name == 0 || osd_get_path_info(pathtype, pathindex, name) == PATH_IS_DIRECTORY)
		{
			/* now look for path/gamename/filename.ext */
			compose_path(name, gamename, filename, extension);

			/* if we need checksums, load it into RAM and compute it along the way */
			if (flags & FILEFLAG_HASH)
			{
				if (checksum_file(pathtype, pathindex, name, &file.data, &file.length, file.hash) == 0)
				{
					file.type = RAM_FILE;
					break;
				}
			}

			/* otherwise, just open it straight */
			else
			{
				file.type = PLAIN_FILE;
				file.file = osd_fopen(pathtype, pathindex, name, access_modes[flags & 3]);
				if (file.file == NULL && (flags & 3) == 3)
					file.file = osd_fopen(pathtype, pathindex, name, "w+b");
				if (file.file != NULL)
					break;
			}
		}

		/* ----------------- STEP 2: OPEN THE FILE IN A ZIP -------------------- */

		/* now look for it within a ZIP file */
		if (!(flags & (FILEFLAG_OPENWRITE | FILEFLAG_NOZIP)))
		{
			/* first look for path/gamename.zip */
			compose_path(name, gamename, NULL, "zip");
			LOG(("Trying %s file\n", name));

			/* if the ZIP file exists, proceed */
			if (osd_get_path_info(pathtype, pathindex, name) == PATH_IS_FILE)
			{
				UINT32 ziplength;

				/* if the file was able to be extracted from the ZIP, continue */
				compose_path(tempname, NULL, filename, extension);

				/* verify-only case */
				if (flags & FILEFLAG_VERIFY_ONLY)
				{
					UINT8 crcs[4];
					UINT32 crc = 0;

					/* Since this is a .ZIP file, we extract the CRC from the expected hash
					   (if any), so that we can load by CRC if needed. We must check that
					   the hash really contains a CRC, because it could be a NO_DUMP rom
					   for which we do not know the CRC yet. */
					if (hash && hash_data_extract_binary_checksum(hash, HASH_CRC, crcs) != 0)
					{
						/* Store the CRC in a single DWORD */
						crc = ((unsigned long)crcs[0] << 24) |
							  ((unsigned long)crcs[1] << 16) |
							  ((unsigned long)crcs[2] <<  8) |
							  ((unsigned long)crcs[3] <<  0);
					}

					hash_data_clear(file.hash);
						
					if (checksum_zipped_file(pathtype, pathindex, name, tempname, &ziplength, &crc) == 0)
					{
						file.length = ziplength;
						file.type = UNLOADED_ZIPPED_FILE;
						crcs[0] = (UINT8)(crc >> 24);
						crcs[1] = (UINT8)(crc >> 16);
						crcs[2] = (UINT8)(crc >> 8);
						crcs[3] = (UINT8)(crc >> 0);
						hash_data_insert_binary_checksum(file.hash, HASH_CRC, crcs);
						break;
					}
				}
Exemplo n.º 14
0
static void
new_minor_version(const unsigned char *versions_file, const unsigned char *checksum_prefix, const unsigned char *version)
{
    struct verdb *latest = &verdb[verdb_u - 1];

    unsigned char id_str[1024];
    int changed_flag = 0;
    id_str[0] = 0;
    int mode = 0;

    if (get_file_type(".git") == FILE_DIR) {
        snprintf(id_str, sizeof(id_str), "tagged");
        mode = MODE_GIT;
    } else if (get_file_type(".svn") == FILE_DIR) {
        if (read_svn_status(id_str, sizeof(id_str), &changed_flag) < 0) {
            fprintf(stderr, "Failed to parse SVN status\n");
            exit(1);
        }
        int id_val = 0;
        if (sscanf(id_str, "%d", &id_val) != 1) {
            fprintf(stderr, "Invalid revision id\n");
            exit(1);
        }
        snprintf(id_str, sizeof(id_str), "%d", id_val + 1);
        mode = MODE_SVN;
    } else {
        fprintf(stderr, "SVN/GIT is not connected\n");
        exit(1);
    }
    if (changed_flag) {
        fprintf(stderr, "There are uncommited changes\n");
        exit(1);
    }

    int patch = 0;
    if (!version) {
        version = latest->version;
        patch = latest->patch + 1;
    }

    unsigned char full_version[1024];
    snprintf(full_version, sizeof(full_version), "%s.%d", version, patch);
    unsigned char out_file[1024];
    snprintf(out_file, sizeof(out_file), "%s%s", checksum_prefix, full_version);

    FILE *cf = fopen(out_file, "w");
    if (!cf) {
        fprintf(stderr, "Cannot open '%s' for writing\n", out_file);
        exit(1);
    }
    for (int i = 0; i < svn_info_u; ++i) {
        unsigned char checksum_buf[1024];
        if (checksum_file(svn_infos[i].path, checksum_buf, sizeof(checksum_buf)) < 0) {
            fprintf(stderr, "Failed to checksum file '%s'\n", svn_infos[i].path);
            exit(1);
        }
        fprintf(cf, "%s %s\n", checksum_buf, svn_infos[i].path);
    }
    fclose(cf); cf = NULL;

    time_t cur_time = time(0);
    struct tm *ptm = localtime(&cur_time);

    FILE *f = fopen(versions_file, "a");
    if (!f) {
        fprintf(stderr, "Cannot open '%s' for appending\n", versions_file);
        exit(1);
    }
    fprintf(f, "%s %s %d %04d/%02d/%02d\n", id_str, version, patch,
            ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
    fclose(f); f = NULL;

    if (mode == MODE_GIT) {
        unsigned char cmd_buf[1024];
        snprintf(cmd_buf, sizeof(cmd_buf), "git add \"%s\" \"%s\"", out_file, versions_file);
        if (system(cmd_buf) != 0) {
            fprintf(stderr, "Command '%s' failed\n", cmd_buf);
            exit(1);
        }
        snprintf(cmd_buf, sizeof(cmd_buf), "git commit -m \"version %s\"", full_version);
        if (system(cmd_buf) != 0) {
            fprintf(stderr, "Command '%s' failed\n", cmd_buf);
            exit(1);
        }
        snprintf(cmd_buf, sizeof(cmd_buf), "git tag \"v%s\"", full_version);
        if (system(cmd_buf) != 0) {
            fprintf(stderr, "Command '%s' failed\n", cmd_buf);
            exit(1);
        }
    }
}