Skip to content

Instantly share code, notes, and snippets.

@hezi
Created July 24, 2020 17:04
Show Gist options
  • Save hezi/8e0f37d8df25b4813b3cad13453a6e0f to your computer and use it in GitHub Desktop.
Save hezi/8e0f37d8df25b4813b3cad13453a6e0f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#pragma pack(push, 1)
/* Library Header */
typedef struct {
unsigned short id; /* Library header id (=0CA01H) */
char copyright[50]; /* Copyright notice */
unsigned short version; /* gxLib version */
char label[40]; /* Library volume label */
unsigned short entries; /* Total image entries */
char reserved[32]; /* Reserved for GX kernel */
} GXLIBHEADER;
/* Library File Information */
typedef struct {
char pack; /* Packing type */
char filename[13]; /* Image file name */
unsigned int fileoffs; /* File offset (absolute) */
unsigned int filesize; /* File size */
unsigned short date; /* File date */
unsigned short time; /* File time */
} GXFINFO;
#pragma pack(pop)
int fsize(FILE *fp) {
int prev = ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz = ftell(fp);
fseek(fp, prev, SEEK_SET);
return sz;
}
char reserved_blob[] = {0x58,0x19,0xFA,0x6C,0x15,0x2F,0x02,0x00,0x17,0x3A,0x17,0x3A,0x58,0x19,0x58,0x19,0x03,0x00,0xE0,0x24,0xA2,0x37,0x9B,0x1F,0x3B,0x6D,0x15,0x2F,0xA1,0x13,0x9B,0x1F};
char *copyright_text = "Copyright (c) Genus Microprogramming, Inc. 1988-94";
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: gxpack <directory>\n\n");
return 1;
}
DIR *dr = opendir(argv[1]);
if (dr == NULL) {
printf("Could not open current directory" );
return 1;
}
short filecount = 0;
struct dirent *de;
while ((de = readdir(dr)) != NULL) {
char *name = de->d_name;
if(name[0] == '.') {
continue;
}
printf("%s\n", name);
filecount++;
}
GXFINFO *entries = malloc(filecount * sizeof(GXFINFO));
memset(entries, 0, filecount * sizeof(GXFINFO));
char buffer[1024*1024];
int i = 0;
rewinddir(dr);
int buffsize = 0;
char fp[255];
FILE *fe;
while ((de = readdir(dr)) != NULL) {
if(de->d_name[0] == '.') {
continue;
}
// FILE NAME //
char input[13];
strlcpy(input, de->d_name, 13);
char *name = strtok(input, ".");
char *ext = strtok(NULL, ".");
sprintf(entries[i].filename, "%-8s%c%s", name, '.', ext);
char *s = entries[i].filename;
do {
*s = toupper(*s);
s++;
} while (*s);
// FILE NAME //
sprintf(fp, "%s/%s", argv[1], de->d_name);
fe = fopen(fp, "rb");
entries[i].filesize = fsize(fe);
entries[i].fileoffs = sizeof(GXFINFO)*filecount + sizeof(GXLIBHEADER) + buffsize;
fread(&buffer[buffsize], 1, entries[i].filesize, fe);
fclose(fe);
buffsize += entries[i].filesize;
i++;
}
closedir(dr);
// HEADER
GXLIBHEADER header;
header.id = 0xca01;
memcpy(header.copyright, copyright_text, 50);
header.version = 0x64;
header.entries = filecount;
memcpy(header.reserved, reserved_blob, 32);
FILE *f = fopen("test.gxl", "wb");
if (f == NULL) {
printf("Could not open output file");
return 1;
}
fwrite(&header, sizeof(header), 1, f);
fwrite(entries, sizeof(GXFINFO), filecount, f);
fwrite(buffer, 1, buffsize, f);
fclose(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment