|
|
|
@ -24,26 +24,25 @@ typedef struct
|
|
|
|
|
b8 serializing;
|
|
|
|
|
} SerState;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ser_bytes(SerState *ser, u8 *bytes, u64 bytes_size)
|
|
|
|
|
{
|
|
|
|
|
if(!ser->data && !ser->serializing)
|
|
|
|
|
if (!ser->data && !ser->serializing)
|
|
|
|
|
{
|
|
|
|
|
ser->cur_error = (SerError){.failed = true, .why = S8Lit("Deserializing but the data is null")};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!ser->cur_error.failed)
|
|
|
|
|
if (!ser->cur_error.failed)
|
|
|
|
|
{
|
|
|
|
|
if(ser->data)
|
|
|
|
|
if (ser->data)
|
|
|
|
|
{
|
|
|
|
|
// maximum doesn't matter unless writing to data
|
|
|
|
|
if(ser->cur + bytes_size > ser->max)
|
|
|
|
|
if (ser->cur + bytes_size > ser->max)
|
|
|
|
|
{
|
|
|
|
|
ser->cur_error = (SerError){.failed = true, .why = S8Lit("Too big bro")};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(ser->serializing)
|
|
|
|
|
if (ser->serializing)
|
|
|
|
|
{
|
|
|
|
|
memcpy(ser->data + ser->cur, bytes, bytes_size);
|
|
|
|
|
}
|
|
|
|
@ -52,17 +51,17 @@ void ser_bytes(SerState *ser, u8 *bytes, u64 bytes_size)
|
|
|
|
|
memcpy(bytes, ser->data + ser->cur, bytes_size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ser->cur += bytes_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SER_MAKE_FOR_TYPE(type) void ser_##type(SerState *ser, type *into) \
|
|
|
|
|
{ \
|
|
|
|
|
ser_bytes(ser, (u8*)into, sizeof(*into)); \
|
|
|
|
|
}
|
|
|
|
|
#define SER_MAKE_FOR_TYPE(type) \
|
|
|
|
|
void ser_##type(SerState *ser, type *into) \
|
|
|
|
|
{ \
|
|
|
|
|
ser_bytes(ser, (u8 *)into, sizeof(*into)); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SER_MAKE_FOR_TYPE(int);
|
|
|
|
|
SER_MAKE_FOR_TYPE(u64);
|
|
|
|
@ -72,7 +71,7 @@ SER_MAKE_FOR_TYPE(u64);
|
|
|
|
|
void ser_String8(SerState *ser, String8 *s, Arena *allocate_onto)
|
|
|
|
|
{
|
|
|
|
|
ser_u64(ser, &s->size);
|
|
|
|
|
if(ser->serializing)
|
|
|
|
|
if (ser->serializing)
|
|
|
|
|
{
|
|
|
|
|
ser_bytes(ser, s->str, s->size);
|
|
|
|
|
}
|
|
|
|
@ -82,4 +81,3 @@ void ser_String8(SerState *ser, String8 *s, Arena *allocate_onto)
|
|
|
|
|
ser_bytes(ser, s->str, s->size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|