11#include "proto-minecraft.h"
22#include "unusedparm.h"
33
4- #define STATE_READ_PACKET_LENGTH 1
5- #define STATE_READ_PACKET_ID 2
6- #define STATE_READ_DATA_LENGTH 3
7- #define STATE_READ_DATA 4
8- #define STATE_MALFORMED 5
4+ #define STATE_READ_PACKETLEN 1
5+ #define STATE_READ_PACKET_ID 2
6+ #define STATE_READ_DATALEN 3
7+ #define STATE_READ_DATA 4
98
109static const char minecraft_hello [] = {
1110 0x15 , // length
@@ -26,62 +25,43 @@ static void minecraft_parse(const struct Banner1 *banner1,
2625 struct InteractiveData * more ) {
2726
2827 unsigned state = stream_state -> state ; // assuming this starts out at zero
29- struct MINECRAFTSTUFF * mc = & stream_state -> sub .minecraft ;
30-
28+
3129 UNUSEDPARM (banner1_private );
3230 UNUSEDPARM (banner1 );
3331
34- // TODO: figure out why the banner gets cut off occasionally
35- // LOG(0, "minecraft_parse: %c%c%c%c\n",px[0],px[1],px[2],px[3]);
36-
3732 // beware: the `bytes_read` field is reused
3833 for (size_t i = 0 ; i < length ; i ++ ) {
3934 switch (state ) {
4035
4136 // initial: set up some stuff
4237 case 0 :
43- mc -> varint_accum = 0 ;
44- mc -> bytes_read = 0 ;
45- state = STATE_READ_PACKET_LENGTH ;
38+ state = STATE_READ_PACKETLEN ;
4639 // !!! fall through !!!
4740
4841 // read varint
4942 // reuse same code for both varints
50- case STATE_READ_PACKET_LENGTH :
51- case STATE_READ_DATA_LENGTH :
52- if (mc -> bytes_read > 5 ) { // varint too long
53- state = STATE_MALFORMED ;
54- break ;
55- }
56- mc -> varint_accum |= (unsigned )(px [i ] & 0x7f ) << (mc -> bytes_read * 7 );
57- if (!(px [i ] & 0x80 )) { // msb indicates whether there are more bytes in the varint
58- if (state == STATE_READ_PACKET_LENGTH ) {
43+ case STATE_READ_PACKETLEN :
44+ case STATE_READ_DATALEN :
45+ // MSB indicates whether there are more bytes in the varint
46+ // varints shouldn't be longer than 5 bytes, but we don't enforce this restriction
47+ if (!(px [i ] & 0x80 )) {
48+ if (state == STATE_READ_PACKETLEN )
5949 state = STATE_READ_PACKET_ID ;
60- } else {
61- mc -> bytes_read = 0 ;
50+ else
6251 state = STATE_READ_DATA ;
63- }
6452 }
65- mc -> bytes_read ++ ;
6653 break ;
6754
6855 case STATE_READ_PACKET_ID :
6956 if (px [i ] == 0x00 ) {
70- mc -> varint_accum = 0 ;
71- mc -> bytes_read = 0 ;
72- state = STATE_READ_DATA_LENGTH ;
57+ state = STATE_READ_DATALEN ;
7358 } else {
74- state = STATE_MALFORMED ;
59+ state = 0xffffffff ;
7560 }
7661 break ;
7762
7863 case STATE_READ_DATA :
79- if (mc -> bytes_read == mc -> varint_accum ) {
80- state = STATE_MALFORMED ;
81- break ;
82- }
8364 banout_append_char (banout , PROTO_MINECRAFT , px [i ]);
84- mc -> bytes_read ++ ;
8565 break ;
8666
8767 // skip to the end if something went wrong
0 commit comments