// Carry this state across arbitrary network reads. No payload buffering is required. pub fn netstring_start() -> vector { [0, 0, 0] } // Slots: parser phase, pending length/remaining bytes, event. // Events: -1 invalid; 0 header; 1 payload byte; 2 completed frame. pub fn netstring_step(state: vector, byte: f64, limit: f64) -> vector { if len(state) != 3 || limit < 0.0 || limit > 1073741824.0 || byte < 0.0 || byte > 255.0 || !(byte % 1.0 == 0.0 && limit % 1.0 == 0.0) { return [-1, 0, -1]; } let phase = at(state, 0); let count = at(state, 1); if !(count >= 0.0 && count <= limit && count % 1.0 == 0.0) { return [-1, 0, -1]; } if phase == 0.0 || phase == 1.0 { if phase == 1.0 && byte == 58.0 { if count == 0.0 { return [3, 0, 0]; } return [2, count, 0]; } if byte < 48.0 || byte > 57.0 || (phase == 1.0 && count == 0.0) { return [-1, 0, -1]; } let next = count * 10.0 + (byte - 48.0); if next > limit { return [-1, 0, -1]; } return [1, next, 0]; } if phase == 2.0 && count >= 1.0 { if count == 1.0 { return [3, 0, 1]; } return [2, count - 1.0, 1]; } if phase == 3.0 && count == 0.0 && byte == 44.0 { return [0, 0, 2]; } return [-1, 0, -1]; } pub fn netstring_complete(state: vector) -> bool { if len(state) != 3 { return false; } at(state, 0) == 0.0 && at(state, 1) == 0.0 }