From fba72c9ebd067eb85edf7e8c4b20f6d2d20161c8 Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Tue, 26 Oct 2010 22:51:07 +0000 Subject: [PATCH] Removed struct packing everywhere, except RGB 24bit struct (and thus 768-byte palettes). This might fix some issue on Caanoo, where either the packing didn't work (and the struct had wrong size) or it worked and caused unaligned reads/writes. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1639 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- src/fileformats.c | 11 --------- src/init.c | 6 ++--- src/loadsave.h | 2 -- src/miscfileformats.c | 56 +++++++++++++++++++++++++------------------ src/struct.h | 12 ++++------ 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/fileformats.c b/src/fileformats.c index 4aef1ab4..0d96635e 100644 --- a/src/fileformats.c +++ b/src/fileformats.c @@ -217,7 +217,6 @@ void Save_IMG(T_IO_Context * context) //////////////////////////////////// LBM //////////////////////////////////// -#pragma pack(1) typedef struct { word Width; @@ -234,7 +233,6 @@ typedef struct word X_screen; word Y_screen; } T_LBM_Header; -#pragma pack() byte * LBM_buffer; FILE *LBM_file; @@ -695,13 +693,9 @@ void Load_LBM(T_IO_Context * context) if ( !File_error ) { Read_dword_be(LBM_file,§ion_size); - //swab((char *)&header.Width ,(char *)&context->Width,2); - //swab((char *)&header.Height,(char *)&context->Height,2); context->Width = header.Width; context->Height = header.Height; - //swab((char *)&header.X_screen,(char *)&Original_screen_X,2); - //swab((char *)&header.Y_screen,(char *)&Original_screen_Y,2); Original_screen_X = header.X_screen; Original_screen_Y = header.Y_screen; @@ -979,7 +973,6 @@ void Save_LBM(T_IO_Context * context) // On corrige la largeur de l'image pour qu'elle soit multiple de 2 real_width=context->Width+(context->Width&1); - //swab((byte *)&real_width,(byte *)&header.Width,2); header.Width=context->Width; header.Height=context->Height; header.X_org=0; @@ -1659,7 +1652,6 @@ void Save_BMP(T_IO_Context * context) //////////////////////////////////// GIF //////////////////////////////////// -#pragma pack(1) typedef struct { word Width; // Width of the complete image area @@ -1678,7 +1670,6 @@ typedef struct byte Indicator; // Misc image information byte Nb_bits_pixel; // Nb de bits par pixel } T_GIF_IDB; // Image Descriptor Block -#pragma pack() typedef struct { @@ -2696,7 +2687,6 @@ void Save_GIF(T_IO_Context * context) //////////////////////////////////// PCX //////////////////////////////////// -#pragma pack(1) typedef struct { byte Manufacturer; // |_ Il font chier ces cons! Ils auraient pu @@ -2718,7 +2708,6 @@ typedef struct word Screen_Y; // | l'écran d'origine byte Filler[54]; // Ca... J'adore! } T_PCX_Header; -#pragma pack() T_PCX_Header PCX_header; diff --git a/src/init.c b/src/init.c index 66c094f0..eeeefdc8 100644 --- a/src/init.c +++ b/src/init.c @@ -1824,7 +1824,7 @@ int Load_CFG(int reload_all) if ((Handle=fopen(filename,"rb"))==NULL) return ERROR_CFG_MISSING; - if ( (file_size<(long)sizeof(cfg_header)) + if ( (file_size<7) || (!Read_bytes(Handle, &cfg_header.Signature, 3)) || memcmp(cfg_header.Signature,"CFG",3) || (!Read_byte(Handle, &cfg_header.Version1)) @@ -1865,7 +1865,7 @@ int Load_CFG(int reload_all) case CHUNK_KEYS: // Touches if (reload_all) { - for (index=0; index<(long)(Chunk.Size/sizeof(cfg_shortcut_info)); index++) + for (index=0; index<(long)(Chunk.Size/6); index++) { if (!Read_word_le(Handle, &cfg_shortcut_info.Number) || !Read_word_le(Handle, &cfg_shortcut_info.Key) || @@ -1918,7 +1918,7 @@ int Load_CFG(int reload_all) } break; case CHUNK_VIDEO_MODES: // Modes vidéo - for (index=0; index<(long)(Chunk.Size/sizeof(cfg_video_mode)); index++) + for (index=0; index<(long)(Chunk.Size/5); index++) { if (!Read_byte(Handle, &cfg_video_mode.State) || !Read_word_le(Handle, &cfg_video_mode.Width) || diff --git a/src/loadsave.h b/src/loadsave.h index 1f8bb579..70fe2758 100644 --- a/src/loadsave.h +++ b/src/loadsave.h @@ -226,7 +226,6 @@ void Set_layer(T_IO_Context *context, byte layer); // ================================================================= // This is here and not in fileformats.c because the emergency save uses it... -#pragma pack(1) typedef struct { byte Filler1[6]; @@ -235,7 +234,6 @@ typedef struct byte Filler2[118]; T_Palette Palette; } T_IMG_Header; -#pragma pack() // Data for 24bit loading diff --git a/src/miscfileformats.c b/src/miscfileformats.c index 6c032a7b..2a304583 100644 --- a/src/miscfileformats.c +++ b/src/miscfileformats.c @@ -367,7 +367,8 @@ void Load_PKM(T_IO_Context * context) Compteur_de_donnees_packees=0; Compteur_de_pixels=0; - Taille_pack=(file_size)-sizeof(T_PKM_Header)-header.Jump; + // Header size is 780 + Taille_pack=(file_size)-780-header.Jump; // Boucle de décompression: while ( (Compteur_de_pixels>1)*header1.Height)!=size ) ) { // Tentative de reconnaissance de la signature des nouveaux fichiers @@ -973,7 +972,6 @@ void Save_CEL(T_IO_Context * context) //////////////////////////////////// KCF //////////////////////////////////// -#pragma pack(1) typedef struct { struct @@ -985,7 +983,6 @@ typedef struct } color[16]; } Palette[10]; } T_KCF_Header; -#pragma pack() // -- Tester si un fichier est au format KCF -------------------------------- @@ -993,7 +990,7 @@ void Test_KCF(T_IO_Context * context) { char filename[MAX_PATH_CHARACTERS]; FILE *file; - T_KCF_Header buffer; + T_KCF_Header header1; T_CEL_Header2 header2; int pal_index; int color_index; @@ -1002,13 +999,17 @@ void Test_KCF(T_IO_Context * context) Get_full_filename(filename, context->File_name, context->File_directory); if ((file=fopen(filename, "rb"))) { - if (File_length_file(file)==sizeof(T_KCF_Header)) + if (File_length_file(file)==320) { - Read_bytes(file,&buffer,sizeof(T_KCF_Header)); + for (pal_index=0;pal_index<10 && !File_error;pal_index++) + for (color_index=0;color_index<16 && !File_error;color_index++) + if (!Read_byte(file,&header1.Palette[pal_index].color[color_index].Byte1) || + !Read_byte(file,&header1.Palette[pal_index].color[color_index].Byte2)) + File_error=1; // On vérifie une propriété de la structure de palette: for (pal_index=0;pal_index<10;pal_index++) for (color_index=0;color_index<16;color_index++) - if ((buffer.Palette[pal_index].color[color_index].Byte2>>4)!=0) + if ((header1.Palette[pal_index].color[color_index].Byte2>>4)!=0) File_error=1; } else @@ -1048,7 +1049,7 @@ void Load_KCF(T_IO_Context * context) { char filename[MAX_PATH_CHARACTERS]; FILE *file; - T_KCF_Header buffer; + T_KCF_Header header1; T_CEL_Header2 header2; byte bytes[3]; int pal_index; @@ -1062,11 +1063,16 @@ void Load_KCF(T_IO_Context * context) if ((file=fopen(filename, "rb"))) { file_size=File_length_file(file); - if (file_size==sizeof(T_KCF_Header)) + if (file_size==320) { // Fichier KCF à l'ancien format + for (pal_index=0;pal_index<10 && !File_error;pal_index++) + for (color_index=0;color_index<16 && !File_error;color_index++) + if (!Read_byte(file,&header1.Palette[pal_index].color[color_index].Byte1) || + !Read_byte(file,&header1.Palette[pal_index].color[color_index].Byte2)) + File_error=1; - if (Read_bytes(file,&buffer,sizeof(T_KCF_Header))) + if (!File_error) { // Pre_load(context, ?); // Pas possible... pas d'image... @@ -1078,9 +1084,9 @@ void Load_KCF(T_IO_Context * context) for (color_index=0;color_index<16;color_index++) { index=16+(pal_index*16)+color_index; - context->Palette[index].R=((buffer.Palette[pal_index].color[color_index].Byte1 >> 4) << 4); - context->Palette[index].B=((buffer.Palette[pal_index].color[color_index].Byte1 & 15) << 4); - context->Palette[index].G=((buffer.Palette[pal_index].color[color_index].Byte2 & 15) << 4); + context->Palette[index].R=((header1.Palette[pal_index].color[color_index].Byte1 >> 4) << 4); + context->Palette[index].B=((header1.Palette[pal_index].color[color_index].Byte1 & 15) << 4); + context->Palette[index].G=((header1.Palette[pal_index].color[color_index].Byte2 & 15) << 4); } for (index=0;index<16;index++) @@ -1167,7 +1173,7 @@ void Save_KCF(T_IO_Context * context) { char filename[MAX_PATH_CHARACTERS]; FILE *file; - T_KCF_Header buffer; + T_KCF_Header header1; T_CEL_Header2 header2; byte bytes[3]; int pal_index; @@ -1195,12 +1201,16 @@ void Save_KCF(T_IO_Context * context) for (color_index=0;color_index<16;color_index++) { index=16+(pal_index*16)+color_index; - buffer.Palette[pal_index].color[color_index].Byte1=((context->Palette[index].R>>4)<<4) | (context->Palette[index].B>>4); - buffer.Palette[pal_index].color[color_index].Byte2=context->Palette[index].G>>4; + header1.Palette[pal_index].color[color_index].Byte1=((context->Palette[index].R>>4)<<4) | (context->Palette[index].B>>4); + header1.Palette[pal_index].color[color_index].Byte2=context->Palette[index].G>>4; } - if (! Write_bytes(file,&buffer,sizeof(T_KCF_Header))) - File_error=1; + // Write all + for (pal_index=0;pal_index<10 && !File_error;pal_index++) + for (color_index=0;color_index<16 && !File_error;color_index++) + if (!Write_byte(file,header1.Palette[pal_index].color[color_index].Byte1) || + !Write_byte(file,header1.Palette[pal_index].color[color_index].Byte2)) + File_error=1; } else { @@ -2296,13 +2306,13 @@ int Save_C64_window(byte *saveWhat, byte *loadAddr) Print_in_window(13,18,"Data:",MC_Dark,MC_Light); what=Window_set_dropdown_button(10,28,90,15,70,what_label[*saveWhat],1, 0, 1, LEFT_SIDE,0); Window_dropdown_clear_items(what); - for (i=0; i