1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: replace []byte with []u8

This commit is contained in:
Alexander Medvednikov
2022-04-15 15:35:35 +03:00
parent 0527ac633e
commit fb192d949b
164 changed files with 533 additions and 533 deletions

View File

@@ -4,7 +4,7 @@ import net
import time
// socket_read reads from socket into the provided buffer
fn (mut ws Client) socket_read(mut buffer []byte) ?int {
fn (mut ws Client) socket_read(mut buffer []u8) ?int {
lock {
if ws.state in [.closed, .closing] || ws.conn.sock.handle <= 1 {
return error('socket_read: trying to read a closed socket')
@@ -52,7 +52,7 @@ fn (mut ws Client) socket_read_ptr(buf_ptr &byte, len int) ?int {
}
// socket_write writes the provided byte array to the socket
fn (mut ws Client) socket_write(bytes []byte) ?int {
fn (mut ws Client) socket_write(bytes []u8) ?int {
lock {
if ws.state == .closed || ws.conn.sock.handle <= 1 {
ws.debug_log('socket_write: Socket allready closed')

View File

@@ -11,7 +11,7 @@ const (
// Fragment represents a websocket data fragment
struct Fragment {
data []byte // included data payload data in a fragment
data []u8 // included data payload data in a fragment
opcode OPCode // interpretation of the payload data
}
@@ -81,11 +81,11 @@ fn is_data_frame(opcode OPCode) bool {
}
// read_payload reads the message payload from the socket
fn (mut ws Client) read_payload(frame &Frame) ?[]byte {
fn (mut ws Client) read_payload(frame &Frame) ?[]u8 {
if frame.payload_len == 0 {
return []byte{}
return []u8{}
}
mut buffer := []byte{cap: frame.payload_len}
mut buffer := []u8{cap: frame.payload_len}
mut read_buf := [1]byte{}
mut bytes_read := 0
for bytes_read < frame.payload_len {
@@ -109,7 +109,7 @@ fn (mut ws Client) read_payload(frame &Frame) ?[]byte {
// validate_utf_8 validates payload for valid utf8 encoding
// - Future implementation needs to support fail fast utf errors for strict autobahn conformance
fn (mut ws Client) validate_utf_8(opcode OPCode, payload []byte) ? {
fn (mut ws Client) validate_utf_8(opcode OPCode, payload []u8) ? {
if opcode in [.text_frame, .close] && !utf8.validate(payload.data, payload.len) {
ws.logger.error('malformed utf8 payload, payload len: ($payload.len)')
ws.send_error_event('Recieved malformed utf8.')
@@ -181,7 +181,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
}
// payload_from_fragments returs the whole paylaod from fragmented message
fn (ws Client) payload_from_fragments(fin_payload []byte) ?[]byte {
fn (ws Client) payload_from_fragments(fin_payload []u8) ?[]u8 {
mut total_size := 0
for f in ws.fragments {
if f.data.len > 0 {
@@ -190,9 +190,9 @@ fn (ws Client) payload_from_fragments(fin_payload []byte) ?[]byte {
}
total_size += fin_payload.len
if total_size == 0 {
return []byte{}
return []u8{}
}
mut total_buffer := []byte{cap: total_size}
mut total_buffer := []u8{cap: total_size}
for f in ws.fragments {
if f.data.len > 0 {
total_buffer << f.data
@@ -288,7 +288,7 @@ pub fn (mut ws Client) parse_frame_header() ?Frame {
}
// unmask_sequence unmask any given sequence
fn (f Frame) unmask_sequence(mut buffer []byte) {
fn (f Frame) unmask_sequence(mut buffer []u8) {
for i in 0 .. buffer.len {
buffer[i] ^= f.masking_key[i % 4] & 0xff
}

View File

@@ -5,8 +5,8 @@ import crypto.sha1
import encoding.base64
// htonl64 converts payload length to header bits
fn htonl64(payload_len u64) []byte {
mut ret := []byte{len: 8}
fn htonl64(payload_len u64) []u8 {
mut ret := []u8{len: 8}
ret[0] = u8(((payload_len & (u64(0xff) << 56)) >> 56) & 0xff)
ret[1] = u8(((payload_len & (u64(0xff) << 48)) >> 48) & 0xff)
ret[2] = u8(((payload_len & (u64(0xff) << 40)) >> 40) & 0xff)
@@ -19,9 +19,9 @@ fn htonl64(payload_len u64) []byte {
}
// create_masking_key returs a new masking key to use when masking websocket messages
fn create_masking_key() []byte {
fn create_masking_key() []u8 {
mask_bit := rand.u8()
buf := []byte{len: 4, init: `0`}
buf := []u8{len: 4, init: `0`}
unsafe { C.memcpy(buf.data, &mask_bit, 4) }
return buf
}
@@ -45,7 +45,7 @@ fn create_key_challenge_response(seckey string) ?string {
// get_nonce creates a randomized array used in handshake process
fn get_nonce(nonce_size int) string {
mut nonce := []byte{len: nonce_size, cap: nonce_size}
mut nonce := []u8{len: nonce_size, cap: nonce_size}
alphanum := '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz'
for i in 0 .. nonce_size {
nonce[i] = alphanum[rand.intn(alphanum.len) or { 0 }]

View File

@@ -12,7 +12,7 @@ import log
import rand
const (
empty_bytearr = []byte{} // used as empty response to avoid allocation
empty_bytearr = []u8{} // used as empty response to avoid allocation
)
// Client represents websocket client
@@ -60,7 +60,7 @@ pub enum State {
pub struct Message {
pub:
opcode OPCode // websocket frame type of this message
payload []byte // payload of the message
payload []u8 // payload of the message
}
// OPCode represents the supported websocket frame types
@@ -182,7 +182,7 @@ pub fn (mut ws Client) listen() ? {
ws.close(1002, 'invalid close code: $code') ?
return error('invalid close code: $code')
}
reason := if msg.payload.len > 2 { msg.payload[2..] } else { []byte{} }
reason := if msg.payload.len > 2 { msg.payload[2..] } else { []u8{} }
if reason.len > 0 {
ws.validate_utf_8(.close, reason) ?
}
@@ -240,7 +240,7 @@ pub fn (mut ws Client) write_ptr(bytes &byte, payload_len int, code OPCode) ?int
if !ws.is_server {
header_len += 4
}
mut header := []byte{len: header_len, init: `0`} // [`0`].repeat(header_len)
mut header := []u8{len: header_len, init: `0`} // [`0`].repeat(header_len)
header[0] = u8(int(code)) | 0x80
masking_key := create_masking_key()
if ws.is_server {
@@ -284,7 +284,7 @@ pub fn (mut ws Client) write_ptr(bytes &byte, payload_len int, code OPCode) ?int
}
}
len := header.len + payload_len
mut frame_buf := []byte{len: len}
mut frame_buf := []u8{len: len}
unsafe {
C.memcpy(&frame_buf[0], &u8(header.data), header.len)
if payload_len > 0 {
@@ -306,7 +306,7 @@ pub fn (mut ws Client) write_ptr(bytes &byte, payload_len int, code OPCode) ?int
}
// write writes a byte array with a websocket messagetype to socket
pub fn (mut ws Client) write(bytes []byte, code OPCode) ?int {
pub fn (mut ws Client) write(bytes []u8, code OPCode) ?int {
return ws.write_ptr(&u8(bytes.data), bytes.len, code)
}
@@ -332,7 +332,7 @@ pub fn (mut ws Client) close(code int, message string) ? {
if code > 0 {
code_ := C.htons(code)
message_len := message.len + 2
mut close_frame := []byte{len: message_len}
mut close_frame := []u8{len: message_len}
close_frame[0] = u8(code_ & 0xFF)
close_frame[1] = u8(code_ >> 8)
// code32 = (close_frame[0] << 8) + close_frame[1]
@@ -348,14 +348,14 @@ pub fn (mut ws Client) close(code int, message string) ? {
}
// send_control_frame sends a control frame to the server
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []byte) ? {
fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u8) ? {
ws.debug_log('send control frame $code, frame_type: $frame_typ')
if ws.state !in [.open, .closing] && ws.conn.sock.handle > 1 {
return error('socket is not connected')
}
header_len := if ws.is_server { 2 } else { 6 }
frame_len := header_len + payload.len
mut control_frame := []byte{len: frame_len}
mut control_frame := []u8{len: frame_len}
mut masking_key := if !ws.is_server { create_masking_key() } else { websocket.empty_bytearr }
defer {
unsafe {
@@ -378,7 +378,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b
if code == .close {
if payload.len >= 2 {
if !ws.is_server {
mut parsed_payload := []byte{len: payload.len + 1}
mut parsed_payload := []u8{len: payload.len + 1}
unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) }
parsed_payload[payload.len] = `\0`
for i in 0 .. payload.len {