1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /*
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
22 * LICENSE).
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 * SOFTWARE.
41 */
42
43 #include "properties.h"
44
45 /* if needed, re-allocate buffer to ensure size bytes can be written into it
46 * at offset */
47 void
prop_copy_ensure_buffer(guint8 ** buffer,guint64 * bsize,guint64 * offset,guint64 size)48 prop_copy_ensure_buffer (guint8 ** buffer, guint64 * bsize, guint64 * offset,
49 guint64 size)
50 {
51 if (buffer && *bsize - *offset < size) {
52 *bsize += size + 10 * 1024;
53 *buffer = g_realloc (*buffer, *bsize);
54 }
55 }
56
57 static guint64
copy_func(void * prop,guint size,guint8 ** buffer,guint64 * bsize,guint64 * offset)58 copy_func (void *prop, guint size, guint8 ** buffer, guint64 * bsize,
59 guint64 * offset)
60 {
61 if (buffer) {
62 prop_copy_ensure_buffer (buffer, bsize, offset, size);
63 memcpy (*buffer + *offset, prop, size);
64 }
65 *offset += size;
66 return size;
67 }
68
69 #define INT_ARRAY_COPY_FUNC_FAST(name, datatype) \
70 guint64 prop_copy_ ## name ## _array (datatype *prop, guint size, \
71 guint8 ** buffer, guint64 * bsize, guint64 * offset) { \
72 return copy_func (prop, sizeof (datatype) * size, buffer, bsize, offset);\
73 }
74
75 #define INT_ARRAY_COPY_FUNC(name, datatype) \
76 guint64 prop_copy_ ## name ## _array (datatype *prop, guint size, \
77 guint8 ** buffer, guint64 * bsize, guint64 * offset) { \
78 guint i; \
79 \
80 for (i = 0; i < size; i++) { \
81 prop_copy_ ## name (prop[i], buffer, bsize, offset); \
82 } \
83 return sizeof (datatype) * size; \
84 }
85
86 /* INTEGERS */
87 guint64
prop_copy_uint8(guint8 prop,guint8 ** buffer,guint64 * size,guint64 * offset)88 prop_copy_uint8 (guint8 prop, guint8 ** buffer, guint64 * size,
89 guint64 * offset)
90 {
91 return copy_func (&prop, sizeof (guint8), buffer, size, offset);
92 }
93
94 guint64
prop_copy_uint16(guint16 prop,guint8 ** buffer,guint64 * size,guint64 * offset)95 prop_copy_uint16 (guint16 prop, guint8 ** buffer, guint64 * size,
96 guint64 * offset)
97 {
98 prop = GUINT16_TO_BE (prop);
99 return copy_func (&prop, sizeof (guint16), buffer, size, offset);
100 }
101
102 guint64
prop_copy_uint32(guint32 prop,guint8 ** buffer,guint64 * size,guint64 * offset)103 prop_copy_uint32 (guint32 prop, guint8 ** buffer, guint64 * size,
104 guint64 * offset)
105 {
106 prop = GUINT32_TO_BE (prop);
107 return copy_func (&prop, sizeof (guint32), buffer, size, offset);
108 }
109
110 guint64
prop_copy_uint64(guint64 prop,guint8 ** buffer,guint64 * size,guint64 * offset)111 prop_copy_uint64 (guint64 prop, guint8 ** buffer, guint64 * size,
112 guint64 * offset)
113 {
114 prop = GUINT64_TO_BE (prop);
115 return copy_func (&prop, sizeof (guint64), buffer, size, offset);
116 }
117
118 guint64
prop_copy_int32(gint32 prop,guint8 ** buffer,guint64 * size,guint64 * offset)119 prop_copy_int32 (gint32 prop, guint8 ** buffer, guint64 * size,
120 guint64 * offset)
121 {
122 prop = GINT32_TO_BE (prop);
123 return copy_func (&prop, sizeof (guint32), buffer, size, offset);
124 }
125
126 /* uint8 can use direct copy in any case, and may be used for large quantity */
127 INT_ARRAY_COPY_FUNC_FAST (uint8, guint8);
128 /* not used in large quantity anyway */
129 INT_ARRAY_COPY_FUNC (uint16, guint16);
130 INT_ARRAY_COPY_FUNC (uint32, guint32);
131 INT_ARRAY_COPY_FUNC (uint64, guint64);
132
133 /* FOURCC */
134 guint64
prop_copy_fourcc(guint32 prop,guint8 ** buffer,guint64 * size,guint64 * offset)135 prop_copy_fourcc (guint32 prop, guint8 ** buffer, guint64 * size,
136 guint64 * offset)
137 {
138 prop = GINT32_TO_LE (prop);
139 return copy_func (&prop, sizeof (guint32), buffer, size, offset);
140 }
141
142 INT_ARRAY_COPY_FUNC (fourcc, guint32);
143
144 /**
145 * prop_copy_fixed_size_string:
146 * @string: the string to be copied
147 * @str_size: size of the string
148 * @buffer: the array to copy the string to
149 * @offset: the position in the buffer array.
150 * This value is updated to the point right after the copied string.
151 *
152 * Copies a string of bytes without placing its size at the beginning.
153 *
154 * Returns: the number of bytes copied
155 */
156 guint64
prop_copy_fixed_size_string(guint8 * string,guint str_size,guint8 ** buffer,guint64 * size,guint64 * offset)157 prop_copy_fixed_size_string (guint8 * string, guint str_size, guint8 ** buffer,
158 guint64 * size, guint64 * offset)
159 {
160 return copy_func (string, str_size * sizeof (guint8), buffer, size, offset);
161 }
162
163 /**
164 * prop_copy_size_string:
165 *
166 * @string: the string to be copied
167 * @str_size: size of the string
168 * @buffer: the array to copy the string to
169 * @offset: the position in the buffer array.
170 * This value is updated to the point right after the copied string.
171 *
172 * Copies a string and its size to an array. Example:
173 * string = 'abc\0'
174 * result in the array: [3][a][b][c] (each [x] represents a position)
175 *
176 * Returns: the number of bytes copied
177 */
178 guint64
prop_copy_size_string(guint8 * string,guint str_size,guint8 ** buffer,guint64 * size,guint64 * offset)179 prop_copy_size_string (guint8 * string, guint str_size, guint8 ** buffer,
180 guint64 * size, guint64 * offset)
181 {
182 guint64 original_offset = *offset;
183
184 prop_copy_uint8 (str_size, buffer, size, offset);
185 prop_copy_fixed_size_string (string, str_size, buffer, size, offset);
186 return *offset - original_offset;
187 }
188
189 /**
190 * prop_copy_null_terminated_string:
191 * @string: the string to be copied
192 * @buffer: the array to copy the string to
193 * @offset: the position in the buffer array.
194 * This value is updated to the point right after the copied string.
195 *
196 * Copies a string including its null terminating char to an array.
197 *
198 * Returns: the number of bytes copied
199 */
200 guint64
prop_copy_null_terminated_string(gchar * string,guint8 ** buffer,guint64 * size,guint64 * offset)201 prop_copy_null_terminated_string (gchar * string, guint8 ** buffer,
202 guint64 * size, guint64 * offset)
203 {
204 guint64 original_offset = *offset;
205 guint len = strlen (string);
206
207 prop_copy_fixed_size_string ((guint8 *) string, len, buffer, size, offset);
208 prop_copy_uint8 ('\0', buffer, size, offset);
209 return *offset - original_offset;
210 }
211