/* load AMF data from stream */ amf0_data * amf0_data_read(read_proc_t read_proc, void * user_data) { uint8_t type; if (read_proc(&type, sizeof(uint8_t), user_data) == sizeof(uint8_t)) { switch (type) { case AMF0_TYPE_NUMBER: return amf0_number_read(read_proc, user_data); case AMF0_TYPE_BOOLEAN: return amf0_boolean_read(read_proc, user_data); case AMF0_TYPE_STRING: return amf0_string_read(read_proc, user_data); case AMF0_TYPE_OBJECT: return amf0_object_read(read_proc, user_data); case AMF0_TYPE_NULL: return amf0_null_new(); case AMF0_TYPE_UNDEFINED: return amf0_undefined_new(); /*case AMF0_TYPE_REFERENCE:*/ case AMF0_TYPE_ECMA_ARRAY: return amf0_associative_array_read(read_proc, user_data); case AMF0_TYPE_STRICT_ARRAY: return amf0_array_read(read_proc, user_data); case AMF0_TYPE_DATE: return amf0_date_read(read_proc, user_data); /*case AMF0_TYPE_SIMPLEOBJECT:*/ case AMF0_TYPE_XML_DOCUMENT: case AMF0_TYPE_TYPED_OBJECT: case AMF0_TYPE_OBJECT_END: return NULL; /* end of composite object */ default: break; } } return NULL; }
/* read an object */ static amf0_data * amf0_object_read(read_proc_t read_proc, void * user_data) { amf0_data * data = amf0_object_new(); if (data != NULL) { amf0_data * name; amf0_data * element; while (1) { name = amf0_string_read(read_proc, user_data); if (name != NULL) { element = amf0_data_read(read_proc, user_data); if (element != NULL) { if (amf0_object_add(data, (char *)amf0_string_get_uint8_ts(name), element) == NULL) { amf0_data_free(name); amf0_data_free(element); amf0_data_free(data); return NULL; } } else { amf0_data_free(name); break; } } else { /* invalid name: error */ amf0_data_free(data); return NULL; } } } return data; }
/* read an associative array */ static amf0_data * amf0_associative_array_read(read_proc_t read_proc, void * user_data) { amf0_data * name; amf0_data * element; uint32_t size; uint8_t error_code; amf0_data * data; data = amf0_associative_array_new(); if (data == NULL) { return NULL; } /* we ignore the 32 bits array size marker */ if (read_proc(&size, sizeof(uint32_t), user_data) < sizeof(uint32_t)) { amf0_data_free(data); return amf0_data_error(AMF0_ERROR_EOF); } while(1) { name = amf0_string_read(read_proc, user_data); error_code = amf0_data_get_error(name); if (error_code != AMF0_ERROR_OK) { /* invalid name: error */ amf0_data_free(name); amf0_data_free(data); return amf0_data_error(error_code); } element = amf0_data_read(read_proc, user_data); error_code = amf0_data_get_error(element); if (amf0_string_get_size(name) == 0 || error_code == AMF0_ERROR_END_TAG || error_code == AMF0_ERROR_UNKNOWN_TYPE) { /* end tag or unknown element: end of data, exit loop */ amf0_data_free(name); amf0_data_free(element); break; } else if (error_code != AMF0_ERROR_OK) { amf0_data_free(name); amf0_data_free(data); amf0_data_free(element); return amf0_data_error(error_code); } if (amf0_associative_array_add(data, (char *)amf0_string_get_bytes(name), element) == NULL) { amf0_data_free(name); amf0_data_free(element); amf0_data_free(data); return NULL; } else { amf0_data_free(name); } } return data; }
/* read an object */ static amf0_data * amf0_object_read(read_proc_t read_proc, void * user_data) { amf0_data * name; amf0_data * element; uint8_t error_code; amf0_data * data; data = amf0_object_new(); if (data == NULL) { return NULL; } while (1) { name = amf0_string_read(read_proc, user_data); error_code = amf0_data_get_error(name); if (error_code != AMF0_ERROR_OK) { /* invalid name: error */ amf0_data_free(name); amf0_data_free(data); return amf0_data_error(error_code); } element = amf0_data_read(read_proc, user_data); error_code = amf0_data_get_error(element); if (error_code == AMF0_ERROR_END_TAG || error_code == AMF0_ERROR_UNKNOWN_TYPE) { /* end tag or unknown element: end of data, exit loop */ amf0_data_free(name); amf0_data_free(element); break; } else if (error_code != AMF0_ERROR_OK) { amf0_data_free(name); amf0_data_free(data); amf0_data_free(element); return amf0_data_error(error_code); } if (amf0_object_add(data, (char *)amf0_string_get_bytes(name), element) == NULL) { amf0_data_free(name); amf0_data_free(element); amf0_data_free(data); return NULL; } else { amf0_data_free(name); } } return data; }
/* load AMF data from stream */ amf0_data * amf0_data_read(read_proc_t read_proc, void * user_data) { uint8_t type; if (read_proc(&type, sizeof(uint8_t), user_data) == sizeof(uint8_t)) { switch (type) { case AMF0_TYPE_NUMBER: return amf0_number_read(read_proc, user_data); case AMF0_TYPE_BOOLEAN: return amf0_boolean_read(read_proc, user_data); case AMF0_TYPE_STRING: return amf0_string_read(read_proc, user_data); case AMF0_TYPE_OBJECT: return amf0_object_read(read_proc, user_data); case AMF0_TYPE_MOVIECLIP: return NULL; /* not supported */ case AMF0_TYPE_NULL: return amf0_null_new(); case AMF0_TYPE_UNDEFINED: return amf0_undefined_new(); case AMF0_TYPE_REFERENCE: /* TODO */ return NULL; case AMF0_TYPE_ECMA_ARRAY: return amf0_associative_array_read(read_proc, user_data); case AMF0_TYPE_OBJECT_END: return amf0_data_error(AMF0_ERROR_END_TAG); /* end of composite object */ case AMF0_TYPE_STRICT_ARRAY: return amf0_array_read(read_proc, user_data); case AMF0_TYPE_DATE: return amf0_date_read(read_proc, user_data); case AMF0_TYPE_LONG_STRING: /* TODO */ case AMF0_TYPE_UNSUPPORTED: /* TODO */ case AMF0_TYPE_RECORDSET: /* not supported */ case AMF0_TYPE_XML_DOCUMENT: /* TODO */ case AMF0_TYPE_TYPED_OBJECT: /* TODO */ return NULL; default: break; } } return NULL; }
/* read an associative array */ static amf0_data * amf0_associative_array_read(read_proc_t read_proc, void * user_data) { amf0_data * data = amf0_associative_array_new(); if (data != NULL) { amf0_data * name; amf0_data * element; uint32_t size; if (read_proc(&size, sizeof(uint32_t), user_data) == sizeof(uint32_t)) { /* we ignore the 32 bits array size marker */ while(1) { name = amf0_string_read(read_proc, user_data); if (name != NULL) { element = amf0_data_read(read_proc, user_data); if (element != NULL) { if (amf0_associative_array_add(data, (char *)amf0_string_get_uint8_ts(name), element) == NULL) { amf0_data_free(name); amf0_data_free(element); amf0_data_free(data); return NULL; } } else { amf0_data_free(name); break; } } else { /* invalid name: error */ amf0_data_free(data); return NULL; } } } else { amf0_data_free(data); return NULL; } } return data; }