This repository was archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbytestream.mini
More file actions
135 lines (119 loc) · 3.63 KB
/
bytestream.mini
File metadata and controls
135 lines (119 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use std::bytearray::ByteArray;
use std::bytearray::bytearray_new;
use std::bytearray::bytearray_size;
use std::bytearray::bytearray_buffer;
use std::bytearray::bytearray_slice;
use std::bytearray::bytearray_wrap_slice;
use std::bytearray::buffer_getCalldataUnits;
type ByteStream = struct {
buf: buffer,
capacity: uint,
slice: uint,
currentOffset: uint,
};
public func bytestream_new(contents: ByteArray) -> ByteStream {
struct {
buf: bytearray_buffer(contents),
capacity: bytearray_size(contents),
slice: bytearray_slice(contents),
currentOffset: 0,
}
}
public func bytestream_atEof(bs: ByteStream) -> bool {
bs.currentOffset >= bs.capacity
}
public func bytestream_bytesReadSoFar(bs: ByteStream) -> uint {
bs.currentOffset
}
public func bytestream_getCalldataUnits(bs: ByteStream, nbytes: uint) -> uint {
buffer_getCalldataUnits(bs.buf, bs.slice, nbytes)
}
public func bytestream_bytesRemaining(bs: ByteStream) -> uint {
if bs.currentOffset >= bs.capacity {
0
} else {
bs.capacity - bs.currentOffset
}
}
public func bytestream_skipBytes(bs: ByteStream, nbytes: uint) -> option<ByteStream> {
let newOffset = bs.currentOffset + nbytes;
if newOffset <= bs.capacity {
Some(bs with { currentOffset: newOffset })
} else {
None
}
}
public func bytestream_truncate(bs: ByteStream, size: uint) -> ByteStream {
if size < bs.capacity {
if size < bs.currentOffset {
bs with { capacity: bs.currentOffset }
} else {
bs with { capacity: size }
}
} else {
bs
}
}
public func bytestream_getByte(bs: ByteStream) -> option<(ByteStream, uint)> {
if bs.currentOffset >= bs.capacity {
None
} else {
Some((
bs with { currentOffset: bs.currentOffset+1 },
getbuffer8(bs.buf, bs.currentOffset+bs.slice)
))
}
}
public func bytestream_get64(bs: ByteStream) -> option<(ByteStream, uint)> {
if bs.currentOffset+8 > bs.capacity {
None
} else {
Some((
bs with { currentOffset: bs.currentOffset+8 },
getbuffer64(bs.buf, bs.currentOffset+bs.slice)
))
}
}
public func bytestream_get256(bs: ByteStream) -> option<(ByteStream, uint)> {
if bs.currentOffset+32 > bs.capacity {
None<(ByteStream, uint)>
} else {
Some((
bs with { currentOffset: bs.currentOffset+32 },
getbuffer256(bs.buf, bs.currentOffset+bs.slice)
))
}
}
public func bytestream_getPartialWord(bs: ByteStream, nbytes: uint) -> option<(ByteStream, uint)> {
// read part of the next word -- just get the first nbytes bytes of it
// nbytes must be <= 32
if nbytes > 32 {
None
} else if bs.currentOffset+nbytes > bs.capacity {
None
} else {
Some((
bs with { currentOffset: bs.currentOffset+nbytes },
(getbuffer256(bs.buf, bs.currentOffset+bs.slice) >> (256-8*nbytes)),
))
}
}
public func bytestream_getN(bs: ByteStream, nbytes: uint) -> option<(ByteStream, ByteArray)> {
if bs.currentOffset + nbytes > bs.capacity {
return None;
}
Some((
bs with { currentOffset: bs.currentOffset + nbytes },
bytearray_wrap_slice(bs.buf, bs.currentOffset + bs.slice, nbytes)
))
}
public func bytestream_getRemainingBytes(bs: ByteStream) -> ByteArray {
if bs.capacity > bs.currentOffset {
bytearray_wrap_slice(bs.buf, bs.currentOffset + bs.slice, bs.capacity - bs.currentOffset)
} else {
bytearray_new(0)
}
}