/* read a string */ static amf0_data * amf0_string_read(read_proc_t read_proc, void * user_data) { uint16_t strsize; uint8_t * buffer; if (read_proc(&strsize, sizeof(uint16_t), user_data) < sizeof(uint16_t)) { return amf0_data_error(AMF0_ERROR_EOF); } strsize = swap_uint16(strsize); if (strsize == 0) { return amf0_string_new(NULL, 0); } buffer = calloc(strsize, sizeof(uint8_t)); if (buffer == NULL) { return NULL; } if (read_proc(buffer, strsize, user_data) == strsize) { amf0_data * data = amf0_string_new(buffer, strsize); free(buffer); return data; } else { free(buffer); return amf0_data_error(AMF0_ERROR_EOF); } }
/* clone AMF data */ amf0_data * amf0_data_clone(amf0_data * data) { /* we copy data recursively */ if (data != NULL) { switch (data->type) { case AMF0_TYPE_NUMBER: return amf0_number_new(amf0_number_get_value(data)); case AMF0_TYPE_BOOLEAN: return amf0_boolean_new(amf0_boolean_get_value(data)); case AMF0_TYPE_STRING: if (data->string_data.mbstr != NULL) { return amf0_string_new((uint8_t *)strdup((char *)amf0_string_get_uint8_ts(data)), amf0_string_get_size(data)); } else { return amf0_str(NULL); } case AMF0_TYPE_NULL: return NULL; case AMF0_TYPE_UNDEFINED: return NULL; /*case AMF0_TYPE_REFERENCE:*/ case AMF0_TYPE_OBJECT: case AMF0_TYPE_ECMA_ARRAY: case AMF0_TYPE_STRICT_ARRAY: { amf0_data * d = amf0_data_new(data->type); if (d != NULL) { amf0_list_init(&d->list_data); amf0_list_clone(&data->list_data, &d->list_data); } return d; } case AMF0_TYPE_DATE: return amf0_date_new(amf0_date_get_milliseconds(data), amf0_date_get_timezone(data)); /*case AMF0_TYPE_SIMPLEOBJECT:*/ case AMF0_TYPE_XML_DOCUMENT: return NULL; case AMF0_TYPE_TYPED_OBJECT: return NULL; } } return NULL; }
/* read a string */ static amf0_data * amf0_string_read(read_proc_t read_proc, void * user_data) { uint16_t strsize; uint8_t * buffer; if (read_proc(&strsize, sizeof(uint16_t), user_data) == sizeof(uint16_t)) { strsize = swap_uint16(strsize); if (strsize > 0) { buffer = (uint8_t*)calloc(strsize, sizeof(uint8_t)); if (buffer != NULL && read_proc(buffer, strsize, user_data) == strsize) { amf0_data * data = amf0_string_new(buffer, strsize); free(buffer); return data; } } else { return amf0_string_new(NULL, 0); } } return NULL; }
/* clone AMF data */ amf0_data * amf0_data_clone(const amf0_data *data) { /* we copy data recursively */ if (data != NULL) { switch (data->type) { case AMF0_TYPE_NUMBER: return amf0_number_new(amf0_number_get_value(data)); case AMF0_TYPE_BOOLEAN: return amf0_boolean_new(amf0_boolean_get_value(data)); case AMF0_TYPE_STRING: if (data->string_data.mbstr != NULL) { return amf0_string_new((uint8_t *)strdup((char *)amf0_string_get_bytes(data)), amf0_string_get_size(data)); } else { return amf0_str(NULL); } case AMF0_TYPE_MOVIECLIP: /* not supported */ case AMF0_TYPE_NULL: case AMF0_TYPE_UNDEFINED: case AMF0_TYPE_REFERENCE: /* TODO */ case AMF0_TYPE_OBJECT_END: return NULL; case AMF0_TYPE_OBJECT: case AMF0_TYPE_ECMA_ARRAY: case AMF0_TYPE_STRICT_ARRAY: { amf0_data * d = amf0_data_new(data->type); if (d != NULL) { amf0_list_init(&d->list_data); amf0_list_clone(&data->list_data, &d->list_data); } return d; } case AMF0_TYPE_DATE: return amf0_date_new(amf0_date_get_milliseconds(data), amf0_date_get_timezone(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 */ default: return NULL; } } return NULL; }
amf0_data * amf0_str(const char * str) { return amf0_string_new((uint8_t *)str, (uint16_t)(str != NULL ? strlen(str) : 0)); }