websocket, utf: u8 fixes

Alexander Medvednikov 2022-04-15 16:24:02 +03:00 committed by Jef Roosens
parent 69eec2590d
commit 9521665f00
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 18 additions and 18 deletions

View File

@ -3,7 +3,7 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module builtin module builtin
pub fn utf8_char_len(b byte) int { pub fn utf8_char_len(b u8) int {
return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1 return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1
} }
@ -22,7 +22,7 @@ pub fn utf32_to_str(code u32) string {
} }
[manualfree; unsafe] [manualfree; unsafe]
pub fn utf32_to_str_no_malloc(code u32, buf &byte) string { pub fn utf32_to_str_no_malloc(code u32, buf &u8) string {
unsafe { unsafe {
len := utf32_decode_to_buffer(code, buf) len := utf32_decode_to_buffer(code, buf)
if len == 0 { if len == 0 {
@ -34,7 +34,7 @@ pub fn utf32_to_str_no_malloc(code u32, buf &byte) string {
} }
[manualfree; unsafe] [manualfree; unsafe]
pub fn utf32_decode_to_buffer(code u32, buf &byte) int { pub fn utf32_decode_to_buffer(code u32, buf &u8) int {
unsafe { unsafe {
icode := int(code) // Prevents doing casts everywhere icode := int(code) // Prevents doing casts everywhere
mut buffer := &u8(buf) mut buffer := &u8(buf)
@ -112,7 +112,7 @@ pub fn (_bytes []u8) utf8_to_utf32() ?rune {
} }
// Calculate length to read from the first byte // Calculate length to read from the first byte
fn utf8_len(c byte) int { fn utf8_len(c u8) int {
mut b := 0 mut b := 0
mut x := c mut x := c
if (x & 240) != 0 { if (x & 240) != 0 {

View File

@ -11,7 +11,7 @@ const (
// Fragment represents a websocket data fragment // Fragment represents a websocket data fragment
struct Fragment { struct Fragment {
data []u8 // included data payload data in a fragment data []u8 // included data payload data in a fragment
opcode OPCode // interpretation of the payload data opcode OPCode // interpretation of the payload data
} }
@ -22,14 +22,14 @@ mut:
header_len int = 2 header_len int = 2
// size of total frame // size of total frame
frame_size int = 2 frame_size int = 2
fin bool // true if final fragment of message fin bool // true if final fragment of message
rsv1 bool // reserved for future use in websocket RFC rsv1 bool // reserved for future use in websocket RFC
rsv2 bool // reserved for future use in websocket RFC rsv2 bool // reserved for future use in websocket RFC
rsv3 bool // reserved for future use in websocket RFC rsv3 bool // reserved for future use in websocket RFC
opcode OPCode // interpretation of the payload data opcode OPCode // interpretation of the payload data
has_mask bool // true if the payload data is masked has_mask bool // true if the payload data is masked
payload_len int // payload length payload_len int // payload length
masking_key [4]u8 // all frames from client to server is masked with this key masking_key [4]u8 // all frames from client to server is masked with this key
} }
const ( const (
@ -86,7 +86,7 @@ fn (mut ws Client) read_payload(frame &Frame) ?[]u8 {
return []u8{} return []u8{}
} }
mut buffer := []u8{cap: frame.payload_len} mut buffer := []u8{cap: frame.payload_len}
mut read_buf := [1]byte{} mut read_buf := [1]u8{}
mut bytes_read := 0 mut bytes_read := 0
for bytes_read < frame.payload_len { for bytes_read < frame.payload_len {
len := ws.socket_read_ptr(&read_buf[0], 1) ? len := ws.socket_read_ptr(&read_buf[0], 1) ?
@ -209,10 +209,10 @@ fn (ws Client) opcode_from_fragments() OPCode {
// parse_frame_header parses next message by decoding the incoming frames // parse_frame_header parses next message by decoding the incoming frames
pub fn (mut ws Client) parse_frame_header() ?Frame { pub fn (mut ws Client) parse_frame_header() ?Frame {
mut buffer := [256]byte{} mut buffer := [256]u8{}
mut bytes_read := 0 mut bytes_read := 0
mut frame := Frame{} mut frame := Frame{}
mut rbuff := [1]byte{} mut rbuff := [1]u8{}
mut mask_end_byte := 0 mut mask_end_byte := 0
for ws.state == .open { for ws.state == .open {
read_bytes := ws.socket_read_ptr(&rbuff[0], 1) ? read_bytes := ws.socket_read_ptr(&rbuff[0], 1) ?

View File

@ -1,4 +1,4 @@
type MyByte = byte type MyByte = u8
type MyInt = int type MyInt = int

View File

@ -9,7 +9,7 @@ import strings
// Encoder encodes the an `Any` type into JSON representation. // Encoder encodes the an `Any` type into JSON representation.
// It provides parameters in order to change the end result. // It provides parameters in order to change the end result.
pub struct Encoder { pub struct Encoder {
newline byte newline u8
newline_spaces_count int newline_spaces_count int
escape_unicode bool = true escape_unicode bool = true
} }