+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define FAT_SIZE 65536
+#define FAT_START 0x27400
+#define DATA_START 0x42800
+#define CLUSTER_SIZE 16384
+unsigned short fat[FAT_SIZE];
+
+int main(int argc, char **argv)
+{
+ int fd = open("/home/sesse/minnekort.img", O_RDONLY);
+ int ofd;
+ int offset;
+ int cluster;
+ int left;
+ unsigned char de[32];
+ char filename[8 + 1 + 3 + 1];
+ char data[CLUSTER_SIZE];
+
+ if (fd == -1) {
+ perror("/home/sesse/minnekort.img");
+ exit(1);
+ }
+
+ // read the FAT
+ lseek(fd, FAT_START, SEEK_SET);
+ read(fd, fat, FAT_SIZE * sizeof(unsigned short));
+
+ // read the directory structure in question
+ sscanf(argv[1], "%i", &offset);
+ lseek(fd, offset, SEEK_SET);
+ read(fd, de, 32);
+
+ memcpy(filename, de, 8);
+ filename[8] = '.';
+ memcpy(filename + 9, de + 8, 3);
+ filename[12] = '\0';
+
+ printf("Filename = %s\n", filename);
+ ofd = open(filename, O_WRONLY | O_CREAT, 0644);
+
+ left = *((unsigned int *)(de + 0x1c));
+ printf("Size = %u\n", left);
+
+ printf("Recovering:");
+ cluster = *((unsigned short *)(de + 0x1a));
+// printf(" %u(0x%x)", cluster, cluster * CLUSTER_SIZE + DATA_START);
+// fflush(stdout);
+
+ for ( ;; ) {
+ int towrite = (CLUSTER_SIZE < left) ? CLUSTER_SIZE : left;
+
+ lseek(fd, cluster * CLUSTER_SIZE + DATA_START, SEEK_SET);
+ read(fd, data, CLUSTER_SIZE);
+ write(ofd, data, towrite);
+
+ // read the next entry in the FAT
+ cluster = fat[cluster];
+
+ /* if (towrite < CLUSTER_SIZE)
+ printf("(trunc)");
+ printf(" %u", cluster);
+ fflush(stdout);*/
+
+ if (cluster < 0x0002) {
+ printf(" (ERROR)");
+ break;
+ }
+ if (cluster >= 0xfff0 && cluster < 0xfff8) {
+ printf(" (ERROR)");
+ break;
+ }
+ if (cluster >= 0xfff8) {
+ printf(" (EOF)");
+ break;
+ }
+ if (towrite < CLUSTER_SIZE) {
+ printf(" (EXPECTED EOF)\n");
+ }
+
+ // printf("(0x%x)", cluster * CLUSTER_SIZE + DATA_START);
+
+ left -= towrite;
+ }
+ printf("\n");
+ close(fd);
+ close(ofd);
+
+ return 0;
+}