Ejemplo n.º 1
0
static void romident(const char *filename, romident_status *status)
{
	osd_directory *directory;

	/* reset the status */
	memset(status, 0, sizeof(*status));

	/* first try to open as a directory */
	directory = osd_opendir(filename);
	if (directory != NULL)
	{
		const osd_directory_entry *entry;

		/* iterate over all files in the directory */
		while ((entry = osd_readdir(directory)) != NULL)
			if (entry->type == ENTTYPE_FILE)
			{
				astring *curfile = astring_assemble_3(astring_alloc(), filename, PATH_SEPARATOR, entry->name);
				identify_file(astring_c(curfile), status);
				astring_free(curfile);
			}
		osd_closedir(directory);
	}

	/* if that failed, and the filename ends with .zip, identify as a ZIP file */
	else if (core_filename_ends_with(filename, ".zip"))
	{
		/* first attempt to examine it as a valid ZIP file */
		zip_file *zip = NULL;
		zip_error ziperr = zip_file_open(filename, &zip);
		if (ziperr == ZIPERR_NONE && zip != NULL)
		{
			const zip_file_header *entry;

			/* loop over entries in the ZIP, skipping empty files and directories */
			for (entry = zip_file_first_file(zip); entry; entry = zip_file_next_file(zip))
				if (entry->uncompressed_length != 0)
				{
					UINT8 *data = (UINT8 *)malloc(entry->uncompressed_length);
					if (data != NULL)
					{
						/* decompress data into RAM and identify it */
						ziperr = zip_file_decompress(zip, data, entry->uncompressed_length);
						if (ziperr == ZIPERR_NONE)
							identify_data(entry->filename, data, entry->uncompressed_length, status);
						free(data);
					}
				}

			/* close up */
			zip_file_close(zip);
		}
	}

	/* otherwise, identify as a raw file */
	else
		identify_file(filename, status);
}
Ejemplo n.º 2
0
static void identify_file(const char *name, romident_status *status)
{
	file_error filerr;
	osd_file *file;
	UINT64 length;

	/* open for read and process if it opens and has a valid length */
	filerr = osd_open(name, OPEN_FLAG_READ, &file, &length);
	if (filerr == FILERR_NONE && length > 0 && (UINT32)length == length)
	{
		UINT8 *data = (UINT8 *)malloc(length);
		if (data != NULL)
		{
			UINT32 bytes;

			/* read file data into RAM and identify it */
			filerr = osd_read(file, data, 0, length, &bytes);
			if (filerr == FILERR_NONE)
				identify_data(name, data, bytes, status);
			free(data);
		}
		osd_close(file);
	}
}
Ejemplo n.º 3
0
/* newdata():
 *
 * Called whenever new data arrives.
 *
 * First, checks if data needs to be buffered before a previously
 * identified request can be fulfilled. If so, the incoming data is
 * buffered and the data handler is called. If no data needs to be
 * buffered, the code proceeds to identify the incoming request. If
 * the incoming request can be processed immediately (i.e., all data
 * is contained in this message) the data handler is invoked. If data
 * has to be buffered to fulfill the request, this is noted and taken
 * care of the next time this function is invoked (i.e., for the next
 * incoming data chunk).
 */
