-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebref.go
More file actions
188 lines (153 loc) · 3.34 KB
/
Copy pathwebref.go
File metadata and controls
188 lines (153 loc) · 3.34 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package webref
import (
"errors"
"fmt"
)
type Ref []Stage
func (r Ref) Validate() error {
if len(r) == 0 {
return errors.New("zero-length ref")
}
if !r[0].IsSource() {
return errors.New("first ref element is not a Source")
}
if len(r) > 1 {
for i, s := range r[1:] {
if s.IsSource() {
return fmt.Errorf("webref: source found at index > 0: %d", i+1)
}
}
}
return nil
}
func (r Ref) IsMutable() bool {
for i := len(r); i >= 0; i-- {
s := r[i]
switch {
case s.File != nil:
return true
case s.HTTP != nil:
return true
case s.IPFS != nil:
return false
case s.Cipher != nil:
case s.Slice != nil:
case s.Hash != nil:
return false
case s.Any != nil:
return true
case s.Table != nil:
// If any of the regions in the table are mutable, then the table is mutable.
for _, te := range s.Table {
if te.Target.IsMutable() {
return true
}
}
return false
default:
panic(s)
}
}
return false
}
type Stage struct {
File *FileSource `json:"file,omitempty"`
HTTP *HTTPSource `json:"http,omitempty"`
IPFS *IPFSSource `json:"ipfs,omitempty"`
Cipher *CipherStage `json:"cipher,omitempty"`
AEAD *AEADStage `json:"aead,omitempty`
Compress *CompressStage `json:"compress,omitempty`
Slice *SliceStage `json:"slice,omitempty"`
Hash *HashCheck `json:"hash,omitempty"`
Any AnySource `json:"any,omitempty"`
Table TableSource `json:"table,omitempty"`
}
func (s Stage) IsSource() bool {
switch {
case s.File != nil:
return true
case s.HTTP != nil:
return true
case s.IPFS != nil:
return true
case s.Any != nil:
return true
case s.Table != nil:
return true
default:
return false
}
}
func (s Stage) String() string {
switch {
case s.File != nil:
return fmt.Sprintf("File(%s)", *s.File)
case s.HTTP != nil:
return fmt.Sprintf("HTTP{url=%s, headers=%v}", s.HTTP.URL, s.HTTP.Headers)
case s.Hash != nil:
return fmt.Sprintf("Hash{algo=%v sum=%x}", s.Hash.Algo, s.Hash.Sum)
case s.Cipher != nil:
return fmt.Sprintf("Cipher{algo=%v}", s.Cipher.Algo)
case s.Compress != nil:
return fmt.Sprintf("Compress{algo=%v}", s.Compress.Algo)
default:
return "EMPTY"
}
}
type FileSource string
type HTTPSource struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
}
type IPFSSource string
type CipherAlgo string
const (
Cipher_AES256_CTR = "AES256_CTR"
Cipher_CHACHA20 = "CHACHA20"
Cipher_XCHACHAX20 = "XCHACHA20"
)
type CipherStage struct {
Algo CipherAlgo `json:"algo"`
Key []byte `json:"key"`
Nonce []byte `json:"nonce"`
}
type AEADAlgo string
const (
AEAD_AES256_GCM = "AES256_GCM"
AEAD_XCHACHAPOLY1305 = "XCHACHAPOLY1305"
)
type AEADStage struct {
Algo AEADAlgo `json:"algo"`
Key []byte `json:"key"`
Nonce []byte `json:"nonce"`
}
type SliceStage struct {
Begin int `json:"begin"`
End int `json:"end"`
}
type CompressAlgo string
const (
GZIP = "gzip"
SNAPPY = "snap"
)
type CompressStage struct {
Algo CompressAlgo `json:"algo"`
}
type HashAlgo string
const (
Hash_SHA256 = "SHA256"
Hash_SHA3_256 = "SHA3_256"
Hash_BLAKE2B = "BLAKE2B"
Hash_BLAKE2S = "BLAKE2S"
Hash_BLAKE3 = "BLAKE3"
)
type HashCheck struct {
Algo HashAlgo `json:"algo"`
Sum []byte `json:"sum"`
}
type AnySource []Ref
type TableSource []TableEntry
type TableEntry struct {
Offset uint64
Target Ref
}