1 /**
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <string.h>
20
24
25 #define FF_ENCRYPTION_INFO_EXTRA 24
26
27 // The format of the AVEncryptionInfo side data:
28 // u32be scheme
29 // u32be crypt_byte_block
30 // u32be skip_byte_block
31 // u32be key_id_size
32 // u32be iv_size
33 // u32be subsample_count
34 // u8[key_id_size] key_id
35 // u8[iv_size] iv
36 // {
37 // u32be bytes_of_clear_data
38 // u32be bytes_of_protected_data
39 // }[subsample_count]
40
42 {
44
48
50 info->key_id_size = key_id_size;
52 info->iv_size = iv_size;
54 info->subsample_count = subsample_count;
55
56 // Allow info->subsamples to be NULL if there are no subsamples.
57 if (!
info->key_id || !
info->iv || (!
info->subsamples && subsample_count)) {
60 }
61
63 }
64
66 {
68
72
74 ret->crypt_byte_block =
info->crypt_byte_block;
75 ret->skip_byte_block =
info->skip_byte_block;
77 memcpy(
ret->key_id,
info->key_id,
info->key_id_size);
78 memcpy(
ret->subsamples,
info->subsamples,
sizeof(*
info->subsamples) *
info->subsample_count);
80 }
81
83 {
89 }
90 }
91
93 {
95 uint64_t key_id_size, iv_size, subsample_count,
i;
96
99
103
106
110
114 memcpy(
info->key_id,
buffer + 24, key_id_size);
115 memcpy(
info->iv,
buffer + key_id_size + 24, iv_size);
116
117 buffer += key_id_size + iv_size + 24;
118 for (
i = 0;
i < subsample_count;
i++) {
122 }
123
125 }
126
128 {
129 uint8_t *
buffer, *cur_buffer;
131
132 if (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA < info->key_id_size ||
136 }
137
139 (
info->subsample_count * 8);
143
150 cur_buffer += 24;
151 memcpy(cur_buffer,
info->key_id,
info->key_id_size);
152 cur_buffer +=
info->key_id_size;
153 memcpy(cur_buffer,
info->iv,
info->iv_size);
154 cur_buffer +=
info->iv_size;
155 for (
i = 0;
i <
info->subsample_count;
i++) {
156 AV_WB32(cur_buffer,
info->subsamples[
i].bytes_of_clear_data);
157 AV_WB32(cur_buffer + 4,
info->subsamples[
i].bytes_of_protected_data);
158 cur_buffer += 8;
159 }
160
162 }
163
164 // The format of the AVEncryptionInitInfo side data:
165 // u32be init_info_count
166 // {
167 // u32be system_id_size
168 // u32be num_key_ids
169 // u32be key_id_size
170 // u32be data_size
171 // u8[system_id_size] system_id
172 // u8[key_id_size][num_key_id] key_ids
173 // u8[data_size] data
174 // }[init_info_count]
175
176 #define FF_ENCRYPTION_INIT_INFO_EXTRA 16
177
179 uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
180 {
183
187
189 info->system_id_size = system_id_size;
191 info->num_key_ids = num_key_ids;
192 info->key_id_size = key_id_size;
194 info->data_size = data_size;
195
196 // Allow pointers to be NULL if the size is 0.
197 if ((!
info->system_id && system_id_size) || (!
info->data && data_size) ||
198 (!
info->key_ids && num_key_ids && key_id_size)) {
201 }
202
203 if (key_id_size) {
204 for (
i = 0;
i < num_key_ids;
i++) {
206 if (!
info->key_ids[
i]) {
209 }
210 }
211 }
212
214 }
215
217 {
220 for (
i = 0;
i <
info->num_key_ids;
i++) {
222 }
228 }
229 }
230
232 const uint8_t *side_data, size_t side_data_size)
233 {
234 // |ret| tracks the front of the list, |info| tracks the back.
236 uint64_t system_id_size, num_key_ids, key_id_size, data_size,
i, j;
237 uint64_t init_info_count;
238
239 if (!side_data || side_data_size < 4)
241
242 init_info_count =
AV_RB32(side_data);
243 side_data += 4;
244 side_data_size -= 4;
245 for (
i = 0;
i < init_info_count;
i++) {
249 }
250
251 system_id_size =
AV_RB32(side_data);
252 num_key_ids =
AV_RB32(side_data + 4);
253 key_id_size =
AV_RB32(side_data + 8);
254 data_size =
AV_RB32(side_data + 12);
255
256 // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
260 }
263
265 if (!temp_info) {
268 }
271 } else {
272 info->next = temp_info;
274 }
275
276 memcpy(
info->system_id, side_data, system_id_size);
277 side_data += system_id_size;
278 side_data_size -= system_id_size;
279 for (j = 0; j < num_key_ids; j++) {
280 memcpy(
info->key_ids[j], side_data, key_id_size);
281 side_data += key_id_size;
282 side_data_size -= key_id_size;
283 }
284 memcpy(
info->data, side_data, data_size);
285 side_data += data_size;
286 side_data_size -= data_size;
287 }
288
290 }
291
293 {
295 uint8_t *
buffer, *cur_buffer;
296 uint32_t
i, init_info_count;
297 uint64_t temp_side_data_size;
298
299 temp_side_data_size = 4;
300 init_info_count = 0;
301 for (cur_info =
info; cur_info; cur_info = cur_info->
next) {
303 if (init_info_count == UINT32_MAX || temp_side_data_size > UINT32_MAX) {
305 }
306 init_info_count++;
307
310 if (temp_side_data_size > UINT32_MAX) {
312 }
313 }
314 }
315 *side_data_size = temp_side_data_size;
316
320
321 AV_WB32(cur_buffer, init_info_count);
322 cur_buffer += 4;
323 for (cur_info =
info; cur_info; cur_info = cur_info->
next) {
328 cur_buffer += 16;
329
335 }
339 }
340 }
341
343 }