blob: ffe092b52a4426781420e5d75dbcd8840c581758 [file] [log] [blame]
Anurag Singh9cec85b2012-09-04 21:46:24 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
Duy Truong70222452013-02-10 06:35:11 -080012 * * Neither the name of The Linux Foundation nor the names of its
Anurag Singh9cec85b2012-09-04 21:46:24 -070013 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33
34#include "metadata-defs.h"
35
36int parse_metadata(char *metadata, char **metadata_saveptr,
Adrian Salidoff139872017-03-29 15:58:38 -070037 char *attribute, unsigned int attribute_size, char *value, unsigned int value_size)
Anurag Singh9cec85b2012-09-04 21:46:24 -070038{
39 char *attribute_string;
40 char *attribute_value_delim;
41 unsigned int bytes_to_copy;
42
43 attribute_string = strtok_r(metadata, ATTRIBUTE_STRING_DELIM,
44 metadata_saveptr);
45
46 if (attribute_string == NULL)
47 return METADATA_PARSING_DONE;
48
49 attribute[0] = value[0] = '\0';
50
51 if ((attribute_value_delim = strchr(attribute_string,
52 ATTRIBUTE_VALUE_DELIM)) != NULL) {
Adrian Salidoff139872017-03-29 15:58:38 -070053 unsigned int attribute_len = (unsigned int) (attribute_value_delim - attribute_string);
54 /* copy only attribute len + NUL character, or as much as can be fit */
55 bytes_to_copy = MIN(attribute_len + 1, attribute_size);
Anurag Singh9cec85b2012-09-04 21:46:24 -070056
Adrian Salidoff139872017-03-29 15:58:38 -070057 strlcpy(attribute, attribute_string, bytes_to_copy);
58 strlcpy(value, attribute_value_delim + 1, value_size);
Anurag Singh9cec85b2012-09-04 21:46:24 -070059 }
60
61 return METADATA_PARSING_CONTINUE;
62}
63
Anurag Singh6ec12062012-10-02 09:59:01 -070064int parse_video_encode_metadata(char *metadata,
Anurag Singh9cec85b2012-09-04 21:46:24 -070065 struct video_encode_metadata_t *video_encode_metadata)
66{
67 char attribute[1024], value[1024], *saveptr;
68 char *temp_metadata = metadata;
69 int parsing_status;
70
71 while ((parsing_status = parse_metadata(temp_metadata, &saveptr,
72 attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) {
Anurag Singh6ec12062012-10-02 09:59:01 -070073 if (strlen(attribute) == strlen("hint_id") &&
74 (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
75 if (strlen(value) > 0) {
76 video_encode_metadata->hint_id = atoi(value);
77 }
78 }
79
Anurag Singh9cec85b2012-09-04 21:46:24 -070080 if (strlen(attribute) == strlen("state") &&
81 (strncmp(attribute, "state", strlen("state")) == 0)) {
82 if (strlen(value) > 0) {
83 video_encode_metadata->state = atoi(value);
84 }
85 }
86
87 temp_metadata = NULL;
88 }
89
90 if (parsing_status == METADATA_PARSING_ERR)
91 return -1;
92
93 return 0;
94}
95
Anurag Singh6ec12062012-10-02 09:59:01 -070096int parse_video_decode_metadata(char *metadata,
97 struct video_decode_metadata_t *video_decode_metadata)
98{
99 char attribute[1024], value[1024], *saveptr;
100 char *temp_metadata = metadata;
101 int parsing_status;
102
103 while ((parsing_status = parse_metadata(temp_metadata, &saveptr,
104 attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) {
105 if (strlen(attribute) == strlen("hint_id") &&
106 (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
107 if (strlen(value) > 0) {
108 video_decode_metadata->hint_id = atoi(value);
109 }
110 }
111
112 if (strlen(attribute) == strlen("state") &&
113 (strncmp(attribute, "state", strlen("state")) == 0)) {
114 if (strlen(value) > 0) {
115 video_decode_metadata->state = atoi(value);
116 }
117 }
118
119 temp_metadata = NULL;
120 }
121
122 if (parsing_status == METADATA_PARSING_ERR)
123 return -1;
124
125 return 0;
126}