Complete encoding of main types

This commit is contained in:
Muaz Ahmad 2023-08-15 14:27:28 +05:00
parent a526753112
commit ae9bb63d04

View file

@ -18,16 +18,46 @@ func Encode(amf_root_obj AMFObj) ([]byte, error) {
return tmp_buffer[:bytes_encoded], nil
}
func encode_marker(marker_val uint8, tmp_buffer *[]byte, bytes_encoded *int) {
(*tmp_buffer)[*bytes_encoded] = marker_val
*bytes_encoded++
}
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
binary.BigEndian.PutUint64((*tmp_buffer)[*bytes_encoded:*bytes_encoded + 8], math.Float64bits(num_to_enc))
*bytes_encoded += 8
}
func encode_string(str_to_enc string, tmp_buffer *[]byte, bytes_encoded *int) {
str_len := len(str_to_enc)
binary.BigEndian.PutUint16((*tmp_buffer)[*bytes_encoded:*bytes_encoded + 2], uint16(str_len))
*bytes_encoded += 2
copy((*tmp_buffer)[*bytes_encoded:*bytes_encoded + str_len], str_to_enc)
*bytes_encoded += str_len
}
func encode_object(obj_to_enc AMFObj, tmp_buffer *[]byte, bytes_encoded *int) {
for prop, value := range obj_to_enc {
encode_string(prop.(string), tmp_buffer, bytes_encoded)
encode_next(value, tmp_buffer, bytes_encoded)
}
}
func encode_next(amf_obj interface{}, tmp_buffer *[]byte, bytes_encoded *int) (error) {
switch amf_obj.(type) {
case float64:
encode_marker(0, tmp_buffer, bytes_encoded)
encode_number(amf_obj.(float64), tmp_buffer, bytes_encoded)
case string:
encode_marker(2, tmp_buffer, bytes_encoded)
encode_string(amf_obj.(string), tmp_buffer, bytes_encoded)
case AMFObj:
encode_marker(3, tmp_buffer, bytes_encoded)
encode_object(amf_obj.(AMFObj), tmp_buffer, bytes_encoded)
encode_string("", tmp_buffer, bytes_encoded)
encode_marker(9, tmp_buffer, bytes_encoded)
case nil:
encode_marker(5, tmp_buffer, bytes_encoded)
default:
return errors.New("Type not implemented")
}