static int drive_snapshot_write_p64image_module(snapshot_t *s, unsigned int dnr) { char snap_module_name[10]; snapshot_module_t *m; drive_t *drive; TP64MemoryStream P64MemoryStreamInstance; PP64Image P64Image; drive = drive_context[dnr]->drive; sprintf(snap_module_name, "P64IMAGE%i", dnr); m = snapshot_module_create(s, snap_module_name, GCRIMAGE_SNAP_MAJOR, GCRIMAGE_SNAP_MINOR); if (m == NULL) { return -1; } P64Image = (void*)drive->p64; if (P64Image == NULL) { if (m != NULL) { snapshot_module_close(m); } return -1; } P64MemoryStreamCreate(&P64MemoryStreamInstance); P64MemoryStreamClear(&P64MemoryStreamInstance); if (!P64ImageWriteToStream(P64Image, &P64MemoryStreamInstance)) { P64MemoryStreamDestroy(&P64MemoryStreamInstance); return -1; } if (SMW_DW(m, P64MemoryStreamInstance.Size) < 0 || SMW_BA(m, P64MemoryStreamInstance.Data, P64MemoryStreamInstance.Size) < 0) { if (m != NULL) { snapshot_module_close(m); } P64MemoryStreamDestroy(&P64MemoryStreamInstance); return -1; } P64MemoryStreamDestroy(&P64MemoryStreamInstance); if (snapshot_module_close(m) < 0) { return -1; } return 0; }
static int fsimage_create_p64(disk_image_t *image) { TP64MemoryStream P64MemoryStreamInstance; TP64Image P64Image; BYTE gcr_track[7928], *gcrptr; unsigned int track, sector; fsimage_t *fsimage; int rc = -1; fsimage = image->media.fsimage; P64ImageCreate(&P64Image); for (track = 0; track < MAX_TRACKS_1541; track++) { const int raw_track_size[4] = { 6250, 6666, 7142, 7692 }; memset(&gcr_track[0], 0x55, 7928); gcrptr = &gcr_track[0]; for (sector = 0; sector < disk_image_sector_per_track(DISK_IMAGE_TYPE_D64, track + 1); sector++) { BYTE chksum, id[2], rawdata[260]; int i; id[0] = id[1] = 0xa0; memset(rawdata, 0, 260); rawdata[0] = 7; chksum = rawdata[1]; for (i = 1; i < 256; i++) { chksum ^= rawdata[i + 1]; } rawdata[257] = chksum; gcr_convert_sector_to_GCR(rawdata, gcrptr, track + 1, sector, id[0], id[1], 0); gcrptr += 360; } P64PulseStreamConvertFromGCR(&P64Image.PulseStreams[(track + 1) << 1], (void*)&gcr_track[0], raw_track_size[disk_image_speed_map_1541(track)] << 3); } P64MemoryStreamCreate(&P64MemoryStreamInstance); P64MemoryStreamClear(&P64MemoryStreamInstance); if (P64ImageWriteToStream(&P64Image,&P64MemoryStreamInstance)) { if (fwrite(P64MemoryStreamInstance.Data, P64MemoryStreamInstance.Size, 1, fsimage->fd) < 1) { log_error(createdisk_log, "Cannot write image data."); rc = -1; } else { rc = 0; } } P64MemoryStreamDestroy(&P64MemoryStreamInstance); P64ImageDestroy(&P64Image); return rc; }
int fsimage_write_p64_image(disk_image_t *image) { TP64MemoryStream P64MemoryStreamInstance; PP64Image P64Image = (void*)image->p64; int rc; fsimage_t *fsimage; fsimage = image->media.fsimage; P64MemoryStreamCreate(&P64MemoryStreamInstance); P64MemoryStreamClear(&P64MemoryStreamInstance); if (P64ImageWriteToStream(P64Image,&P64MemoryStreamInstance)) { fseek(fsimage->fd, 0, SEEK_SET); if (fwrite(P64MemoryStreamInstance.Data, P64MemoryStreamInstance.Size, 1, fsimage->fd) < 1) { rc = -1; log_error(fsimage_p64_log, "Could not write P64 disk image."); } else { fflush(fsimage->fd); rc = 0; } } else { rc = -1; log_error(fsimage_p64_log, "Could not write P64 disk image stream."); } P64MemoryStreamDestroy(&P64MemoryStreamInstance); return rc; }
int fsimage_read_p64_image(disk_image_t *image) { TP64MemoryStream P64MemoryStreamInstance; PP64Image P64Image = (void*)image->p64; int lSize, rc; void *buffer; fsimage_t *fsimage; fsimage = image->media.fsimage; if (image->gcr) { image->gcr->max_track_size = NUM_MAX_MEM_BYTES_TRACK; } fseek(fsimage->fd, 0, SEEK_END); lSize = ftell(fsimage->fd); fseek(fsimage->fd, 0, SEEK_SET); buffer = (char*)lib_malloc(sizeof(char) * lSize); if (fread(buffer, 1, lSize, fsimage->fd) < 1) { lib_free(buffer); log_error(fsimage_p64_log, "Could not read P64 disk image."); return -1; } /*num_tracks = image->tracks;*/ P64MemoryStreamCreate(&P64MemoryStreamInstance); P64MemoryStreamWrite(&P64MemoryStreamInstance,buffer,lSize); P64MemoryStreamSeek(&P64MemoryStreamInstance,0); if (P64ImageReadFromStream(P64Image,&P64MemoryStreamInstance)) { rc = 0; } else { rc = -1; log_error(fsimage_p64_log, "Could not read P64 disk image stream."); } P64MemoryStreamDestroy(&P64MemoryStreamInstance); lib_free(buffer); return rc; }
static int drive_snapshot_read_p64image_module(snapshot_t *s, unsigned int dnr) { BYTE major_version, minor_version; snapshot_module_t *m; char snap_module_name[10]; BYTE *tmpbuf; drive_t *drive; TP64MemoryStream P64MemoryStreamInstance; PP64Image P64Image; DWORD size; drive = drive_context[dnr]->drive; sprintf(snap_module_name, "P64IMAGE%i", dnr); m = snapshot_module_open(s, snap_module_name, &major_version, &minor_version); if (m == NULL) { return 0; } P64Image = (void*)drive->p64; if (P64Image == NULL) { if (m != NULL) { snapshot_module_close(m); } return -1; } if (major_version > P64IMAGE_SNAP_MAJOR || minor_version > P64IMAGE_SNAP_MINOR) { log_error(drive_snapshot_log, "Snapshot module version (%d.%d) newer than %d.%d.", major_version, minor_version, P64IMAGE_SNAP_MAJOR, P64IMAGE_SNAP_MINOR); } if (SMR_DW(m, &size) < 0) { if (m != NULL) { snapshot_module_close(m); } return -1; } tmpbuf = lib_malloc(size); if (SMR_BA(m, tmpbuf, size) < 0) { if (m != NULL) { snapshot_module_close(m); } lib_free(tmpbuf); return -1; } P64MemoryStreamCreate(&P64MemoryStreamInstance); P64MemoryStreamClear(&P64MemoryStreamInstance); P64MemoryStreamWrite(&P64MemoryStreamInstance, tmpbuf, size); P64MemoryStreamSeek(&P64MemoryStreamInstance, 0); if (!P64ImageReadFromStream(P64Image, &P64MemoryStreamInstance)) { if (m != NULL) { snapshot_module_close(m); } lib_free(tmpbuf); P64MemoryStreamDestroy(&P64MemoryStreamInstance); return -1; } P64MemoryStreamDestroy(&P64MemoryStreamInstance); snapshot_module_close(m); m = NULL; lib_free(tmpbuf); drive->P64_image_loaded = 1; drive->complicated_image_loaded = 1; drive->image = NULL; return 0; }