protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

parser.c (585B)


      1 
      2 #include "parser.h"
      3 #include <stdio.h>
      4 
      5 
      6 int consume_bytes(struct cursor *cursor, const unsigned char *match, int len)
      7 {
      8 	int i;
      9 
     10 	if (cursor->p + len > cursor->end) {
     11 		fprintf(stderr, "consume_bytes overflow\n");
     12 		return 0;
     13 	}
     14 
     15 	for (i = 0; i < len; i++) {
     16 		if (cursor->p[i] != match[i])
     17 			return 0;
     18 	}
     19 
     20 	cursor->p += len;
     21 
     22 	return 1;
     23 }
     24 
     25 int consume_byte(struct cursor *cursor, unsigned char match)
     26 {
     27 	return consume_bytes(cursor, &match, 1);
     28 }
     29 
     30 int consume_u32(struct cursor *cursor, unsigned int match)
     31 {
     32 	return consume_bytes(cursor, (unsigned char*)&match, sizeof(match));
     33 }