@@ -1868,8 +1868,27 @@ namespace serialize
18681868
18691869 #define read_float serialize_float
18701870 #define read_double serialize_double
1871- #define read_bytes serialize_bytes
1872- #define read_string serialize_string
1871+
1872+ #define read_bytes ( stream, data, bytes ) \
1873+ do \
1874+ { \
1875+ uint8_t * data_ptr = (uint8_t *) data; \
1876+ if ( !stream.SerializeBytes ( data_ptr, bytes ) ) \
1877+ { \
1878+ return false ; \
1879+ } \
1880+ } while (0 )
1881+
1882+ #define read_string ( stream, string, buffer_size ) \
1883+ do \
1884+ { \
1885+ char * string_ptr = (char *) string; \
1886+ if ( !serialize_string_internal ( stream, string_ptr, buffer_size ) ) \
1887+ { \
1888+ return false ; \
1889+ } \
1890+ } while (0 )
1891+
18731892 #define read_align serialize_align
18741893 #define read_check serialize_check
18751894 #define read_object serialize_object
@@ -2170,7 +2189,7 @@ struct TestObject
21702189 data.int_relative = 5 ;
21712190
21722191 for ( int i = 0 ; i < (int ) sizeof ( data.bytes ); ++i )
2173- data.bytes [i] = rand () % 255 ;
2192+ data.bytes [i] = ( uint8_t ) ( i + 5 ) * 13 ;
21742193
21752194 serialize_copy_string ( data.string , " hello world!" , sizeof (data.string ) - 1 );
21762195 }
@@ -2259,39 +2278,108 @@ inline void test_serialize()
22592278
22602279bool ReadFunction ( serialize::ReadStream & readStream )
22612280{
2262- (void ) readStream;
2281+ // IMPORTANT: You wouldn't normally write a read function like this, but I'm just checking each value as it's read in
2282+ // Note that the only thing the read function has to have is to return bool: true on success, false on failing to read.
2283+ // This is important because protects you from maliciously crafted packets.
2284+
2285+ {
2286+ uint32_t value;
2287+ read_bits ( readStream, value, 4 );
2288+ serialize_check ( value == 13 );
2289+ }
2290+
2291+ {
2292+ bool value;
2293+ read_bool ( readStream, value );
2294+ serialize_check ( value == true );
2295+ }
2296+
2297+ {
2298+ uint8_t value;
2299+ read_uint8 ( readStream, value );
2300+ serialize_check ( value == 255 );
2301+ }
2302+
2303+ {
2304+ uint16_t value;
2305+ read_uint16 ( readStream, value );
2306+ serialize_check ( value == 65535 );
2307+ }
2308+
2309+ {
2310+ uint32_t value;
2311+ read_uint32 ( readStream, value );
2312+ serialize_check ( value == 0xFFFFFFFF );
2313+ }
2314+
2315+ {
2316+ uint64_t value;
2317+ read_uint64 ( readStream, value );
2318+ serialize_check ( value == 0xFFFFFFFFFFFFFFFFULL ); // i am very full
2319+ }
2320+
2321+ {
2322+ int value;
2323+ read_int ( readStream, value, 10 , 90 );
2324+ serialize_check ( value == 55 );
2325+ }
2326+
2327+ {
2328+ float value;
2329+ read_float ( readStream, value );
2330+ serialize_check ( value == 100 .0f );
2331+ }
22632332
2264- /*
2265- write_bits( writeStream, 13, 4 );
2266- write_bool( writeStream, true );
2267- write_uint8( writeStream, 255 );
2268- write_uint16( writeStream, 65535 );
2269- write_uint32( writeStream, 0xFFFFFFFF );
2270- write_uint32( writeStream, 0xFFFFFFFFFFFFFFFFULL );
2271- write_float( writeStream, 100.0f );
2272- write_double( writeStream, 1000000000.0f );
2333+ {
2334+ double value;
2335+ read_double ( readStream, value );
2336+ serialize_check ( value == 1000000000.0 );
2337+ }
22732338
2274- char data[5] = { 1, 2, 3, 4, 5 };
2275- write_bytes( writeStream, data, 5 );
2339+ {
2340+ char value[5 ];
2341+ read_bytes ( readStream, value, 5 );
2342+ serialize_check ( value[0 ] == 1 );
2343+ serialize_check ( value[1 ] == 2 );
2344+ serialize_check ( value[2 ] == 3 );
2345+ serialize_check ( value[3 ] == 4 );
2346+ serialize_check ( value[4 ] == 5 );
2347+ }
22762348
2277- const char * string = "hello";
2278- write_string( writeStream, string, 10 );
2349+ {
2350+ char string[10 ];
2351+ read_string ( readStream, string, 10 );
2352+ serialize_check ( string[0 ] == ' h' );
2353+ serialize_check ( string[1 ] == ' e' );
2354+ serialize_check ( string[2 ] == ' l' );
2355+ serialize_check ( string[3 ] == ' l' );
2356+ serialize_check ( string[4 ] == ' o' );
2357+ serialize_check ( string[5 ] == ' \0 ' );
2358+ }
22792359
2280- write_align( writeStream );
2360+ read_align ( readStream );
22812361
22822362 TestContext context;
22832363 context.min = -10 ;
22842364 context.max = +10 ;
22852365
2286- writeStream.SetContext( &context );
2366+ readStream.SetContext ( &context );
2367+ {
2368+ TestObject expectedObject;
2369+ expectedObject.Init ();
2370+
2371+ TestObject readObject;
22872372
2288- TestObject object;
2289- object.Init();
2373+ read_object ( readStream, readObject );
22902374
2291- write_object( writeStream, object );
2375+ serialize_check ( readObject == expectedObject );
2376+ }
22922377
2293- write_int_relative( writeStream, 100, 105 );
2294- */
2378+ {
2379+ int value;
2380+ read_int_relative ( readStream, 100 , value );
2381+ serialize_check ( value == 105 );
2382+ }
22952383
22962384 return true ;
22972385}
@@ -2314,7 +2402,8 @@ inline void test_read_write()
23142402 write_uint8 ( writeStream, 255 );
23152403 write_uint16 ( writeStream, 65535 );
23162404 write_uint32 ( writeStream, 0xFFFFFFFF );
2317- write_uint32 ( writeStream, 0xFFFFFFFFFFFFFFFFULL );
2405+ write_uint64 ( writeStream, 0xFFFFFFFFFFFFFFFFULL );
2406+ write_int ( writeStream, 55 , 10 , 90 );
23182407 write_float ( writeStream, 100 .0f );
23192408 write_double ( writeStream, 1000000000 .0f );
23202409
@@ -2349,7 +2438,7 @@ inline void test_read_write()
23492438 // read from the buffer
23502439 {
23512440 serialize::ReadStream readStream;
2352- readStream.Initialize ( buffer, BufferSize );
2441+ readStream.Initialize ( buffer, bytesWritten );
23532442 serialize_check ( ReadFunction ( readStream ) );
23542443 }
23552444}
@@ -2364,7 +2453,7 @@ inline void test_read_write()
23642453
23652454inline void serialize_test ()
23662455{
2367- // while ( 1 )
2456+ while ( 1 )
23682457 {
23692458 SERIALIZE_RUN_TEST ( test_endian );
23702459 SERIALIZE_RUN_TEST ( test_bitpacker );
0 commit comments