FFmpeg
encryption_info.c
Go to the documentation of this file.
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 
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "libavutil/avassert.h"
25 
26 static const AVSubsampleEncryptionInfo test_subsamples[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
27 static const size_t test_subsample_count = sizeof(test_subsamples) / sizeof(test_subsamples[0]);
28 static const uint8_t test_iv[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
29 static const uint8_t test_key_id[] = {0x21, 0x22, 0x23, 0x24};
30 static const uint8_t test_key_id_2[] = {0x31, 0x32, 0x33, 0x34};
31 static const uint8_t test_system_id[] = {0x41, 0x42, 0x43};
32 static const uint8_t test_data[] = {0x51, 0x52};
33 
35  if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block ||
36  a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size ||
37  a->iv_size != b->iv_size || a->subsample_count != b->subsample_count)
38  return 1;
39 
40  if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 ||
41  memcmp(a->iv, b->iv, a->iv_size) != 0 ||
42  memcmp(a->subsamples, b->subsamples, a->subsample_count * sizeof(a->subsamples[0])))
43  return 1;
44 
45  return 0;
46 }
47 
49  if (!a || !b || a->system_id_size != b->system_id_size ||
50  a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size ||
51  a->data_size != b->data_size)
52  return 1;
53 
54  if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 ||
55  memcmp(a->data, b->data, a->data_size) != 0)
56  return 1;
57 
58  for (uint32_t i = 0; i < a->num_key_ids; i++) {
59  if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0)
60  return 1;
61  }
62 
63  if (a->next || b->next) {
64  if (!a->next || !b->next)
65  return 1;
66  if (compare_encryption_init_info(a->next, b->next) != 0)
67  return 1;
68  }
69 
70  return 0;
71 }
72 
73 static void run_encryption_info_test(void)
74 {
76  uint8_t *side_data;
77  size_t side_data_size;
78 
81  av_assert0(info->key_id);
82  av_assert0(info->key_id_size == sizeof(test_key_id));
83  av_assert0(info->iv);
84  av_assert0(info->iv_size == sizeof(test_iv));
85  av_assert0(info->subsamples);
86  av_assert0(info->subsample_count == test_subsample_count);
87 
88  info->scheme = 1234;
89  info->crypt_byte_block = 333;
90  info->skip_byte_block = 444;
91  memcpy(info->key_id, test_key_id, sizeof(test_key_id));
92  memcpy(info->iv, test_iv, sizeof(test_iv));
93  memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples));
94 
97  av_assert0(copy != info);
100 
101  side_data = av_encryption_info_add_side_data(info, &side_data_size);
102  av_assert0(side_data);
103  av_assert0(side_data_size > 0);
104 
105  copy = av_encryption_info_get_side_data(side_data, side_data_size);
106  av_assert0(copy);
107  av_assert0(copy != info);
110  av_free(side_data);
111 
113 }
114 
116 {
118 
120  av_assert0(info);
121  av_assert0(info->system_id);
122  av_assert0(info->system_id_size == sizeof(test_system_id));
123  av_assert0(info->key_ids);
124  av_assert0(info->num_key_ids == 2);
125  av_assert0(info->key_id_size == sizeof(test_key_id));
126  av_assert0(info->key_ids[0]);
127  av_assert0(info->key_ids[1]);
128  av_assert0(info->data);
129  av_assert0(info->data_size == sizeof(test_data));
130  av_assert0(!info->next);
131 
132  memcpy(info->system_id, test_system_id, sizeof(test_system_id));
133  memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id));
134  memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2));
135  memcpy(info->data, test_data, sizeof(test_data));
136 
137  return info;
138 }
139 
141 {
143  uint8_t *side_data;
144  size_t side_data_size;
145 
147 
148  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
149  av_assert0(side_data);
150  av_assert0(side_data_size > 0);
151  copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
152  av_assert0(copy);
155  av_free(side_data);
156 
157  // Make the first init info different from the second to test the correct order.
158  memset(info->system_id, 0, info->system_id_size);
159  info->next = create_init_info();
160  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
161  av_assert0(side_data);
162  copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
163  av_assert0(copy);
166  av_free(side_data);
167 
169 }
170 
171 int main(int argc, char **argv)
172 {
175  return 0;
176 }
av_encryption_info_get_side_data
AVEncryptionInfo * av_encryption_info_get_side_data(const uint8_t *buffer, size_t size)
Creates a copy of the AVEncryptionInfo that is contained in the given side data.
Definition: encryption_info.c:90
av_encryption_init_info_free
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
Definition: encryption_info.c:214
compare_encryption_init_info
static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b)
Definition: encryption_info.c:48
b
#define b
Definition: input.c:41
test_subsamples
static const AVSubsampleEncryptionInfo test_subsamples[]
Definition: encryption_info.c:26
av_encryption_init_info_get_side_data
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
Definition: encryption_info.c:229
run_encryption_info_test
static void run_encryption_info_test(void)
Definition: encryption_info.c:73
test_data
static const uint8_t test_data[]
Definition: encryption_info.c:32
test_key_id
static const uint8_t test_key_id[]
Definition: encryption_info.c:29
test_key_id_2
static const uint8_t test_key_id_2[]
Definition: encryption_info.c:30
av_encryption_info_clone
AVEncryptionInfo * av_encryption_info_clone(const AVEncryptionInfo *info)
Allocates an AVEncryptionInfo structure with a copy of the given data.
Definition: encryption_info.c:63
avassert.h
run_encryption_init_info_test
static void run_encryption_init_info_test(void)
Definition: encryption_info.c:140
info
MIPS optimizations info
Definition: mips.txt:2
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVEncryptionInitInfo
This describes info used to initialize an encryption key system.
Definition: encryption_info.h:88
test_system_id
static const uint8_t test_system_id[]
Definition: encryption_info.c:31
av_encryption_info_add_side_data
uint8_t * av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
Allocates and initializes side data that holds a copy of the given encryption info.
Definition: encryption_info.c:125
test_subsample_count
static const size_t test_subsample_count
Definition: encryption_info.c:27
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:194
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
av_encryption_info_free
void av_encryption_info_free(AVEncryptionInfo *info)
Frees the given encryption info object.
Definition: encryption_info.c:80
AVSubsampleEncryptionInfo
This file is part of FFmpeg.
Definition: encryption_info.h:25
av_encryption_init_info_alloc
AVEncryptionInitInfo * av_encryption_init_info_alloc(uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
Allocates an AVEncryptionInitInfo structure and sub-pointers to hold the given sizes.
Definition: encryption_info.c:176
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
uint8_t
uint8_t
Definition: audio_convert.c:194
AVEncryptionInfo
This describes encryption info for a packet.
Definition: encryption_info.h:43
create_init_info
static AVEncryptionInitInfo * create_init_info(void)
Definition: encryption_info.c:115
compare_encryption_info
static int compare_encryption_info(const AVEncryptionInfo *a, const AVEncryptionInfo *b)
Definition: encryption_info.c:34
encryption_info.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
test_iv
static const uint8_t test_iv[]
Definition: encryption_info.c:28
av_encryption_info_alloc
AVEncryptionInfo * av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
Allocates an AVEncryptionInfo structure and sub-pointers to hold the given number of subsamples.
Definition: encryption_info.c:39
main
int main(int argc, char **argv)
Definition: encryption_info.c:171
av_encryption_init_info_add_side_data
uint8_t * av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
Allocates and initializes side data that holds a copy of the given encryption init info.
Definition: encryption_info.c:290