]> git.draconx.ca Git - upkg.git/blob - src/engine/music.c
Add simple sanity checks to Engine.Music export.
[upkg.git] / src / engine / music.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib-object.h>
4
5 #include "serializable.h"
6 #include "exportable.h"
7 #include "music.h"
8 #include "upkg.h"
9
10 #define MUSIC_GET_PRIV(o) \
11         G_TYPE_INSTANCE_GET_PRIVATE(o, ENGINE_MUSIC_TYPE, struct music_priv)
12
13 struct music_priv {
14         unsigned type;
15
16         struct upkg_file *f;
17         size_t base, len;
18
19         unsigned char buf[2048];
20         unsigned long nbuf;
21 };
22
23 static int export(GObject *o, FILE *f)
24 {
25         struct music_priv *priv = MUSIC_GET_PRIV(o);
26         size_t left = priv->len, rc;
27
28         unsigned char buf[1024];
29
30         if (!priv->f)
31                 return -1;
32
33         if (upkg_export_seek(priv->f, priv->base, SEEK_SET) != 0)
34                 return -1;
35
36         while (left > 0) {
37                 rc = upkg_export_read(priv->f, buf, MIN(left, sizeof buf));
38                 if (rc == 0)
39                         return -1;
40                 if (fwrite(buf, 1, rc, f) != rc)
41                         return -1;
42                 left -= rc;
43         }
44
45         return 0;
46 }
47
48 static int export_name(GObject *o, char *buf, size_t n)
49 {
50         struct music_priv *priv = MUSIC_GET_PRIV(o);
51         return snprintf(buf, n, "%s", priv->f ? priv->f->name : "");
52 }
53
54 static void exportable_init(UPkgExportable *e)
55 {
56         e->export      = export;
57         e->export_name = export_name;
58 }
59
60 static int deserialize(GObject *o, struct upkg_file *f)
61 {
62         struct music_priv *priv = MUSIC_GET_PRIV(o);
63         EngineMusic *m = ENGINE_MUSIC(o);
64         long size;
65
66         size_t rc;
67
68         priv->nbuf = upkg_export_read(f, priv->buf, sizeof priv->buf);
69         priv->base = 0;
70
71         /* Random field #1 */
72         if (priv->nbuf - priv->base < 2)
73                 return -1;
74         // unpack_16_le(priv->buf+0);
75         priv->base += 2;
76
77         if (f->pkg->version > 61) {
78                 /* Random field #2 */
79                 if (priv->nbuf - priv->base < 4)
80                         return -1;
81                 // unpack_32_le(priv->buf+2);
82                 priv->base += 4;
83         }
84
85         rc = upkg_decode_index(&size, priv->buf+priv->base, priv->nbuf-priv->base);
86         if (rc == 0 || size < 0)
87                 return -1;
88
89         priv->base += rc;
90         priv->len   = size;
91
92         priv->nbuf -= priv->base;
93         memmove(priv->buf, priv->buf+priv->base, priv->nbuf);
94
95         priv->f = f;
96         return 0;
97 }
98
99 static void serializable_init(UPkgSerializable *s)
100 {
101         s->deserialize = deserialize;
102 }
103
104 static void engine_music_init(EngineMusic *m)
105 {
106 }
107
108 static void engine_music_class_init(EngineMusicClass *class)
109 {
110         g_type_class_add_private(class, sizeof (struct music_priv));
111 }
112
113 static void engine_music_class_finalize(EngineMusicClass *class)
114 {
115 }
116
117 G_DEFINE_DYNAMIC_TYPE_EXTENDED(EngineMusic, engine_music, G_TYPE_OBJECT, 0,
118         G_IMPLEMENT_INTERFACE(UPKG_TYPE_SERIALIZABLE, serializable_init)
119         G_IMPLEMENT_INTERFACE(UPKG_TYPE_EXPORTABLE, exportable_init)
120 );
121
122 void music_register(GTypeModule *m)
123 {
124         engine_music_register_type(m);
125 }