summaryrefslogtreecommitdiff
path: root/internal/xmpmeta/xmp_parser.cc
blob: 6d08032cbb35cb0db898b30838a7d39a192ce117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "xmpmeta/xmp_parser.h"

#include <algorithm>
#include <cassert>
#include <cstring>
#include <sstream>
#include <stack>

#include "android-base/logging.h"
#include "strings/case.h"
#include "strings/numbers.h"
#include "xmpmeta/base64.h"
#include "xmpmeta/jpeg_io.h"
#include "xmpmeta/xml/const.h"
#include "xmpmeta/xml/deserializer_impl.h"
#include "xmpmeta/xml/search.h"
#include "xmpmeta/xml/utils.h"
#include "xmpmeta/xmp_const.h"

using ::dynamic_depth::xmpmeta::xml::DepthFirstSearch;
using ::dynamic_depth::xmpmeta::xml::DeserializerImpl;
using ::dynamic_depth::xmpmeta::xml::FromXmlChar;
using ::dynamic_depth::xmpmeta::xml::GetFirstDescriptionElement;

namespace dynamic_depth {
namespace xmpmeta {
namespace {

const char kJpgExtension[] = "jpg";
const char kJpegExtension[] = "jpeg";

bool BoolStringToBool(const string& bool_str, bool* value) {
  if (dynamic_depth::StringCaseEqual(bool_str, "true")) {
    *value = true;
    return true;
  }
  if (dynamic_depth::StringCaseEqual(bool_str, "false")) {
    *value = false;
    return true;
  }
  return false;
}

// Converts string_property to the type T.
template <typename T>
bool ConvertStringPropertyToType(const string& string_property, T* value);

// Gets the end of the XMP meta content. If there is no packet wrapper, returns
// data.length, otherwise returns 1 + the position of last '>' without '?'
// before it. Usually the packet wrapper end is "<?xpacket end="w"?>.
size_t GetXmpContentEnd(const string& data) {
  if (data.empty()) {
    return 0;
  }
  for (size_t i = data.size() - 1; i >= 1; --i) {
    if (data[i] == '>') {
      if (data[i - 1] != '?') {
        return i + 1;
      }
    }
  }
  // It should not reach here for a valid XMP meta.
  LOG(WARNING) << "Failed to find the end of the XMP meta content.";
  return data.size();
}

// True if 's' starts with substring 'x'.
bool StartsWith(const string& s, const string& x) {
  return s.size() >= x.size() && !s.compare(0, x.size(), x);
}
// True if 's' ends with substring 'x'.
bool EndsWith(const string& s, const string& x) {
  return s.size() >= x.size() && !s.compare(s.size() - x.size(), x.size(), x);
}

// Parses the first valid XMP section. Any other valid XMP section will be
// ignored.
bool ParseFirstValidXMPSection(const std::vector<Section>& sections,
                               XmpData* xmp) {
  for (const Section& section : sections) {
    if (StartsWith(section.data, XmpConst::Header())) {
      const size_t end = GetXmpContentEnd(section.data);
      // Increment header length by 1 for the null termination.
      const size_t header_length = strlen(XmpConst::Header()) + 1;
      // Check for integer underflow before subtracting.
      if (header_length >= end) {
        LOG(ERROR) << "Invalid content length: "
                   << static_cast<int>(end - header_length);
        return false;
      }
      const size_t content_length = end - header_length;
      // header_length is guaranteed to be <= data.size due to the if condition
      // above. If this contract changes we must add an additonal check.
      const char* content_start = &section.data[header_length];
      // xmlReadMemory requires an int. Before casting size_t to int we must
      // check for integer overflow.
      if (content_length > INT_MAX) {
        LOG(ERROR) << "First XMP section too large, size: " << content_length;
        return false;
      }
      *xmp->MutableStandardSection() = xmlReadMemory(
          content_start, static_cast<int>(content_length), nullptr, nullptr, 0);
      if (xmp->StandardSection() == nullptr) {
        LOG(WARNING) << "Failed to parse standard section.";
        return false;
      }
      return true;
    }
  }
  return false;
}

// Collects the extended XMP sections with the given name into a string. Other
// sections will be ignored.
string GetExtendedXmpSections(const std::vector<Section>& sections,
                              const string& section_name) {
  string extended_header = XmpConst::ExtensionHeader();
  extended_header += '\0' + section_name;
  // section_name is dynamically extracted from the xml file and can have an
  // arbitrary size. Check for integer overflow before addition.
  if (extended_header.size() > SIZE_MAX - XmpConst::ExtensionHeaderOffset()) {
    return "";
  }
  const size_t section_start_offset =
      extended_header.size() + XmpConst::ExtensionHeaderOffset();

  // Compute the size of the buffer to parse the extended sections.
  std::vector<const Section*> xmp_sections;
  std::vector<size_t> xmp_end_offsets;
  size_t buffer_size = 0;
  for (const Section& section : sections) {
    if (extended_header.empty() || StartsWith(section.data, extended_header)) {
      const size_t end_offset = section.data.size();
      const size_t section_size = end_offset - section_start_offset;
      if (end_offset < section_start_offset ||
          section_size > SIZE_MAX - buffer_size) {
        return "";
      }
      buffer_size += section_size;
      xmp_sections.push_back(&section);
      xmp_end_offsets.push_back(end_offset);
    }
  }

  // Copy all the relevant sections' data into a buffer.
  string buffer(buffer_size, '\0');
  if (buffer.size() != buffer_size) {
    return "";
  }
  size_t offset = 0;
  for (int i = 0; i < xmp_sections.size(); ++i) {
    const Section* section = xmp_sections[i];
    const size_t length = xmp_end_offsets[i] - section_start_offset;
    std::copy_n(&section->data[section_start_offset], length, &buffer[offset]);
    offset += length;
  }
  return buffer;
}

// Parses the extended XMP sections with the given name. All other sections
// will be ignored.
bool ParseExtendedXmpSections(const std::vector<Section>& sections,
                              const string& section_name, XmpData* xmp_data) {
  const string extended_sections =
      GetExtendedXmpSections(sections, section_name);
  // xmlReadMemory requires an int. Before casting size_t to int we must check
  // for integer overflow.
  if (extended_sections.size() > INT_MAX) {
    LOG(WARNING) << "Extended sections too large, size: "
                 << extended_sections.size();
    return false;
  }
  *xmp_data->MutableExtendedSection() = xmlReadMemory(
      extended_sections.data(), static_cast<int>(extended_sections.size()),
      nullptr, nullptr, XML_PARSE_HUGE);
  if (xmp_data->ExtendedSection() == nullptr) {
    LOG(WARNING) << "Failed to parse extended sections.";
    return false;
  }
  return true;
}

// Extracts a XmpData from a JPEG image stream.
bool ExtractXmpMeta(const bool skip_extended, std::istream* file,
                    XmpData* xmp_data) {
  // We cannot use CHECK because this is ported to AOSP.
  assert(xmp_data != nullptr);  // NOLINT
  xmp_data->Reset();

  ParseOptions parse_options;
  parse_options.read_meta_only = true;
  if (skip_extended) {
    parse_options.section_header = XmpConst::Header();
    parse_options.section_header_return_first = true;
  }
  const std::vector<Section> sections = Parse(parse_options, file);
  if (sections.empty()) {
    LOG(WARNING) << "No sections found.";
    return false;
  }

  if (!ParseFirstValidXMPSection(sections, xmp_data)) {
    LOG(WARNING) << "Could not parse first section.";
    return false;
  }
  if (skip_extended) {
    return true;
  }
  string extension_name;
  DeserializerImpl deserializer(
      GetFirstDescriptionElement(xmp_data->StandardSection()));
  if (!deserializer.ParseString(XmpConst::HasExtensionPrefix(),
                                XmpConst::HasExtension(), &extension_name)) {
    // No extended sections present, so nothing to parse.
    return true;
  }
  if (!ParseExtendedXmpSections(sections, extension_name, xmp_data)) {
    LOG(WARNING) << "Extended sections present, but could not be parsed.";
    return false;
  }
  return true;
}

// Extracts the specified string attribute.
bool GetStringProperty(const xmlNodePtr node, const char* prefix,
                       const char* property, string* value) {
  const xmlDocPtr doc = node->doc;
  for (const _xmlAttr* attribute = node->properties; attribute != nullptr;
       attribute = attribute->next) {
    if (attribute->ns &&
        strcmp(FromXmlChar(attribute->ns->prefix), prefix) == 0 &&
        strcmp(FromXmlChar(attribute->name), property) == 0) {
      xmlChar* attribute_string =
          xmlNodeListGetString(doc, attribute->children, 1);
      *value = FromXmlChar(attribute_string);
      xmlFree(attribute_string);
      return true;
    }
  }
  return false;
}

// Reads the contents of a node.
// E.g. <prefix:node_name>Contents Here</prefix:node_name>
bool ReadNodeContent(const xmlNodePtr node, const char* prefix,
                     const char* node_name, string* value) {
  auto* element = DepthFirstSearch(node, node_name);
  if (element == nullptr) {
    return false;
  }
  if (prefix != nullptr &&
      (element->ns == nullptr || element->ns->prefix == nullptr ||
       strcmp(FromXmlChar(element->ns->prefix), prefix) != 0)) {
    return false;
  }
  xmlChar* node_content = xmlNodeGetContent(element);
  *value = FromXmlChar(node_content);
  free(node_content);
  return true;
}

template <typename T>
bool ConvertStringPropertyToType(const string& string_property, T* value) {
  QCHECK(value) << "Cannot call this method on a generic type";
  return false;
}

template <>
bool ConvertStringPropertyToType<bool>(const string& string_property,
                                       bool* value) {
  return BoolStringToBool(string_property, value);
}

template <>
bool ConvertStringPropertyToType<double>(const string& string_property,
                                         double* value) {
  *value = std::stod(string_property);
  return true;
}

template <>
bool ConvertStringPropertyToType<int>(const string& string_property,
                                      int* value) {
  *value = 0;
  for (int i = 0; i < string_property.size(); ++i) {
    if (!isdigit(string_property[i])) {
      return false;
    }
  }

  *value = std::atoi(string_property.c_str());  // NOLINT
  return true;
}

template <>
bool ConvertStringPropertyToType<int64>(const string& string_property,
                                        int64* value) {
  *value = std::stol(string_property);
  return true;
}

}  // namespace

bool ReadXmpHeader(const string& filename, const bool skip_extended,
                   XmpData* xmp_data) {
  string filename_lower = filename;
  std::transform(filename_lower.begin(), filename_lower.end(),
                 filename_lower.begin(), ::tolower);
  if (!EndsWith(filename_lower, kJpgExtension) &&
      !EndsWith(filename_lower, kJpegExtension)) {
    LOG(WARNING) << "XMP parse: only JPEG file is supported";
    return false;
  }

  std::ifstream file(filename.c_str(), std::ios::binary);
  if (!file.is_open()) {
    LOG(WARNING) << " Could not read file: " << filename;
    return false;
  }
  return ExtractXmpMeta(skip_extended, &file, xmp_data);
}

bool ReadXmpFromMemory(const string& jpeg_contents, const bool skip_extended,
                       XmpData* xmp_data) {
  std::istringstream stream(jpeg_contents);
  return ExtractXmpMeta(skip_extended, &stream, xmp_data);
}

bool ReadXmpHeader(std::istream* input_stream, bool skip_extended,
                   XmpData* xmp_data) {
  return ExtractXmpMeta(skip_extended, input_stream, xmp_data);
}

}  // namespace xmpmeta
}  // namespace dynamic_depth