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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #define XIPH_MAX_HEADER_COUNT (256)
27 static inline unsigned int xiph_CountHeaders( const void *extra )
29 return *( (const uint8_t*) extra ) + 1;
32 static inline int xiph_SplitHeaders(unsigned packet_size[], void *packet[], unsigned *packet_count,
33 unsigned extra_size, void *extra)
35 uint8_t *current = extra;
36 const uint8_t *end = ¤t[extra_size];
40 /* Parse the packet count and their sizes */
41 const unsigned count = xiph_CountHeaders( current++ );
43 *packet_count = count;
45 for (unsigned i = 0; i < count - 1; i++) {
50 packet_size[i] += *current;
51 if (*current++ != 255)
54 size += packet_size[i];
56 if (end - current < size)
58 packet_size[count - 1] = end - current - size;
60 for (unsigned i = 0; i < count; i++)
61 if (packet_size[i] > 0) {
63 current += packet_size[i];
69 static inline int xiph_PackHeaders(int *extra_size, void **extra,
70 unsigned packet_size[], const void *packet[], unsigned packet_count )
72 if (packet_count <= 0 || packet_count > XIPH_MAX_HEADER_COUNT)
75 /* Compute the size needed for the whole extra data */
76 unsigned payload_size = 0;
77 unsigned header_size = 1;
78 for (unsigned i = 0; i < packet_count; i++) {
79 payload_size += packet_size[i];
80 if (i < packet_count - 1)
81 header_size += 1 + packet_size[i] / 255;
85 *extra_size = header_size + payload_size;
86 *extra = malloc(*extra_size);
90 /* Write the header */
91 uint8_t *current = (uint8_t*)*extra;
92 *current++ = packet_count - 1;
93 for (unsigned i = 0; i < packet_count - 1; i++) {
94 unsigned t = packet_size[i];
106 /* Copy the payloads */
107 for (unsigned i = 0; i < packet_count; i++) {
108 if (packet_size[i] > 0) {
109 memcpy(current, packet[i], packet_size[i]);
110 current += packet_size[i];
113 assert(current == (uint8_t*)*extra + *extra_size);
117 static inline int xiph_AppendHeaders(int *extra_size, void **extra,
118 unsigned size, const void *data)
120 unsigned packet_size[XIPH_MAX_HEADER_COUNT];
121 void *packet[XIPH_MAX_HEADER_COUNT];
123 if (*extra_size > 0 && *extra) {
124 if (xiph_SplitHeaders(packet_size, packet, &count, *extra_size, *extra))
129 if (count >= XIPH_MAX_HEADER_COUNT)
134 packet_size[count] = size;
135 packet[count] = (void*)data;
136 if (xiph_PackHeaders(extra_size, extra, packet_size,
137 (const void **)packet, count + 1)) {
144 if (*extra_size <= 0)