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,unsigned int attribute_size,char * value,unsigned int value_size)36 int parse_metadata(char *metadata, char **metadata_saveptr,
37         char *attribute, unsigned int attribute_size, char *value, unsigned 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         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);
56 
57         strlcpy(attribute, attribute_string, bytes_to_copy);
58         strlcpy(value, attribute_value_delim + 1, value_size);
59     }
60 
61     return METADATA_PARSING_CONTINUE;
62 }
63 
parse_cam_preview_metadata(char * metadata,struct cam_preview_metadata_t * cam_preview_metadata)64 int parse_cam_preview_metadata(char *metadata,
65     struct cam_preview_metadata_t *cam_preview_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) {
73         if (strlen(attribute) == strlen("hint_id") &&
74             (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
75             if (strlen(value) > 0) {
76                 cam_preview_metadata->hint_id = atoi(value);
77             }
78         }
79 
80         if (strlen(attribute) == strlen("state") &&
81             (strncmp(attribute, "state", strlen("state")) == 0)) {
82             if (strlen(value) > 0) {
83                 cam_preview_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 
parse_video_encode_metadata(char * metadata,struct video_encode_metadata_t * video_encode_metadata)96 int parse_video_encode_metadata(char *metadata,
97     struct video_encode_metadata_t *video_encode_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_encode_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_encode_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 }
127 
parse_video_decode_metadata(char * metadata,struct video_decode_metadata_t * video_decode_metadata)128 int parse_video_decode_metadata(char *metadata,
129     struct video_decode_metadata_t *video_decode_metadata)
130 {
131     char attribute[1024], value[1024], *saveptr;
132     char *temp_metadata = metadata;
133     int parsing_status;
134 
135     while ((parsing_status = parse_metadata(temp_metadata, &saveptr,
136             attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) {
137         if (strlen(attribute) == strlen("hint_id") &&
138             (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) {
139             if (strlen(value) > 0) {
140                 video_decode_metadata->hint_id = atoi(value);
141             }
142         }
143 
144         if (strlen(attribute) == strlen("state") &&
145             (strncmp(attribute, "state", strlen("state")) == 0)) {
146             if (strlen(value) > 0) {
147                 video_decode_metadata->state = atoi(value);
148             }
149         }
150 
151         temp_metadata = NULL;
152     }
153 
154     if (parsing_status == METADATA_PARSING_ERR)
155         return -1;
156 
157     return 0;
158 }
159