David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 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 | |
| 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 | /* Replace strncpy with strlcpy |
| 56 | * Add +1 to bytes_to_copy as strlcpy copies size-1 bytes */ |
| 57 | strlcpy(attribute, attribute_string, |
| 58 | bytes_to_copy+1); |
| 59 | |
| 60 | bytes_to_copy = MIN(strlen(attribute_string) - strlen(attribute) - 1, |
| 61 | value_size - 1); |
| 62 | /* Replace strncpy with strlcpy |
| 63 | * Add +1 to bytes_to_copy as strlcpy copies size-1 bytes */ |
| 64 | strlcpy(value, attribute_value_delim + 1, |
| 65 | bytes_to_copy+1); |
| 66 | } |
| 67 | |
| 68 | return METADATA_PARSING_CONTINUE; |
| 69 | } |
| 70 | |
| 71 | int parse_video_encode_metadata(char *metadata, |
| 72 | struct video_encode_metadata_t *video_encode_metadata) |
| 73 | { |
| 74 | char attribute[1024], value[1024], *saveptr; |
| 75 | char *temp_metadata = metadata; |
| 76 | int parsing_status; |
| 77 | |
| 78 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 79 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 80 | if (strlen(attribute) == strlen("hint_id") && |
| 81 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 82 | if (strlen(value) > 0) { |
| 83 | video_encode_metadata->hint_id = atoi(value); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (strlen(attribute) == strlen("state") && |
| 88 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 89 | if (strlen(value) > 0) { |
| 90 | video_encode_metadata->state = atoi(value); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | temp_metadata = NULL; |
| 95 | } |
| 96 | |
| 97 | if (parsing_status == METADATA_PARSING_ERR) |
| 98 | return -1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int parse_video_decode_metadata(char *metadata, |
| 104 | struct video_decode_metadata_t *video_decode_metadata) |
| 105 | { |
| 106 | char attribute[1024], value[1024], *saveptr; |
| 107 | char *temp_metadata = metadata; |
| 108 | int parsing_status; |
| 109 | |
| 110 | while ((parsing_status = parse_metadata(temp_metadata, &saveptr, |
| 111 | attribute, sizeof(attribute), value, sizeof(value))) == METADATA_PARSING_CONTINUE) { |
| 112 | if (strlen(attribute) == strlen("hint_id") && |
| 113 | (strncmp(attribute, "hint_id", strlen("hint_id")) == 0)) { |
| 114 | if (strlen(value) > 0) { |
| 115 | video_decode_metadata->hint_id = atoi(value); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (strlen(attribute) == strlen("state") && |
| 120 | (strncmp(attribute, "state", strlen("state")) == 0)) { |
| 121 | if (strlen(value) > 0) { |
| 122 | video_decode_metadata->state = atoi(value); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | temp_metadata = NULL; |
| 127 | } |
| 128 | |
| 129 | if (parsing_status == METADATA_PARSING_ERR) |
| 130 | return -1; |
| 131 | |
| 132 | return 0; |
| 133 | } |