PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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