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

make function arguments immutable by default

This commit is contained in:
Alexander Medvednikov
2019-08-07 08:19:27 +02:00
parent 06b8bd9382
commit 34e0b164eb
22 changed files with 107 additions and 83 deletions

View File

@ -8,7 +8,7 @@ module cipher
// xor_bytes xors the bytes in a and b. The destination should have enough
// space, otherwise xor_bytes will panic. Returns the number of bytes xor'd.
pub fn xor_bytes(dst, a, b []byte) int {
pub fn xor_bytes(dst mut []byte, a, b []byte) int {
mut n := a.len
if b.len < n {
n = b.len
@ -17,13 +17,13 @@ pub fn xor_bytes(dst, a, b []byte) int {
return 0
}
safe_xor_bytes(dst, a, b, n)
safe_xor_bytes(mut dst, a, b, n)
return n
}
// n needs to be smaller or equal than the length of a and b.
pub fn safe_xor_bytes(dst, a, b []byte, n int) {
pub fn safe_xor_bytes(dst mut []byte, a, b []byte, n int) {
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
@ -32,5 +32,5 @@ pub fn safe_xor_bytes(dst, a, b []byte, n int) {
// fast_xor_words XORs multiples of 4 or 8 bytes (depending on architecture.)
// The slice arguments a and b are assumed to be of equal length.
pub fn xor_words(dst, a, b []byte) {
safe_xor_bytes(dst, a, b, b.len)
safe_xor_bytes(mut dst, a, b, b.len)
}

View File

@ -55,7 +55,7 @@ pub fn new() *Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@ -106,8 +106,8 @@ pub fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 1 + 63 + 8]
tmp[0] = 0x80
pad := (55 - int(d.len)) % 64 // calculate number of padding bytes
binary.little_endian_put_u64(tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(tmp.left(1+pad+8))
binary.little_endian_put_u64(mut tmp.right(1+pad), u64(d.len<<u64(3))) // append length in bits
d.write(mut tmp.left(1+pad+8))
// The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed.
@ -117,10 +117,10 @@ pub fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.little_endian_put_u32(digest, d.s[0])
binary.little_endian_put_u32(digest.right(4), d.s[1])
binary.little_endian_put_u32(digest.right(8), d.s[2])
binary.little_endian_put_u32(digest.right(12), d.s[3])
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest.right(4), d.s[1])
binary.little_endian_put_u32(mut digest.right(8), d.s[2])
binary.little_endian_put_u32(mut digest.right(12), d.s[3])
return digest
}

View File

@ -13,7 +13,7 @@ import (
encoding.binary
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
// load state
mut a := dig.s[0]
mut b := dig.s[1]

View File

@ -58,7 +58,7 @@ pub fn new() &Digest {
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
pub fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
@ -108,9 +108,9 @@ fn (d mut Digest) checksum() []byte {
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.

View File

@ -17,7 +17,7 @@ const (
_K3 = 0xCA62C1D6
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 16]
mut h0 := dig.h[0]
mut h1 := dig.h[1]

View File

@ -51,7 +51,7 @@ mut:
is224 bool // mark if this digest is SHA-224
}
fn (d &Digest) reset() {
fn (d mut Digest) reset() {
d.h = [u32(0); 8]
d.x = [byte(0); Chunk]
if !d.is224 {
@ -92,7 +92,7 @@ pub fn new224() *Digest {
return d
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p mut []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
@ -145,14 +145,14 @@ fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 64]
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
d.write(mut tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
d.write(mut tmp.left(64+56-int(len)%64))
}
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, len)
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp.left(8))
if d.nx != 0 {
@ -161,13 +161,13 @@ fn (d mut Digest) checksum() []byte {
digest := [byte(0); Size]
binary.big_endian_put_u32(digest, d.h[0])
binary.big_endian_put_u32(digest.right(4), d.h[1])
binary.big_endian_put_u32(digest.right(8), d.h[2])
binary.big_endian_put_u32(digest.right(12), d.h[3])
binary.big_endian_put_u32(digest.right(16), d.h[4])
binary.big_endian_put_u32(digest.right(20), d.h[5])
binary.big_endian_put_u32(digest.right(24), d.h[6])
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest.right(4), d.h[1])
binary.big_endian_put_u32(mut digest.right(8), d.h[2])
binary.big_endian_put_u32(mut digest.right(12), d.h[3])
binary.big_endian_put_u32(mut digest.right(16), d.h[4])
binary.big_endian_put_u32(mut digest.right(20), d.h[5])
binary.big_endian_put_u32(mut digest.right(24), d.h[6])
if !d.is224 {
binary.big_endian_put_u32(digest.right(28), d.h[7])
}

View File

@ -80,7 +80,7 @@ const (
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u32(0); 64]
mut h0 := dig.h[0]

View File

@ -146,14 +146,15 @@ fn new384() *Digest {
return _new(crypto.Hash.SHA384)
}
fn (d mut Digest) write(p []byte) ?int {
fn (d mut Digest) write(p_ []byte) ?int {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
block(d, d.x)
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
@ -164,7 +165,7 @@ fn (d mut Digest) write(p []byte) ?int {
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
block(d, p.left(n))
block(mut d, p.left(n))
if n >= p.len {
p = []byte
} else {
@ -217,8 +218,8 @@ fn (d mut Digest) checksum() []byte {
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(tmp.right(8), len)
binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(mut tmp.right(8), len)
d.write(tmp.left(16))
if d.nx != 0 {
@ -227,15 +228,15 @@ fn (d mut Digest) checksum() []byte {
mut digest := [byte(0); Size]
binary.big_endian_put_u64(digest, d.h[0])
binary.big_endian_put_u64(digest.right(8), d.h[1])
binary.big_endian_put_u64(digest.right(16), d.h[2])
binary.big_endian_put_u64(digest.right(24), d.h[3])
binary.big_endian_put_u64(digest.right(32), d.h[4])
binary.big_endian_put_u64(digest.right(40), d.h[5])
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest.right(8), d.h[1])
binary.big_endian_put_u64(mut digest.right(16), d.h[2])
binary.big_endian_put_u64(mut digest.right(24), d.h[3])
binary.big_endian_put_u64(mut digest.right(32), d.h[4])
binary.big_endian_put_u64(mut digest.right(40), d.h[5])
if d.function != crypto.Hash.SHA384 {
binary.big_endian_put_u64(digest.right(48), d.h[6])
binary.big_endian_put_u64(digest.right(56), d.h[7])
binary.big_endian_put_u64(mut digest.right(48), d.h[6])
binary.big_endian_put_u64(mut digest.right(56), d.h[7])
}
return digest
@ -278,10 +279,10 @@ pub fn sum512_256(data []byte) []byte {
return sum256
}
fn block(dig &Digest, p []byte) {
fn block(dig mut Digest, p []byte) {
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(dig, p)
block_generic(mut dig, p)
}
pub fn (d &Digest) size() int {

View File

@ -94,7 +94,7 @@ const(
]
)
fn block_generic(dig &Digest, p []byte) {
fn block_generic(dig mut Digest, p []byte) {
mut w := [u64(0); 80]
mut h0 := dig.h[0]