-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprim_bytearray.go
276 lines (232 loc) · 8.39 KB
/
prim_bytearray.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file contains the bytearray primitive functions.
package golisp
import (
"fmt"
"unsafe"
)
func RegisterBytearrayPrimitives() {
MakePrimitiveFunction("list-to-bytearray", "1", ListToBytesImpl)
MakePrimitiveFunction("list->bytearray", "1", ListToBytesImpl)
MakePrimitiveFunction("bytearray-to-list", "1", BytesToListImpl)
MakePrimitiveFunction("bytearray->list", "1", BytesToListImpl)
MakePrimitiveFunction("replace-byte", "3", ReplaceByteImpl)
MakePrimitiveFunction("replace-byte!", "3", ReplaceByteBangImpl)
MakePrimitiveFunction("extract-byte", "2", ExtractByteImpl)
MakePrimitiveFunction("append-bytes", "*", AppendBytesImpl)
MakePrimitiveFunction("append-bytes!", "*", AppendBytesBangImpl)
MakePrimitiveFunction("extract-bytes", "3", ExtractBytesImpl)
}
func ListToBytesImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
list := Car(args)
if NilP(list) {
err = ProcessError("Argument to list->bytes can not be nil.", env)
return
}
if !ListP(list) {
err = ProcessError("Argument to list->bytes must be a list.", env)
return
}
bytes := make([]byte, 0, int(Length(list)))
for c := list; NotNilP(c); c = Cdr(c) {
n := Car(c)
if !IntegerP(n) && !(ObjectP(n) && ObjectType(n) == "[]byte") {
err = ProcessError(fmt.Sprintf("Byte arrays can only contain numbers, but found %v.", n), env)
return
}
if IntegerP(n) {
b := IntegerValue(n)
if b < 0 || b > 255 {
err = ProcessError(fmt.Sprintf("Byte arrays can only contain bytes, but found %d.", b), env)
return
}
bytes = append(bytes, byte(b))
} else {
otherArrayBytes := *(*[]byte)(ObjectValue(n))
bytes = append(bytes, otherArrayBytes...)
}
}
return ObjectWithTypeAndValue("[]byte", unsafe.Pointer(&bytes)), nil
}
func BytesToListImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
dataByteObject := Car(args)
if !ObjectP(dataByteObject) || ObjectType(dataByteObject) != "[]byte" {
err = ProcessError(fmt.Sprintf("Bytearray object should return []byte but returned %s.", ObjectType(dataByteObject)), env)
return
}
dataBytes := (*[]byte)(ObjectValue(dataByteObject))
var bytes = make([]*Data, 0, len(*dataBytes))
for _, b := range *dataBytes {
bytes = append(bytes, IntegerWithValue(int64(b)))
}
result = ArrayToList(bytes)
return
}
func internalReplaceByte(args *Data, env *SymbolTableFrame, makeCopy bool) (result *Data, err error) {
dataByteObject := First(args)
if !ObjectP(dataByteObject) || ObjectType(dataByteObject) != "[]byte" {
err = ProcessError(fmt.Sprintf("replace-byte expects a bytearray as it's first argument but received %s.", ObjectType(dataByteObject)), env)
return
}
dataBytes := (*[]byte)(ObjectValue(dataByteObject))
var newBytes *[]byte
if makeCopy {
temp := make([]byte, len(*dataBytes))
newBytes = &temp
copy(*newBytes, *dataBytes)
} else {
newBytes = dataBytes
}
indexObject := Second(args)
if !IntegerP(indexObject) {
err = ProcessError("Bytearray index should be an integer.", env)
return
}
index := int(IntegerValue(indexObject))
if index >= len(*dataBytes) {
err = ProcessError(fmt.Sprintf("replace-byte index was out of range. Was %d but bytearray has length of %d.", index, len(*dataBytes)), env)
return
}
if index < 0 {
err = ProcessError(fmt.Sprintf("replace-byte index was out of range: %d.", index), env)
return
}
valueObject := Third(args)
if !IntegerP(valueObject) {
err = ProcessError("Bytearray value should be an integer.", env)
return
}
if IntegerValue(valueObject) < 0 || IntegerValue(valueObject) > 255 {
err = ProcessError(fmt.Sprintf("replace-byte value was not a byte. Was %d.", index), env)
return
}
value := byte(IntegerValue(valueObject))
(*newBytes)[index] = value
if makeCopy {
result = ObjectWithTypeAndValue("[]byte", unsafe.Pointer(newBytes))
} else {
result = dataByteObject
}
return
}
func ReplaceByteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
return internalReplaceByte(args, env, true)
}
func ReplaceByteBangImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
return internalReplaceByte(args, env, false)
}
func ExtractByteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
dataByteObject := Car(args)
if !ObjectP(dataByteObject) || ObjectType(dataByteObject) != "[]byte" {
err = ProcessError(fmt.Sprintf("Bytearray object should return []byte but returned %s.", ObjectType(dataByteObject)), env)
return
}
dataBytes := (*[]byte)(ObjectValue(dataByteObject))
indexObject := Cadr(args)
if !IntegerP(indexObject) {
err = ProcessError("Bytearray index should be a number.", env)
return
}
index := int(IntegerValue(indexObject))
if index >= len(*dataBytes) {
err = ProcessError(fmt.Sprintf("extract-byte index was out of range. Was %d but bytearray has length of %d.", index, len(*dataBytes)), env)
return
}
if index < 0 {
err = ProcessError(fmt.Sprintf("extract-byte index was out of range: %d.", index), env)
return
}
extractedValue := (*dataBytes)[index]
result = IntegerWithValue(int64(extractedValue))
return
}
func internalAppendBytes(args *Data, env *SymbolTableFrame) (newBytes *[]byte, err error) {
dataByteObject := Car(args)
if !ObjectP(dataByteObject) || ObjectType(dataByteObject) != "[]byte" {
err = ProcessError(fmt.Sprintf("append-bytes extects first argument to be a bytearray, but was %s.", ObjectType(dataByteObject)), env)
return
}
dataBytes := (*[]byte)(ObjectValue(dataByteObject))
var extraByteObj *Data
if Length(args) == 2 {
secondArg := Cadr(args)
if ObjectP(secondArg) && ObjectType(secondArg) == "[]byte" {
extraByteObj = secondArg
} else if ListP(secondArg) {
extraByteObj, err = ListToBytesImpl(InternalMakeList(secondArg), env)
} else {
extraByteObj, err = ListToBytesImpl(InternalMakeList(Cdr(args)), env)
}
} else {
extraByteObj, err = ListToBytesImpl(InternalMakeList(Cdr(args)), env)
}
if err != nil {
return
}
extraBytes := (*[]byte)(ObjectValue(extraByteObj))
temp := make([]byte, len(*dataBytes)+len(*extraBytes))
newBytes = &temp
copy(*newBytes, *dataBytes)
copy((*newBytes)[len(*dataBytes):], *extraBytes)
return
}
func AppendBytesImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
newBytesPtr, err := internalAppendBytes(args, env)
if err != nil {
return
}
result = ObjectWithTypeAndValue("[]byte", unsafe.Pointer(newBytesPtr))
return
}
func AppendBytesBangImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
newBytesPtr, err := internalAppendBytes(args, env)
if err != nil {
return
}
dataByteObject := Car(args)
objectData := BoxedObjectValue(dataByteObject)
objectData.Obj = unsafe.Pointer(newBytesPtr)
result = dataByteObject
return
}
func ExtractBytesImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
dataByteObject := Car(args)
if !ObjectP(dataByteObject) || ObjectType(dataByteObject) != "[]byte" {
err = ProcessError(fmt.Sprintf("Bytearray object should return []byte but returned %s.", ObjectType(dataByteObject)), env)
return
}
dataBytes := (*[]byte)(ObjectValue(dataByteObject))
indexObject := Cadr(args)
if !IntegerP(indexObject) {
err = ProcessError("Bytearray index should be a number.", env)
return
}
index := int(IntegerValue(indexObject))
if index < 0 || index >= len(*dataBytes) {
err = ProcessError(fmt.Sprintf("extract-bytes index was out of range. Was %d but bytearray has length of %d.", index, len(*dataBytes)), env)
return
}
numToExtractObject := Caddr(args)
if !IntegerP(numToExtractObject) {
err = ProcessError(fmt.Sprintf("Number to extract must be a number, but was %s.", TypeName(TypeOf(numToExtractObject))), env)
return
}
numToExtract := int(IntegerValue(numToExtractObject))
if numToExtract < 0 {
err = ProcessError("Number to extract can not be negative.", env)
return
}
if index+numToExtract > len(*dataBytes) {
err = ProcessError(fmt.Sprintf("extract-bytes final index was out of range. Was %d but bytearray has length of %d.", index+numToExtract-1, len(*dataBytes)), env)
return
}
result, err = DropImpl(InternalMakeList(indexObject, dataByteObject), Global)
if err != nil {
return
}
result, err = TakeImpl(InternalMakeList(numToExtractObject, result), Global)
return
}