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

vlib: add mut for the first parameter of builtin.copy, arrays.copy and crypto (#13702)

This commit is contained in:
Nick Treleaven
2022-03-09 18:26:00 +00:00
committed by GitHub
parent 4c33003f86
commit 7231a3f135
31 changed files with 84 additions and 88 deletions

View File

@@ -21,7 +21,7 @@ fn test_aes_cbc() {
fn aes_cbc_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}

View File

@@ -18,7 +18,7 @@ fn test_aes_cfb() {
fn aes_cfb_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@@ -20,7 +20,7 @@ fn test_aes_ofb() {
fn aes_ofb_en(mut src []byte, key []byte, iv []byte) {
block := aes.new_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@@ -41,7 +41,7 @@ pub fn new_cbc(b Block, iv []byte) Cbc {
// encrypt_blocks encrypts the blocks in `src_` to `dst_`.
// Please note: `dst_` is mutable for performance reasons.
pub fn (x &Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
pub fn (mut x Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@@ -69,7 +69,7 @@ pub fn (x &Cbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
dst = dst[x.block_size..]
}
// Save the iv for the next crypt_blocks call.
copy(x.iv, iv)
copy(mut x.iv, iv)
}
}
@@ -94,7 +94,7 @@ pub fn (mut x Cbc) decrypt_blocks(mut dst []byte, src []byte) {
mut start := end - x.block_size
mut prev := start - x.block_size
// Copy the last block of ciphertext in preparation as the new iv.
copy(x.tmp, src[start..end])
copy(mut x.tmp, src[start..end])
// Loop over all but the first block.
for start > 0 {
src_chunk := src[start..end]
@@ -113,9 +113,9 @@ pub fn (mut x Cbc) decrypt_blocks(mut dst []byte, src []byte) {
x.tmp = x.iv
}
fn (x &Cbc) set_iv(iv []byte) {
fn (mut x Cbc) set_iv(iv []byte) {
if iv.len != x.iv.len {
panic('cipher: incorrect length IV')
}
copy(x.iv, iv)
copy(mut x.iv, iv)
}

View File

@@ -39,20 +39,18 @@ fn new_cfb(b Block, iv []byte, decrypt bool) Cfb {
if iv.len != block_size {
panic('cipher.new_cfb: IV length must be equal block size')
}
x := Cfb{
mut x := Cfb{
b: b
out: []byte{len: b.block_size}
next: []byte{len: b.block_size}
out_used: block_size
decrypt: decrypt
}
copy(x.next, iv)
copy(mut x.next, iv)
return x
}
pub fn (x &Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
pub fn (mut x Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@@ -71,12 +69,12 @@ pub fn (x &Cfb) xor_key_stream(mut dst_ []byte, src_ []byte) {
}
if x.decrypt {
copy(x.next[x.out_used..], src)
copy(mut x.next[x.out_used..], src)
}
n := xor_bytes(mut dst, src, x.out[x.out_used..])
if !x.decrypt {
copy(x.next[x.out_used..], dst)
copy(mut x.next[x.out_used..], dst)
}
dst = dst[n..]
src = src[n..]

View File

@@ -50,6 +50,6 @@ interface BlockMode {
// fn dup(p []byte) []byte {
// q := make([]byte, p.len)
// copy(q, p)
// copy(mut q, p)
// return q
// }

View File

@@ -31,7 +31,7 @@ fn test_des_cbc() {
fn des_cbc_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}
@@ -43,7 +43,7 @@ fn des_cbc_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_cbc_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cbc(block, iv)
mut mode := cipher.new_cbc(block, iv)
mode.encrypt_blocks(mut src, src.clone())
}

View File

@@ -31,7 +31,7 @@ fn test_des_cfb() {
fn des_cfb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
@@ -43,12 +43,12 @@ fn des_cfb_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_cfb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cfb_encrypter(block, iv)
mut mode := cipher.new_cfb_encrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
fn triple_des_cfb_de(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_cfb_decrypter(block, iv)
mut mode := cipher.new_cfb_decrypter(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@@ -31,7 +31,7 @@ fn test_des_ofb() {
fn des_ofb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}
@@ -43,7 +43,7 @@ fn des_ofb_de(mut src []byte, key []byte, iv []byte) {
fn triple_des_ofb_en(mut src []byte, key []byte, iv []byte) {
block := des.new_triple_des_cipher(key)
mode := cipher.new_ofb(block, iv)
mut mode := cipher.new_ofb(block, iv)
mode.xor_key_stream(mut src, src.clone())
}

View File

@@ -25,19 +25,17 @@ pub fn new_ofb(b Block, iv []byte) Ofb {
if iv.len != block_size {
panic('cipher.new_ofb: IV length must be equal block size')
}
x := Ofb{
mut x := Ofb{
b: b
out: []byte{len: b.block_size}
next: []byte{len: b.block_size}
out_used: block_size
}
copy(x.next, iv)
copy(mut x.next, iv)
return x
}
pub fn (x &Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
pub fn (mut x Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
unsafe {
mut dst := *dst_
mut src := src_
@@ -55,7 +53,7 @@ pub fn (x &Ofb) xor_key_stream(mut dst_ []byte, src_ []byte) {
x.out_used = 0
}
copy(x.next, x.out)
copy(mut x.next, x.out)
n := xor_bytes(mut dst, src, x.out)
dst = dst[n..]