+void DiskSelector::ReadManifest(FILE * fp, DiskSet * ds)
+{
+ char line[0x10000];
+ int disksFound = 0;
+ int lineNo = 0;
+
+ while (!feof(fp))
+ {
+ fgets(line, 0x10000, fp);
+ lineNo++;
+
+ if ((line[0] == '#') || (line[0] == '\n'))
+ ; // Do nothing with comments or blank lines...
+ else
+ {
+ char buf[1024];
+ char crcbuf[16];
+ char altName[1024];
+
+ if (strncmp(line, "diskset", 7) == 0)
+ {
+ sscanf(line, "diskset=\"%[^\"]\"", buf);
+ ds->name = buf;
+ }
+ else if (strncmp(line, "disks", 5) == 0)
+ {
+ sscanf(line, "disks=%hhd", &ds->num);
+ }
+ else if (strncmp(line, "disk", 4) == 0)
+ {
+ int n = sscanf(line, "disk=%s %s (%s)", buf, crcbuf, altName);
+
+ if ((n == 2) || (n == 3))
+ {
+ ds->image[disksFound] = buf;
+ ds->crc[disksFound] = strtoul(crcbuf, NULL, 16);
+ disksFound++;
+
+ if (n == 3)
+ ds->imgName[disksFound] = altName;
+ else
+ {
+ // Find the file's extension, if any
+ char * ext = strrchr(buf, '.');
+
+ // Kill the disk extension, if it exists
+ if (ext != NULL)
+ *ext = 0;
+
+ ds->imgName[disksFound] = buf;
+ }
+ }
+ else
+ WriteLog("Malformed disk descriptor in manifest at line %d\n", lineNo);
+ }
+ }
+ }
+
+ if (disksFound != ds->num)
+ WriteLog("Found only %d entries in manifest, expected %hhd\n", disksFound, ds->num);
+}
+
+
+bool DiskSelector::CheckManifest(const char * path, DiskSet * ds)
+{
+ uint8_t found = 0;
+
+ for(int i=0; i<ds->num; i++)
+ {
+ std::string filename = path;
+ filename += "/";
+ filename += ds->image[i];
+ uint32_t size;
+ uint8_t * buf = ReadFile(filename.c_str(), &size);
+
+ if (buf != NULL)
+ {
+ ds->crcFound[i] = CRC32(buf, size);
+ free(buf);
+ found++;
+
+ if (ds->crc[i] != ds->crcFound[i])
+ {
+ WriteLog("Warning: Bad CRC32 for '%s'. Expected: %08X, found: %08X\n", ds->image[i], ds->crc[i], ds->crcFound[i]);
+ }
+ }
+ }
+
+ return (found == ds->num ? true : false);
+}
+
+
+uint8_t * DiskSelector::ReadFile(const char * filename, uint32_t * size)
+{
+ FILE * fp = fopen(filename, "r");
+
+ if (!fp)
+ return NULL;
+
+ fseek(fp, 0, SEEK_END);
+ *size = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ uint8_t * buffer = (uint8_t *)malloc(*size);
+ fread(buffer, 1, *size, fp);
+ fclose(fp);
+
+ return buffer;
+}
+
+