[gnome-games] playstation: Add DiscFileInfo



commit 1f85380f8c7f043465507f1187f52c9eb6929770
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sat Jul 15 11:34:02 2017 +0200

    playstation: Add DiscFileInfo
    
    This will be used in a subsequent commit to parse PlayStation disc
    images.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775255

 plugins/playstation/src/Makefile.am      |    1 +
 plugins/playstation/src/disc-file-info.c |   79 ++++++++++++++++++++++++++++++
 plugins/playstation/src/disc-file-info.h |   36 ++++++++++++++
 3 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/plugins/playstation/src/Makefile.am b/plugins/playstation/src/Makefile.am
index 8e96934..418d093 100644
--- a/plugins/playstation/src/Makefile.am
+++ b/plugins/playstation/src/Makefile.am
@@ -36,6 +36,7 @@ libgames_playstation_plugin_la_DEPENDENCIES = \
 
 libgames_playstation_plugin_la_SOURCES = \
        $(BUILT_SOURCES) \
+       disc-file-info.c \
        disc-image-time.c \
        playstation-error.vala \
        playstation-game-factory.vala \
diff --git a/plugins/playstation/src/disc-file-info.c b/plugins/playstation/src/disc-file-info.c
new file mode 100644
index 0000000..cb8d9e6
--- /dev/null
+++ b/plugins/playstation/src/disc-file-info.c
@@ -0,0 +1,79 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+#include "disc-file-info.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <gio/gio.h>
+
+/* Private */
+
+static gboolean
+games_disc_file_info_is_valid (const GamesDiscFileInfo *file_info)
+{
+  // FIXME Magic number, I have no idea what it is but it works.
+  const gsize MAGIC_SIZE = 47;
+
+  g_return_val_if_fail (file_info != NULL, FALSE);
+
+  return file_info->length >= MAGIC_SIZE + file_info->name_length;
+}
+
+static gchar *
+games_disc_file_info_get_name (GamesDiscFileInfo *file_info)
+{
+  g_return_val_if_fail (file_info != NULL, NULL);
+
+  return g_strndup (games_disc_file_info_access_name (file_info), file_info->name_length);
+}
+
+static GamesDiscFileInfo *
+games_disc_file_info_get_next (const GamesDiscFileInfo *file_info)
+{
+  g_return_val_if_fail (file_info != NULL, NULL);
+
+  if (!games_disc_file_info_is_valid (file_info))
+    return NULL;
+
+  return (GamesDiscFileInfo *) ((gpointer) file_info + file_info->length);
+}
+
+/* Public */
+
+gboolean
+games_disc_file_info_is_directory (GamesDiscFileInfo *file_info)
+{
+  g_return_val_if_fail (file_info != NULL, FALSE);
+
+  return file_info->flags & 0x2;
+}
+
+gchar *
+games_disc_file_info_access_name (GamesDiscFileInfo *file_info)
+{
+  g_return_val_if_fail (file_info != NULL, NULL);
+
+  return (gchar *) file_info + sizeof (GamesDiscFileInfo);
+}
+
+void
+games_disc_file_info_foreach_file (GamesDiscFileInfo                *file_info,
+                                   gsize                             size,
+                                   GamesDiscFileInfoForeachCallback  callback,
+                                   gpointer                          user_data)
+{
+  GamesDiscFileInfo *current;
+  GamesDiscFileInfo *next;
+
+  g_return_if_fail (file_info != NULL);
+
+  for (current = file_info; current != NULL && games_disc_file_info_is_valid (current); current = 
games_disc_file_info_get_next (current)) {
+    // The file info should never go beyond the end of the buffer.
+    if ((gpointer) current - (gpointer) file_info + sizeof (GamesDiscFileInfo) >= size ||
+        (gpointer) current - (gpointer) file_info + current->length >= size)
+      break;
+
+    if (!callback (current, user_data))
+      break;
+  }
+}
diff --git a/plugins/playstation/src/disc-file-info.h b/plugins/playstation/src/disc-file-info.h
new file mode 100644
index 0000000..77fe855
--- /dev/null
+++ b/plugins/playstation/src/disc-file-info.h
@@ -0,0 +1,36 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+#ifndef DISC_FILE_INFO_H
+#define DISC_FILE_INFO_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GamesDiscFileInfo GamesDiscFileInfo;
+
+struct _GamesDiscFileInfo {
+  guint8 length;
+  guint8 ext_attr_length;
+  guint8 extent[8];
+  guint8 size[8];
+  guint8 date[7];
+  guint8 flags;
+  guint8 file_unit_size;
+  guint8 interleave;
+  guint8 volume_sequence_number[4];
+  guint8 name_length;
+};
+
+typedef gboolean (*GamesDiscFileInfoForeachCallback) (GamesDiscFileInfo *file_info, gpointer user_data);
+
+gboolean games_disc_file_info_is_directory (GamesDiscFileInfo *file_info);
+gchar *games_disc_file_info_access_name (GamesDiscFileInfo *file_info);
+void games_disc_file_info_foreach_file (GamesDiscFileInfo                *file_info,
+                                        gsize                             size,
+                                        GamesDiscFileInfoForeachCallback  callback,
+                                        gpointer                          user_data);
+
+G_END_DECLS
+
+#endif /* DISC_FILE_INFO_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]