38 lines
929 B
Go
38 lines
929 B
Go
|
package amf
|
||
|
|
||
|
import (
|
||
|
"math"
|
||
|
"encoding/binary"
|
||
|
"errors"
|
||
|
)
|
||
|
|
||
|
func Encode(amf_root_obj AMFObj, msg_ptr *Message) (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 err
|
||
|
}
|
||
|
}
|
||
|
msg_ptr.msg_len = bytes_encoded
|
||
|
msg_ptr.data = tmp_buffer[:bytes_encoded]
|
||
|
return 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
|
||
|
}
|