protoverse

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

parser.h (748B)


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