• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for GstByteReader
4  *
5  * Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <gst/gst.h>
28 #include <gst/check/gstcheck.h>
29 #include <gst/base/gstbytereader.h>
30 #include "gst/glib-compat-private.h"
31 
32 #ifndef fail_unless_equals_int64
33 #define fail_unless_equals_int64(a, b)					\
34 G_STMT_START {								\
35   gint64 first = a;							\
36   gint64 second = b;							\
37   fail_unless(first == second,						\
38     "'" #a "' (%" G_GINT64_FORMAT ") is not equal to '" #b"' (%"	\
39     G_GINT64_FORMAT ")", first, second);				\
40 } G_STMT_END;
41 #endif
42 
GST_START_TEST(test_initialization)43 GST_START_TEST (test_initialization)
44 {
45   guint8 data[] = { 0x01, 0x02, 0x03, 0x04 };
46   GstBuffer *buffer = gst_buffer_new ();
47   GstByteReader reader = GST_BYTE_READER_INIT (data, 4);
48   GstByteReader *reader2;
49   guint8 x = 0;
50   GstMapInfo info;
51 
52   gst_buffer_insert_memory (buffer, -1,
53       gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, 4, 0, 4, NULL,
54           NULL));
55 
56   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
57   fail_unless_equals_int (x, 0x01);
58   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
59   fail_unless_equals_int (x, 0x02);
60 
61   memset (&reader, 0, sizeof (GstByteReader));
62 
63   gst_byte_reader_init (&reader, data, 4);
64   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
65   fail_unless_equals_int (x, 0x01);
66   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
67   fail_unless_equals_int (x, 0x02);
68 
69   fail_unless (gst_buffer_map (buffer, &info, GST_MAP_READ));
70   gst_byte_reader_init (&reader, info.data, info.size);
71   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
72   fail_unless_equals_int (x, 0x01);
73   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
74   fail_unless_equals_int (x, 0x02);
75   gst_buffer_unmap (buffer, &info);
76 
77   reader2 = gst_byte_reader_new (data, 4);
78   fail_unless (gst_byte_reader_get_uint8 (reader2, &x));
79   fail_unless_equals_int (x, 0x01);
80   fail_unless (gst_byte_reader_get_uint8 (reader2, &x));
81   fail_unless_equals_int (x, 0x02);
82   gst_byte_reader_free (reader2);
83 
84   fail_unless (gst_buffer_map (buffer, &info, GST_MAP_READ));
85   reader2 = gst_byte_reader_new (info.data, info.size);
86   fail_unless (gst_byte_reader_get_uint8 (reader2, &x));
87   fail_unless_equals_int (x, 0x01);
88   fail_unless (gst_byte_reader_get_uint8 (reader2, &x));
89   fail_unless_equals_int (x, 0x02);
90   gst_byte_reader_free (reader2);
91   gst_buffer_unmap (buffer, &info);
92 
93   gst_buffer_unref (buffer);
94 }
95 
96 GST_END_TEST;
97 
98 #define GET_CHECK8(reader, dest, val) { \
99   fail_unless (gst_byte_reader_get_uint8 (reader, &dest)); \
100   fail_unless_equals_uint64 (dest, val); \
101 }
102 
103 #define GET_CHECK(reader, dest, bits, endianness, val) { \
104   fail_unless (gst_byte_reader_get_uint##bits##_##endianness (reader, &dest)); \
105   fail_unless_equals_uint64 (dest, val); \
106 }
107 
108 #define GET_CHECK_FAIL8(reader, dest) { \
109   fail_if (gst_byte_reader_get_uint8 (reader, &dest)); \
110 }
111 
112 #define GET_CHECK_FAIL(reader, dest, bits, endianness) { \
113   fail_if (gst_byte_reader_get_uint##bits##_##endianness (reader, &dest)); \
114 }
115 
116 #define PEEK_CHECK8(reader, dest, val) { \
117   fail_unless (gst_byte_reader_peek_uint8 (reader, &dest)); \
118   fail_unless_equals_uint64 (dest, val); \
119 }
120 
121 #define PEEK_CHECK(reader, dest, bits, endianness, val) { \
122   fail_unless (gst_byte_reader_peek_uint##bits##_##endianness (reader, &dest)); \
123   fail_unless_equals_uint64 (dest, val); \
124 }
125 
126 #define PEEK_CHECK_FAIL8(reader, dest) { \
127   fail_if (gst_byte_reader_peek_uint8 (reader, &dest)); \
128 }
129 
130 #define PEEK_CHECK_FAIL(reader, dest, bits, endianness) { \
131   fail_if (gst_byte_reader_peek_uint##bits##_##endianness (reader, &dest)); \
132 }
133 
GST_START_TEST(test_get_uint_le)134 GST_START_TEST (test_get_uint_le)
135 {
136   guint8 data[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
137     0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21
138   };
139   GstByteReader reader = GST_BYTE_READER_INIT (data, 16);
140   guint8 a = 0;
141   guint16 b = 0;
142   guint32 c = 0;
143   guint64 d = 0;
144 
145   GET_CHECK8 (&reader, a, 0x12);
146   GET_CHECK (&reader, b, 16, le, 0x5634);
147   GET_CHECK (&reader, c, 24, le, 0xab9078);
148   GET_CHECK (&reader, c, 32, le, 0xdcfeefcd);
149   fail_unless (gst_byte_reader_set_pos (&reader, 0));
150   GET_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (0xefcdab9078563412));
151   GET_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (0x2143658709badcfe));
152 
153   GET_CHECK_FAIL8 (&reader, a);
154   GET_CHECK_FAIL (&reader, b, 16, le);
155   GET_CHECK_FAIL (&reader, c, 24, le);
156   GET_CHECK_FAIL (&reader, c, 32, le);
157   GET_CHECK_FAIL (&reader, d, 64, le);
158 
159   fail_unless (gst_byte_reader_set_pos (&reader, 0));
160 
161   PEEK_CHECK8 (&reader, a, 0x12);
162   PEEK_CHECK (&reader, b, 16, le, 0x3412);
163   PEEK_CHECK (&reader, c, 24, le, 0x563412);
164   PEEK_CHECK (&reader, c, 32, le, 0x78563412);
165   PEEK_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (0xefcdab9078563412));
166 
167   fail_unless (gst_byte_reader_set_pos (&reader, 16));
168   PEEK_CHECK_FAIL8 (&reader, a);
169   PEEK_CHECK_FAIL (&reader, b, 16, le);
170   PEEK_CHECK_FAIL (&reader, c, 24, le);
171   PEEK_CHECK_FAIL (&reader, c, 32, le);
172   PEEK_CHECK_FAIL (&reader, d, 64, le);
173 }
174 
175 GST_END_TEST;
176 
GST_START_TEST(test_get_uint_be)177 GST_START_TEST (test_get_uint_be)
178 {
179   guint8 data[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
180     0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21
181   };
182   GstByteReader reader = GST_BYTE_READER_INIT (data, 16);
183   guint8 a = 0;
184   guint16 b = 0;
185   guint32 c = 0;
186   guint64 d = 0;
187 
188   GET_CHECK8 (&reader, a, 0x12);
189   GET_CHECK (&reader, b, 16, be, 0x3456);
190   GET_CHECK (&reader, c, 24, be, 0x7890ab);
191   GET_CHECK (&reader, c, 32, be, 0xcdeffedc);
192   fail_unless (gst_byte_reader_set_pos (&reader, 0));
193   GET_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (0x1234567890abcdef));
194   GET_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (0xfedcba0987654321));
195 
196   GET_CHECK_FAIL8 (&reader, a);
197   GET_CHECK_FAIL (&reader, b, 16, be);
198   GET_CHECK_FAIL (&reader, c, 24, be);
199   GET_CHECK_FAIL (&reader, c, 32, be);
200   GET_CHECK_FAIL (&reader, d, 64, be);
201 
202   fail_unless (gst_byte_reader_set_pos (&reader, 0));
203 
204   PEEK_CHECK8 (&reader, a, 0x12);
205   PEEK_CHECK (&reader, b, 16, be, 0x1234);
206   PEEK_CHECK (&reader, c, 24, be, 0x123456);
207   PEEK_CHECK (&reader, c, 32, be, 0x12345678);
208   PEEK_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (0x1234567890abcdef));
209 
210   fail_unless (gst_byte_reader_set_pos (&reader, 16));
211   PEEK_CHECK_FAIL8 (&reader, a);
212   PEEK_CHECK_FAIL (&reader, b, 16, be);
213   PEEK_CHECK_FAIL (&reader, c, 24, be);
214   PEEK_CHECK_FAIL (&reader, c, 32, be);
215   PEEK_CHECK_FAIL (&reader, d, 64, be);
216 }
217 
218 GST_END_TEST;
219 
220 #undef GET_CHECK8
221 #undef GET_CHECK
222 #undef PEEK_CHECK8
223 #undef PEEK_CHECK
224 #undef GET_CHECK_FAIL8
225 #undef GET_CHECK_FAIL
226 #undef PEEK_CHECK_FAIL8
227 #undef PEEK_CHECK_FAIL
228 
229 #define GET_CHECK8(reader, dest, val) { \
230   fail_unless (gst_byte_reader_get_int8 (reader, &dest)); \
231   fail_unless_equals_int64 (dest, val); \
232 }
233 
234 #define GET_CHECK(reader, dest, bits, endianness, val) { \
235   fail_unless (gst_byte_reader_get_int##bits##_##endianness (reader, &dest)); \
236   fail_unless_equals_int64 (dest, val); \
237 }
238 
239 #define GET_CHECK_FAIL8(reader, dest) { \
240   fail_if (gst_byte_reader_get_int8 (reader, &dest)); \
241 }
242 
243 #define GET_CHECK_FAIL(reader, dest, bits, endianness) { \
244   fail_if (gst_byte_reader_get_int##bits##_##endianness (reader, &dest)); \
245 }
246 
247 #define PEEK_CHECK8(reader, dest, val) { \
248   fail_unless (gst_byte_reader_peek_int8 (reader, &dest)); \
249   fail_unless_equals_int64 (dest, val); \
250 }
251 
252 #define PEEK_CHECK(reader, dest, bits, endianness, val) { \
253   fail_unless (gst_byte_reader_peek_int##bits##_##endianness (reader, &dest)); \
254   fail_unless_equals_int64 (dest, val); \
255 }
256 
257 #define PEEK_CHECK_FAIL8(reader, dest) { \
258   fail_if (gst_byte_reader_peek_int8 (reader, &dest)); \
259 }
260 
261 #define PEEK_CHECK_FAIL(reader, dest, bits, endianness) { \
262   fail_if (gst_byte_reader_peek_int##bits##_##endianness (reader, &dest)); \
263 }
264 
GST_START_TEST(test_get_int_le)265 GST_START_TEST (test_get_int_le)
266 {
267   guint8 data[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
268     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
269   };
270   GstByteReader reader = GST_BYTE_READER_INIT (data, 16);
271   gint8 a = 0;
272   gint16 b = 0;
273   gint32 c = 0;
274   gint64 d = 0;
275 
276   GET_CHECK8 (&reader, a, -1);
277   GET_CHECK (&reader, b, 16, le, -1);
278   GET_CHECK (&reader, c, 24, le, -1);
279   GET_CHECK (&reader, c, 32, le, -1);
280   fail_unless (gst_byte_reader_set_pos (&reader, 0));
281   GET_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (-1));
282   GET_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (-1));
283 
284   GET_CHECK_FAIL8 (&reader, a);
285   GET_CHECK_FAIL (&reader, b, 16, le);
286   GET_CHECK_FAIL (&reader, c, 24, le);
287   GET_CHECK_FAIL (&reader, c, 32, le);
288   GET_CHECK_FAIL (&reader, d, 64, le);
289 
290   fail_unless (gst_byte_reader_set_pos (&reader, 0));
291 
292   PEEK_CHECK8 (&reader, a, -1);
293   PEEK_CHECK (&reader, b, 16, le, -1);
294   PEEK_CHECK (&reader, c, 24, le, -1);
295   PEEK_CHECK (&reader, c, 32, le, -1);
296   PEEK_CHECK (&reader, d, 64, le, G_GINT64_CONSTANT (-1));
297 
298   fail_unless (gst_byte_reader_set_pos (&reader, 16));
299   PEEK_CHECK_FAIL8 (&reader, a);
300   PEEK_CHECK_FAIL (&reader, b, 16, le);
301   PEEK_CHECK_FAIL (&reader, c, 24, le);
302   PEEK_CHECK_FAIL (&reader, c, 32, le);
303   PEEK_CHECK_FAIL (&reader, d, 64, le);
304 
305 }
306 
307 GST_END_TEST;
308 
GST_START_TEST(test_get_int_be)309 GST_START_TEST (test_get_int_be)
310 {
311   guint8 data[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
312     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
313   };
314   GstByteReader reader = GST_BYTE_READER_INIT (data, 16);
315   gint8 a = 0;
316   gint16 b = 0;
317   gint32 c = 0;
318   gint64 d = 0;
319 
320   GET_CHECK8 (&reader, a, -1);
321   GET_CHECK (&reader, b, 16, be, -1);
322   GET_CHECK (&reader, c, 24, be, -1);
323   GET_CHECK (&reader, c, 32, be, -1);
324   fail_unless (gst_byte_reader_set_pos (&reader, 0));
325   GET_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (-1));
326   GET_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (-1));
327 
328   GET_CHECK_FAIL8 (&reader, a);
329   GET_CHECK_FAIL (&reader, b, 16, be);
330   GET_CHECK_FAIL (&reader, c, 24, be);
331   GET_CHECK_FAIL (&reader, c, 32, be);
332   GET_CHECK_FAIL (&reader, d, 64, be);
333 
334   fail_unless (gst_byte_reader_set_pos (&reader, 0));
335 
336   PEEK_CHECK8 (&reader, a, -1);
337   PEEK_CHECK (&reader, b, 16, be, -1);
338   PEEK_CHECK (&reader, c, 24, be, -1);
339   PEEK_CHECK (&reader, c, 32, be, -1);
340   PEEK_CHECK (&reader, d, 64, be, G_GINT64_CONSTANT (-1));
341 
342   fail_unless (gst_byte_reader_set_pos (&reader, 16));
343   PEEK_CHECK_FAIL8 (&reader, a);
344   PEEK_CHECK_FAIL (&reader, b, 16, be);
345   PEEK_CHECK_FAIL (&reader, c, 24, be);
346   PEEK_CHECK_FAIL (&reader, c, 32, be);
347   PEEK_CHECK_FAIL (&reader, d, 64, be);
348 
349 }
350 
351 GST_END_TEST;
352 
353 #undef GET_CHECK8
354 #undef GET_CHECK
355 #undef PEEK_CHECK8
356 #undef PEEK_CHECK
357 #undef GET_CHECK_FAIL8
358 #undef GET_CHECK_FAIL
359 #undef PEEK_CHECK_FAIL8
360 #undef PEEK_CHECK_FAIL
361 
362 #define GET_CHECK(reader, dest, bits, endianness, val) { \
363   fail_unless (gst_byte_reader_get_float##bits##_##endianness (reader, &dest)); \
364   fail_unless_equals_float (dest, val); \
365 }
366 
367 #define GET_CHECK_FAIL(reader, dest, bits, endianness) { \
368   fail_if (gst_byte_reader_get_float##bits##_##endianness (reader, &dest)); \
369 }
370 
371 #define PEEK_CHECK(reader, dest, bits, endianness, val) { \
372   fail_unless (gst_byte_reader_peek_float##bits##_##endianness (reader, &dest)); \
373   fail_unless_equals_float (dest, val); \
374 }
375 
376 #define PEEK_CHECK_FAIL(reader, dest, bits, endianness) { \
377   fail_if (gst_byte_reader_peek_float##bits##_##endianness (reader, &dest)); \
378 }
379 
GST_START_TEST(test_get_float_le)380 GST_START_TEST (test_get_float_le)
381 {
382   guint8 data[] = {
383     0x00, 0x00, 0x80, 0x3f,
384     0x00, 0x00, 0x80, 0xbf,
385     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
386     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xbf,
387   };
388   GstByteReader reader = GST_BYTE_READER_INIT (data, 24);
389   gfloat a = 0.0;
390   gdouble b = 0.0;
391 
392   PEEK_CHECK (&reader, a, 32, le, 1.0);
393   GET_CHECK (&reader, a, 32, le, 1.0);
394   GET_CHECK (&reader, a, 32, le, -1.0);
395   PEEK_CHECK (&reader, b, 64, le, 1.0);
396   GET_CHECK (&reader, b, 64, le, 1.0);
397   GET_CHECK (&reader, b, 64, le, -1.0);
398   GET_CHECK_FAIL (&reader, a, 32, le);
399   GET_CHECK_FAIL (&reader, b, 64, le);
400   PEEK_CHECK_FAIL (&reader, a, 32, le);
401   PEEK_CHECK_FAIL (&reader, b, 64, le);
402 }
403 
404 GST_END_TEST;
405 
GST_START_TEST(test_get_float_be)406 GST_START_TEST (test_get_float_be)
407 {
408   guint8 data[] = {
409     0x3f, 0x80, 0x00, 0x00,
410     0xbf, 0x80, 0x00, 0x00,
411     0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
412     0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
413   };
414   GstByteReader reader = GST_BYTE_READER_INIT (data, 24);
415   gfloat a = 0.0;
416   gdouble b = 0.0;
417 
418   PEEK_CHECK (&reader, a, 32, be, 1.0);
419   GET_CHECK (&reader, a, 32, be, 1.0);
420   GET_CHECK (&reader, a, 32, be, -1.0);
421   PEEK_CHECK (&reader, b, 64, be, 1.0);
422   GET_CHECK (&reader, b, 64, be, 1.0);
423   GET_CHECK (&reader, b, 64, be, -1.0);
424   GET_CHECK_FAIL (&reader, a, 32, be);
425   GET_CHECK_FAIL (&reader, b, 64, be);
426   PEEK_CHECK_FAIL (&reader, a, 32, be);
427   PEEK_CHECK_FAIL (&reader, b, 64, be);
428 }
429 
430 GST_END_TEST;
431 
432 #undef GET_CHECK
433 #undef PEEK_CHECK
434 #undef GET_CHECK_FAIL
435 #undef PEEK_CHECK_FAIL
436 
GST_START_TEST(test_position_tracking)437 GST_START_TEST (test_position_tracking)
438 {
439   guint8 data[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
440     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
441   };
442   GstByteReader reader = GST_BYTE_READER_INIT (data, 16);
443   guint8 a = 0;
444 
445   fail_unless_equals_int (gst_byte_reader_get_pos (&reader), 0);
446   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 16);
447 
448   fail_unless (gst_byte_reader_get_uint8 (&reader, &a));
449   fail_unless_equals_int (gst_byte_reader_get_pos (&reader), 1);
450   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 16 - 1);
451 
452   fail_unless (gst_byte_reader_set_pos (&reader, 8));
453   fail_unless_equals_int (gst_byte_reader_get_pos (&reader), 8);
454   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 16 - 8);
455 
456   fail_unless (gst_byte_reader_skip (&reader, 4));
457   fail_unless_equals_int (gst_byte_reader_get_pos (&reader), 12);
458   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 16 - 12);
459 
460   fail_unless (gst_byte_reader_set_pos (&reader, 16));
461   fail_unless_equals_int (gst_byte_reader_get_pos (&reader), 16);
462   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 0);
463 
464   fail_unless (gst_byte_reader_skip (&reader, 0));
465   fail_if (gst_byte_reader_skip (&reader, 1));
466 }
467 
468 GST_END_TEST;
469 
470 #define do_scan(r,m,p,o,s,x) \
471 G_STMT_START {								\
472     fail_unless_equals_int (gst_byte_reader_masked_scan_uint32 (r,m,p,o,s), x); \
473     if (x != -1) { \
474       guint32 v, res_v; \
475       const guint8 *rdata = NULL; \
476       fail_unless (gst_byte_reader_peek_data (r, x + 4, &rdata)); \
477       res_v = GST_READ_UINT32_BE (rdata + x); \
478       fail_unless_equals_int (gst_byte_reader_masked_scan_uint32_peek (r,m,p,o,s,&v), x); \
479       fail_unless_equals_int (v, res_v); \
480     } \
481 } G_STMT_END;
482 
GST_START_TEST(test_scan)483 GST_START_TEST (test_scan)
484 {
485   GstByteReader reader;
486   guint8 data[200];
487   guint i;
488 
489   /* fill half the buffer with a pattern */
490   for (i = 0; i < 100; i++)
491     data[i] = i;
492 
493   gst_byte_reader_init (&reader, data, 100);
494 
495   /* find first bytes */
496   do_scan (&reader, 0xffffffff, 0x00010203, 0, 100, 0);
497   do_scan (&reader, 0xffffffff, 0x01020304, 0, 100, 1);
498   do_scan (&reader, 0xffffffff, 0x01020304, 1, 99, 1);
499   /* offset is past the pattern start */
500   do_scan (&reader, 0xffffffff, 0x01020304, 2, 98, -1);
501   /* not enough bytes to find the pattern */
502   do_scan (&reader, 0xffffffff, 0x02030405, 2, 3, -1);
503   do_scan (&reader, 0xffffffff, 0x02030405, 2, 4, 2);
504   /* size does not include the last scanned byte */
505   do_scan (&reader, 0xffffffff, 0x40414243, 0, 0x41, -1);
506   do_scan (&reader, 0xffffffff, 0x40414243, 0, 0x43, -1);
507   do_scan (&reader, 0xffffffff, 0x40414243, 0, 0x44, 0x40);
508   /* past the start */
509   do_scan (&reader, 0xffffffff, 0x40414243, 65, 10, -1);
510   do_scan (&reader, 0xffffffff, 0x40414243, 64, 5, 64);
511   do_scan (&reader, 0xffffffff, 0x60616263, 65, 35, 0x60);
512   do_scan (&reader, 0xffffffff, 0x60616263, 0x60, 4, 0x60);
513   /* past the start */
514   do_scan (&reader, 0xffffffff, 0x60616263, 0x61, 3, -1);
515   do_scan (&reader, 0xffffffff, 0x60616263, 99, 1, -1);
516 
517   /* add more data to the buffer */
518   for (i = 100; i < 200; i++)
519     data[i] = i;
520   gst_byte_reader_init (&reader, data, 200);
521 
522   /* past the start */
523   do_scan (&reader, 0xffffffff, 0x60616263, 0x61, 6, -1);
524   /* this should work */
525   do_scan (&reader, 0xffffffff, 0x61626364, 0x61, 4, 0x61);
526   /* not enough data */
527   do_scan (&reader, 0xffffffff, 0x62636465, 0x61, 4, -1);
528   do_scan (&reader, 0xffffffff, 0x62636465, 0x61, 5, 0x62);
529   do_scan (&reader, 0xffffffff, 0x62636465, 0, 120, 0x62);
530 
531   /* border conditions */
532   do_scan (&reader, 0xffffffff, 0x62636465, 0, 200, 0x62);
533   do_scan (&reader, 0xffffffff, 0x63646566, 0, 200, 0x63);
534   /* we completely searched the first list */
535   do_scan (&reader, 0xffffffff, 0x64656667, 0, 200, 0x64);
536   /* skip first buffer */
537   do_scan (&reader, 0xffffffff, 0x64656667, 0x64, 100, 0x64);
538   /* past the start */
539   do_scan (&reader, 0xffffffff, 0x64656667, 0x65, 10, -1);
540   /* not enough data to scan */
541   do_scan (&reader, 0xffffffff, 0x64656667, 0x63, 4, -1);
542   do_scan (&reader, 0xffffffff, 0x64656667, 0x63, 5, 0x64);
543   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0, 199, -1);
544   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0x62, 102, 0xc4);
545   /* different masks */
546   do_scan (&reader, 0x00ffffff, 0x00656667, 0x64, 100, 0x64);
547   do_scan (&reader, 0x000000ff, 0x00000000, 0, 100, -1);
548   do_scan (&reader, 0x000000ff, 0x00000003, 0, 100, 0);
549   do_scan (&reader, 0x000000ff, 0x00000061, 0x61, 100, -1);
550   do_scan (&reader, 0xff000000, 0x61000000, 0, 0x62, -1);
551   /* does not even exist */
552   do_scan (&reader, 0x00ffffff, 0xffffffff, 0x65, 99, -1);
553 
554   /* flush some bytes */
555   fail_unless (gst_byte_reader_skip (&reader, 0x20));
556 
557   do_scan (&reader, 0xffffffff, 0x20212223, 0, 100, 0);
558   do_scan (&reader, 0xffffffff, 0x20212223, 0, 4, 0);
559   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0x62, 70, 0xa4);
560   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0, 168, 0xa4);
561 
562   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 164, 4, 0xa4);
563   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0x44, 100, 0xa4);
564 
565   /* not enough bytes */
566   do_scan (&reader, 0xffffffff, 0xc4c5c6c7, 0x44, 99, -1);
567 
568   /* check special code path that exists for 00 00 01 sync marker */
569   {
570     const guint8 sync_data[] = { 0xA0, 0x00, 0x00, 0x00, 0x01, 0xA5, 0xA6,
571       0xA7, 0xA8, 0xA9, 0xAA, 0x00, 0x00, 0x00, 0x01, 0xAF, 0xB0, 0xB1
572     };
573     guint32 val = 0;
574     guint8 *m;
575     gint found;
576 
577     /* dup so valgrind can detect out of bounds access more easily */
578     m = g_memdup2 (sync_data, sizeof (sync_data));
579     gst_byte_reader_init (&reader, m, sizeof (sync_data));
580 
581     found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00,
582         0x00000100, 0, sizeof (sync_data), &val);
583     fail_unless_equals_int (found, 2);
584     fail_unless_equals_int (val, 0x000001A5);
585 
586     found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00,
587         0x00000100, 2, sizeof (sync_data) - 2, &val);
588     fail_unless_equals_int (found, 2);
589     fail_unless_equals_int (val, 0x000001A5);
590 
591     found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00,
592         0x00000100, 3, sizeof (sync_data) - 3, &val);
593     fail_unless_equals_int (found, 12);
594     fail_unless_equals_int (val, 0x000001AF);
595 
596     found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00,
597         0x00000100, 12, sizeof (sync_data) - 12, &val);
598     fail_unless_equals_int (found, 12);
599     fail_unless_equals_int (val, 0x000001AF);
600 
601     found = gst_byte_reader_masked_scan_uint32_peek (&reader, 0xffffff00,
602         0x00000100, 13, sizeof (sync_data) - 13, &val);
603     fail_unless_equals_int (found, -1);
604 
605     g_free (m);
606   }
607 }
608 
609 GST_END_TEST;
610 
GST_START_TEST(test_string_funcs)611 GST_START_TEST (test_string_funcs)
612 {
613   GstByteReader reader, backup;
614   const gchar *s8;
615   guint32 *c32;
616   guint16 *c16;
617   gchar *c8;
618   guint8 data[200], *d = 0;
619   guint i;
620 
621   /* fill half the buffer with a pattern */
622   for (i = 0; i < 100; i++)
623     data[i] = i + 1;
624 
625   gst_byte_reader_init (&reader, data, 100);
626 
627   /* no NUL terminator, so these should all fail */
628   fail_if (gst_byte_reader_get_string (&reader, &s8));
629   fail_if (gst_byte_reader_get_string_utf8 (&reader, &s8));
630   fail_if (gst_byte_reader_dup_string (&reader, &c8));
631   fail_if (gst_byte_reader_dup_string_utf8 (&reader, &c8));
632   fail_if (gst_byte_reader_skip_string (&reader));
633   fail_if (gst_byte_reader_skip_string_utf8 (&reader));
634   fail_if (gst_byte_reader_skip_string_utf16 (&reader));
635   fail_if (gst_byte_reader_skip_string_utf32 (&reader));
636   fail_if (gst_byte_reader_peek_string (&reader, &s8));
637   fail_if (gst_byte_reader_peek_string_utf8 (&reader, &s8));
638   fail_if (gst_byte_reader_dup_string_utf16 (&reader, &c16));
639   fail_if (gst_byte_reader_dup_string_utf32 (&reader, &c32));
640 
641   /* let's add a single NUL terminator */
642   data[80] = '\0';
643   backup = reader;
644   fail_if (gst_byte_reader_skip_string_utf32 (&reader));
645   fail_if (gst_byte_reader_skip_string_utf16 (&reader));
646   fail_if (gst_byte_reader_dup_string_utf16 (&reader, &c16));
647   fail_if (gst_byte_reader_dup_string_utf32 (&reader, &c32));
648   fail_unless (gst_byte_reader_skip_string (&reader));
649   reader = backup;
650   fail_unless (gst_byte_reader_skip_string_utf8 (&reader));
651   reader = backup;
652   fail_unless (gst_byte_reader_peek_string (&reader, &s8));
653   fail_unless (gst_byte_reader_peek_string_utf8 (&reader, &s8));
654   fail_if (gst_byte_reader_dup_string_utf16 (&reader, &c16));
655   fail_if (gst_byte_reader_dup_string_utf32 (&reader, &c32));
656 
657   /* let's add another NUL terminator */
658   data[81] = '\0';
659   reader = backup;
660   fail_if (gst_byte_reader_skip_string_utf32 (&reader));
661   fail_if (gst_byte_reader_dup_string_utf32 (&reader, &c32));
662   fail_unless (gst_byte_reader_skip_string_utf16 (&reader));
663   reader = backup;
664   fail_unless (gst_byte_reader_dup_string_utf16 (&reader, &c16));
665   g_free (c16);
666   reader = backup;
667   fail_unless (gst_byte_reader_skip_string (&reader));
668   reader = backup;
669   fail_unless (gst_byte_reader_skip_string_utf8 (&reader));
670   reader = backup;
671   fail_unless (gst_byte_reader_peek_string (&reader, &s8));
672   fail_unless (gst_byte_reader_peek_string_utf8 (&reader, &s8));
673   fail_if (gst_byte_reader_dup_string_utf32 (&reader, &c32));
674 
675   /* two more NUL terminators */
676   data[79] = '\0';
677   data[82] = '\0';
678   reader = backup;
679   /* we're at pos. 80 now, so have only 3 NUL terminators in front of us */
680   fail_if (gst_byte_reader_skip_string_utf32 (&reader));
681   /* let's rewind */
682   gst_byte_reader_init (&reader, data, 100);
683   backup = reader;
684   /* oops, 79 is not dividable by 4, so not aligned, so should fail as well! */
685   fail_if (gst_byte_reader_skip_string_utf32 (&reader));
686   /* let's try that again */
687   data[83] = '\0';
688   gst_byte_reader_init (&reader, data, 100);
689   backup = reader;
690   fail_unless (gst_byte_reader_skip_string_utf16 (&reader));
691   reader = backup;
692   fail_unless (gst_byte_reader_skip_string (&reader));
693   reader = backup;
694   fail_unless (gst_byte_reader_skip_string_utf8 (&reader));
695   reader = backup;
696   fail_unless (gst_byte_reader_peek_string (&reader, &s8));
697   fail_unless (gst_byte_reader_peek_string_utf8 (&reader, &s8));
698   fail_unless (gst_byte_reader_dup_string_utf16 (&reader, &c16));
699   g_free (c16);
700   reader = backup;
701   fail_unless (gst_byte_reader_dup_string_utf32 (&reader, &c32));
702   g_free (c32);
703 
704   /* and again from the start */
705   gst_byte_reader_init (&reader, data, 100);
706   fail_unless (gst_byte_reader_skip_string_utf16 (&reader));
707   fail_if (gst_byte_reader_dup_data (&reader, 200, &d));
708   fail_if (gst_byte_reader_dup_data (&reader, 100, &d));
709   fail_if (gst_byte_reader_dup_data (&reader, 20, &d));
710   fail_unless (gst_byte_reader_dup_data (&reader, 10, &d));
711   fail_unless_equals_int (d[0], 0);
712   fail_unless_equals_int (d[1], 0);
713   fail_unless_equals_int (d[2], 85);
714   fail_unless_equals_int (d[3], 86);
715   g_free (d);
716 }
717 
718 GST_END_TEST;
719 
GST_START_TEST(test_dup_string)720 GST_START_TEST (test_dup_string)
721 {
722   const gchar moredata[] = { 0x99, 0x10, 'f', '0', '0', '!', '\0', 0xff };
723   GstByteReader reader;
724   guint16 num = 0;
725   guint8 x = 0;
726   gchar *s;
727 
728   gst_byte_reader_init (&reader, (guint8 *) moredata, sizeof (moredata));
729   fail_unless (gst_byte_reader_get_uint16_be (&reader, &num));
730   fail_unless_equals_int (num, 0x9910);
731   fail_unless (gst_byte_reader_dup_string (&reader, &s));
732   fail_unless_equals_string (s, "f00!");
733   fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
734   fail_unless_equals_int (x, 0xff);
735   g_free (s);
736 }
737 
738 GST_END_TEST;
739 
GST_START_TEST(test_sub_reader)740 GST_START_TEST (test_sub_reader)
741 {
742   const guint8 memdata[] = {
743     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
744     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
745   };
746   GstByteReader reader = GST_BYTE_READER_INIT (memdata, sizeof (memdata));
747   GstByteReader sub;
748   const guint8 *data = NULL, *sub_data = NULL;
749   guint16 v = 0;
750 
751   /* init sub reader */
752   fail_if (gst_byte_reader_peek_sub_reader (&reader, &sub, 17));
753   fail_unless (gst_byte_reader_peek_sub_reader (&reader, &sub, 16));
754   fail_unless_equals_int (gst_byte_reader_get_remaining (&sub), 16);
755   fail_unless (gst_byte_reader_peek_data (&reader, 16, &data));
756   fail_unless (gst_byte_reader_peek_data (&sub, 16, &sub_data));
757   fail_unless (memcmp (data, sub_data, 16) == 0);
758 
759   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 16);
760   fail_unless (gst_byte_reader_skip (&reader, 3));
761   fail_if (gst_byte_reader_peek_sub_reader (&reader, &sub, 14));
762   fail_unless (gst_byte_reader_peek_sub_reader (&reader, &sub, 13));
763   fail_unless_equals_int (gst_byte_reader_get_remaining (&sub), 13);
764   fail_unless (gst_byte_reader_peek_data (&reader, 13, &data));
765   fail_unless (gst_byte_reader_peek_data (&sub, 13, &sub_data));
766   fail_unless (memcmp (data, sub_data, 13) == 0);
767   fail_unless (memcmp (memdata + 3, sub_data, 13) == 0);
768 
769   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 13);
770   fail_unless (gst_byte_reader_peek_sub_reader (&reader, &sub, 3));
771   fail_unless_equals_int (gst_byte_reader_get_remaining (&sub), 3);
772   fail_if (gst_byte_reader_peek_data (&sub, 10, &sub_data));
773   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
774   fail_unless_equals_int (v, 0x0304);
775   fail_if (gst_byte_reader_get_uint16_be (&sub, &v));
776   fail_unless_equals_int (gst_byte_reader_get_remaining (&sub), 1);
777 
778   fail_unless (gst_byte_reader_get_uint16_be (&reader, &v));
779   fail_unless_equals_int (v, 0x0304);
780   fail_unless (gst_byte_reader_get_uint16_be (&reader, &v));
781   fail_unless_equals_int (v, 0x0506);
782   fail_unless_equals_int (gst_byte_reader_get_remaining (&reader), 9);
783 
784   /* get sub reader */
785   gst_byte_reader_init (&reader, memdata, sizeof (memdata));
786   fail_if (gst_byte_reader_get_sub_reader (&reader, &sub, 17));
787   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 16));
788   fail_if (gst_byte_reader_get_sub_reader (&reader, &sub, 1));
789   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 0));
790 
791   gst_byte_reader_init (&reader, memdata, sizeof (memdata));
792   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 2));
793   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
794   fail_unless_equals_int (v, 0x0001);
795   fail_if (gst_byte_reader_get_uint16_be (&sub, &v));
796   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 3));
797   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
798   fail_unless_equals_int (v, 0x0203);
799   fail_if (gst_byte_reader_get_uint16_be (&sub, &v));
800   fail_unless_equals_int (gst_byte_reader_get_uint8_unchecked (&sub), 0x04);
801   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 9));
802   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
803   fail_unless_equals_int (v, 0x0506);
804   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
805   fail_unless_equals_int (v, 0x0708);
806   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
807   fail_unless_equals_int (v, 0x090a);
808   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
809   fail_unless_equals_int (v, 0x0b0c);
810   fail_if (gst_byte_reader_get_uint16_be (&sub, &v));
811   fail_unless_equals_int (gst_byte_reader_get_uint8_unchecked (&sub), 0x0d);
812   fail_if (gst_byte_reader_get_sub_reader (&reader, &sub, 3));
813   fail_unless (gst_byte_reader_get_sub_reader (&reader, &sub, 2));
814   fail_unless (gst_byte_reader_get_uint16_be (&sub, &v));
815   fail_unless_equals_int (v, 0x0e0f);
816   fail_if (gst_byte_reader_get_uint16_be (&sub, &v));
817   fail_if (gst_byte_reader_get_uint16_be (&reader, &v));
818 }
819 
820 GST_END_TEST;
821 
822 static Suite *
gst_byte_reader_suite(void)823 gst_byte_reader_suite (void)
824 {
825   Suite *s = suite_create ("GstByteReader");
826   TCase *tc_chain = tcase_create ("general");
827 
828   suite_add_tcase (s, tc_chain);
829 
830   tcase_add_test (tc_chain, test_initialization);
831   tcase_add_test (tc_chain, test_get_uint_le);
832   tcase_add_test (tc_chain, test_get_uint_be);
833   tcase_add_test (tc_chain, test_get_int_le);
834   tcase_add_test (tc_chain, test_get_int_be);
835   tcase_add_test (tc_chain, test_get_float_le);
836   tcase_add_test (tc_chain, test_get_float_be);
837   tcase_add_test (tc_chain, test_position_tracking);
838   tcase_add_test (tc_chain, test_scan);
839   tcase_add_test (tc_chain, test_string_funcs);
840   tcase_add_test (tc_chain, test_dup_string);
841   tcase_add_test (tc_chain, test_sub_reader);
842 
843   return s;
844 }
845 
846 
847 GST_CHECK_MAIN (gst_byte_reader);
848