-
Notifications
You must be signed in to change notification settings - Fork 443
/
Copy pathparse_utils.c
212 lines (175 loc) · 3.59 KB
/
parse_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "media_format.h"
#include "parse_utils.h"
static int
parse_utils_get_hex_char_value(int ch)
{
if (ch >= '0' && ch <= '9')
{
return (ch - '0');
}
ch = (ch | 0x20); // lower case
if (ch >= 'a' && ch <= 'f')
{
return (ch - 'a' + 10);
}
return -1;
}
vod_status_t
parse_utils_parse_guid_string(vod_str_t* str, u_char* output)
{
u_char* cur_pos;
u_char* end_pos;
u_char* output_end = output + VOD_GUID_SIZE;
int c1;
int c2;
cur_pos = str->data;
end_pos = cur_pos + str->len;
while (cur_pos + 1 < end_pos)
{
if (*cur_pos == '-')
{
cur_pos++;
continue;
}
if (output >= output_end)
{
return VOD_BAD_DATA;
}
c1 = parse_utils_get_hex_char_value(cur_pos[0]);
c2 = parse_utils_get_hex_char_value(cur_pos[1]);
if (c1 < 0 || c2 < 0)
{
return VOD_BAD_DATA;
}
*output++ = ((c1 << 4) | c2);
cur_pos += 2;
}
if (output < output_end)
{
return VOD_BAD_DATA;
}
return VOD_OK;
}
static vod_status_t
parse_utils_base64_exact_decoded_length(vod_str_t* base64, size_t* decoded_len)
{
size_t padding_size;
u_char *cur_pos;
if ((base64->len & 3) != 0)
{
return VOD_BAD_DATA;
}
padding_size = 0;
for (cur_pos = base64->data + base64->len - 1; cur_pos >= base64->data; cur_pos--)
{
if (*cur_pos != '=')
{
break;
}
padding_size++;
}
if (padding_size > 2)
{
return VOD_BAD_DATA;
}
*decoded_len = (base64->len >> 2) * 3 - padding_size;
return VOD_OK;
}
vod_status_t
parse_utils_parse_fixed_base64_string(vod_str_t* str, u_char* output, size_t output_size)
{
vod_str_t output_str;
vod_status_t rc;
size_t decoded_len;
rc = parse_utils_base64_exact_decoded_length(str, &decoded_len);
if (rc != VOD_OK)
{
return rc;
}
if (decoded_len != output_size)
{
return VOD_BAD_DATA;
}
output_str.data = output;
if (vod_decode_base64(&output_str, str) != VOD_OK)
{
return VOD_BAD_DATA;
}
if (output_str.len != output_size)
{
return VOD_BAD_DATA;
}
return VOD_OK;
}
vod_status_t
parse_utils_parse_variable_base64_string(vod_pool_t* pool, vod_str_t* str, vod_str_t* result)
{
result->data = vod_alloc(pool, vod_base64_decoded_length(str->len));
if (result->data == NULL)
{
return VOD_ALLOC_FAILED;
}
if (vod_decode_base64(result, str) != VOD_OK)
{
return VOD_BAD_DATA;
}
return VOD_OK;
}
u_char*
parse_utils_extract_uint32_token(u_char* start_pos, u_char* end_pos, uint32_t* result)
{
uint32_t value = 0;
for (; start_pos < end_pos && *start_pos >= '0' && *start_pos <= '9'; start_pos++)
{
value = value * 10 + *start_pos - '0';
}
*result = value;
return start_pos;
}
u_char*
parse_utils_extract_track_tokens(u_char* start_pos, u_char* end_pos, track_mask_t* result)
{
uint32_t stream_index;
u_char* next_pos;
int media_type;
// by default use the first audio and first video streams
if (start_pos >= end_pos || (*start_pos != 'a' && *start_pos != 'v'))
{
for (media_type = 0; media_type < MEDIA_TYPE_COUNT; media_type++)
{
vod_set_bit(result[media_type], 0);
}
return start_pos;
}
while (start_pos < end_pos)
{
switch (*start_pos)
{
case 'v':
media_type = MEDIA_TYPE_VIDEO;
break;
case 'a':
media_type = MEDIA_TYPE_AUDIO;
break;
default:
return start_pos;
}
start_pos++; // skip the v/a
next_pos = parse_utils_extract_uint32_token(start_pos, end_pos, &stream_index);
if (stream_index == 0)
{
// no index => all streams of the media type
vod_track_mask_set_all_bits(result[media_type]);
}
else
{
vod_set_bit(result[media_type], stream_index - 1);
}
start_pos = next_pos;
if (start_pos < end_pos && *start_pos == '-')
{
start_pos++;
}
}
return start_pos;
}