Files
HxCFloppyEmulator/libhxcfe/sources/loaders/xml_loader/xml_writer.c
T

453 lines
14 KiB
C

/*
//
// Copyright (C) 2006-2026 Jean-François DEL NERO
//
// This file is part of the HxCFloppyEmulator library
//
// HxCFloppyEmulator may be used and distributed without restriction provided
// that this copyright statement is not removed from the file and that any
// derivative work contains the original copyright notice and the associated
// disclaimer.
//
// HxCFloppyEmulator is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// HxCFloppyEmulator is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with HxCFloppyEmulator; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "internal_libhxcfe.h"
#include "tracks/track_generator.h"
#include "libhxcfe.h"
#include "xml_loader.h"
#include "tracks/sector_extractor.h"
#include "libhxcadaptor.h"
#include "floppy_utils.h"
#include "version.h"
void gettracktype(HXCFE_SECTORACCESS* ss,int track,int side,int * nbsect,int *firstsectid,char * format,int * format_id, int *sectorsize)
{
int i;
HXCFE_SECTCFG** sca;
int32_t nb_sectorfound;
*sectorsize = 512;
*firstsectid = 1;
sprintf(format,"IBM_MFM");
*format_id = IBMFORMAT_DD;
*nbsect = 0;
sca = hxcfe_getAllTrackISOSectors(ss,track,side,&nb_sectorfound);
if(nb_sectorfound)
{
*sectorsize = sca[0]->sectorsize;
*firstsectid = 0xFF;
*nbsect = nb_sectorfound;
if(
( sca[0]->trackencoding == IBMFORMAT_SD ) ||
( sca[0]->trackencoding == ISOFORMAT_SD )
)
{
sprintf(format,"IBM_FM");
*format_id = IBMFORMAT_SD;
}
else
{
sprintf(format,"IBM_MFM");
*format_id = IBMFORMAT_DD;
}
for(i=0;i<nb_sectorfound;i++)
{
if(sca[i]->sector<*firstsectid)
*firstsectid = sca[i]->sector;
hxcfe_freeSectorConfig (ss,sca[i]);
}
free(sca);
}
}
typedef struct sect_offset_
{
HXCFE_SECTCFG* ss;
uint32_t offset;
}sect_offset;
static void exchange(sect_offset ** table, int a, int b)
{
sect_offset * temp;
temp = table[a];
table[a] = table[b];
table[b] = temp;
}
static void quickSort(sect_offset ** table, int start, int end)
{
int left = start-1;
int right = end+1;
const int pivot = table[start]->ss->sector;
if(start >= end)
return;
while(1)
{
do right--; while(table[right]->ss->sector > pivot);
do left++; while(table[left]->ss->sector < pivot);
if(left < right)
exchange(table, left, right);
else break;
}
quickSort(table, start, right);
quickSort(table, right+1, end);
}
int XML_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename)
{
int i,j,k,s,vs;
FILE * xmlfile;
int fileoffset;
int export_mode;
int32_t nb_sectorfound,nb_validsectorfound;
int nbsect,firstsectid,sectorsize,imagesize;
char trackformat[32];
HXCFE_SECTORACCESS* ss;
HXCFE_SECTCFG** sca;
HXCFE_SECTCFG* sectp;
sect_offset ** sorted_sectoffset,**sectoffset;
uint32_t crc32;
int trackformatid;
sectoffset = NULL;
sorted_sectoffset = NULL;
sca = NULL;
imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Write XML file %s...",filename);
fileoffset = 0;
export_mode = hxcfe_getEnvVarValue( imgldr_ctx->hxcfe, "XMLEXPORT_MODE" );
xmlfile=hxc_fopen(filename,"w+");
if(xmlfile)
{
imagesize = hxcfe_getFloppySize(imgldr_ctx->hxcfe,floppy,0,0);
ss=hxcfe_initSectorAccess(imgldr_ctx->hxcfe,floppy);
if(ss)
{
fprintf(xmlfile,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
fprintf(xmlfile,"<!-- HxC Floppy Emulator Disk Layout -->\n");
fprintf(xmlfile,"<disk_layout>\n");
fprintf(xmlfile,"\t<libhxcfe_version>v%s,Release_Date:%s,Build_Date:%s</libhxcfe_version>\n",hxcfe_getVersion(imgldr_ctx->hxcfe),STR_DATE, __DATE__ );
fprintf(xmlfile,"\t<disk_layout_name>AUTOGENERATEDLAYOUT</disk_layout_name>\n");
fprintf(xmlfile,"\t<disk_layout_description>Auto Generated Disk Layout</disk_layout_description>\n");
fprintf(xmlfile,"\t<prefered_file_extension>img</prefered_file_extension>\n");
fprintf(xmlfile,"\t<interface_mode>GENERIC_SHUGART_DD_FLOPPYMODE</interface_mode>\n");
if( hxcfe_floppyGetFlags( imgldr_ctx->hxcfe, floppy ) & HXCFE_FLOPPY_WRPROTECTED_FLAG )
{
fprintf(xmlfile,"\t<write_protected>1</write_protected>\n");
}
else
{
fprintf(xmlfile,"\t<write_protected>0</write_protected>\n");
}
fprintf(xmlfile,"\t<file_size>%d</file_size>\n",imagesize);
fprintf(xmlfile,"\t<layout>\n");
fprintf(xmlfile,"\t\t<number_of_track>%d</number_of_track>\n",floppy->floppyNumberOfTrack);
fprintf(xmlfile,"\t\t<number_of_side>%d</number_of_side>\n",floppy->floppyNumberOfSide);
gettracktype(ss,0,0,&nbsect,&firstsectid,(char*)&trackformat,&trackformatid,&sectorsize);
fprintf(xmlfile,"\t\t<format>%s</format>\n",trackformat);
fprintf(xmlfile,"\t\t<start_sector_id>%d</start_sector_id>\n",firstsectid);
fprintf(xmlfile,"\t\t<sector_per_track>%d</sector_per_track>\n",nbsect);
fprintf(xmlfile,"\t\t<sector_size>%d</sector_size>\n",sectorsize);
fprintf(xmlfile,"\t\t<formatvalue>%d</formatvalue>\n",0x00);
fprintf(xmlfile,"\t\t<gap3>%d</gap3>\n",0xFF);
if(floppy->floppyBitRate!=-1)
{
fprintf(xmlfile,"\t\t<bitrate>%d</bitrate>\n",floppy->floppyBitRate);
}
else
{
fprintf(xmlfile,"\t\t<bitrate>%d</bitrate>\n",(int)((float)(floppy->tracks[0]->sides[0]->tracklen/2) * ( (float)floppy->tracks[0]->floppyRPM / (float)60)));
}
fprintf(xmlfile,"\t\t<pregap>%d</pregap>\n",0);
fprintf(xmlfile,"\t\t<rpm>%d</rpm>\n",floppy->tracks[0]->floppyRPM);
hxcfe_getHash( imgldr_ctx->hxcfe, floppy, -1, -1, -1, (0x1<<ISOIBM_FM_ENCODING) | (0x1<<ISOIBM_MFM_ENCODING), HASH_TYPE_CRC32, HASH_FLAG_DATA | HASH_FLAG_PHYSICALORDER, &crc32, sizeof(crc32) );
fprintf(xmlfile,"\t\t<crc32>0x%.8X</crc32>\n",crc32);
fprintf(xmlfile,"\t\t<track_list>\n");
for(j=0;j<floppy->floppyNumberOfTrack;j++)
{
for(i=0;i<floppy->floppyNumberOfSide;i++)
{
fprintf(xmlfile,"\t\t\t<track track_number=\"%.2d\" side_number=\"%d\">\n",j,i);
fprintf(xmlfile,"\t\t\t\t<data_offset>0x%.6X</data_offset>\n",fileoffset);
if( export_mode > 0 )
{
fprintf(xmlfile,"\t\t\t\t<track_length_cells>%d</track_length_cells>\n",floppy->tracks[j]->sides[i]->tracklen);
fprintf(xmlfile,"\t\t\t\t<track_length_us>%.2f</track_length_us>\n",GetTrackPeriod(imgldr_ctx->hxcfe,floppy->tracks[j]->sides[i])*1000*1000);
}
gettracktype(ss,j,i,&nbsect,&firstsectid,(char*)&trackformat,&trackformatid,&sectorsize);
fprintf(xmlfile,"\t\t\t\t<format>%s</format>\n",trackformat);
nb_sectorfound = 0;
nb_validsectorfound = 0;
sca = hxcfe_getAllTrackISOSectors(ss,j,i,&nb_sectorfound);
fprintf(xmlfile,"\t\t\t\t<sector_list>\n");
if(nb_sectorfound > 128 * 1024)
nb_sectorfound = 128 * 1024;
if(sca && nb_sectorfound)
{
sectoffset = calloc( 1, sizeof(sect_offset*) * (int)nb_sectorfound);
if( !sectoffset )
goto alloc_error;
sorted_sectoffset = calloc( 1, sizeof(sect_offset*) * (int)nb_sectorfound);
if( !sorted_sectoffset )
goto alloc_error;
if(sorted_sectoffset && sectoffset)
{
// Filter and sort the sectors file offsets.
for(s=0;s<nb_sectorfound;s++)
{
if( sca[s]->sectorsize )
{
sorted_sectoffset[nb_validsectorfound] = malloc(sizeof(sect_offset));
if( !sorted_sectoffset[nb_validsectorfound] )
goto alloc_error;
sorted_sectoffset[nb_validsectorfound]->ss = sca[s];
sorted_sectoffset[nb_validsectorfound]->offset = 0;
nb_validsectorfound++;
}
else
{
hxcfe_freeSectorConfig (ss,sca[s]);
}
}
memcpy(sectoffset,sorted_sectoffset,sizeof(sect_offset*) * (int)nb_validsectorfound);
if(nb_validsectorfound)
quickSort(sorted_sectoffset, 0, nb_validsectorfound - 1);
for(s=0;s<nb_validsectorfound;s++)
{
sorted_sectoffset[s]->offset = fileoffset;
fileoffset += sorted_sectoffset[s]->ss->sectorsize;
}
free(sorted_sectoffset);
sorted_sectoffset = NULL;
vs = 0;
for( s = 0; s < nb_validsectorfound; s++ )
{
sectp = sectoffset[s]->ss;
if( sectp->sectorsize )
{
fprintf(xmlfile,"\t\t\t\t\t<sector sector_id=\"%d\" sector_size=\"%d\">\n",sectp->sector,sectp->sectorsize);
if( ( trackformatid != IBMFORMAT_SD ) &&
(
( sectp->trackencoding == IBMFORMAT_SD ) ||
( sectp->trackencoding == ISOFORMAT_SD )
)
)
{
fprintf(xmlfile,"\t\t\t\t\t\t<format>IBM_FM</format>\n");
}
else
{
if( ( trackformatid != IBMFORMAT_DD ) &&
(
( sectp->trackencoding == IBMFORMAT_DD ) ||
( sectp->trackencoding == ISOFORMAT_DD )
)
)
{
fprintf(xmlfile,"\t\t\t\t\t\t<format>IBM_MFM</format>\n");
}
}
if(sectp->fill_byte_used)
{
fprintf(xmlfile,"\t\t\t\t\t\t<data_fill>0x%.2X</data_fill>\n",sectp->fill_byte);
}
else
{
if(sectp->input_data)
{
fprintf(xmlfile,"\t\t\t\t\t\t<sector_data>");
k=0;
do
{
fprintf(xmlfile,"%.2X",sectp->input_data[k]);
k++;
}while(k<(int)sectp->sectorsize);
fprintf(xmlfile,"</sector_data>\n");
}
}
if(sectp->head != i || export_mode > 0)
{
fprintf(xmlfile,"\t\t\t\t\t\t<side_id>0x%.2X</side_id>\n",sectp->head);
}
if(sectp->cylinder != j || export_mode > 0)
{
fprintf(xmlfile,"\t\t\t\t\t\t<track_id>0x%.2X</track_id>\n",sectp->cylinder);
}
if(export_mode > 0)
{
fprintf(xmlfile,"\t\t\t\t\t\t<start_sector_cell>%d</start_sector_cell>\n",sectp->startsectorindex);
fprintf(xmlfile,"\t\t\t\t\t\t<start_datasector_cell>%d</start_datasector_cell>\n",sectp->startdataindex);
fprintf(xmlfile,"\t\t\t\t\t\t<end_sector_cell>%d</end_sector_cell>\n",sectp->endsectorindex);
fprintf(xmlfile,"\t\t\t\t\t\t<start_sector_us>%.2f</start_sector_us>\n",MeasureTrackTiming(imgldr_ctx->hxcfe,floppy->tracks[j]->sides[i],0,sectp->startsectorindex) * 1000 * 1000);
fprintf(xmlfile,"\t\t\t\t\t\t<sector_duration_us>%.2f</sector_duration_us>\n",MeasureTrackTiming(imgldr_ctx->hxcfe,floppy->tracks[j]->sides[i],sectp->startsectorindex,sectp->endsectorindex) * 1000 * 1000);
}
if(sectp->use_alternate_datamark || export_mode > 0)
{
fprintf(xmlfile,"\t\t\t\t\t\t<datamark>0x%.2X</datamark>\n",sectp->alternate_datamark);
}
fprintf(xmlfile,"\t\t\t\t\t\t<data_offset>0x%.6X</data_offset>\n",(unsigned int)sectoffset[vs]->offset);
if(sectp->use_alternate_header_crc || export_mode > 0)
{
if(sectp->use_alternate_header_crc & 1)
{
fprintf(xmlfile,"\t\t\t\t\t\t<header_crc_status>Bad</header_crc_status>\n");
fprintf(xmlfile,"\t\t\t\t\t\t<header_crc>0x%.4X</header_crc>\n",(unsigned int)sectp->header_crc);
}
else
{
fprintf(xmlfile,"\t\t\t\t\t\t<header_crc_status>Valid</header_crc_status>\n");
fprintf(xmlfile,"\t\t\t\t\t\t<header_crc>0x%.4X</header_crc>\n",(unsigned int)sectp->header_crc);
}
}
if(sectp->use_alternate_data_crc || export_mode > 0)
{
if(sectp->use_alternate_data_crc & 1)
{
fprintf(xmlfile,"\t\t\t\t\t\t<data_crc_status>Bad</data_crc_status>\n");
fprintf(xmlfile,"\t\t\t\t\t\t<data_crc>0x%.4X</data_crc>\n",(unsigned int)sectp->data_crc);
}
else
{
fprintf(xmlfile,"\t\t\t\t\t\t<data_crc_status>Valid</data_crc_status>\n");
fprintf(xmlfile,"\t\t\t\t\t\t<data_crc>0x%.4X</data_crc>\n",(unsigned int)sectp->data_crc);
}
}
fprintf(xmlfile,"\t\t\t\t\t</sector>\n");
vs++;
}
else
{
fprintf(xmlfile,"\t\t\t\t\t<!-- Invalid sector --/>\n");
}
hxcfe_freeSectorConfig (ss,sectp);
}
for(s=0;s<nb_validsectorfound;s++)
{
free(sectoffset[s]);
}
free(sectoffset);
sectoffset = NULL;
}
free(sca);
sca = NULL;
}
fprintf(xmlfile,"\t\t\t\t</sector_list>\n");
fprintf(xmlfile,"\t\t\t</track>\n");
}
}
hxcfe_deinitSectorAccess(ss);
}
fprintf(xmlfile,"\t\t</track_list>\n");
fprintf(xmlfile,"\t</layout>\n");
fprintf(xmlfile,"</disk_layout>\n");
hxc_fclose(xmlfile);
}
return HXCFE_NOERROR;
alloc_error:
if( xmlfile )
hxc_fclose( xmlfile );
if(sorted_sectoffset)
{
for(s=0;s<nb_sectorfound;s++)
{
free(sorted_sectoffset[s]);
}
}
free( sectoffset );
free( sorted_sectoffset );
free( sca );
return HXCFE_INTERNALERROR;
}