/src/amf.h

https://code.google.com/ · C Header · 230 lines · 141 code · 30 blank · 59 comment · 0 complexity · b7d09d623471ae53b4ab0a06f95f9f42 MD5 · raw file

  1. /*
  2. $Id: amf.h 231 2011-06-27 13:46:19Z marc.noirot $
  3. FLV Metadata updater
  4. Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  5. This file is part of FLVMeta.
  6. FLVMeta is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. FLVMeta is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with FLVMeta; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __AMF_H__
  19. #define __AMF_H__
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include "types.h"
  24. /* AMF data types */
  25. #define AMF_TYPE_NUMBER ((byte)0x00)
  26. #define AMF_TYPE_BOOLEAN ((byte)0x01)
  27. #define AMF_TYPE_STRING ((byte)0x02)
  28. #define AMF_TYPE_OBJECT ((byte)0x03)
  29. #define AMF_TYPE_NULL ((byte)0x05)
  30. #define AMF_TYPE_UNDEFINED ((byte)0x06)
  31. /* #define AMF_TYPE_REFERENCE ((byte)0x07) */
  32. #define AMF_TYPE_ASSOCIATIVE_ARRAY ((byte)0x08)
  33. #define AMF_TYPE_END ((byte)0x09)
  34. #define AMF_TYPE_ARRAY ((byte)0x0A)
  35. #define AMF_TYPE_DATE ((byte)0x0B)
  36. /* #define AMF_TYPE_SIMPLEOBJECT ((byte)0x0D) */
  37. #define AMF_TYPE_XML ((byte)0x0F)
  38. #define AMF_TYPE_CLASS ((byte)0x10)
  39. /* AMF error codes */
  40. #define AMF_ERROR_OK ((byte)0x00)
  41. #define AMF_ERROR_EOF ((byte)0x01)
  42. #define AMF_ERROR_UNKNOWN_TYPE ((byte)0x02)
  43. #define AMF_ERROR_END_TAG ((byte)0x03)
  44. #define AMF_ERROR_NULL_POINTER ((byte)0x04)
  45. #define AMF_ERROR_MEMORY ((byte)0x05)
  46. #define AMF_ERROR_UNSUPPORTED_TYPE ((byte)0x06)
  47. typedef struct __amf_node * p_amf_node;
  48. /* string type */
  49. typedef struct __amf_string {
  50. uint16 size;
  51. byte * mbstr;
  52. } amf_string;
  53. /* array type */
  54. typedef struct __amf_list {
  55. uint32 size;
  56. p_amf_node first_element;
  57. p_amf_node last_element;
  58. } amf_list;
  59. /* date type */
  60. typedef struct __amf_date {
  61. number64 milliseconds;
  62. sint16 timezone;
  63. } amf_date;
  64. /* XML string type */
  65. typedef struct __amf_xmlstring {
  66. uint32 size;
  67. byte * mbstr;
  68. } amf_xmlstring;
  69. /* class type */
  70. typedef struct __amf_class {
  71. amf_string name;
  72. amf_list elements;
  73. } amf_class;
  74. /* structure encapsulating the various AMF objects */
  75. typedef struct __amf_data {
  76. byte type;
  77. byte error_code;
  78. union {
  79. number64 number_data;
  80. uint8 boolean_data;
  81. amf_string string_data;
  82. amf_list list_data;
  83. amf_date date_data;
  84. amf_xmlstring xmlstring_data;
  85. amf_class class_data;
  86. };
  87. } amf_data;
  88. /* node used in lists, relies on amf_data */
  89. typedef struct __amf_node {
  90. amf_data * data;
  91. p_amf_node prev;
  92. p_amf_node next;
  93. } amf_node;
  94. #ifdef __cplusplus
  95. extern "C" {
  96. #endif /* __cplusplus */
  97. /* Pluggable backend support */
  98. typedef size_t (*amf_read_proc)(void * out_buffer, size_t size, void * user_data);
  99. typedef size_t (*amf_write_proc)(const void * in_buffer, size_t size, void * user_data);
  100. /* read AMF data */
  101. amf_data * amf_data_read(amf_read_proc read_proc, void * user_data);
  102. /* write AMF data */
  103. size_t amf_data_write(const amf_data * data, amf_write_proc write_proc, void * user_data);
  104. /* generic functions */
  105. /* allocate an AMF data object */
  106. amf_data * amf_data_new(byte type);
  107. /* load AMF data from buffer */
  108. amf_data * amf_data_buffer_read(byte * buffer, size_t maxbytes);
  109. /* load AMF data from stream */
  110. amf_data * amf_data_file_read(FILE * stream);
  111. /* AMF data size */
  112. size_t amf_data_size(const amf_data * data);
  113. /* write encoded AMF data into a buffer */
  114. size_t amf_data_buffer_write(amf_data * data, byte * buffer, size_t maxbytes);
  115. /* write encoded AMF data into a stream */
  116. size_t amf_data_file_write(const amf_data * data, FILE * stream);
  117. /* get the type of AMF data */
  118. byte amf_data_get_type(const amf_data * data);
  119. /* get the error code of AMF data */
  120. byte amf_data_get_error_code(const amf_data * data);
  121. /* return a new copy of AMF data */
  122. amf_data * amf_data_clone(const amf_data * data);
  123. /* release the memory of AMF data */
  124. void amf_data_free(amf_data * data);
  125. /* dump AMF data into a stream as text */
  126. void amf_data_dump(FILE * stream, const amf_data * data, int indent_level);
  127. /* return a null AMF object with the specified error code attached to it */
  128. amf_data * amf_data_error(byte error_code);
  129. /* number functions */
  130. amf_data * amf_number_new(number64 value);
  131. number64 amf_number_get_value(const amf_data * data);
  132. void amf_number_set_value(amf_data * data, number64 value);
  133. /* boolean functions */
  134. amf_data * amf_boolean_new(uint8 value);
  135. uint8 amf_boolean_get_value(const amf_data * data);
  136. void amf_boolean_set_value(amf_data * data, uint8 value);
  137. /* string functions */
  138. amf_data * amf_string_new(byte * str, uint16 size);
  139. amf_data * amf_str(const char * str);
  140. uint16 amf_string_get_size(const amf_data * data);
  141. byte * amf_string_get_bytes(const amf_data * data);
  142. /* object functions */
  143. amf_data * amf_object_new(void);
  144. uint32 amf_object_size(const amf_data * data);
  145. amf_data * amf_object_add(amf_data * data, const char * name, amf_data * element);
  146. amf_data * amf_object_get(const amf_data * data, const char * name);
  147. amf_data * amf_object_set(amf_data * data, const char * name, amf_data * element);
  148. amf_data * amf_object_delete(amf_data * data, const char * name);
  149. amf_node * amf_object_first(const amf_data * data);
  150. amf_node * amf_object_last(const amf_data * data);
  151. amf_node * amf_object_next(amf_node * node);
  152. amf_node * amf_object_prev(amf_node * node);
  153. amf_data * amf_object_get_name(amf_node * node);
  154. amf_data * amf_object_get_data(amf_node * node);
  155. /* null functions */
  156. #define amf_null_new() amf_data_new(AMF_TYPE_NULL)
  157. /* undefined functions */
  158. #define amf_undefined_new() amf_data_new(AMF_TYPE_UNDEFINED)
  159. /* associative array functions */
  160. amf_data * amf_associative_array_new(void);
  161. #define amf_associative_array_size(d) amf_object_size(d)
  162. #define amf_associative_array_add(d, n, e) amf_object_add(d, n, e)
  163. #define amf_associative_array_get(d, n) amf_object_get(d, n)
  164. #define amf_associative_array_set(d, n, e) amf_object_set(d, n, e)
  165. #define amf_associative_array_delete(d, n) amf_object_delete(d, n)
  166. #define amf_associative_array_first(d) amf_object_first(d)
  167. #define amf_associative_array_last(d) amf_object_last(d)
  168. #define amf_associative_array_next(n) amf_object_next(n)
  169. #define amf_associative_array_prev(n) amf_object_prev(n)
  170. #define amf_associative_array_get_name(n) amf_object_get_name(n)
  171. #define amf_associative_array_get_data(n) amf_object_get_data(n)
  172. /* array functions */
  173. amf_data * amf_array_new(void);
  174. uint32 amf_array_size(const amf_data * data);
  175. amf_data * amf_array_push(amf_data * data, amf_data * element);
  176. amf_data * amf_array_pop(amf_data * data);
  177. amf_node * amf_array_first(const amf_data * data);
  178. amf_node * amf_array_last(const amf_data * data);
  179. amf_node * amf_array_next(amf_node * node);
  180. amf_node * amf_array_prev(amf_node * node);
  181. amf_data * amf_array_get(amf_node * node);
  182. amf_data * amf_array_get_at(const amf_data * data, uint32 n);
  183. amf_data * amf_array_delete(amf_data * data, amf_node * node);
  184. amf_data * amf_array_insert_before(amf_data * data, amf_node * node, amf_data * element);
  185. amf_data * amf_array_insert_after(amf_data * data, amf_node * node, amf_data * element);
  186. /* date functions */
  187. amf_data * amf_date_new(number64 milliseconds, sint16 timezone);
  188. number64 amf_date_get_milliseconds(const amf_data * data);
  189. sint16 amf_date_get_timezone(const amf_data * data);
  190. time_t amf_date_to_time_t(const amf_data * data);
  191. #ifdef __cplusplus
  192. }
  193. #endif /* __cplusplus */
  194. #endif /* __AMF_H__ */