From 82b39078044e1373c27d9037d15db5e40a74c9fc Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Sun, 5 Nov 2023 20:36:07 +1000
Subject: [PATCH] dep/libchdr: Add subtype parsing functions

---
 dep/libchdr/include/libchdr/cdrom.h | 26 +++++++++++++++++---------
 dep/libchdr/src/libchdr_cdrom.c     | 29 +++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/dep/libchdr/include/libchdr/cdrom.h b/dep/libchdr/include/libchdr/cdrom.h
index 816e6a5c4..0e978473e 100644
--- a/dep/libchdr/include/libchdr/cdrom.h
+++ b/dep/libchdr/include/libchdr/cdrom.h
@@ -10,10 +10,12 @@
 
 #pragma once
 
-#ifndef __CDROM_H__
-#define __CDROM_H__
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 #include <stdint.h>
+#include <stdbool.h>
 #include <libchdr/chdconfig.h>
 
 /***************************************************************************
@@ -47,11 +49,15 @@ enum
 
 enum
 {
-	CD_SUB_NORMAL = 0,          /* "cooked" 96 bytes per sector */
-	CD_SUB_RAW,                 /* raw uninterleaved 96 bytes per sector */
-	CD_SUB_NONE                 /* no subcode data stored */
+	CD_SUB_NONE = 0,            /* no subcode data stored */
+	CD_SUB_RAW_INTERLEAVED,     /* raw interleaved 96 bytes per sector */
+	CD_SUB_RAW,                 /* raw non-interleaved 96 bytes per sector */
 };
 
+const char* cdrom_get_subtype_string(uint32_t subtype);
+bool cdrom_parse_subtype_string(const char* typestring, uint32_t* subtype, uint32_t* subsize);
+
+
 #define CD_FLAG_GDROM   0x00000001  /* disc is a GD-ROM, all tracks should be stored with GD-ROM metadata */
 #define CD_FLAG_GDROMLE 0x00000002  /* legacy GD-ROM, with little-endian CDDA data */
 
@@ -81,10 +87,10 @@ static inline uint32_t lba_to_msf(uint32_t lba)
 {
 	uint8_t m, s, f;
 
-	m = lba / (60 * 75);
+	m = (uint8_t)(lba / (60 * 75));
 	lba -= m * (60 * 75);
-	s = lba / 75;
-	f = lba % 75;
+	s = (uint8_t)(lba / 75);
+	f = (uint8_t)(lba % 75);
 
 	return ((m / 10) << 20) | ((m % 10) << 16) |
 			((s / 10) << 12) | ((s % 10) <<  8) |
@@ -107,4 +113,6 @@ static inline uint32_t lba_to_msf_alt(int lba)
 	return ret;
 }
 
-#endif  /* __CDROM_H__ */
+#ifdef __cplusplus
+} // extern "C"
+#endif
\ No newline at end of file
diff --git a/dep/libchdr/src/libchdr_cdrom.c b/dep/libchdr/src/libchdr_cdrom.c
index 112ea2e14..aa06b4924 100644
--- a/dep/libchdr/src/libchdr_cdrom.c
+++ b/dep/libchdr/src/libchdr_cdrom.c
@@ -20,6 +20,35 @@
 
 #include <libchdr/cdrom.h>
 
+const char* cdrom_get_subtype_string(uint32_t subtype)
+{
+	switch (subtype)
+	{
+	case CD_SUB_RAW:              return "RW";
+	case CD_SUB_RAW_INTERLEAVED:  return "RW_RAW";
+	default:                      return "NONE";
+	}
+}
+
+bool cdrom_parse_subtype_string(const char* typestring, uint32_t* subtype, uint32_t* subsize)
+{
+	// https://github.com/mamedev/mame/blob/d2d54fb8ed53a2e86d308067da8414f85b5929b0/src/lib/util/cdrom.cpp#L767
+  if (!strcmp(typestring, "RW"))
+  {
+    *subtype = CD_SUB_RAW;
+    *subsize = 96;
+		return true;
+  }
+  else if (!strcmp(typestring, "RW_RAW"))
+  {
+    *subtype = CD_SUB_RAW_INTERLEAVED;
+    *subsize = 96;
+		return true;
+  }
+
+	return false;
+}
+
 #ifdef WANT_RAW_DATA_SECTOR
 
 /***************************************************************************