1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #define XIPH_MAX_HEADER_COUNT (256)
27 static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
28 unsigned extra_size, const void *extra)
30 const uint8_t *current = (const uint8_t*)extra;
31 const uint8_t *end = ¤t[extra_size];
35 /* Parse the packet count and their sizes */
36 const unsigned count = 1 + *current++;
38 *packet_count = count;
40 for (unsigned i = 0; i < count - 1; i++) {
45 packet_size[i] += *current;
46 if (*current++ != 255)
49 size += packet_size[i];
51 if (end - current < (int)size)
53 packet_size[count - 1] = end - current - size;
55 /* Copy the payloads */
56 for (unsigned i = 0; i < count; i++) {
57 packet[i] = malloc(packet_size[i]);
59 for (unsigned j = 0; j < i; j++)
63 if (packet_size[i] > 0) {
64 memcpy(packet[i], current, packet_size[i]);
65 current += packet_size[i];
71 static inline int xiph_PackHeaders(int *extra_size, void **extra,
72 unsigned packet_size[], const void *packet[], unsigned packet_count )
74 if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
77 /* Compute the size needed for the whole extra data */
78 unsigned payload_size = 0;
79 unsigned header_size = 1;
80 for (unsigned i = 0; i < packet_count; i++) {
81 payload_size += packet_size[i];
82 if (i < packet_count - 1)
83 header_size += 1 + packet_size[i] / 255;
87 *extra_size = header_size + payload_size;
88 *extra = malloc(*extra_size);
92 /* Write the header */
93 uint8_t *current = (uint8_t*)*extra;
94 *current++ = packet_count - 1;
95 for (unsigned i = 0; i < packet_count - 1; i++) {
96 unsigned t = packet_size[i];
108 /* Copy the payloads */
109 for (unsigned i = 0; i < packet_count; i++) {
110 if (packet_size[i] > 0) {
111 memcpy(current, packet[i], packet_size[i]);
112 current += packet_size[i];
115 assert(current == (uint8_t*)*extra + *extra_size);
119 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
120 unsigned size, const void *data)
122 unsigned packet_size[XIPH_MAX_HEADER_COUNT];
123 void *packet[XIPH_MAX_HEADER_COUNT];
125 if (*extra_size > 0 && *extra) {
126 if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
131 if (count >= XIPH_MAX_HEADER_COUNT)
136 packet_size[count] = size;
137 packet[count] = (void*)data;
138 if (xiph_PackHeaders(extra_size, extra, packet_size,
139 (const void **)packet, count + 1)) {
143 for (unsigned i = 0; i < count; i++)
146 if (*extra_size <= 0)