1 /* 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.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       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 
parse_metadata(char * metadata,char ** metadata_saveptr,char * attribute,int attribute_size,char * value,int value_size)36 int parse_metadata(char *metadata, char **metadata_saveptr,
37         char *attribute, int attribute_size, char *value, int value_size)
38 {
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) {
53         bytes_to_copy = MIN((attribute_value_delim - attribute_string),
54                 attribute_size - 1);
55         strncpy(attribute, attribute_string,
56                 bytes_to_copy);
57         attribute[bytes_to_copy] = '\0';
58 
59         bytes_to_copy = MIN(strlen(attribute_string) - strlen(attribute) - 1,
60                 value_size - 1);
61         strncpy(value, attribute_value_delim + 1,
62                 bytes_to_copy);
63         value[bytes_to_copy] = '\0';
64     }
65 
66     return METADATA_PARSING_CONTINUE;
67 }
68 
parse_video_encode_metadata(char * metadata,struct video_encode_metadata_t * video_encode_metadata)69 int parse_video_encode_metadata(char *metadata,
70     struct video_encode_metadata_t *video_encode_metadata)
71 {
72     char attribute[1024], value[1024], *saveptr;
73     char *temp_metadata = metadata;
74     int parsing_status;
75 
76     while ((parsing_status = parse_metadata(temp_metadata, &saveptr,
77             attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) {
78         if (strlen(attribute) == strlen("hint_id") &&
79             (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
80             if (strlen(value) > 0) {
81                 video_encode_metadata->hint_id = atoi(value);
82             }
83         }
84 
85         if (strlen(attribute) == strlen("state") &&
86             (strncmp(attribute, "state", strlen("state")) == 0)) {
87             if (strlen(value) > 0) {
88                 video_encode_metadata->state = atoi(value);
89             }
90         }
91 
92         temp_metadata = NULL;
93     }
94 
95     if (parsing_status == METADATA_PARSING_ERR)
96         return -1;
97 
98     return 0;
99 }
100 
parse_video_decode_metadata(char * metadata,struct video_decode_metadata_t * video_decode_metadata)101 int parse_video_decode_metadata(char *metadata,
102     struct video_decode_metadata_t *video_decode_metadata)
103 {
104     char attribute[1024], value[1024], *saveptr;
105     char *temp_metadata = metadata;
106     int parsing_status;
107 
108     while ((parsing_status = parse_metadata(temp_metadata, &saveptr,
109             attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) {
110         if (strlen(attribute) == strlen("hint_id") &&
111             (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
112             if (strlen(value) > 0) {
113                 video_decode_metadata->hint_id = atoi(value);
114             }
115         }
116 
117         if (strlen(attribute) == strlen("state") &&
118             (strncmp(attribute, "state", strlen("state")) == 0)) {
119             if (strlen(value) > 0) {
120                 video_decode_metadata->state = atoi(value);
121             }
122         }
123 
124         temp_metadata = NULL;
125     }
126 
127     if (parsing_status == METADATA_PARSING_ERR)
128         return -1;
129 
130     return 0;
131 }
132