• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "camera_metadata"
18 
19 #include <system/camera_metadata.h>
20 #include <camera_metadata_hidden.h>
21 
22 #include <assert.h>
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #include <log/log.h>
29 
30 #define OK              0
31 #define ERROR           1
32 #define NOT_FOUND       (-ENOENT)
33 #define SN_EVENT_LOG_ID 0x534e4554
34 
35 #define ALIGN_TO(val, alignment) \
36     (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
37 
38 /**
39  * A single metadata entry, storing an array of values of a given type. If the
40  * array is no larger than 4 bytes in size, it is stored in the data.value[]
41  * array; otherwise, it can found in the parent's data array at index
42  * data.offset.
43  */
44 #define ENTRY_ALIGNMENT ((size_t) 4)
45 typedef struct camera_metadata_buffer_entry {
46     uint32_t tag;
47     uint32_t count;
48     union {
49         uint32_t offset;
50         uint8_t  value[4];
51     } data;
52     uint8_t  type;
53     uint8_t  reserved[3];
54 } camera_metadata_buffer_entry_t;
55 
56 typedef uint32_t metadata_uptrdiff_t;
57 typedef uint32_t metadata_size_t;
58 
59 /**
60  * A packet of metadata. This is a list of entries, each of which may point to
61  * its values stored at an offset in data.
62  *
63  * It is assumed by the utility functions that the memory layout of the packet
64  * is as follows:
65  *
66  *   |-----------------------------------------------|
67  *   | camera_metadata_t                             |
68  *   |                                               |
69  *   |-----------------------------------------------|
70  *   | reserved for future expansion                 |
71  *   |-----------------------------------------------|
72  *   | camera_metadata_buffer_entry_t #0             |
73  *   |-----------------------------------------------|
74  *   | ....                                          |
75  *   |-----------------------------------------------|
76  *   | camera_metadata_buffer_entry_t #entry_count-1 |
77  *   |-----------------------------------------------|
78  *   | free space for                                |
79  *   | (entry_capacity-entry_count) entries          |
80  *   |-----------------------------------------------|
81  *   | start of camera_metadata.data                 |
82  *   |                                               |
83  *   |-----------------------------------------------|
84  *   | free space for                                |
85  *   | (data_capacity-data_count) bytes              |
86  *   |-----------------------------------------------|
87  *
88  * With the total length of the whole packet being camera_metadata.size bytes.
89  *
90  * In short, the entries and data are contiguous in memory after the metadata
91  * header.
92  */
93 #define METADATA_ALIGNMENT ((size_t) 4)
94 struct camera_metadata {
95     metadata_size_t          size;
96     uint32_t                 version;
97     uint32_t                 flags;
98     metadata_size_t          entry_count;
99     metadata_size_t          entry_capacity;
100     metadata_uptrdiff_t      entries_start; // Offset from camera_metadata
101     metadata_size_t          data_count;
102     metadata_size_t          data_capacity;
103     metadata_uptrdiff_t      data_start; // Offset from camera_metadata
104     uint32_t                 padding;    // padding to 8 bytes boundary
105     metadata_vendor_id_t     vendor_id;
106 };
107 
108 /**
109  * A datum of metadata. This corresponds to camera_metadata_entry_t::data
110  * with the difference that each element is not a pointer. We need to have a
111  * non-pointer type description in order to figure out the largest alignment
112  * requirement for data (DATA_ALIGNMENT).
113  */
114 #define DATA_ALIGNMENT ((size_t) 8)
115 typedef union camera_metadata_data {
116     uint8_t u8;
117     int32_t i32;
118     float   f;
119     int64_t i64;
120     double  d;
121     camera_metadata_rational_t r;
122 } camera_metadata_data_t;
123 
124 _Static_assert(sizeof(metadata_size_t) == 4,
125          "Size of metadata_size_t must be 4");
126 _Static_assert(sizeof(metadata_uptrdiff_t) == 4,
127          "Size of metadata_uptrdiff_t must be 4");
128 _Static_assert(sizeof(metadata_vendor_id_t) == 8,
129          "Size of metadata_vendor_id_t must be 8");
130 _Static_assert(sizeof(camera_metadata_data_t) == 8,
131          "Size of camera_metadata_data_t must be 8");
132 
133 _Static_assert(offsetof(camera_metadata_buffer_entry_t, tag) == 0,
134          "Offset of tag must be 0");
135 _Static_assert(offsetof(camera_metadata_buffer_entry_t, count) == 4,
136          "Offset of count must be 4");
137 _Static_assert(offsetof(camera_metadata_buffer_entry_t, data) == 8,
138          "Offset of data must be 8");
139 _Static_assert(offsetof(camera_metadata_buffer_entry_t, type) == 12,
140          "Offset of type must be 12");
141 _Static_assert(sizeof(camera_metadata_buffer_entry_t) == 16,
142          "Size of camera_metadata_buffer_entry_t must be 16");
143 
144 _Static_assert(offsetof(camera_metadata_t, size) == 0,
145          "Offset of size must be 0");
146 _Static_assert(offsetof(camera_metadata_t, version) == 4,
147          "Offset of version must be 4");
148 _Static_assert(offsetof(camera_metadata_t, flags) == 8,
149          "Offset of flags must be 8");
150 _Static_assert(offsetof(camera_metadata_t, entry_count) == 12,
151          "Offset of entry_count must be 12");
152 _Static_assert(offsetof(camera_metadata_t, entry_capacity) == 16,
153          "Offset of entry_capacity must be 16");
154 _Static_assert(offsetof(camera_metadata_t, entries_start) == 20,
155          "Offset of entries_start must be 20");
156 _Static_assert(offsetof(camera_metadata_t, data_count) == 24,
157          "Offset of data_count must be 24");
158 _Static_assert(offsetof(camera_metadata_t, data_capacity) == 28,
159          "Offset of data_capacity must be 28");
160 _Static_assert(offsetof(camera_metadata_t, data_start) == 32,
161          "Offset of data_start must be 32");
162 _Static_assert(offsetof(camera_metadata_t, vendor_id) == 40,
163          "Offset of vendor_id must be 40");
164 _Static_assert(sizeof(camera_metadata_t) == 48,
165          "Size of camera_metadata_t must be 48");
166 
167 /**
168  * The preferred alignment of a packet of camera metadata. In general,
169  * this is the lowest common multiple of the constituents of a metadata
170  * package, i.e, of DATA_ALIGNMENT and ENTRY_ALIGNMENT.
171  */
172 #define MAX_ALIGNMENT(A, B) (((A) > (B)) ? (A) : (B))
173 #define METADATA_PACKET_ALIGNMENT \
174     MAX_ALIGNMENT(MAX_ALIGNMENT(DATA_ALIGNMENT, METADATA_ALIGNMENT), ENTRY_ALIGNMENT)
175 
176 /** Versioning information */
177 #define CURRENT_METADATA_VERSION 1
178 
179 /** Flag definitions */
180 #define FLAG_SORTED 0x00000001
181 
182 /** Tag information */
183 
184 typedef struct tag_info {
185     const char *tag_name;
186     uint8_t     tag_type;
187 } tag_info_t;
188 
189 #include "camera_metadata_tag_info.c"
190 
191 const size_t camera_metadata_type_size[NUM_TYPES] = {
192     [TYPE_BYTE]     = sizeof(uint8_t),
193     [TYPE_INT32]    = sizeof(int32_t),
194     [TYPE_FLOAT]    = sizeof(float),
195     [TYPE_INT64]    = sizeof(int64_t),
196     [TYPE_DOUBLE]   = sizeof(double),
197     [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
198 };
199 
200 const char *camera_metadata_type_names[NUM_TYPES] = {
201     [TYPE_BYTE]     = "byte",
202     [TYPE_INT32]    = "int32",
203     [TYPE_FLOAT]    = "float",
204     [TYPE_INT64]    = "int64",
205     [TYPE_DOUBLE]   = "double",
206     [TYPE_RATIONAL] = "rational"
207 };
208 
get_entries(const camera_metadata_t * metadata)209 static camera_metadata_buffer_entry_t *get_entries(
210         const camera_metadata_t *metadata) {
211     return (camera_metadata_buffer_entry_t*)
212             ((uint8_t*)metadata + metadata->entries_start);
213 }
214 
get_data(const camera_metadata_t * metadata)215 static uint8_t *get_data(const camera_metadata_t *metadata) {
216     return (uint8_t*)metadata + metadata->data_start;
217 }
218 
get_camera_metadata_alignment()219 size_t get_camera_metadata_alignment() {
220     return METADATA_PACKET_ALIGNMENT;
221 }
222 
allocate_copy_camera_metadata_checked(const camera_metadata_t * src,size_t src_size)223 camera_metadata_t *allocate_copy_camera_metadata_checked(
224         const camera_metadata_t *src,
225         size_t src_size) {
226 
227     if (src == NULL) {
228         return NULL;
229     }
230 
231     if (src_size < sizeof(camera_metadata_t)) {
232         ALOGE("%s: Source size too small!", __FUNCTION__);
233         android_errorWriteLog(0x534e4554, "67782345");
234         return NULL;
235     }
236 
237     void *buffer = calloc(1, src_size);
238     memcpy(buffer, src, src_size);
239 
240     camera_metadata_t *metadata = (camera_metadata_t*) buffer;
241     if (validate_camera_metadata_structure(metadata, &src_size) != OK) {
242         free(buffer);
243         return NULL;
244     }
245 
246     return metadata;
247 }
248 
allocate_camera_metadata(size_t entry_capacity,size_t data_capacity)249 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
250                                             size_t data_capacity) {
251 
252     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
253                                                           data_capacity);
254     void *buffer = calloc(1, memory_needed);
255     camera_metadata_t *metadata = place_camera_metadata(
256         buffer, memory_needed, entry_capacity, data_capacity);
257     if (!metadata) {
258         /* This should not happen when memory_needed is the same
259          * calculated in this function and in place_camera_metadata.
260          */
261         free(buffer);
262     }
263     return metadata;
264 }
265 
place_camera_metadata(void * dst,size_t dst_size,size_t entry_capacity,size_t data_capacity)266 camera_metadata_t *place_camera_metadata(void *dst,
267                                          size_t dst_size,
268                                          size_t entry_capacity,
269                                          size_t data_capacity) {
270     if (dst == NULL) return NULL;
271 
272     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
273                                                           data_capacity);
274     if (memory_needed > dst_size) {
275       ALOGE("%s: Memory needed to place camera metadata (%zu) > dst size (%zu)", __FUNCTION__,
276               memory_needed, dst_size);
277       return NULL;
278     }
279 
280     camera_metadata_t *metadata = (camera_metadata_t*)dst;
281     metadata->version = CURRENT_METADATA_VERSION;
282     metadata->flags = 0;
283     metadata->entry_count = 0;
284     metadata->entry_capacity = entry_capacity;
285     metadata->entries_start =
286             ALIGN_TO(sizeof(camera_metadata_t), ENTRY_ALIGNMENT);
287     metadata->data_count = 0;
288     metadata->data_capacity = data_capacity;
289     metadata->size = memory_needed;
290     size_t data_unaligned = (uint8_t*)(get_entries(metadata) +
291             metadata->entry_capacity) - (uint8_t*)metadata;
292     metadata->data_start = ALIGN_TO(data_unaligned, DATA_ALIGNMENT);
293     metadata->vendor_id = CAMERA_METADATA_INVALID_VENDOR_ID;
294 
295     assert(validate_camera_metadata_structure(metadata, NULL) == OK);
296     return metadata;
297 }
free_camera_metadata(camera_metadata_t * metadata)298 void free_camera_metadata(camera_metadata_t *metadata) {
299     free(metadata);
300 }
301 
calculate_camera_metadata_size(size_t entry_count,size_t data_count)302 size_t calculate_camera_metadata_size(size_t entry_count,
303                                       size_t data_count) {
304     size_t memory_needed = sizeof(camera_metadata_t);
305     // Start entry list at aligned boundary
306     memory_needed = ALIGN_TO(memory_needed, ENTRY_ALIGNMENT);
307     memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
308     // Start buffer list at aligned boundary
309     memory_needed = ALIGN_TO(memory_needed, DATA_ALIGNMENT);
310     memory_needed += sizeof(uint8_t[data_count]);
311     // Make sure camera metadata can be stacked in continuous memory
312     memory_needed = ALIGN_TO(memory_needed, METADATA_PACKET_ALIGNMENT);
313     return memory_needed;
314 }
315 
get_camera_metadata_size(const camera_metadata_t * metadata)316 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
317     if (metadata == NULL) return ERROR;
318 
319     return metadata->size;
320 }
321 
get_camera_metadata_compact_size(const camera_metadata_t * metadata)322 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
323     if (metadata == NULL) return ERROR;
324 
325     return calculate_camera_metadata_size(metadata->entry_count,
326                                           metadata->data_count);
327 }
328 
get_camera_metadata_entry_count(const camera_metadata_t * metadata)329 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
330     return metadata->entry_count;
331 }
332 
get_camera_metadata_entry_capacity(const camera_metadata_t * metadata)333 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
334     return metadata->entry_capacity;
335 }
336 
get_camera_metadata_data_count(const camera_metadata_t * metadata)337 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
338     return metadata->data_count;
339 }
340 
get_camera_metadata_data_capacity(const camera_metadata_t * metadata)341 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
342     return metadata->data_capacity;
343 }
344 
copy_camera_metadata(void * dst,size_t dst_size,const camera_metadata_t * src)345 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
346         const camera_metadata_t *src) {
347     size_t memory_needed = get_camera_metadata_compact_size(src);
348 
349     if (dst == NULL) return NULL;
350     if (dst_size < memory_needed) {
351         ALOGE("%s: Memory needed to place camera metadata (%zu) > dst size (%zu)", __FUNCTION__,
352                 memory_needed, dst_size);
353       return NULL;
354     }
355 
356     camera_metadata_t *metadata =
357         place_camera_metadata(dst, dst_size, src->entry_count, src->data_count);
358 
359     metadata->flags = src->flags;
360     metadata->entry_count = src->entry_count;
361     metadata->data_count = src->data_count;
362     metadata->vendor_id = src->vendor_id;
363 
364     memcpy(get_entries(metadata), get_entries(src),
365             sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
366     memcpy(get_data(metadata), get_data(src),
367             sizeof(uint8_t[metadata->data_count]));
368 
369     assert(validate_camera_metadata_structure(metadata, NULL) == OK);
370     return metadata;
371 }
372 
373 // This method should be used when the camera metadata cannot be trusted. For example, when it's
374 // read from Parcel.
validate_and_calculate_camera_metadata_entry_data_size(size_t * data_size,uint8_t type,size_t data_count)375 static int validate_and_calculate_camera_metadata_entry_data_size(size_t *data_size, uint8_t type,
376         size_t data_count) {
377     if (type >= NUM_TYPES) return ERROR;
378 
379     // Check for overflow
380     if (data_count != 0 &&
381             camera_metadata_type_size[type] > (SIZE_MAX - DATA_ALIGNMENT + 1) / data_count) {
382         android_errorWriteLog(SN_EVENT_LOG_ID, "30741779");
383         return ERROR;
384     }
385 
386     size_t data_bytes = data_count * camera_metadata_type_size[type];
387 
388     if (data_size) {
389         *data_size = data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
390     }
391 
392     return OK;
393 }
394 
calculate_camera_metadata_entry_data_size(uint8_t type,size_t data_count)395 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
396         size_t data_count) {
397     if (type >= NUM_TYPES) return 0;
398 
399     size_t data_bytes = data_count *
400             camera_metadata_type_size[type];
401 
402     return data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
403 }
404 
validate_camera_metadata_structure(const camera_metadata_t * metadata,const size_t * expected_size)405 int validate_camera_metadata_structure(const camera_metadata_t *metadata,
406                                        const size_t *expected_size) {
407 
408     if (metadata == NULL) {
409         ALOGE("%s: metadata is null!", __FUNCTION__);
410         return CAMERA_METADATA_VALIDATION_ERROR;
411     }
412 
413     uintptr_t aligned_ptr = ALIGN_TO(metadata, METADATA_PACKET_ALIGNMENT);
414     const uintptr_t alignmentOffset = aligned_ptr - (uintptr_t) metadata;
415 
416     // Check that the metadata pointer is well-aligned first.
417     {
418         static const struct {
419             const char *name;
420             size_t alignment;
421         } alignments[] = {
422             {
423                 .name = "camera_metadata",
424                 .alignment = METADATA_ALIGNMENT
425             },
426             {
427                 .name = "camera_metadata_buffer_entry",
428                 .alignment = ENTRY_ALIGNMENT
429             },
430             {
431                 .name = "camera_metadata_data",
432                 .alignment = DATA_ALIGNMENT
433             },
434         };
435 
436         for (size_t i = 0; i < sizeof(alignments)/sizeof(alignments[0]); ++i) {
437             uintptr_t aligned_ptr_section = ALIGN_TO((uintptr_t) metadata + alignmentOffset,
438                     alignments[i].alignment);
439 
440             if ((uintptr_t)metadata + alignmentOffset != aligned_ptr_section) {
441                 ALOGE("%s: Metadata pointer is not aligned (actual %p, "
442                       "expected %p, offset %" PRIuPTR ") to type %s",
443                       __FUNCTION__, metadata,
444                       (void*)aligned_ptr_section, alignmentOffset, alignments[i].name);
445                 return CAMERA_METADATA_VALIDATION_ERROR;
446             }
447         }
448     }
449 
450     /**
451      * Check that the metadata contents are correct
452      */
453     if (expected_size != NULL && sizeof(camera_metadata_t) > *expected_size) {
454         ALOGE("%s: Metadata size (%zu) should be <= expected size (%zu)",
455                 __FUNCTION__, sizeof(camera_metadata_t), *expected_size);
456         return CAMERA_METADATA_VALIDATION_ERROR;
457     }
458 
459     // Create an aligned header
460     camera_metadata_t headerCopy;
461     const camera_metadata_t *header;
462     if (alignmentOffset != 0) {
463         memcpy(&headerCopy, metadata, sizeof(camera_metadata_t));
464         header = &headerCopy;
465     } else {
466         header = metadata;
467     }
468 
469     if (expected_size != NULL && header->size > *expected_size) {
470         ALOGE("%s: Metadata size (%" PRIu32 ") should be <= expected size (%zu)",
471               __FUNCTION__, header->size, *expected_size);
472         return CAMERA_METADATA_VALIDATION_ERROR;
473     }
474 
475     if (header->entry_count > header->entry_capacity) {
476         ALOGE("%s: Entry count (%" PRIu32 ") should be <= entry capacity "
477               "(%" PRIu32 ")",
478               __FUNCTION__, header->entry_count, header->entry_capacity);
479         return CAMERA_METADATA_VALIDATION_ERROR;
480     }
481 
482     if (header->data_count > header->data_capacity) {
483         ALOGE("%s: Data count (%" PRIu32 ") should be <= data capacity "
484               "(%" PRIu32 ")",
485               __FUNCTION__, header->data_count, header->data_capacity);
486         android_errorWriteLog(SN_EVENT_LOG_ID, "30591838");
487         return CAMERA_METADATA_VALIDATION_ERROR;
488     }
489 
490     const metadata_uptrdiff_t entries_end =
491         header->entries_start + header->entry_capacity;
492     if (entries_end < header->entries_start || // overflow check
493         entries_end > header->data_start) {
494 
495         ALOGE("%s: Entry start + capacity (%" PRIu32 ") should be <= data start "
496               "(%" PRIu32 ")",
497                __FUNCTION__,
498               (header->entries_start + header->entry_capacity),
499               header->data_start);
500         return CAMERA_METADATA_VALIDATION_ERROR;
501     }
502 
503     const metadata_uptrdiff_t data_end =
504         header->data_start + header->data_capacity;
505     if (data_end < header->data_start || // overflow check
506         data_end > header->size) {
507 
508         ALOGE("%s: Data start + capacity (%" PRIu32 ") should be <= total size "
509               "(%" PRIu32 ")",
510                __FUNCTION__,
511               (header->data_start + header->data_capacity),
512               header->size);
513         return CAMERA_METADATA_VALIDATION_ERROR;
514     }
515 
516     // Validate each entry
517     const metadata_size_t entry_count = header->entry_count;
518     camera_metadata_buffer_entry_t *entries = get_entries(metadata);
519 
520     for (size_t i = 0; i < entry_count; ++i) {
521 
522         if ((uintptr_t)&entries[i] + alignmentOffset !=
523                 ALIGN_TO((uintptr_t)&entries[i] + alignmentOffset, ENTRY_ALIGNMENT)) {
524             ALOGE("%s: Entry index %zu had bad alignment (address %p),"
525                   " expected alignment %zu",
526                   __FUNCTION__, i, &entries[i], ENTRY_ALIGNMENT);
527             return CAMERA_METADATA_VALIDATION_ERROR;
528         }
529 
530         camera_metadata_buffer_entry_t entry;
531         if (alignmentOffset != 0) {
532             memcpy(&entry, entries + i, sizeof(camera_metadata_buffer_entry_t));
533         } else {
534             entry = entries[i];
535         }
536 
537         if (entry.type >= NUM_TYPES) {
538             ALOGE("%s: Entry index %zu had a bad type %d",
539                   __FUNCTION__, i, entry.type);
540             return CAMERA_METADATA_VALIDATION_ERROR;
541         }
542 
543         // TODO: fix vendor_tag_ops across processes so we don't need to special
544         //       case vendor-specific tags
545         uint32_t tag_section = entry.tag >> 16;
546         int tag_type = get_local_camera_metadata_tag_type(entry.tag, header);
547         if (tag_type != (int)entry.type && tag_section < VENDOR_SECTION) {
548             ALOGE("%s: Entry index %zu had tag type %d, but the type was %d",
549                   __FUNCTION__, i, tag_type, entry.type);
550             return CAMERA_METADATA_VALIDATION_ERROR;
551         }
552 
553         size_t data_size;
554         if (validate_and_calculate_camera_metadata_entry_data_size(&data_size, entry.type,
555                 entry.count) != OK) {
556             ALOGE("%s: Entry data size is invalid. type: %u count: %u", __FUNCTION__, entry.type,
557                     entry.count);
558             return CAMERA_METADATA_VALIDATION_ERROR;
559         }
560 
561         if (data_size != 0) {
562             camera_metadata_data_t *data =
563                     (camera_metadata_data_t*) (get_data(metadata) +
564                                                entry.data.offset);
565 
566             if ((uintptr_t)data + alignmentOffset !=
567                         ALIGN_TO((uintptr_t)data + alignmentOffset, DATA_ALIGNMENT)) {
568                 ALOGE("%s: Entry index %zu had bad data alignment (address %p),"
569                       " expected align %zu, (tag name %s, data size %zu)",
570                       __FUNCTION__, i, data, DATA_ALIGNMENT,
571                       get_local_camera_metadata_tag_name(entry.tag, metadata) ?
572                               : "unknown", data_size);
573                 return CAMERA_METADATA_VALIDATION_ERROR;
574             }
575 
576             size_t data_entry_end = entry.data.offset + data_size;
577             if (data_entry_end < entry.data.offset || // overflow check
578                 data_entry_end > metadata->data_capacity) {
579 
580                 ALOGE("%s: Entry index %zu data ends (%zu) beyond the capacity "
581                       "%" PRIu32, __FUNCTION__, i, data_entry_end,
582                       metadata->data_capacity);
583                 return CAMERA_METADATA_VALIDATION_ERROR;
584             }
585 
586         } else if (entry.count == 0) {
587             if (entry.data.offset != 0) {
588                 ALOGE("%s: Entry index %zu had 0 items, but offset was non-0 "
589                      "(%" PRIu32 "), tag name: %s", __FUNCTION__, i, entry.data.offset,
590                         get_local_camera_metadata_tag_name(entry.tag, metadata) ? : "unknown");
591                 return CAMERA_METADATA_VALIDATION_ERROR;
592             }
593         } // else data stored inline, so we look at value which can be anything.
594     }
595 
596     if (alignmentOffset == 0) {
597         return OK;
598     }
599     return CAMERA_METADATA_VALIDATION_SHIFTED;
600 }
601 
append_camera_metadata(camera_metadata_t * dst,const camera_metadata_t * src)602 int append_camera_metadata(camera_metadata_t *dst,
603         const camera_metadata_t *src) {
604     if (dst == NULL || src == NULL ) return ERROR;
605 
606     // Check for overflow
607     if (src->entry_count + dst->entry_count < src->entry_count) return ERROR;
608     if (src->data_count + dst->data_count < src->data_count) return ERROR;
609     // Check for space
610     if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
611     if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
612 
613     if ((dst->vendor_id != CAMERA_METADATA_INVALID_VENDOR_ID) &&
614             (src->vendor_id != CAMERA_METADATA_INVALID_VENDOR_ID)) {
615         if (dst->vendor_id != src->vendor_id) {
616             ALOGE("%s: Append for metadata from different vendors is"
617                     "not supported!", __func__);
618             return ERROR;
619         }
620     }
621 
622     memcpy(get_entries(dst) + dst->entry_count, get_entries(src),
623             sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
624     memcpy(get_data(dst) + dst->data_count, get_data(src),
625             sizeof(uint8_t[src->data_count]));
626     if (dst->data_count != 0) {
627         camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
628         for (size_t i = 0; i < src->entry_count; i++, entry++) {
629             if ( calculate_camera_metadata_entry_data_size(entry->type,
630                             entry->count) > 0 ) {
631                 entry->data.offset += dst->data_count;
632             }
633         }
634     }
635     if (dst->entry_count == 0) {
636         // Appending onto empty buffer, keep sorted state
637         dst->flags |= src->flags & FLAG_SORTED;
638     } else if (src->entry_count != 0) {
639         // Both src, dst are nonempty, cannot assume sort remains
640         dst->flags &= ~FLAG_SORTED;
641     } else {
642         // Src is empty, keep dst sorted state
643     }
644     dst->entry_count += src->entry_count;
645     dst->data_count += src->data_count;
646 
647     if (dst->vendor_id == CAMERA_METADATA_INVALID_VENDOR_ID) {
648         dst->vendor_id = src->vendor_id;
649     }
650 
651     assert(validate_camera_metadata_structure(dst, NULL) == OK);
652     return OK;
653 }
654 
clone_camera_metadata(const camera_metadata_t * src)655 camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src) {
656     int res;
657     if (src == NULL) return NULL;
658     camera_metadata_t *clone = allocate_camera_metadata(
659         get_camera_metadata_entry_count(src),
660         get_camera_metadata_data_count(src));
661     if (clone != NULL) {
662         res = append_camera_metadata(clone, src);
663         if (res != OK) {
664             free_camera_metadata(clone);
665             clone = NULL;
666         }
667     }
668     assert(validate_camera_metadata_structure(clone, NULL) == OK);
669     return clone;
670 }
671 
add_camera_metadata_entry_raw(camera_metadata_t * dst,uint32_t tag,uint8_t type,const void * data,size_t data_count)672 static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
673         uint32_t tag,
674         uint8_t  type,
675         const void *data,
676         size_t data_count) {
677 
678     if (dst == NULL) return ERROR;
679     if (dst->entry_count == dst->entry_capacity) return ERROR;
680     if (data_count && data == NULL) return ERROR;
681 
682     size_t data_bytes =
683             calculate_camera_metadata_entry_data_size(type, data_count);
684     if (data_bytes + dst->data_count > dst->data_capacity) return ERROR;
685 
686     size_t data_payload_bytes =
687             data_count * camera_metadata_type_size[type];
688     camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
689     memset(entry, 0, sizeof(camera_metadata_buffer_entry_t));
690     entry->tag = tag;
691     entry->type = type;
692     entry->count = data_count;
693 
694     if (data_bytes == 0) {
695         memcpy(entry->data.value, data,
696                 data_payload_bytes);
697     } else {
698         entry->data.offset = dst->data_count;
699         memcpy(get_data(dst) + entry->data.offset, data,
700                 data_payload_bytes);
701         dst->data_count += data_bytes;
702     }
703     dst->entry_count++;
704     dst->flags &= ~FLAG_SORTED;
705     assert(validate_camera_metadata_structure(dst, NULL) == OK);
706     return OK;
707 }
708 
add_camera_metadata_entry(camera_metadata_t * dst,uint32_t tag,const void * data,size_t data_count)709 int add_camera_metadata_entry(camera_metadata_t *dst,
710         uint32_t tag,
711         const void *data,
712         size_t data_count) {
713 
714     int type = get_local_camera_metadata_tag_type(tag, dst);
715     if (type == -1) {
716         ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
717         return ERROR;
718     }
719 
720     return add_camera_metadata_entry_raw(dst,
721             tag,
722             type,
723             data,
724             data_count);
725 }
726 
compare_entry_tags(const void * p1,const void * p2)727 static int compare_entry_tags(const void *p1, const void *p2) {
728     uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
729     uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
730     return  tag1 < tag2 ? -1 :
731             tag1 == tag2 ? 0 :
732             1;
733 }
734 
sort_camera_metadata(camera_metadata_t * dst)735 int sort_camera_metadata(camera_metadata_t *dst) {
736     if (dst == NULL) return ERROR;
737     if (dst->flags & FLAG_SORTED) return OK;
738 
739     qsort(get_entries(dst), dst->entry_count,
740             sizeof(camera_metadata_buffer_entry_t),
741             compare_entry_tags);
742     dst->flags |= FLAG_SORTED;
743 
744     assert(validate_camera_metadata_structure(dst, NULL) == OK);
745     return OK;
746 }
747 
get_camera_metadata_entry(camera_metadata_t * src,size_t index,camera_metadata_entry_t * entry)748 int get_camera_metadata_entry(camera_metadata_t *src,
749         size_t index,
750         camera_metadata_entry_t *entry) {
751     if (src == NULL || entry == NULL) return ERROR;
752     if (index >= src->entry_count) return ERROR;
753 
754     camera_metadata_buffer_entry_t *buffer_entry = get_entries(src) + index;
755 
756     entry->index = index;
757     entry->tag = buffer_entry->tag;
758     entry->type = buffer_entry->type;
759     entry->count = buffer_entry->count;
760     if (buffer_entry->count *
761             camera_metadata_type_size[buffer_entry->type] > 4) {
762         entry->data.u8 = get_data(src) + buffer_entry->data.offset;
763     } else {
764         entry->data.u8 = buffer_entry->data.value;
765     }
766     return OK;
767 }
768 
get_camera_metadata_ro_entry(const camera_metadata_t * src,size_t index,camera_metadata_ro_entry_t * entry)769 int get_camera_metadata_ro_entry(const camera_metadata_t *src,
770         size_t index,
771         camera_metadata_ro_entry_t *entry) {
772     return get_camera_metadata_entry((camera_metadata_t*)src, index,
773             (camera_metadata_entry_t*)entry);
774 }
775 
find_camera_metadata_entry(camera_metadata_t * src,uint32_t tag,camera_metadata_entry_t * entry)776 int find_camera_metadata_entry(camera_metadata_t *src,
777         uint32_t tag,
778         camera_metadata_entry_t *entry) {
779     if (src == NULL) return ERROR;
780 
781     uint32_t index;
782     if (src->flags & FLAG_SORTED) {
783         // Sorted entries, do a binary search
784         camera_metadata_buffer_entry_t *search_entry = NULL;
785         camera_metadata_buffer_entry_t key;
786         key.tag = tag;
787         search_entry = bsearch(&key,
788                 get_entries(src),
789                 src->entry_count,
790                 sizeof(camera_metadata_buffer_entry_t),
791                 compare_entry_tags);
792         if (search_entry == NULL) return NOT_FOUND;
793         index = search_entry - get_entries(src);
794     } else {
795         // Not sorted, linear search
796         camera_metadata_buffer_entry_t *search_entry = get_entries(src);
797         for (index = 0; index < src->entry_count; index++, search_entry++) {
798             if (search_entry->tag == tag) {
799                 break;
800             }
801         }
802         if (index == src->entry_count) return NOT_FOUND;
803     }
804 
805     return get_camera_metadata_entry(src, index,
806             entry);
807 }
808 
find_camera_metadata_ro_entry(const camera_metadata_t * src,uint32_t tag,camera_metadata_ro_entry_t * entry)809 int find_camera_metadata_ro_entry(const camera_metadata_t *src,
810         uint32_t tag,
811         camera_metadata_ro_entry_t *entry) {
812     return find_camera_metadata_entry((camera_metadata_t*)src, tag,
813             (camera_metadata_entry_t*)entry);
814 }
815 
816 
delete_camera_metadata_entry(camera_metadata_t * dst,size_t index)817 int delete_camera_metadata_entry(camera_metadata_t *dst,
818         size_t index) {
819     if (dst == NULL) return ERROR;
820     if (index >= dst->entry_count) return ERROR;
821 
822     camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
823     size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
824             entry->count);
825 
826     if (data_bytes > 0) {
827         // Shift data buffer to overwrite deleted data
828         uint8_t *start = get_data(dst) + entry->data.offset;
829         uint8_t *end = start + data_bytes;
830         size_t length = dst->data_count - entry->data.offset - data_bytes;
831         memmove(start, end, length);
832 
833         // Update all entry indices to account for shift
834         camera_metadata_buffer_entry_t *e = get_entries(dst);
835         size_t i;
836         for (i = 0; i < dst->entry_count; i++) {
837             if (calculate_camera_metadata_entry_data_size(
838                     e->type, e->count) > 0 &&
839                     e->data.offset > entry->data.offset) {
840                 e->data.offset -= data_bytes;
841             }
842             ++e;
843         }
844         dst->data_count -= data_bytes;
845     }
846     // Shift entry array
847     memmove(entry, entry + 1,
848             sizeof(camera_metadata_buffer_entry_t) *
849             (dst->entry_count - index - 1) );
850     dst->entry_count -= 1;
851 
852     assert(validate_camera_metadata_structure(dst, NULL) == OK);
853     return OK;
854 }
855 
update_camera_metadata_entry(camera_metadata_t * dst,size_t index,const void * data,size_t data_count,camera_metadata_entry_t * updated_entry)856 int update_camera_metadata_entry(camera_metadata_t *dst,
857         size_t index,
858         const void *data,
859         size_t data_count,
860         camera_metadata_entry_t *updated_entry) {
861     if (dst == NULL) return ERROR;
862     if (index >= dst->entry_count) return ERROR;
863 
864     camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
865 
866     size_t data_bytes =
867             calculate_camera_metadata_entry_data_size(entry->type,
868                     data_count);
869     size_t data_payload_bytes =
870             data_count * camera_metadata_type_size[entry->type];
871 
872     size_t entry_bytes =
873             calculate_camera_metadata_entry_data_size(entry->type,
874                     entry->count);
875     if (data_bytes != entry_bytes) {
876         // May need to shift/add to data array
877         if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
878             // No room
879             return ERROR;
880         }
881         if (entry_bytes != 0) {
882             // Remove old data
883             uint8_t *start = get_data(dst) + entry->data.offset;
884             uint8_t *end = start + entry_bytes;
885             size_t length = dst->data_count - entry->data.offset - entry_bytes;
886             memmove(start, end, length);
887             dst->data_count -= entry_bytes;
888 
889             // Update all entry indices to account for shift
890             camera_metadata_buffer_entry_t *e = get_entries(dst);
891             size_t i;
892             for (i = 0; i < dst->entry_count; i++) {
893                 if (calculate_camera_metadata_entry_data_size(
894                         e->type, e->count) > 0 &&
895                         e->data.offset > entry->data.offset) {
896                     e->data.offset -= entry_bytes;
897                 }
898                 ++e;
899             }
900         }
901 
902         if (data_bytes != 0) {
903             // Append new data
904             entry->data.offset = dst->data_count;
905 
906             memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
907             dst->data_count += data_bytes;
908         }
909     } else if (data_bytes != 0) {
910         // data size unchanged, reuse same data location
911         memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
912     }
913 
914     if (data_bytes == 0) {
915         // Data fits into entry
916         memcpy(entry->data.value, data,
917                 data_payload_bytes);
918     }
919 
920     entry->count = data_count;
921 
922     if (updated_entry != NULL) {
923         get_camera_metadata_entry(dst,
924                 index,
925                 updated_entry);
926     }
927 
928     assert(validate_camera_metadata_structure(dst, NULL) == OK);
929     return OK;
930 }
931 
932 static const vendor_tag_ops_t *vendor_tag_ops = NULL;
933 static const struct vendor_tag_cache_ops *vendor_cache_ops = NULL;
934 
935 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
get_local_camera_metadata_section_name_vendor_id(uint32_t tag,metadata_vendor_id_t id)936 const char *get_local_camera_metadata_section_name_vendor_id(uint32_t tag,
937         metadata_vendor_id_t id) {
938     uint32_t tag_section = tag >> 16;
939     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
940                id != CAMERA_METADATA_INVALID_VENDOR_ID) {
941            return vendor_cache_ops->get_section_name(tag, id);
942     } else if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
943         return vendor_tag_ops->get_section_name(
944             vendor_tag_ops,
945             tag);
946     }
947     if (tag_section >= ANDROID_SECTION_COUNT) {
948         return NULL;
949     }
950     return camera_metadata_section_names[tag_section];
951 }
952 
953 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
get_local_camera_metadata_tag_name_vendor_id(uint32_t tag,metadata_vendor_id_t id)954 const char *get_local_camera_metadata_tag_name_vendor_id(uint32_t tag,
955         metadata_vendor_id_t id) {
956     uint32_t tag_section = tag >> 16;
957     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
958                 id != CAMERA_METADATA_INVALID_VENDOR_ID) {
959             return vendor_cache_ops->get_tag_name(tag, id);
960     } else  if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
961         return vendor_tag_ops->get_tag_name(
962             vendor_tag_ops,
963             tag);
964     }
965     if (tag_section >= ANDROID_SECTION_COUNT ||
966         tag >= camera_metadata_section_bounds[tag_section][1] ) {
967         return NULL;
968     }
969     uint32_t tag_index = tag & 0xFFFF;
970     return tag_info[tag_section][tag_index].tag_name;
971 }
972 
973 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
get_local_camera_metadata_tag_type_vendor_id(uint32_t tag,metadata_vendor_id_t id)974 int get_local_camera_metadata_tag_type_vendor_id(uint32_t tag,
975         metadata_vendor_id_t id) {
976     uint32_t tag_section = tag >> 16;
977     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
978                 id != CAMERA_METADATA_INVALID_VENDOR_ID) {
979             return vendor_cache_ops->get_tag_type(tag, id);
980     } else if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
981         return vendor_tag_ops->get_tag_type(
982             vendor_tag_ops,
983             tag);
984     }
985     if (tag_section >= ANDROID_SECTION_COUNT ||
986             tag >= camera_metadata_section_bounds[tag_section][1] ) {
987         return -1;
988     }
989     uint32_t tag_index = tag & 0xFFFF;
990     return tag_info[tag_section][tag_index].tag_type;
991 }
992 
get_camera_metadata_section_name(uint32_t tag)993 const char *get_camera_metadata_section_name(uint32_t tag) {
994     return get_local_camera_metadata_section_name(tag, NULL);
995 }
996 
get_camera_metadata_tag_name(uint32_t tag)997 const char *get_camera_metadata_tag_name(uint32_t tag) {
998     return get_local_camera_metadata_tag_name(tag, NULL);
999 }
1000 
get_camera_metadata_tag_type(uint32_t tag)1001 int get_camera_metadata_tag_type(uint32_t tag) {
1002     return get_local_camera_metadata_tag_type(tag, NULL);
1003 }
1004 
get_local_camera_metadata_section_name(uint32_t tag,const camera_metadata_t * meta)1005 const char *get_local_camera_metadata_section_name(uint32_t tag,
1006         const camera_metadata_t *meta) {
1007     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
1008             meta->vendor_id;
1009 
1010     return get_local_camera_metadata_section_name_vendor_id(tag, id);
1011 }
1012 
get_local_camera_metadata_tag_name(uint32_t tag,const camera_metadata_t * meta)1013 const char *get_local_camera_metadata_tag_name(uint32_t tag,
1014         const camera_metadata_t *meta) {
1015     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
1016             meta->vendor_id;
1017 
1018     return get_local_camera_metadata_tag_name_vendor_id(tag, id);
1019 }
1020 
get_local_camera_metadata_tag_type(uint32_t tag,const camera_metadata_t * meta)1021 int get_local_camera_metadata_tag_type(uint32_t tag,
1022         const camera_metadata_t *meta) {
1023     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
1024             meta->vendor_id;
1025 
1026     return get_local_camera_metadata_tag_type_vendor_id(tag, id);
1027 }
1028 
get_camera_metadata_permission_needed(uint32_t * tag_count)1029 const int32_t *get_camera_metadata_permission_needed(uint32_t *tag_count) {
1030     if (NULL == tag_count) {
1031         return NULL;
1032     }
1033 
1034     *tag_count = sizeof(tag_permission_needed) / sizeof(tag_permission_needed[0]);
1035     return tag_permission_needed;
1036 }
1037 
set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t * ops)1038 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t* ops) {
1039     // **DEPRECATED**
1040     (void) ops;
1041     ALOGE("%s: This function has been deprecated", __FUNCTION__);
1042     return ERROR;
1043 }
1044 
1045 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
set_camera_metadata_vendor_ops(const vendor_tag_ops_t * ops)1046 int set_camera_metadata_vendor_ops(const vendor_tag_ops_t* ops) {
1047     vendor_tag_ops = ops;
1048     return OK;
1049 }
1050 
1051 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
set_camera_metadata_vendor_cache_ops(const struct vendor_tag_cache_ops * query_cache_ops)1052 int set_camera_metadata_vendor_cache_ops(
1053         const struct vendor_tag_cache_ops *query_cache_ops) {
1054     vendor_cache_ops = query_cache_ops;
1055     return OK;
1056 }
1057 
1058 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
set_camera_metadata_vendor_id(camera_metadata_t * meta,metadata_vendor_id_t id)1059 void set_camera_metadata_vendor_id(camera_metadata_t *meta,
1060         metadata_vendor_id_t id) {
1061     if (NULL != meta) {
1062         meta->vendor_id = id;
1063     }
1064 }
1065 
1066 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
get_camera_metadata_vendor_id(const camera_metadata_t * meta)1067 metadata_vendor_id_t get_camera_metadata_vendor_id(
1068         const camera_metadata_t *meta) {
1069     metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID;
1070 
1071     if (NULL != meta) {
1072         ret = meta->vendor_id;
1073     }
1074 
1075     return ret;
1076 }
1077 
1078 static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag, int type,
1079         int count,
1080         int indentation);
1081 
dump_camera_metadata(const camera_metadata_t * metadata,int fd,int verbosity)1082 void dump_camera_metadata(const camera_metadata_t *metadata,
1083         int fd,
1084         int verbosity) {
1085     dump_indented_camera_metadata(metadata, fd, verbosity, 0);
1086 }
1087 
dump_indented_camera_metadata(const camera_metadata_t * metadata,int fd,int verbosity,int indentation)1088 void dump_indented_camera_metadata(const camera_metadata_t *metadata,
1089         int fd,
1090         int verbosity,
1091         int indentation) {
1092     if (metadata == NULL) {
1093         dprintf(fd, "%*sDumping camera metadata array: Not allocated\n",
1094                 indentation, "");
1095         return;
1096     }
1097     unsigned int i;
1098     dprintf(fd,
1099             "%*sDumping camera metadata array: %" PRIu32 " / %" PRIu32 " entries, "
1100             "%" PRIu32 " / %" PRIu32 " bytes of extra data.\n", indentation, "",
1101             metadata->entry_count, metadata->entry_capacity,
1102             metadata->data_count, metadata->data_capacity);
1103     dprintf(fd, "%*sVersion: %d, Flags: %08x\n",
1104             indentation + 2, "",
1105             metadata->version, metadata->flags);
1106     camera_metadata_buffer_entry_t *entry = get_entries(metadata);
1107     for (i=0; i < metadata->entry_count; i++, entry++) {
1108 
1109         const char *tag_name, *tag_section;
1110         tag_section = get_local_camera_metadata_section_name(entry->tag, metadata);
1111         if (tag_section == NULL) {
1112             tag_section = "unknownSection";
1113         }
1114         tag_name = get_local_camera_metadata_tag_name(entry->tag, metadata);
1115         if (tag_name == NULL) {
1116             tag_name = "unknownTag";
1117         }
1118         const char *type_name;
1119         if (entry->type >= NUM_TYPES) {
1120             type_name = "unknown";
1121         } else {
1122             type_name = camera_metadata_type_names[entry->type];
1123         }
1124         dprintf(fd, "%*s%s.%s (%05x): %s[%" PRIu32 "]\n",
1125              indentation + 2, "",
1126              tag_section,
1127              tag_name,
1128              entry->tag,
1129              type_name,
1130              entry->count);
1131 
1132         if (verbosity < 1) continue;
1133 
1134         if (entry->type >= NUM_TYPES) continue;
1135 
1136         size_t type_size = camera_metadata_type_size[entry->type];
1137         uint8_t *data_ptr;
1138         if ( type_size * entry->count > 4 ) {
1139             if (entry->data.offset >= metadata->data_count) {
1140                 ALOGE("%s: Malformed entry data offset: %" PRIu32 " (max %" PRIu32 ")",
1141                         __FUNCTION__,
1142                         entry->data.offset,
1143                         metadata->data_count);
1144                 continue;
1145             }
1146             data_ptr = get_data(metadata) + entry->data.offset;
1147         } else {
1148             data_ptr = entry->data.value;
1149         }
1150         int count = entry->count;
1151         if (verbosity < 2 && count > 16) count = 16;
1152 
1153         print_data(fd, data_ptr, entry->tag, entry->type, count, indentation);
1154     }
1155 }
1156 
print_data(int fd,const uint8_t * data_ptr,uint32_t tag,int type,int count,int indentation)1157 static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag,
1158         int type, int count, int indentation) {
1159     static int values_per_line[NUM_TYPES] = {
1160         [TYPE_BYTE]     = 16,
1161         [TYPE_INT32]    = 4,
1162         [TYPE_FLOAT]    = 8,
1163         [TYPE_INT64]    = 2,
1164         [TYPE_DOUBLE]   = 4,
1165         [TYPE_RATIONAL] = 2,
1166     };
1167     size_t type_size = camera_metadata_type_size[type];
1168     char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
1169     uint32_t value;
1170 
1171     int lines = count / values_per_line[type];
1172     if (count % values_per_line[type] != 0) lines++;
1173 
1174     int index = 0;
1175     int j, k;
1176     for (j = 0; j < lines; j++) {
1177         dprintf(fd, "%*s[", indentation + 4, "");
1178         for (k = 0;
1179              k < values_per_line[type] && count > 0;
1180              k++, count--, index += type_size) {
1181 
1182             switch (type) {
1183                 case TYPE_BYTE:
1184                     value = *(data_ptr + index);
1185                     if (camera_metadata_enum_snprint(tag,
1186                                                      value,
1187                                                      value_string_tmp,
1188                                                      sizeof(value_string_tmp))
1189                         == OK) {
1190                         dprintf(fd, "%s ", value_string_tmp);
1191                     } else {
1192                         dprintf(fd, "%hhu ",
1193                                 *(data_ptr + index));
1194                     }
1195                     break;
1196                 case TYPE_INT32:
1197                     value =
1198                             *(int32_t*)(data_ptr + index);
1199                     if (camera_metadata_enum_snprint(tag,
1200                                                      value,
1201                                                      value_string_tmp,
1202                                                      sizeof(value_string_tmp))
1203                         == OK) {
1204                         dprintf(fd, "%s ", value_string_tmp);
1205                     } else {
1206                         dprintf(fd, "%" PRId32 " ",
1207                                 *(int32_t*)(data_ptr + index));
1208                     }
1209                     break;
1210                 case TYPE_FLOAT:
1211                     dprintf(fd, "%0.8f ",
1212                             *(float*)(data_ptr + index));
1213                     break;
1214                 case TYPE_INT64:
1215                     dprintf(fd, "%" PRId64 " ",
1216                             *(int64_t*)(data_ptr + index));
1217                     break;
1218                 case TYPE_DOUBLE:
1219                     dprintf(fd, "%0.8f ",
1220                             *(double*)(data_ptr + index));
1221                     break;
1222                 case TYPE_RATIONAL: {
1223                     int32_t numerator = *(int32_t*)(data_ptr + index);
1224                     int32_t denominator = *(int32_t*)(data_ptr + index + 4);
1225                     dprintf(fd, "(%d / %d) ",
1226                             numerator, denominator);
1227                     break;
1228                 }
1229                 default:
1230                     dprintf(fd, "??? ");
1231             }
1232         }
1233         dprintf(fd, "]\n");
1234     }
1235 }
1236