static uint8_t
newdata(void)
{
  uint16_t datalen;
  uint16_t readlen;
  uint8_t *dataptr;
  
  datalen = uip_datalen();
  dataptr = (uint8_t *)uip_appdata;

  PRINTF(("newdata: %d bytes\n", datalen));
  
  /* If we are in a "rectstate", meaning that the incoming data is
     part of a rectangle that is being incrementaly drawn on the
     screen, we handle that first. */
  if(vs->rectstate != VNC_RECTSTATE_NONE) {
    readlen = recv_rectstate(dataptr, datalen);
    PRINTF(("newdata: vs->rectstate %d, datalen %d, readlen %d\n",
	    vs->rectstate, datalen, readlen));
    datalen -= readlen;
    dataptr += readlen;
  }

  /* Next, check if we are supposed to buffer data from the incoming
     segment. */
  while(vs->bufferleft > 0 && datalen > 0) {
    if(datalen >= vs->bufferleft) {
      /* There is more data in the incoming chunk than we need to
	 buffer, so we buffer as much as we can and handle the
	 buffered data, and repeat the (identify->buffer->handle)
	 sequence for the data that is left in the incoming chunk. */
      datalen -= vs->bufferleft;
      dataptr += vs->bufferleft;
      buffer_data((uint8_t *)uip_appdata, vs->bufferleft);
      handle_data(vs->buffer, vs->buffersize);
      clearbuffer();
    } else { /* datalen < vs->bufferleft */
      /* We need to buffer more data than was received with this
         chunk, so we buffer the avaliable data and return. */
      buffer_data(dataptr, datalen);
      return 0;
    }
  }

  /* Finally, if there is data left in the segment, we handle it. */
  while(datalen > 0) {

    if(vs->rectstate != VNC_RECTSTATE_NONE) {
      readlen = recv_rectstate(dataptr, datalen);
      PRINTF(("newdata (2): vs->rectstate %d, datalen %d, readlen %d\n",
	      vs->rectstate, datalen, readlen));
      datalen -= readlen;
      dataptr += readlen;
    } else {
 
      /* We get here if there is data to be identified in the incoming
	 chunk. */
      readlen = identify_data(dataptr, datalen);

      if(readlen == 0) {
	PRINTF(("Identify returned 0\n"));
	return 0;
      }

      PRINTF(("Reading %d bytes more\n", readlen));
      /* The data has been identified and the amount of data that
	 needs to be read to be able to process the data is in the
	 "readlen" variable. If the incoming chunk contains enough
	 data, we handle it directly, otherwise we buffer the incoming
	 data and set the state so that we know that there is more
	 data to be buffered. */
      if(readlen > datalen) {
	clearbuffer(); /* Should not be needed, but just in case... */
	vs->bufferleft = readlen;
	buffer_data(dataptr, datalen);
	return 0;
      }
      if(readlen <= datalen) {
	PRINTF(("Before handle_data %d\n", readlen));
	readlen += handle_data(dataptr, readlen);
	PRINTF(("After handle_data %d\n", readlen));
	datalen -= readlen;
	dataptr += readlen;
      }

    }
    if(datalen > 0) {
      PRINTF(("newdata: there is more data left after first iteration... %d\n", datalen));
    }
    
  }
  
  return 0;
}
Ejemplo n.º 4
0
static void identify_file(core_options *options, const char *name, romident_status *status)
{
	file_error filerr;
	osd_file *file;
	UINT64 length;

	if (core_filename_ends_with(name, ".chd"))
	{
		chd_file *chd;
		chd_error err;
		astring basename;
		int found = 0;

		core_filename_extract_base(&basename, name, FALSE);
		mame_printf_info("%-20s", basename.cstr());

		status->total++;

		err = chd_open(name, CHD_OPEN_READ, NULL, &chd);
		if (err != CHDERR_NONE)
		{
			mame_printf_info("NOT A CHD\n");
			status->nonroms++;
		}
		else
		{
			chd_header header;

			header = *chd_get_header(chd);
			if (header.flags & CHDFLAGS_IS_WRITEABLE)
			{
				mame_printf_info("is a writable CHD\n");
			}
			else
			{
				static const UINT8 nullhash[HASH_BUF_SIZE] = { 0 };
				char			hash[HASH_BUF_SIZE];	/* actual hash information */

				hash_data_clear(hash);

				/* if there's an MD5 or SHA1 hash, add them to the output hash */
				if (memcmp(nullhash, header.md5, sizeof(header.md5)) != 0)
					hash_data_insert_binary_checksum(hash, HASH_MD5, header.md5);
				if (memcmp(nullhash, header.sha1, sizeof(header.sha1)) != 0)
					hash_data_insert_binary_checksum(hash, HASH_SHA1, header.sha1);

				length = header.logicalbytes;

				match_roms(options, hash, length, &found);

				if (found == 0)
				{
					mame_printf_info("NO MATCH\n");
				}

				/* if we did find it, count it as a match */
				else
					status->matches++;
			}

			chd_close(chd);
		}
	}
	else
	{
		/* open for read and process if it opens and has a valid length */
		filerr = osd_open(name, OPEN_FLAG_READ, &file, &length);
		if (filerr == FILERR_NONE && length > 0 && (UINT32)length == length)
		{
			UINT8 *data = global_alloc_array(UINT8, length);
			if (data != NULL)
			{
				UINT32 bytes;

				/* read file data into RAM and identify it */
				filerr = osd_read(file, data, 0, length, &bytes);
				if (filerr == FILERR_NONE)
					identify_data(options, name, data, bytes, status);
				global_free(data);
			}
			osd_close(file);
		}
	}
}