summaryrefslogtreecommitdiff
path: root/xdelta1/libedsio/generic.c
blob: 96d246af28d0baa20105039bd8474314f5e66763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*-Mode: C;-*-
 * $Id: generic.c 1.1 Sun, 28 Jan 2007 10:02:26 -0800 jmacd $
 *
 * Copyright (C) 1998, 1999, Josh MacDonald.
 * All Rights Reserved.
 *
 * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
 */

#include "edsio.h"

/* Type-based selectors for unknown types
 */

typedef struct {
  const char* name;
  gboolean (*unserialize_func) ();
  gboolean (*serialize_func) ();
  guint    (*count_func) ();
  void     (*print_func) ();
  guint32    val;
} SerEntry;

static GArray   *ser_array;
static gboolean  ser_array_sorted;

void
serializeio_initialize_type (const char* name,
			     guint32    val,
			     gboolean (*unserialize_func) (),
			     gboolean (*serialize_func) (),
			     guint    (*count_func) (),
			     void      (*print_func) ())
{
  SerEntry it;

  it.name = name;
  it.val = val;
  it.unserialize_func = unserialize_func;
  it.serialize_func = serialize_func;
  it.count_func = count_func;
  it.print_func = print_func;

  if (ser_array == NULL)
    ser_array = g_array_new (FALSE, TRUE, sizeof (SerEntry));

  g_array_append_val (ser_array, it);

  ser_array_sorted = FALSE;
}

static int
ser_entry_compare (const void* va, const void* vb)
{
  SerEntry* a = (SerEntry*) va;
  SerEntry* b = (SerEntry*) vb;

  return a->val - b->val;
}

static SerEntry*
serializeio_find_entry (SerialType type)
{
  if (! edsio_library_check (type & EDSIO_LIBRARY_OFFSET_MASK))
    return NULL;

  if (ser_array)
    {
      gint high_index = ser_array->len;
      gint low_index = 0;
      gint index;
      gint this_val;

      if (! ser_array_sorted)
	{
	  ser_array_sorted = TRUE;
	  qsort (ser_array->data, ser_array->len, sizeof (SerEntry), ser_entry_compare);
	}

    again:

      index = (low_index + high_index) / 2;

      this_val = g_array_index (ser_array, SerEntry, index).val;

      if (this_val < type)
	{
	  low_index = index + 1;
	  goto again;
	}
      else if (this_val > type)
	{
	  high_index = index - 1;
	  goto again;
	}
      else
	{
	  return & g_array_index (ser_array, SerEntry, index);
	}
    }

  edsio_generate_intint_event (EC_EdsioUnregisteredType,
			       type & EDSIO_LIBRARY_OFFSET_MASK,
			       type >> EDSIO_LIBRARY_OFFSET_BITS);
  return NULL;
}

gboolean
serializeio_unserialize_generic_internal (SerialSource *source,
					  SerialType   *object_type,
					  void        **object,
					  gboolean      set_allocation)
{
  SerialType type = (* source->source_type) (source, set_allocation);
  SerEntry* ent;
  gboolean res = FALSE;

  if (type < 0)
    return FALSE;

  ent = serializeio_find_entry (type);

  (*object_type) = type;

  if (ent)
    {
      res = ent->unserialize_func (source, object);
    }

  return res;
}

gboolean
serializeio_unserialize_generic (SerialSource *source,
				 SerialType   *object_type,
				 void        **object)
{
  return serializeio_unserialize_generic_internal (source, object_type, object, TRUE);
}

gboolean
serializeio_serialize_generic (SerialSink    *sink,
			       SerialType     object_type,
			       void          *object)
{
  return serializeio_serialize_generic_internal (sink, object_type, object, TRUE);
}

gboolean
serializeio_serialize_generic_internal (SerialSink    *sink,
					SerialType     object_type,
					void          *object,
					gboolean       set_allocation)
{
  SerEntry* ent;
  gboolean res = FALSE;

  if (! (* sink->sink_type) (sink, object_type, set_allocation ? serializeio_generic_count (object_type, object) : 0, set_allocation))
    return FALSE;

  ent = serializeio_find_entry (object_type);

  if (ent)
    res = ent->serialize_func (sink, object);

  return res;
}

const char*
serializeio_generic_type_to_string (SerialType type)
{
  SerEntry* ent;
  const char* res = "*Unknown*";

  ent = serializeio_find_entry (type);

  if (ent)
    res = ent->name;

  return res;
}

guint
serializeio_generic_count (SerialType     object_type,
			   void          *object)
{
  SerEntry* ent;
  gboolean res = FALSE;

  ent = serializeio_find_entry (object_type);

  if (ent)
    res = ent->count_func (object);

  return res;
}

void
serializeio_generic_print (SerialType type, void* object, guint indent_spaces)
{
  SerEntry* ent;

  ent = serializeio_find_entry (type);

  if (ent)
    ent->print_func (object, indent_spaces);
  else
    {
      int i = 0;

      for (; i < indent_spaces; i += 1)
	g_print (" ");

      g_print ("*Type Not Registered*\n");
    }
}

gboolean
serializeio_unserialize_generic_acceptable (SerialSource *source,
					    guint32       accept,
					    SerialType   *object_type,
					    void        **object)
{
  gboolean s;

  s = serializeio_unserialize_generic (source, object_type, object);

  if (s)
    {
      if (accept != -1)
	{
	  if ((*object_type & EDSIO_LIBRARY_OFFSET_MASK) != (accept & EDSIO_LIBRARY_OFFSET_MASK))
	    {
	      edsio_generate_intint_event (EC_EdsioUnexpectedLibraryType,
					   accept & EDSIO_LIBRARY_OFFSET_MASK,
					   *object_type & EDSIO_LIBRARY_OFFSET_MASK);

	      return FALSE;
	    }

	  if (! ((*object_type & ~EDSIO_LIBRARY_OFFSET_MASK) |
		 (accept       & ~EDSIO_LIBRARY_OFFSET_MASK)))
	    {
	      edsio_generate_void_event (EC_EdsioUnexpectedType);

	      return FALSE;
	    }
	}
    }

  return s;
}