| 1 | /*
|
|---|
| 2 | librtmp
|
|---|
| 3 | Copyright (C) 2004 ITOYANAGI Kazunori
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Library General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library 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 GNU
|
|---|
| 13 | Library General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Library General Public
|
|---|
| 16 | License along with this library; if not, write to the Free
|
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 18 |
|
|---|
| 19 | ITOYANAGI Kazunori
|
|---|
| 20 | kazunori@itoyanagi.name
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "amf_packet.h"
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | static amf_packet_t *amf_packet_create_number(
|
|---|
| 30 | unsigned char *data, int data_size);
|
|---|
| 31 | static amf_packet_t *amf_packet_create_boolean(
|
|---|
| 32 | unsigned char *data, int data_size);
|
|---|
| 33 | static amf_packet_t *amf_packet_create_string(
|
|---|
| 34 | unsigned char *data, int data_size);
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | amf_packet_t *amf_packet_create(
|
|---|
| 38 | unsigned char *data, int data_size)
|
|---|
| 39 | {
|
|---|
| 40 | amf_packet_t *amf;
|
|---|
| 41 |
|
|---|
| 42 | /* FIXME!: big-endian and little-endian */
|
|---|
| 43 |
|
|---|
| 44 | if (data_size < 1) {
|
|---|
| 45 | return NULL;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | switch (data[0]) {
|
|---|
| 49 | case AMF_DATATYPE_NUMBER:
|
|---|
| 50 | amf = amf_packet_create_number(data + 1, data_size - 1);
|
|---|
| 51 | return amf;
|
|---|
| 52 | case AMF_DATATYPE_BOOLEAN:
|
|---|
| 53 | amf = amf_packet_create_boolean(data + 1, data_size -1);
|
|---|
| 54 | return amf;
|
|---|
| 55 | case AMF_DATATYPE_STRING:
|
|---|
| 56 | amf = amf_packet_create_string(data + 1, data_size -1);
|
|---|
| 57 | return amf;
|
|---|
| 58 | case AMF_DATATYPE_OBJECT:
|
|---|
| 59 | break;
|
|---|
| 60 | case AMF_DATATYPE_NULL:
|
|---|
| 61 | break;
|
|---|
| 62 | case AMF_DATATYPE_UNDEFINED:
|
|---|
| 63 | break;
|
|---|
| 64 | case AMF_DATATYPE_OBJECT_END:
|
|---|
| 65 | break;
|
|---|
| 66 | default:
|
|---|
| 67 | break;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | return NULL;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | amf_packet_t *amf_packet_create_number(
|
|---|
| 74 | unsigned char *data, int data_size)
|
|---|
| 75 | {
|
|---|
| 76 | amf_packet_t *amf;
|
|---|
| 77 | unsigned char number_data[8];
|
|---|
| 78 |
|
|---|
| 79 | /* FIXME!: big-endian and little-endian */
|
|---|
| 80 | if (data_size < 8) {
|
|---|
| 81 | return NULL;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | amf = (amf_packet_t*)malloc(sizeof(amf_packet_number_t));
|
|---|
| 85 | amf->datatype = AMF_DATATYPE_NUMBER;
|
|---|
| 86 | number_data[0] = data[7];
|
|---|
| 87 | number_data[1] = data[6];
|
|---|
| 88 | number_data[2] = data[5];
|
|---|
| 89 | number_data[3] = data[4];
|
|---|
| 90 | number_data[4] = data[3];
|
|---|
| 91 | number_data[5] = data[2];
|
|---|
| 92 | number_data[6] = data[1];
|
|---|
| 93 | number_data[7] = data[0];
|
|---|
| 94 | memmove(&(amf->number.number), number_data, 8);
|
|---|
| 95 | printf("AMF NUMBER:: %f\n", amf->number.number);
|
|---|
| 96 | return amf;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | amf_packet_t *amf_packet_create_boolean(
|
|---|
| 101 | unsigned char *data, int data_size)
|
|---|
| 102 | {
|
|---|
| 103 | amf_packet_t *amf;
|
|---|
| 104 |
|
|---|
| 105 | if (data_size < 1) {
|
|---|
| 106 | return NULL;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | amf = (amf_packet_t*)malloc(sizeof(amf_packet_boolean_t));
|
|---|
| 110 | amf->datatype = AMF_DATATYPE_BOOLEAN;
|
|---|
| 111 |
|
|---|
| 112 | memmove(&(amf->boolean.boolean), &data, 1);
|
|---|
| 113 |
|
|---|
| 114 | printf("AMF BOOLEAN:: %s\n", amf->boolean.boolean?"true":"false");
|
|---|
| 115 |
|
|---|
| 116 | return amf;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | amf_packet_t *amf_packet_create_string(
|
|---|
| 120 | unsigned char *data, int data_size)
|
|---|
| 121 | {
|
|---|
| 122 | amf_packet_t *amf;
|
|---|
| 123 | unsigned char string_data_length[2];
|
|---|
| 124 | char *string_data;
|
|---|
| 125 |
|
|---|
| 126 | if (data_size < 3) {
|
|---|
| 127 | return NULL;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | amf = (amf_packet_t*)malloc(sizeof(amf_packet_string_t));
|
|---|
| 131 | amf->datatype = AMF_DATATYPE_STRING;
|
|---|
| 132 |
|
|---|
| 133 | string_data_length[0] = data[1];
|
|---|
| 134 | string_data_length[1] = data[0];
|
|---|
| 135 |
|
|---|
| 136 | string_data = (char *)malloc( (short)string_data_length[0] + 1 );
|
|---|
| 137 | memset( string_data, 0x00, (short)string_data_length[0] + 1 );
|
|---|
| 138 |
|
|---|
| 139 | memmove( string_data, data + 2, (short)string_data_length[0] );
|
|---|
| 140 |
|
|---|
| 141 | amf->string.string = string_data;
|
|---|
| 142 |
|
|---|
| 143 | printf("AMF STRING:: %s\n", amf->string.string);
|
|---|
| 144 |
|
|---|
| 145 | return amf;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | void amf_packet_free(amf_packet_t *amf)
|
|---|
| 150 | {
|
|---|
| 151 | switch( amf->datatype ){
|
|---|
| 152 | case AMF_DATATYPE_NUMBER:
|
|---|
| 153 | break;
|
|---|
| 154 | case AMF_DATATYPE_BOOLEAN:
|
|---|
| 155 | break;
|
|---|
| 156 | case AMF_DATATYPE_STRING:
|
|---|
| 157 | free(amf->string.string);
|
|---|
| 158 | break;
|
|---|
| 159 | case AMF_DATATYPE_OBJECT:
|
|---|
| 160 | break;
|
|---|
| 161 | case AMF_DATATYPE_NULL:
|
|---|
| 162 | break;
|
|---|
| 163 | case AMF_DATATYPE_UNDEFINED:
|
|---|
| 164 | break;
|
|---|
| 165 | case AMF_DATATYPE_OBJECT_END:
|
|---|
| 166 | break;
|
|---|
| 167 | default:
|
|---|
| 168 | break;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | free(amf);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|