35 lines
899 B
Go
35 lines
899 B
Go
package amf
|
|
|
|
import (
|
|
"math"
|
|
"encoding/binary"
|
|
"errors"
|
|
)
|
|
|
|
func Encode(amf_root_obj AMFObj) ([]byte, error) {
|
|
tmp_buffer := make([]byte, 1024)
|
|
bytes_encoded := 0
|
|
|
|
for i := 0; i < len(amf_root_obj); i++ {
|
|
if err := encode_next(amf_root_obj[i], &tmp_buffer, &bytes_encoded); err != nil {
|
|
return tmp_buffer[:bytes_encoded], err
|
|
}
|
|
}
|
|
return tmp_buffer[:bytes_encoded], nil
|
|
}
|
|
|
|
func encode_number(num_to_enc float64, tmp_buffer *[]byte, bytes_encoded *int) {
|
|
(*tmp_buffer)[*bytes_encoded] = 0
|
|
binary.BigEndian.PutUint64((*tmp_buffer)[*bytes_encoded + 1:*bytes_encoded + 9], math.Float64bits(num_to_enc))
|
|
*bytes_encoded += 9
|
|
}
|
|
|
|
func encode_next(amf_obj interface{}, tmp_buffer *[]byte, bytes_encoded *int) (error) {
|
|
switch amf_obj.(type) {
|
|
case float64:
|
|
encode_number(amf_obj.(float64), tmp_buffer, bytes_encoded)
|
|
default:
|
|
return errors.New("Type not implemented")
|
|
}
|
|
return nil
|
|
}
|