2023-08-15 12:32:44 +05:00
|
|
|
package amf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2023-08-15 13:16:29 +05:00
|
|
|
func Encode(amf_root_obj AMFObj) ([]byte, error) {
|
2023-08-15 12:32:44 +05:00
|
|
|
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 {
|
2023-08-15 13:16:29 +05:00
|
|
|
return tmp_buffer[:bytes_encoded], err
|
2023-08-15 12:32:44 +05:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 13:16:29 +05:00
|
|
|
return tmp_buffer[:bytes_encoded], nil
|
2023-08-15 12:32:44 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|