static UINT64 corefile_filesizeproc(void *file) { long l, sz; l = core_ftell((core_file*)file); if (core_fseek((core_file*)file, 0, SEEK_END)) return (size_t) -1; sz = core_ftell((core_file*)file); if (core_fseek((core_file*)file, l, SEEK_SET)) return (size_t) -1; return (size_t) sz; }
UINT32 stream_read(imgtool_stream *stream, void *buf, UINT32 sz) { UINT32 result = 0; switch(stream->imgtype) { case IMG_FILE: assert(sz == (UINT32) sz); core_fseek(stream->u.file, stream->position, SEEK_SET); result = core_fread(stream->u.file, buf, (UINT32) sz); break; case IMG_MEM: /* do we have to limit sz? */ if (sz > (stream->filesize - stream->position)) sz = (UINT32) (stream->filesize - stream->position); memcpy(buf, stream->u.buffer + stream->position, sz); result = sz; break; default: assert(0); break; } stream->position += result; return result; }
bool cbm_crt_read_data(core_file* file, UINT8 *roml, UINT8 *romh) { offs_t roml_offset = 0; offs_t romh_offset = 0; core_fseek(file, CRT_HEADER_LENGTH, SEEK_SET); while (!core_feof(file)) { cbm_crt_chip chip; core_fread(file, &chip, CRT_CHIP_LENGTH); UINT16 address = pick_integer_be(chip.start_address, 0, 2); UINT16 size = pick_integer_be(chip.image_size, 0, 2); switch (address) { case 0x8000: core_fread(file, roml + roml_offset, size); roml_offset += size; break; case 0xa000: core_fread(file, romh + romh_offset, size); romh_offset += size; break; case 0xe000: core_fread(file, romh + romh_offset, size); romh_offset += size; break; } } return true; }
bool cbm_crt_read_header(core_file* file, size_t *roml_size, size_t *romh_size, int *exrom, int *game) { // read the header cbm_crt_header header; core_fread(file, &header, CRT_HEADER_LENGTH); if (memcmp(header.signature, CRT_SIGNATURE, 16) != 0) return false; UINT16 hardware = pick_integer_be(header.hardware, 0, 2); *exrom = header.exrom; *game = header.game; if (LOG) { logerror("Name: %s\n", header.name); logerror("Hardware: %04x\n", hardware); logerror("Slot device: %s\n", CRT_C64_SLOT_NAMES[hardware]); logerror("EXROM: %u\n", header.exrom); logerror("GAME: %u\n", header.game); } // determine ROM region lengths while (!core_feof(file)) { cbm_crt_chip chip; core_fread(file, &chip, CRT_CHIP_LENGTH); UINT16 address = pick_integer_be(chip.start_address, 0, 2); UINT16 size = pick_integer_be(chip.image_size, 0, 2); UINT16 type = pick_integer_be(chip.chip_type, 0, 2); if (LOG) { logerror("CHIP Address: %04x\n", address); logerror("CHIP Size: %04x\n", size); logerror("CHIP Type: %04x\n", type); } switch (address) { case 0x8000: *roml_size += size; break; case 0xa000: *romh_size += size; break; case 0xe000: *romh_size += size; break; default: logerror("Invalid CHIP loading address!\n"); break; } core_fseek(file, size, SEEK_CUR); } return true; }
int emu_file::seek(INT64 offset, int whence) { // load the ZIP file now if we haven't yet if (compressed_file_ready()) return 1; // seek if we can if (m_file != NULL) return core_fseek(m_file, offset, whence); return 1; }
int emu_file::seek(INT64 offset, int whence) { // load the ZIP file now if we haven't yet if (m_zipfile != NULL && load_zipped_file() != FILERR_NONE) return 1; // seek if we can if (m_file != NULL) return core_fseek(m_file, offset, whence); return 1; }
UINT32 stream_write(imgtool_stream *s, const void *buf, UINT32 sz) { void *new_buffer; UINT32 result = 0; switch(s->imgtype) { case IMG_MEM: if (!s->write_protect) { /* do we have to expand the buffer? */ if (s->filesize < s->position + sz) { /* try to expand the buffer */ if (s->u.buffer) free(s->u.buffer); new_buffer = malloc(s->position + sz); if (new_buffer) { s->u.buffer = (UINT8*)new_buffer; s->filesize = s->position + sz; } } /* do we have to limit sz? */ if (sz > (s->filesize - s->position)) sz = (UINT32) (s->filesize - s->position); memcpy(s->u.buffer + s->position, buf, sz); result = sz; } break; case IMG_FILE: core_fseek(s->u.file, s->position, SEEK_SET); result = core_fwrite(s->u.file, buf, sz); break; default: assert(0); break; } /* advance the file pointer */ s->position += result; /* did we grow the file */ if (s->position > s->filesize) s->filesize = s->position; return result; }
int core_fgetc(core_file *file) { int result; /* refresh buffer, if necessary */ if (file->back_char_head == file->back_char_tail) { utf16_char utf16_buffer[UTF16_CHAR_MAX]; char utf8_buffer[UTF8_CHAR_MAX]; char default_buffer[16]; unicode_char uchar = (unicode_char) ~0; int readlen, charlen; /* do we need to check the byte order marks? */ if (file->offset == 0) { UINT8 bom[4]; int pos = 0; if (core_fread(file, bom, 4) == 4) { if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) { file->text_type = TFT_UTF8; pos = 3; } else if (bom[0] == 0x00 && bom[1] == 0x00 && bom[2] == 0xfe && bom[3] == 0xff) { file->text_type = TFT_UTF32BE; pos = 4; } else if (bom[0] == 0xff && bom[1] == 0xfe && bom[2] == 0x00 && bom[3] == 0x00) { file->text_type = TFT_UTF32LE; pos = 4; } else if (bom[0] == 0xfe && bom[1] == 0xff) { file->text_type = TFT_UTF16BE; pos = 2; } else if (bom[0] == 0xff && bom[1] == 0xfe) { file->text_type = TFT_UTF16LE; pos = 2; } else { file->text_type = TFT_OSD; pos = 0; } } core_fseek(file, pos, SEEK_SET); } /* fetch the next character */ switch (file->text_type) { default: case TFT_OSD: readlen = core_fread(file, default_buffer, sizeof(default_buffer)); if (readlen > 0) { charlen = osd_uchar_from_osdchar(&uchar, default_buffer, readlen / sizeof(default_buffer[0])); core_fseek(file, (INT64) (charlen * sizeof(default_buffer[0])) - readlen, SEEK_CUR); } break; case TFT_UTF8: readlen = core_fread(file, utf8_buffer, sizeof(utf8_buffer)); if (readlen > 0) { charlen = uchar_from_utf8(&uchar, utf8_buffer, readlen / sizeof(utf8_buffer[0])); core_fseek(file, (INT64) (charlen * sizeof(utf8_buffer[0])) - readlen, SEEK_CUR); } break; case TFT_UTF16BE: readlen = core_fread(file, utf16_buffer, sizeof(utf16_buffer)); if (readlen > 0) { charlen = uchar_from_utf16be(&uchar, utf16_buffer, readlen / sizeof(utf16_buffer[0])); core_fseek(file, (INT64) (charlen * sizeof(utf16_buffer[0])) - readlen, SEEK_CUR); } break; case TFT_UTF16LE: readlen = core_fread(file, utf16_buffer, sizeof(utf16_buffer)); if (readlen > 0) { charlen = uchar_from_utf16le(&uchar, utf16_buffer, readlen / sizeof(utf16_buffer[0])); core_fseek(file, (INT64) (charlen * sizeof(utf16_buffer[0])) - readlen, SEEK_CUR); } break; case TFT_UTF32BE: if (core_fread(file, &uchar, sizeof(uchar)) == sizeof(uchar)) uchar = BIG_ENDIANIZE_INT32(uchar); break; case TFT_UTF32LE: if (core_fread(file, &uchar, sizeof(uchar)) == sizeof(uchar)) uchar = LITTLE_ENDIANIZE_INT32(uchar); break; } if (uchar != ~0) { /* place the new character in the ring buffer */ file->back_char_head = 0; file->back_char_tail = utf8_from_uchar(file->back_chars, ARRAY_LENGTH(file->back_chars), uchar); /* assert(file->back_char_tail != -1);*/ } } /* now read from the ring buffer */ if (file->back_char_head != file->back_char_tail) { result = file->back_chars[file->back_char_head++]; file->back_char_head %= ARRAY_LENGTH(file->back_chars); } else result = EOF; return result; }
static int corefile_seekproc(void *file, INT64 offset, int whence) { return core_fseek((core_file*)file, (long) offset, whence); }
static bool render_font_save_cached(render_font &font, const char *filename, UINT32 hash) { // attempt to open the file core_file *file; file_error filerr = core_fopen(filename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, &file); if (filerr != FILERR_NONE) return true; try { // determine the number of characters int numchars = 0; for (int chnum = 0; chnum < 65536; chnum++) if (font.chars[chnum].width > 0) numchars++; // write the header dynamic_buffer tempbuffer(65536); UINT8 *dest = &tempbuffer[0]; *dest++ = 'f'; *dest++ = 'o'; *dest++ = 'n'; *dest++ = 't'; *dest++ = hash >> 24; *dest++ = hash >> 16; *dest++ = hash >> 8; *dest++ = hash & 0xff; *dest++ = font.height >> 8; *dest++ = font.height & 0xff; *dest++ = font.yoffs >> 8; *dest++ = font.yoffs & 0xff; *dest++ = numchars >> 24; *dest++ = numchars >> 16; *dest++ = numchars >> 8; *dest++ = numchars & 0xff; write_data(*file, tempbuffer, dest); // write the empty table to the beginning of the file dynamic_buffer chartable(numchars * CACHED_CHAR_SIZE + 1, 0); write_data(*file, &chartable[0], &chartable[numchars * CACHED_CHAR_SIZE]); // loop over all characters int tableindex = 0; for (int chnum = 0; chnum < 65536; chnum++) { render_font_char &ch = font.chars[chnum]; if (ch.width > 0) { // write out a bit-compressed bitmap if we have one if (ch.bitmap != NULL) { // write the data to the tempbuffer dest = tempbuffer; UINT8 accum = 0; UINT8 accbit = 7; // bit-encode the character data for (int y = 0; y < ch.bmheight; y++) { int desty = y + font.height + font.yoffs - ch.yoffs - ch.bmheight; const UINT32 *src = (desty >= 0 && desty < font.height) ? &ch.bitmap->pix32(desty) : NULL; for (int x = 0; x < ch.bmwidth; x++) { if (src != NULL && src[x] != 0) accum |= 1 << accbit; if (accbit-- == 0) { *dest++ = accum; accum = 0; accbit = 7; } } } // flush any extra if (accbit != 7) *dest++ = accum; // write the data write_data(*file, tempbuffer, dest); // free the bitmap and texture global_free(ch.bitmap); ch.bitmap = NULL; } // compute the table entry dest = &chartable[tableindex++ * CACHED_CHAR_SIZE]; *dest++ = chnum >> 8; *dest++ = chnum & 0xff; *dest++ = ch.width >> 8; *dest++ = ch.width & 0xff; *dest++ = ch.xoffs >> 8; *dest++ = ch.xoffs & 0xff; *dest++ = ch.yoffs >> 8; *dest++ = ch.yoffs & 0xff; *dest++ = ch.bmwidth >> 8; *dest++ = ch.bmwidth & 0xff; *dest++ = ch.bmheight >> 8; *dest++ = ch.bmheight & 0xff; } } // seek back to the beginning and rewrite the table core_fseek(file, CACHED_HEADER_SIZE, SEEK_SET); write_data(*file, &chartable[0], &chartable[numchars * CACHED_CHAR_SIZE]); // all done core_fclose(file); return false; } catch (...) { core_fclose(file); osd_rmfile(filename); return true; } }