Skip to content

Instantly share code, notes, and snippets.

@SergioFLS
Created July 27, 2022 16:18
Show Gist options
  • Select an option

  • Save SergioFLS/ab5080c34b47a1f4c85629e7a161097a to your computer and use it in GitHub Desktop.

Select an option

Save SergioFLS/ab5080c34b47a1f4c85629e7a161097a to your computer and use it in GitHub Desktop.
Reading chunk sizes on a GameMaker IFF file
#ifndef CHUNK_IDS_H
#define CHUNK_IDS_H
#define ID_FORM 0x4d524f46
#define ID_GEN8 0x384e4547
#define ID_OPTN 0x4e54504f
// also under MIT-0, see gmiff.c
#endif
/*
== MIT No Attribution ==
Copyright (c) 2022 SergioFLS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include "chunk_ids.h"
#define BOOL char
#define TRUE 1
#define FALSE 0
// https://codereview.stackexchange.com/a/151070
static inline uint32_t reverseBytes(uint32_t value)
{
return (value & 0xff000000) >> 24 |
(value & 0x00ff0000) >> 8 |
(value & 0x0000ff00) << 8 |
(value & 0x000000ff) << 24;
}
uint32_t efread(FILE *f, BOOL be)
{
uint32_t output;
fread(&output, sizeof(uint32_t), 1, f);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if(be)
return reverseBytes(output);
return output;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
if(be)
return output;
return reverseBytes(output);
#else
#error unsupported endianness
#endif
}
struct ChunkGEN8
{
uint8_t disableDebug;
};
int main()
{
BOOL be = FALSE;
FILE *iff = fopen("pool.win", "rb");
if(iff == NULL)
{
printf("failed to read file!");
return 1;
}
//printf("%d\n", reverseBytes(ID_FORM));
uint32_t buffer;
uint32_t fileSize;
buffer = efread(iff, FALSE);
if(buffer != ID_FORM)
{
if(reverseBytes(buffer) == ID_FORM)
{
printf("Data file is in big endian\n");
be = TRUE;
}
else
{
printf("magic didn't match FORM! (might be a completely different file?)\n");
return 1;
}
}
fileSize = efread(iff, be);
printf("size of FORM is %d bytes long\n", fileSize);
while(ftell(iff) < fileSize + 8)
{
buffer = efread(iff, be);
printf("%c%c%c%c\n",
(char)(buffer & 0x000000FF),
(char)((buffer & 0x0000FF00) >> 8),
(char)((buffer & 0x00FF0000) >> 16),
(char)((buffer & 0xFF000000) >> 24));
buffer = efread(iff, be);
printf("%d\n", buffer);
fseek(iff, buffer, SEEK_CUR);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment