Ejemplo n.º 1
0
int fsimage_open(disk_image_t *image)
{
    fsimage_t *fsimage;

    fsimage = image->media.fsimage;
    fsimage->error_info.map = NULL;

    if (image->read_only) {
        fsimage->fd = zfile_fopen(fsimage->name, MODE_READ);
    } else {
        fsimage->fd = zfile_fopen(fsimage->name, MODE_READ_WRITE);

        /* If we cannot open the image read/write, try to open it read only. */
        if (fsimage->fd == NULL) {
            fsimage->fd = zfile_fopen(fsimage->name, MODE_READ);
            image->read_only = 1;
        }
    }

    if (fsimage->fd == NULL) {
        log_error(fsimage_log, "Cannot open file `%s'.", fsimage->name);
        return -1;
    }

    if (fsimage_probe(image) == 0) {
        return 0;
    }

    log_message(fsimage_log, "Unknown disk image `%s'.", fsimage->name);
    fsimage_close(image);
    return -1;
}
Ejemplo n.º 2
0
int fsimage_open(disk_image_t *image)
{
	fsimage_t *fsimage;

	fsimage = image->media.fsimage;

	if (image->read_only) {
		fsimage->fd = zfile_fopen(fsimage->name, MODE_READ);
	} else  {
		fsimage->fd = zfile_fopen(fsimage->name, MODE_READ_WRITE);

		/* If we cannot open the image read/write, try to open it read only. */
		if (fsimage->fd == NULL) {
			fsimage->fd = zfile_fopen(fsimage->name, MODE_READ);
			image->read_only = 1;
		}
	}

	if (fsimage->fd == NULL)
	{
		#ifdef CELL_DEBUG
		printf("INFO: Cannot open file `%s'.\n", fsimage->name);
		#endif
		return -1;
	}

	if (fsimage_probe(image) == 0)
		return 0;

	zfile_fclose(fsimage->fd);
	#ifdef CELL_DEBUG
	printf("ERROR: Unknown disk image `%s'.\n", fsimage->name);
	#endif
	return -1;
}
Ejemplo n.º 3
0
static int loadsample (TCHAR *path, struct drvsample *ds)
{
	struct zfile *f;
	uae_u8 *buf;
	int size;
	TCHAR name[MAX_DPATH];

	f = zfile_fopen (path, _T("rb"), ZFD_NORMAL);
	if (!f) {
		_tcscpy (name, path);
		_tcscat (name, _T(".wav"));
		f = zfile_fopen (name, _T("rb"), ZFD_NORMAL);
		if (!f) {
			write_log (_T("driveclick: can't open '%s' (or '%s')\n"), path, name);
			return 0;
		}
	}
	zfile_fseek (f, 0, SEEK_END);
	size = zfile_ftell (f);
	buf = xmalloc (uae_u8, size);
	zfile_fseek (f, 0, SEEK_SET);
	zfile_fread (buf, size, 1, f);
	zfile_fclose (f);
	ds->len = size;
	ds->p = decodewav (buf, &ds->len);
	xfree (buf);
	return 1;
}
Ejemplo n.º 4
0
struct zfile *rom_fopen (const TCHAR *name, const TCHAR *mode, int mask)
{
	struct zfile *f;
	//write_log (L"attempting to load '%s'\n", name); 
	f = zfile_fopen (name, mode, mask);
	//write_log (L"=%p\n", f);
	return f;
}
Ejemplo n.º 5
0
bool rp9_parse_file(struct uae_prefs *p, const char *filename)
{
  bool bResult = false;
	struct zfile *zf;
	unzFile uz;
  unz_file_info file_info;
  char *manifest;
  
  add_HDF_DHnum = 0;
  clip_no_hires = false;
  
  zf = zfile_fopen(filename, _T("rb"));
  if(zf != NULL)
  {
    uz = unzOpen(zf);
  	if (uz != NULL)
	  {
    	if (unzLocateFile (uz, RP9_MANIFEST, 1) == UNZ_OK)
      {
      	if (unzGetCurrentFileInfo (uz, &file_info, NULL, 0, NULL, 0, NULL, 0) == UNZ_OK)
      	{
          manifest = (char *)malloc(file_info.uncompressed_size + 1);
          if(manifest != NULL)
          {
            if (unzOpenCurrentFile (uz) == UNZ_OK)
            {
              int readsize = unzReadCurrentFile(uz, manifest, file_info.uncompressed_size);
              unzCloseCurrentFile(uz);

              if(readsize == file_info.uncompressed_size)
              {
                manifest[readsize] = '\0';
                bResult = parse_manifest(p, uz, manifest);
                
                if(bResult)
                {
                  // Fixup some prefs...
                  if(p->m68k_speed >= 0)
                    p->cachesize = 0; // Use JIT only if max. speed selected
                  p->input_joymouse_multiplier = 5; // Most games need slower mouse movement...
                }
              }
            }
            free(manifest);
          }
        }
      }
  
    	unzClose (uz);
    }
    zfile_fclose(zf);  
  }
  
  return bResult;
}
Ejemplo n.º 6
0
static void cdtvcr_battram_reset (void)
{
	struct zfile *f;
	int v;

	memset (cdtvcr_ram, 0, CDTVCR_RAM_SIZE);
	f = zfile_fopen (currprefs.flashfile, _T("rb+"), ZFD_NORMAL);
	if (!f) {
		f = zfile_fopen (currprefs.flashfile, _T("wb"), 0);
		if (f) {
			zfile_fwrite (cdtvcr_ram, CDTVCR_RAM_SIZE, 1, f);
			zfile_fclose (f);
		}
		return;
	}
	v = zfile_fread (cdtvcr_ram, 1, CDTVCR_RAM_SIZE, f);
	if (v < CDTVCR_RAM_SIZE)
		zfile_fwrite (cdtvcr_ram + v, 1, CDTVCR_RAM_SIZE - v, f);
	zfile_fclose (f);
}
Ejemplo n.º 7
0
int32_t inprec_open(char *fname, int32_t record)
{
    uint32_t t = (uint32_t)time(0);
    int32_t i;

    inprec_close();
    inprec_zf = zfile_fopen(fname, record > 0 ? "wb" : "rb");
    if (inprec_zf == NULL)
		return 0;
    inprec_size = 10000;
    inprec_div = 1;
    if (record < 0) {
		uint32_t id;
		zfile_fseek (inprec_zf, 0, SEEK_END);
		inprec_size = zfile_ftell (inprec_zf);
		zfile_fseek (inprec_zf, 0, SEEK_SET);
		inprec_buffer = inprec_p = (uint8_t*)xmalloc (inprec_size);
		zfile_fread (inprec_buffer, inprec_size, 1, inprec_zf);
		inprec_plastptr = inprec_buffer;
		id = inprec_pu32();
		if (id != 'UAE\0') {
		    inprec_close();
		    return 0;
		}
		inprec_pu32();
		t = inprec_pu32();
		i = inprec_pu32();
		while (i-- > 0)
		    inprec_pu8();
		inprec_p = inprec_plastptr;
		oldbuttons[0] = oldbuttons[1] = oldbuttons[2] = oldbuttons[3] = 0;
		oldjoy[0] = oldjoy[1] = 0;
		if (record < -1)
		    inprec_div = maxvpos;
    } else if (record > 0) {
		inprec_buffer = inprec_p = (uint8_t*)xmalloc (inprec_size);
		inprec_ru32('UAE\0');
		inprec_ru8(1);
		inprec_ru8(UAEMAJOR);
		inprec_ru8(UAEMINOR);
		inprec_ru8(UAESUBREV);
		inprec_ru32(t);
		inprec_ru32(0); // extra header size
    } else {
		return 0;
    }
    input_recording = record;
    srand(t);
    CIA_inprec_prepare();
    write_log ("inprec initialized '%s', mode=%d\n", fname, input_recording);
    return 1;
}
Ejemplo n.º 8
0
static void cdtvcr_battram_write (int addr, int v)
{
	struct zfile *f;
	int offset = addr & CDTVCR_RAM_MASK;

	if (offset >= CDTVCR_RAM_SIZE)
		return;
	gui_flicker_led (LED_MD, 0, 2);
	if (cdtvcr_ram[offset] == v)
		return;
	cdtvcr_ram[offset] = v;
	f = zfile_fopen (currprefs.flashfile, _T("rb+"), ZFD_NORMAL);
	if (!f)
		return;
	zfile_fseek (f, offset, SEEK_SET);
	zfile_fwrite (cdtvcr_ram + offset, 1, 1, f);
	zfile_fclose (f);
}
Ejemplo n.º 9
0
void addkeyfile (const TCHAR *path)
{
	struct zfile *f;
	int keysize;
	uae_u8 *keybuf;

	f = zfile_fopen (path, L"rb", ZFD_NORMAL);
	if (!f)
		return;
	zfile_fseek (f, 0, SEEK_END);
	keysize = zfile_ftell (f);
	if (keysize > 0) {
		zfile_fseek (f, 0, SEEK_SET);
		keybuf = xmalloc (uae_u8, keysize);
		zfile_fread (keybuf, 1, keysize, f);
		addkey (keybuf, keysize, path);
	}
	zfile_fclose (f);
}
Ejemplo n.º 10
0
static int zfile_load(const char *filename, BYTE *dest, size_t size)
{
    FILE *fd;

    fd = zfile_fopen(filename, MODE_READ);
    if (!fd) {
        return -1;
    }
    if (util_file_length(fd) != size) {
        zfile_fclose(fd);
        return -1;
    }
    if (fread(dest, size, 1, fd) < 1) {
        zfile_fclose(fd);
        return -1;
    }
    zfile_fclose(fd);
    return 0;
}
Ejemplo n.º 11
0
void amax_init (void)
{
	struct zfile *z;

	if (!currprefs.amaxromfile[0])
		return;
	amax_reset ();
	z = zfile_fopen (currprefs.amaxromfile, _T("rb"), ZFD_NORMAL);
	if (!z) {
		write_log (_T("AMAX: failed to load rom '%s'\n"), currprefs.amaxromfile);
		return;
	}
	zfile_fseek (z, 0, SEEK_END);
	amax_rom_size = zfile_ftell (z);
	zfile_fseek (z, 0, SEEK_SET);
	rom = xmalloc (uae_u8, amax_rom_size);
	zfile_fread (rom, amax_rom_size, 1, z);
	zfile_fclose (z);
	write_log (_T("AMAX: '%s' loaded, %d bytes\n"), currprefs.amaxromfile, amax_rom_size);
	dselect = 0x20;
}
Ejemplo n.º 12
0
static void mergecd32 (uae_u8 *dst, uae_u8 *src, int size)
{
	int i, k;
	k = 0;
	for (i = 0; i < size / 2; i += 2) {
		int j = i + size / 2;
		dst[k + 1] = src[i + 0];
		dst[k + 0] = src[i + 1];
		dst[k + 3] = src[j + 0];
		dst[k + 2] = src[j + 1];
		k += 4;
	}
#if 0
	{
		struct zfile *f;
		f = zfile_fopen ("c:\\d\\1.rom","wb", ZFD_NORMAL);
		zfile_fwrite (dst, 1, size, f);
		zfile_fclose(f);
	}
#endif
}
Ejemplo n.º 13
0
static int loadsample (const char *path, struct drvsample *ds)
{
    struct zfile *f;
    uae_u8 *buf;
    int size;

    f = zfile_fopen (path, "rb");
    if (!f) {
	write_log ("driveclick: can't open '%s'\n", path);
	return 0;
    }
    zfile_fseek (f, 0, SEEK_END);
    size = zfile_ftell (f);
    buf = malloc (size);
    zfile_fseek (f, 0, SEEK_SET);
    zfile_fread (buf, size, 1, f);
    zfile_fclose (f);
    ds->len = size;
    ds->p = decodewav (buf, &ds->len);
    free (buf);
    return 1;
}
Ejemplo n.º 14
0
static int read_rom_file (uae_u8 *buf, const struct romdata *rd)
{
	struct zfile *zf;
	struct romlist *rl = romlist_getrl (rd);
	uae_char tmp[11];

	if (!rl || _tcslen (rl->path) == 0)
		return 0;
	zf = zfile_fopen (rl->path, L"rb", ZFD_NORMAL);
	if (!zf)
		return 0;
	addkeydir (rl->path);
	zfile_fread (tmp, sizeof tmp, 1, zf);
	if (!memcmp (tmp, "AMIROMTYPE1", sizeof tmp)) {
		zfile_fread (buf, rd->size, 1, zf);
		decode_cloanto_rom_do (buf, rd->size, rd->size);
	} else {
		memcpy (buf, tmp, sizeof tmp);
		zfile_fread (buf + sizeof tmp, rd->size - sizeof (tmp), 1, zf);
	}
	zfile_fclose (zf);
	return 1;
}
Ejemplo n.º 15
0
static int zfile_load(const char *filename, BYTE *dest)
{
    FILE *fd;
    size_t fsize;

    fd = zfile_fopen(filename, MODE_READ);
    if (!fd) {
        log_message(fe_log, "Failed to open image `%s'!",
                    filename);
        return -1;
    }
    fsize = util_file_length(fd);

    if (fsize < 0x8000) {
        size_t tsize;
        size_t offs;
        tsize = (fsize + 0x0fff) & 0xfffff000;
        offs = 0x8000 - tsize;
        dest += offs;
        log_message(fe_log, "Size less than 32kB.  Aligning as close as possible to the 32kB boundary in 4kB blocks. (0x%06X-0x%06X)", (unsigned int)offs, (unsigned int)(offs + tsize));
    } else if (fsize < (size_t)CART_ROM_SIZE) {
        log_message(fe_log, "Size less than 512kB, padding.");
    } else if (fsize > (size_t)CART_ROM_SIZE) {
        fsize = CART_ROM_SIZE;
        log_message(fe_log, "Size larger than 512kB, truncating.");
    }
    if (fread(dest, fsize, 1, fd) < 1) {
        log_message(fe_log, "Failed to read image `%s'!", filename);
        zfile_fclose(fd);
        return -1;
    }
    zfile_fclose(fd);
    log_message(fe_log, "Read image `%s'.",
                filename);
    return 0;
}
Ejemplo n.º 16
0
int psid_load_file(const char* filename)
{
    FILE* f;
    BYTE buf[PSID_V2_DATA_OFFSET + 2];
    BYTE* ptr = buf;
    unsigned int length;

    if (vlog == LOG_ERR) {
        vlog = log_open("Vsid");
    }

    if (!(f = zfile_fopen(filename, MODE_READ))) {
        return -1;
    }

    lib_free(psid);
    psid = lib_malloc(sizeof(psid_t));

    if (fread(ptr, 1, 6, f) != 6 || (memcmp(ptr, "PSID", 4) != 0 && memcmp(ptr, "RSID", 4) != 0)) {
        goto fail;
    }

    ptr += 4;
    psid->version = psid_extract_word(&ptr);

    if (psid->version < 1 || psid->version > 3) {
        log_error(vlog, "Unknown PSID version number: %d.", (int)psid->version);
        goto fail;
    }

    length = (unsigned int)((psid->version == 1 ? PSID_V1_DATA_OFFSET : PSID_V2_DATA_OFFSET) - 6);

    if (fread(ptr, 1, length, f) != length) {
        log_error(vlog, "Reading PSID header.");
        goto fail;
    }

    psid->data_offset = psid_extract_word(&ptr);
    psid->load_addr = psid_extract_word(&ptr);
    psid->init_addr = psid_extract_word(&ptr);
    psid->play_addr = psid_extract_word(&ptr);
    psid->songs = psid_extract_word(&ptr);
    psid->start_song = psid_extract_word(&ptr);
    psid->speed = psid_extract_word(&ptr) << 16;
    psid->speed |= psid_extract_word(&ptr);
    psid->frames_played = 0;
    memcpy(psid->name, ptr, 32);
    psid->name[32] = '\0';
    ptr += 32;
    memcpy(psid->author, ptr, 32);
    psid->author[32] = '\0';
    ptr += 32;
    memcpy(psid->copyright, ptr, 32);
    psid->copyright[32] = '\0';
    ptr += 32;
    if (psid->version >= 2) {
        psid->flags = psid_extract_word(&ptr);
        psid->start_page = *ptr++;
        psid->max_pages = *ptr++;
        psid->reserved = psid_extract_word(&ptr);
    } else {
        psid->flags = 0;
        psid->start_page = 0;
        psid->max_pages = 0;
        psid->reserved = 0;
    }

    /* Check for SIDPLAYER MUS files. */
    if (psid->flags & 0x01) {
        log_error(vlog, "SIDPLAYER MUS files not supported.");
        goto fail;
    }

    /* Zero load address => the load address is stored in the
       first two bytes of the binary C64 data. */
    if (psid->load_addr == 0) {
        if (fread(ptr, 1, 2, f) != 2) {
            log_error(vlog, "Reading PSID load address.");
            goto fail;
        }
        psid->load_addr = ptr[0] | ptr[1] << 8;
    }

    /* Zero init address => use load address. */
    if (psid->init_addr == 0) {
        psid->init_addr = psid->load_addr;
    }

    /* Read binary C64 data. */
    psid->data_size = (WORD)fread(psid->data, 1, sizeof(psid->data), f);

    if (ferror(f)) {
        log_error(vlog, "Reading PSID data.");
        goto fail;
    }

    if (!feof(f)) {
        log_error(vlog, "More than 64K PSID data.");
        goto fail;
    }

    /* Relocation setup. */
    if (psid->start_page == 0x00) {
        /* Start and end pages. */
        int startp = psid->load_addr >> 8;
        int endp = (psid->load_addr + psid->data_size - 1) >> 8;

        /* Used memory ranges. */
        unsigned int used[] = {
            0x00,
            0x03,
            0xa0,
            0xbf,
            0xd0,
            0xff,
            0x00,
            0x00
        };        /* calculated below */

        unsigned int pages[256];
        unsigned int last_page = 0;
        unsigned int i, page, tmp;

        /* finish initialization */
        used[6] = startp; used[7] = endp;

        /* Mark used pages in table. */
        memset(pages, 0, sizeof(pages));
        for (i = 0; i < sizeof(used) / sizeof(*used); i += 2) {
            for (page = used[i]; page <= used[i + 1]; page++) {
                pages[page] = 1;
            }
        }

        /* Find largest free range. */
        psid->max_pages = 0x00;
        for (page = 0; page < sizeof(pages) / sizeof(*pages); page++) {
            if (!pages[page]) {
                continue;
            }
            tmp = page - last_page;
            if (tmp > psid->max_pages) {
                psid->start_page = last_page;
                psid->max_pages = tmp;
            }
            last_page = page + 1;
        }

        if (psid->max_pages == 0x00) {
            psid->start_page = 0xff;
        }
    }
Ejemplo n.º 17
0
static int unpack (const TCHAR *src, const TCHAR *filename, const TCHAR *dst, int out, int all, int level)
{
	struct zdirectory *h;
	struct zvolume *zv;
	int ret;
	uae_u8 *b;
	int size;
	TCHAR fn[MAX_DPATH];

	ret = 0;
	zv = zfile_fopen_archive_root (src, ZFD_ALL);
	if (zv == NULL) {
		geterror();
		_tprintf (_T("Couldn't open archive '%s'\n"), src);
		return 0;
	}
	h = zfile_opendir_archive (src);
	if (!h) {
		geterror();
		_tprintf (_T("Couldn't open directory '%s'\n"), src);
		return 0;
	}
	while (zfile_readdir_archive (h, fn)) {
		if (all || !_tcsicmp (filename, fn)) {
			TCHAR tmp[MAX_DPATH];
			struct zfile *s, *d;
			struct mystat st;

			found = 1;
			_tcscpy (tmp, src);
			_tcscat (tmp, sep);
			_tcscat (tmp, fn);
			if (!zfile_stat_archive (tmp, &st)) {
				_tprintf (_T("Couldn't stat '%s'\n"), tmp);
				continue;
			}
			if (dst == NULL || all)
				dst = fn;
			if (st.mode) {
				if (all > 0)
					continue;
				if (all < 0) {
					TCHAR oldcur[MAX_DPATH];
					my_mkdir (fn);
					my_setcurrentdir (fn, oldcur);
					unpack (tmp, fn, dst, out, all, 1);
					my_setcurrentdir (oldcur, NULL);
					setdate (dst, st.mtime.tv_sec);
					continue;
				}
				_tprintf (_T("Directory extraction not yet supported\n"));
				return 0;
			}

			s = zfile_open_archive (tmp, ZFD_ARCHIVE | ZFD_NORECURSE);
			if (!s) {
				geterror();
				_tprintf (_T("Couldn't open '%s' for reading\n"), src);
				continue;
			}
			zfile_fseek (s, 0, SEEK_END);
			size = zfile_ftell (s);
			zfile_fseek (s, 0, SEEK_SET);
			b = xcalloc (uae_u8, size);
			if (b) {
				if (zfile_fread (b, size, 1, s) == 1) {
					if (out) {
						_tprintf (_T("\n"));
						fwrite (b, size, 1, stdout);
					} else {
						d = zfile_fopen (dst, _T("wb"), 0);
						if (d) {
							if (zfile_fwrite (b, size, 1, d) == 1) {
								ret = 1;
								_tprintf (_T("%s extracted, %d bytes\n"), dst, size);
							}
							zfile_fclose (d);
							setdate (dst, st.mtime.tv_sec);
						}
					}
				}
				xfree (b);
			}
			zfile_fclose (s);
			if (!all)
				break;
		}
	}
	geterror ();
	if (!found && !level) {
		if (filename[0])
			_tprintf (_T("'%s' not found\n"), filename);
		else
			_tprintf (_T("nothing extracted\n"));
	}
	return ret;
}
Ejemplo n.º 18
0
int hdf_open_target (struct hardfiledata *hfd, const char *pname)
{
    FILE *h = INVALID_HANDLE_VALUE;
    int i;
    struct uae_driveinfo *udi;
    char *name = strdup (pname);

    if (getenv("FS_DEBUG_HDF")) {
        g_debug = 1;
    }
    if (g_debug) {
        write_log("\n\n-- hdf_open_target pname = %s\n", pname);
    }

    hfd->flags = 0;
    hfd->drive_empty = 0;
    hdf_close (hfd);
    hfd->cache = (uae_u8*)xmalloc (uae_u8, CACHE_SIZE);
    hfd->cache_valid = 0;
    hfd->virtual_size = 0;
    hfd->virtual_rdb = NULL;
    if (!hfd->cache) {
        write_log ("VirtualAlloc(%d) failed, error %d\n", CACHE_SIZE, errno);
        goto end;
    }
    hfd->handle = xcalloc (struct hardfilehandle, 1);
    hfd->handle->h = INVALID_HANDLE_VALUE;
    hfd_log ("hfd open: '%s'\n", name);
    if (_tcslen (name) > 4 && !_tcsncmp (name,"HD_", 3)) {
        hdf_init_target ();
        i = isharddrive (name);
        if (i >= 0) {
            udi = &uae_drives[i];
            hfd->flags = HFD_FLAGS_REALDRIVE;
            if (udi->nomedia)
                hfd->drive_empty = -1;
            if (udi->readonly)
                hfd->ci.readonly = 1;
            h = uae_tfopen (udi->device_path, hfd->ci.readonly ? "rb" : "r+b");
            hfd->handle->h = h;
            if (h == INVALID_HANDLE_VALUE)
                goto end;
            _tcsncpy (hfd->vendor_id, udi->vendor_id, 8);
            _tcsncpy (hfd->product_id, udi->product_id, 16);
            _tcsncpy (hfd->product_rev, udi->product_rev, 4);
            hfd->offset = udi->offset;
            hfd->physsize = hfd->virtsize = udi->size;
            hfd->ci.blocksize = udi->bytespersector;
            if (hfd->offset == 0 && !hfd->drive_empty) {
                int sf = safetycheck (hfd->handle->h, udi->device_path, 0, hfd->cache, hfd->ci.blocksize);
                if (sf > 0)
                    goto end;
                if (sf == 0 && !hfd->ci.readonly && harddrive_dangerous != 0x1234dead) {
                    write_log ("'%s' forced read-only, safetycheck enabled\n", udi->device_path);
                    hfd->dangerous = 1;
                    // clear GENERIC_WRITE
                    fclose (h);
                    h = uae_tfopen (udi->device_path, "r+b");
                    hfd->handle->h = h;
                    if (h == INVALID_HANDLE_VALUE)
                        goto end;
                }
            }
            hfd->handle_valid = HDF_HANDLE_LINUX;
            hfd->emptyname = strdup (name);
        } else {
            hfd->flags = HFD_FLAGS_REALDRIVE;
            hfd->drive_empty = -1;
            hfd->emptyname = strdup (name);
        }
    } else {
        int zmode = 0;
        char *ext = _tcsrchr (name, '.');
        if (ext != NULL) {
            ext++;
            for (i = 0; hdz[i]; i++) {
                if (!_tcsicmp (ext, hdz[i]))
                    zmode = 1;
            }
        }
        h = uae_tfopen (name, hfd->ci.readonly ? "rb" : "r+b");
        if (h == INVALID_HANDLE_VALUE)
            goto end;
        hfd->handle->h = h;
        i = _tcslen (name) - 1;
        while (i >= 0) {
            if ((i > 0 && (name[i - 1] == '/' || name[i - 1] == '\\')) || i == 0) {
                _tcscpy (hfd->vendor_id, "UAE");
                _tcsncpy (hfd->product_id, name + i, 15);
                _tcscpy (hfd->product_rev, "0.3");
                break;
            }
            i--;
        }
        if (h != INVALID_HANDLE_VALUE) {
            // determine size of hdf file
            int ret;
            off_t low = -1;

#if defined(MACOSX) || defined(OPENBSD)
            // check type of file
            struct stat st;
            ret = stat(name,&st);
            if (ret) {
                write_log("osx: can't stat '%s'\n", name);
                goto end;
            }
            // block devices need special handling on BSD and OSX
            if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
                int fh = fileno(h);
#if defined(MACOSX)
                uint32_t block_size;
                uint64_t block_count;
                // get number of blocks
                ret = ioctl(fh, DKIOCGETBLOCKCOUNT, &block_count);
                if (ret) {
                    write_log("osx: can't get block count of '%s' (%d)\n",
                            name, fh);
                    goto end;
                }
                // get block size
                ret = ioctl(fh, DKIOCGETBLOCKSIZE, &block_size);
                if (ret) {
                    write_log("osx: can't get block size of '%s' (%d)\n",
                            name, fh);
                    goto end;
                }
                write_log("osx: found raw device: block_size=%u "
                        "block_count=%llu\n", block_size, block_count);
                low = block_size * block_count;
#elif defined(OPENBSD)
                struct disklabel label;
                if (ioctl(fh, DIOCGDINFO, &label) < 0) {
                    write_log("openbsd: can't get disklabel of '%s' (%d)\n", name, fh);
                    goto end;
                }
                write_log("openbsd: bytes per sector: %u\n", label.d_secsize);
                write_log("openbsd: sectors per unit: %u\n", label.d_secperunit);
                low = label.d_secsize * label.d_secperunit;
                write_log("openbsd: total bytes: %llu\n", low);
#endif
            }
#endif // OPENBSD || MACOSX

            if (low == -1) {
                // assuming regular file; seek to end and ftell
                ret = uae_fseeko64 (h, 0, SEEK_END);
                if (ret)
                    goto end;
                low = uae_ftello64 (h);
                if (low == -1)
                    goto end;
            }

            low &= ~(hfd->ci.blocksize - 1);
            hfd->physsize = hfd->virtsize = low;
            if (g_debug) {
                write_log("set physsize = virtsize = %lld (low)\n",
                        hfd->virtsize);
            }
            hfd->handle_valid = HDF_HANDLE_LINUX;
            if (hfd->physsize < 64 * 1024 * 1024 && zmode) {
                write_log ("HDF '%s' re-opened in zfile-mode\n", name);
                fclose (h);
                hfd->handle->h = INVALID_HANDLE_VALUE;
                hfd->handle->zf = zfile_fopen(name, hfd->ci.readonly ? "rb" : "r+b", ZFD_NORMAL);
                hfd->handle->zfile = 1;
                if (!h)
                    goto end;
                zfile_fseek (hfd->handle->zf, 0, SEEK_END);
                hfd->physsize = hfd->virtsize = zfile_ftell (hfd->handle->zf);
                if (g_debug) {
                    write_log("set physsize = virtsize = %lld\n",
                            hfd->virtsize);
                }
                zfile_fseek (hfd->handle->zf, 0, SEEK_SET);
                hfd->handle_valid = HDF_HANDLE_ZFILE;
            }
        } else {
            write_log ("HDF '%s' failed to open. error = %d\n", name, errno);
        }
    }
    if (hfd->handle_valid || hfd->drive_empty) {
        hfd_log ("HDF '%s' opened, size=%dK mode=%d empty=%d\n",
            name, (int) (hfd->physsize / 1024), hfd->handle_valid, hfd->drive_empty);
        return 1;
    }
end:
    hdf_close (hfd);
    xfree (name);
    return 0;
}
Ejemplo n.º 19
0
static void parse_cmdline (int argc, TCHAR **argv)
{
	int i;
	static bool started;
	bool firstconfig = true;
	bool loaded = false;

	// only parse command line when starting for the first time
	if (started)
		return;
	started = true;

	for (i = 1; i < argc; i++) {
		if (!_tcsncmp (argv[i], _T("-diskswapper="), 13)) {
			TCHAR *txt = parsetextpath (argv[i] + 13);
			parse_diskswapper (txt);
			xfree (txt);
		} else if (_tcsncmp (argv[i], _T("-cfgparam="), 10) == 0) {
			;
		} else if (_tcscmp (argv[i], _T("-cfgparam")) == 0) {
			if (i + 1 < argc)
				i++;
		} else if (_tcsncmp (argv[i], _T("-config="), 8) == 0) {
			TCHAR *txt = parsetextpath (argv[i] + 8);
			currprefs.mountitems = 0;
			target_cfgfile_load (&currprefs, txt, firstconfig ? CONFIG_TYPE_ALL : CONFIG_TYPE_HARDWARE | CONFIG_TYPE_HOST | CONFIG_TYPE_NORESET, 0);
			xfree (txt);
			firstconfig = false;
			loaded = true;
		} else if (_tcsncmp (argv[i], _T("-statefile="), 11) == 0) {
			TCHAR *txt = parsetextpath (argv[i] + 11);
			savestate_state = STATE_DORESTORE;
			_tcscpy (savestate_fname, txt);
			xfree (txt);
			loaded = true;
		} else if (_tcscmp (argv[i], _T("-f")) == 0) {
			/* Check for new-style "-f xxx" argument, where xxx is config-file */
			if (i + 1 == argc) {
				write_log (_T("Missing argument for '-f' option.\n"));
			} else {
				TCHAR *txt = parsetextpath (argv[++i]);
				currprefs.mountitems = 0;
				target_cfgfile_load (&currprefs, txt, firstconfig ? CONFIG_TYPE_ALL : CONFIG_TYPE_HARDWARE | CONFIG_TYPE_HOST | CONFIG_TYPE_NORESET, 0);
				xfree (txt);
				firstconfig = false;
			}
			loaded = true;
		} else if (_tcscmp (argv[i], _T("-s")) == 0) {
			if (i + 1 == argc)
				write_log (_T("Missing argument for '-s' option.\n"));
			else
				cfgfile_parse_line (&currprefs, argv[++i], 0);
		} else if (_tcscmp (argv[i], _T("-h")) == 0 || _tcscmp (argv[i], _T("-help")) == 0) {
			usage ();
			exit (0);
		} else if (_tcsncmp (argv[i], _T("-cdimage="), 9) == 0) {
			TCHAR *txt = parsetextpath (argv[i] + 9);
			TCHAR *txt2 = xmalloc(TCHAR, _tcslen(txt) + 2);
			_tcscpy(txt2, txt);
			if (_tcsrchr(txt2, ',') != NULL)
				_tcscat(txt2, _T(","));
			cfgfile_parse_option (&currprefs, _T("cdimage0"), txt2, 0);
			xfree(txt2);
			xfree(txt);
			loaded = true;
		} else if (argv[i][0] == '-' && argv[i][1] != '\0') {
				const TCHAR *arg = argv[i] + 2;
				int extra_arg = *arg == '\0';
				if (extra_arg)
					arg = i + 1 < argc ? argv[i + 1] : 0;
				if (parse_cmdline_option (&currprefs, argv[i][1], arg) && extra_arg)
					i++;
		} else if (i == argc - 1) {
			// if last config entry is an orphan and nothing else was loaded:
			// check if it is config file or statefile
			if (!loaded) {
				TCHAR *txt = parsetextpath(argv[i]);
				struct zfile *z = zfile_fopen(txt, _T("rb"), ZFD_NORMAL);
				if (z) {
					int type = zfile_gettype(z);
					zfile_fclose(z);
					if (type == ZFILE_CONFIGURATION) {
						currprefs.mountitems = 0;
						target_cfgfile_load(&currprefs, txt, CONFIG_TYPE_ALL, 0);
					} else if (type == ZFILE_STATEFILE) {
						savestate_state = STATE_DORESTORE;
						_tcscpy(savestate_fname, txt);
					}
				}
				xfree(txt);
			}
		}
	}
}
Ejemplo n.º 20
0
static int parsemds (struct cdunit *cdu, struct zfile *zmds, const TCHAR *img)
{
	MDS_Header *head;
	struct cdtoc *t;
	uae_u8 *mds = NULL;
	uae_u64 size;
	MDS_SessionBlock *sb;

	write_log (_T("MDS TOC: '%s'\n"), img);
	size = zfile_size (zmds);
	mds = xmalloc (uae_u8, size);
	if (!mds)
		goto end;
	if (zfile_fread (mds, size, 1, zmds) != 1)
		goto end;

	head = (MDS_Header*)mds;
	if (!memcmp (&head, MEDIA_DESCRIPTOR, strlen (MEDIA_DESCRIPTOR)))
		goto end;
	if (head->version[0] != 1) {
		write_log (_T("unsupported MDS version %d, only v.1 supported\n"), head->version[0]);
		goto end;
	}

	sb = (MDS_SessionBlock*)(mds + head->sessions_blocks_offset);
	cdu->tracks = sb->last_track - sb->first_track + 1;
	for (int i = 0; i < sb->num_all_blocks; i++) {
		MDS_TrackBlock *tb = (MDS_TrackBlock*)(mds + sb->tracks_blocks_offset + i * sizeof (MDS_TrackBlock));
		int point = tb->point;
		int tracknum = -1;
		if (point == 0xa2)
			tracknum = cdu->tracks;
		else if (point >= 1 && point <= 99)
			tracknum = point - 1;
		if (tracknum >= 0) {
			MDS_Footer *footer = tb->footer_offset == 0 ? NULL : (MDS_Footer*)(mds + tb->footer_offset);
			MDS_TrackExtraBlock *teb = tb->extra_offset == 0 ? NULL : (MDS_TrackExtraBlock*)(mds + tb->extra_offset);
			t = &cdu->toc[tracknum];
			t->adr = tb->adr_ctl >> 4;
			t->ctrl = tb->adr_ctl & 15;
			if (point == 0xa2)
				t->address = sb->session_end;
			else
				t->address = tb->start_sector;
			t->track = point;
			t->offset = tb->start_offset;
			t->size = tb->sector_size;

			if (point >= 100)
				continue;

			if (footer) {
				TCHAR *fname = NULL;
				if (footer->widechar_filename == 0)
					fname = au ((char*)(mds + footer->filename_offset));
				else
					fname = my_strdup ((TCHAR*)(mds + footer->filename_offset));
				if (fname[0] == '*' && fname[1] == '.') {
					TCHAR newname[MAX_DPATH];
					_tcscpy (newname, img);
					TCHAR *ext = _tcsrchr (newname, '.');
					if (ext)
						_tcscpy (ext, fname + 1);
					xfree (fname);
					fname = my_strdup (newname);
				}

				t->handle = zfile_fopen (fname, _T("rb"), ZFD_NORMAL);
				t->fname = my_strdup (fname);
				if (t->handle)
					t->filesize = zfile_size (t->handle);
			}

			if (tb->subchannel && t->handle) {
				t->suboffset = t->size;
				t->subcode = 1; // interleaved
				t->subhandle = zfile_dup (t->handle);
				t->skipsize = SUB_CHANNEL_SIZE;
				t->size -= SUB_CHANNEL_SIZE;
			}
			if ((t->ctrl & 0x0c) != 4)
				t->enctype = AUDENC_PCM;
		}
	}
Ejemplo n.º 21
0
static int unpack2 (const TCHAR *src, const TCHAR *match, int level)
{
	struct zdirectory *h;
	struct zvolume *zv;
	int ret;
	uae_u8 *b;
	int size;
	TCHAR fn[MAX_DPATH];

	ret = 0;
	zv = zfile_fopen_archive_root (src, ZFD_ALL);
	if (zv == NULL) {
		geterror();
		_tprintf (_T("Couldn't open archive '%s'\n"), src);
		return 0;
	}
	h = zfile_opendir_archive (src);
	if (!h) {
		geterror();
		_tprintf (_T("Couldn't open directory '%s'\n"), src);
		return 0;
	}
	while (zfile_readdir_archive (h, fn)) {
		TCHAR tmp[MAX_DPATH];
		TCHAR *dst;
		struct zfile *s, *d;
		int isdir, flags;

		_tcscpy (tmp, src);
		_tcscat (tmp, sep);
		_tcscat (tmp, fn);
		zfile_fill_file_attrs_archive (tmp, &isdir, &flags, NULL);
		if (isdir) {
			TCHAR *p = _tcsstr (fn, _T(".DIR"));
			if (isdir == ZNODE_VDIR && p && _tcslen (p) == 4) {
				p[0] = 0;
				if (pattern_match (fn, match))
					continue;
				p[0] = '.';
			}
			unpack2 (tmp, match, 1);
			continue;
		}

		if (pattern_match (fn, match)) {
			struct mystat st;

			if (!zfile_stat_archive (tmp, &st)) {
				st.mtime.tv_sec = st.mtime.tv_usec = -1;
			}
			found = 1;
			dst = fn;
			s = zfile_open_archive (tmp, ZFD_NORECURSE);
			if (!s) {
				geterror();
				_tprintf (_T("Couldn't open '%s' for reading\n"), tmp);
				continue;
			}
			zfile_fseek (s, 0, SEEK_END); 
			size = zfile_ftell (s);
			zfile_fseek (s, 0, SEEK_SET);
			b = xcalloc (uae_u8, size);
			if (b) {
				if (zfile_fread (b, size, 1, s) == 1) {
					d = zfile_fopen (dst, _T("wb"), 0);
					if (d) {
						if (zfile_fwrite (b, size, 1, d) == 1) {
							ret = 1;
							_tprintf (_T("%s extracted, %d bytes\n"), dst, size);
						}
						zfile_fclose (d);
						setdate (dst, st.mtime.tv_sec);
					}
				}
				xfree (b);
			}
			zfile_fclose (s);
		}
	}
	geterror ();
	if (!found && !level) {
		_tprintf (_T("'%s' not matched\n"), match);
	}
	return ret;
}
Ejemplo n.º 22
0
int cartridge_attach_image(int type, const char *filename)
{
    BYTE rawcart[0x4000];
    FILE *fd;
    int addr;
    size_t n;
    int type2 = CARTRIDGE_VIC20_DETECT;

    /* Attaching no cartridge always works.  */
    if (type == CARTRIDGE_NONE || filename==NULL || *filename == '\0')
        return 0;

    log_message(LOG_DEFAULT, "Attached cartridge type %d, file=`%s'.",
                type, filename);

    fd = zfile_fopen(filename, MODE_READ);
    if (!fd)
        return -1;

    addr = fgetc(fd);
    addr = (addr & 0xff) | ((fgetc(fd) << 8) & 0xff00);

    if (addr == 0x6000 || addr == 0x7000) {
        type2 = CARTRIDGE_VIC20_16KB_6000;
    } else if (addr == 0xA000) {
        type2 = CARTRIDGE_VIC20_8KB_A000;
    } else if (addr == 0x2000 || addr == 0x3000) {
        type2 = CARTRIDGE_VIC20_16KB_2000;
    } else if (addr == 0xB000) {
        type2 = CARTRIDGE_VIC20_4KB_B000;
    } else if (addr == 0x4000 || addr == 0x5000) {
        type2 = CARTRIDGE_VIC20_16KB_4000;
    }
    if (type2 == CARTRIDGE_VIC20_DETECT) {
        /* rewind to the beginning of the file (no load address) */
        fseek(fd, 0, SEEK_SET);
    }
    if (type == CARTRIDGE_VIC20_DETECT) {
        type = type2;
    }

    memset(rawcart, 0xff, 0x4000);

    switch (type) {
      case CARTRIDGE_VIC20_16KB_4000:
        if ((n = fread(rawcart, 0x1000, 4, fd)) < 1) {
            zfile_fclose(fd);
            return -1;
        }
        if (n < 4) {
            type = CARTRIDGE_VIC20_8KB_4000;
            if (n < 2) {
                memcpy(rawcart + 0x1000, rawcart, 0x1000);
            }
        }
        util_string_set(&cartfile4, filename);
        break;
      case CARTRIDGE_VIC20_16KB_2000:
        if ((n = fread(rawcart, 0x1000, 4, fd)) < 1) {
            zfile_fclose(fd);
            return -1;
        }
        if (n < 4) {
            type = CARTRIDGE_VIC20_8KB_2000;
            if (n < 2) {
                /* type = CARTRIDGE_VIC20_4KB_2000; */
                memcpy(rawcart + 0x1000, rawcart, 0x1000);
            }
        }
        util_string_set(&cartfile2, filename);
        break;
      case CARTRIDGE_VIC20_16KB_6000:
        if ((n = fread(rawcart, 0x1000, 4, fd)) < 1) {
            zfile_fclose(fd);
            return -1;
        }
        if (n < 4) {
            type = CARTRIDGE_VIC20_8KB_6000;
            if (n < 2) {
                /* type = CARTRIDGE_VIC20_4KB_6000; */
                memcpy(rawcart + 0x1000, rawcart, 0x1000);
            }
        }
        util_string_set(&cartfile6, filename);
        break;
      case CARTRIDGE_VIC20_8KB_A000:
        if ((n = fread(rawcart, 0x1000, 2, fd)) < 1) {
            zfile_fclose(fd);
            return -1;
        }
        if (n < 2) {
            if (cartfileB) {
                type = CARTRIDGE_VIC20_4KB_A000;
            } else {
                memcpy(rawcart + 0x1000, rawcart, 0x1000);
            }
        }
        util_string_set(&cartfileA, filename);
        break;
      case CARTRIDGE_VIC20_4KB_B000:
        if ((n = fread(rawcart, 0x1000, 1, fd)) < 1) {
            zfile_fclose(fd);
            return -1;
        }
        util_string_set(&cartfileB, filename);
        break;
      default:
        zfile_fclose(fd);
        return -1;
    }

    zfile_fclose(fd);

    mem_attach_cartridge(type, rawcart);

    return 0;
}