halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame^] | 8 | #include "SkMD5.h" |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 9 | #include "SkMilestone.h" |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 10 | #include "SkPDFMetadata.h" |
| 11 | #include "SkPDFTypes.h" |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 12 | #include <utility> |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 13 | |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 14 | static SkString pdf_date(const SkTime::DateTime& dt) { |
| 15 | int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes); |
| 16 | char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; |
| 17 | int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; |
| 18 | timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; |
| 19 | return SkStringPrintf( |
| 20 | "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'", |
| 21 | static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth), |
| 22 | static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour), |
| 23 | static_cast<unsigned>(dt.fMinute), |
| 24 | static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours, |
| 25 | timeZoneMinutes); |
| 26 | } |
| 27 | |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 28 | #define SKPDF_STRING(X) SKPDF_STRING_IMPL(X) |
| 29 | #define SKPDF_STRING_IMPL(X) #X |
| 30 | |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 31 | SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 32 | auto dict = sk_make_sp<SkPDFDict>(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 33 | static const char* keys[] = { |
| 34 | "Title", "Author", "Subject", "Keywords", "Creator"}; |
| 35 | for (const char* key : keys) { |
| 36 | for (const SkDocument::Attribute& keyValue : fInfo) { |
| 37 | if (keyValue.fKey.equals(key)) { |
| 38 | dict->insertString(key, keyValue.fValue); |
| 39 | } |
| 40 | } |
| 41 | } |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 42 | dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 43 | if (fCreation) { |
| 44 | dict->insertString("CreationDate", pdf_date(*fCreation.get())); |
| 45 | } |
| 46 | if (fModified) { |
| 47 | dict->insertString("ModDate", pdf_date(*fModified.get())); |
| 48 | } |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 49 | return dict.release(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 50 | } |
| 51 | |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 52 | SkPDFMetadata::UUID SkPDFMetadata::uuid() const { |
| 53 | // The main requirement is for the UUID to be unique; the exact |
| 54 | // format of the data that will be hashed is not important. |
| 55 | SkMD5 md5; |
| 56 | const char uuidNamespace[] = "org.skia.pdf\n"; |
| 57 | md5.write(uuidNamespace, strlen(uuidNamespace)); |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 58 | double msec = SkTime::GetMSecs(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 59 | md5.write(&msec, sizeof(msec)); |
| 60 | SkTime::DateTime dateTime; |
| 61 | SkTime::GetDateTime(&dateTime); |
| 62 | md5.write(&dateTime, sizeof(dateTime)); |
| 63 | if (fCreation) { |
| 64 | md5.write(fCreation.get(), sizeof(fCreation)); |
| 65 | } |
| 66 | if (fModified) { |
| 67 | md5.write(fModified.get(), sizeof(fModified)); |
| 68 | } |
| 69 | for (const auto& kv : fInfo) { |
| 70 | md5.write(kv.fKey.c_str(), kv.fKey.size()); |
| 71 | md5.write("\037", 1); |
| 72 | md5.write(kv.fValue.c_str(), kv.fValue.size()); |
| 73 | md5.write("\036", 1); |
| 74 | } |
| 75 | SkMD5::Digest digest; |
| 76 | md5.finish(digest); |
| 77 | // See RFC 4122, page 6-7. |
| 78 | digest.data[6] = (digest.data[6] & 0x0F) | 0x30; |
| 79 | digest.data[8] = (digest.data[6] & 0x3F) | 0x80; |
| 80 | static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); |
| 81 | SkPDFMetadata::UUID uuid; |
| 82 | memcpy(&uuid, &digest, sizeof(digest)); |
| 83 | return uuid; |
| 84 | } |
| 85 | |
| 86 | SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) { |
| 87 | // /ID [ <81b14aafa313db63dbd6f981e49f94f4> |
| 88 | // <81b14aafa313db63dbd6f981e49f94f4> ] |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 89 | auto array = sk_make_sp<SkPDFArray>(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 90 | static_assert(sizeof(UUID) == 16, "uuid_size"); |
| 91 | array->appendString( |
| 92 | SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); |
| 93 | array->appendString( |
| 94 | SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 95 | return array.release(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Improvement on SkStringPrintf to allow for arbitrarily long output. |
| 99 | // TODO: replace SkStringPrintf. |
| 100 | static SkString sk_string_printf(const char* format, ...) { |
| 101 | #ifdef SK_BUILD_FOR_WIN |
| 102 | va_list args; |
| 103 | va_start(args, format); |
| 104 | char buffer[1024]; |
| 105 | int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args); |
| 106 | va_end(args); |
| 107 | if (length >= 0 && length < (int)sizeof(buffer)) { |
| 108 | return SkString(buffer, length); |
| 109 | } |
| 110 | va_start(args, format); |
| 111 | length = _vscprintf(format, args); |
| 112 | va_end(args); |
| 113 | |
| 114 | SkString string((size_t)length); |
| 115 | va_start(args, format); |
| 116 | SkDEBUGCODE(int check = ) _vsnprintf_s(string.writable_str(), length + 1, |
| 117 | _TRUNCATE, format, args); |
| 118 | va_end(args); |
| 119 | SkASSERT(check == length); |
| 120 | SkASSERT(string[length] == '\0'); |
halcanary | 050ab5d | 2016-04-08 09:55:20 -0700 | [diff] [blame] | 121 | return string; |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 122 | #else // C99/C++11 standard vsnprintf |
| 123 | // TODO: When all compilers support this, remove windows-specific code. |
| 124 | va_list args; |
| 125 | va_start(args, format); |
| 126 | char buffer[1024]; |
| 127 | int length = vsnprintf(buffer, sizeof(buffer), format, args); |
| 128 | va_end(args); |
| 129 | if (length < 0) { |
| 130 | return SkString(); |
| 131 | } |
| 132 | if (length < (int)sizeof(buffer)) { |
| 133 | return SkString(buffer, length); |
| 134 | } |
| 135 | SkString string((size_t)length); |
| 136 | va_start(args, format); |
| 137 | SkDEBUGCODE(int check = ) |
| 138 | vsnprintf(string.writable_str(), length + 1, format, args); |
| 139 | va_end(args); |
| 140 | SkASSERT(check == length); |
| 141 | SkASSERT(string[length] == '\0'); |
halcanary | 050ab5d | 2016-04-08 09:55:20 -0700 | [diff] [blame] | 142 | return string; |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | static const SkString get(const SkTArray<SkDocument::Attribute>& info, |
| 147 | const char* key) { |
| 148 | for (const auto& keyValue : info) { |
| 149 | if (keyValue.fKey.equals(key)) { |
| 150 | return keyValue.fValue; |
| 151 | } |
| 152 | } |
| 153 | return SkString(); |
| 154 | } |
| 155 | |
| 156 | #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \ |
| 157 | do { \ |
| 158 | for (int i = 0; i < (BYTE_COUNT); ++i) { \ |
| 159 | uint8_t value = *(INPUT_PTR)++; \ |
| 160 | *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \ |
| 161 | *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \ |
| 162 | } \ |
| 163 | } while (false) |
| 164 | static SkString uuid_to_string(const SkPDFMetadata::UUID& uuid) { |
| 165 | // 8-4-4-4-12 |
| 166 | char buffer[36]; // [32 + 4] |
| 167 | static const char gHex[] = "0123456789abcdef"; |
| 168 | SkASSERT(strlen(gHex) == 16); |
| 169 | char* ptr = buffer; |
| 170 | const uint8_t* data = uuid.fData; |
| 171 | HEXIFY(data, ptr, gHex, 4); |
| 172 | *ptr++ = '-'; |
| 173 | HEXIFY(data, ptr, gHex, 2); |
| 174 | *ptr++ = '-'; |
| 175 | HEXIFY(data, ptr, gHex, 2); |
| 176 | *ptr++ = '-'; |
| 177 | HEXIFY(data, ptr, gHex, 2); |
| 178 | *ptr++ = '-'; |
| 179 | HEXIFY(data, ptr, gHex, 6); |
| 180 | SkASSERT(ptr == buffer + 36); |
| 181 | SkASSERT(data == uuid.fData + 16); |
| 182 | return SkString(buffer, 36); |
| 183 | } |
| 184 | #undef HEXIFY |
| 185 | |
| 186 | namespace { |
halcanary | 70d1554 | 2015-11-22 12:55:04 -0800 | [diff] [blame] | 187 | class PDFXMLObject final : public SkPDFObject { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 188 | public: |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 189 | PDFXMLObject(SkString xml) : fXML(std::move(xml)) {} |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 190 | void emitObject(SkWStream* stream, |
| 191 | const SkPDFObjNumMap& omap, |
| 192 | const SkPDFSubstituteMap& smap) const override { |
| 193 | SkPDFDict dict("Metadata"); |
| 194 | dict.insertName("Subtype", "XML"); |
| 195 | dict.insertInt("Length", fXML.size()); |
| 196 | dict.emitObject(stream, omap, smap); |
| 197 | static const char streamBegin[] = " stream\n"; |
| 198 | stream->write(streamBegin, strlen(streamBegin)); |
| 199 | // Do not compress this. The standard requires that a |
| 200 | // program that does not understand PDF can grep for |
| 201 | // "<?xpacket" and extracť the entire XML. |
| 202 | stream->write(fXML.c_str(), fXML.size()); |
| 203 | static const char streamEnd[] = "\nendstream"; |
| 204 | stream->write(streamEnd, strlen(streamEnd)); |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | const SkString fXML; |
| 209 | }; |
| 210 | } // namespace |
| 211 | |
| 212 | static int count_xml_escape_size(const SkString& input) { |
| 213 | int extra = 0; |
| 214 | for (size_t i = 0; i < input.size(); ++i) { |
| 215 | if (input[i] == '&') { |
| 216 | extra += 4; // strlen("&") - strlen("&") |
| 217 | } else if (input[i] == '<') { |
| 218 | extra += 3; // strlen("<") - strlen("<") |
| 219 | } |
| 220 | } |
| 221 | return extra; |
| 222 | } |
| 223 | |
| 224 | const SkString escape_xml(const SkString& input, |
| 225 | const char* before = nullptr, |
| 226 | const char* after = nullptr) { |
| 227 | if (input.size() == 0) { |
| 228 | return input; |
| 229 | } |
| 230 | // "&" --> "&" and "<" --> "<" |
| 231 | // text is assumed to be in UTF-8 |
| 232 | // all strings are xml content, not attribute values. |
| 233 | size_t beforeLen = before ? strlen(before) : 0; |
| 234 | size_t afterLen = after ? strlen(after) : 0; |
| 235 | int extra = count_xml_escape_size(input); |
| 236 | SkString output(input.size() + extra + beforeLen + afterLen); |
| 237 | char* out = output.writable_str(); |
| 238 | if (before) { |
| 239 | strncpy(out, before, beforeLen); |
| 240 | out += beforeLen; |
| 241 | } |
| 242 | static const char kAmp[] = "&"; |
| 243 | static const char kLt[] = "<"; |
| 244 | for (size_t i = 0; i < input.size(); ++i) { |
| 245 | if (input[i] == '&') { |
| 246 | strncpy(out, kAmp, strlen(kAmp)); |
| 247 | out += strlen(kAmp); |
| 248 | } else if (input[i] == '<') { |
| 249 | strncpy(out, kLt, strlen(kLt)); |
| 250 | out += strlen(kLt); |
| 251 | } else { |
| 252 | *out++ = input[i]; |
| 253 | } |
| 254 | } |
| 255 | if (after) { |
| 256 | strncpy(out, after, afterLen); |
| 257 | out += afterLen; |
| 258 | } |
| 259 | // Validate that we haven't written outside of our string. |
| 260 | SkASSERT(out == &output.writable_str()[output.size()]); |
| 261 | *out = '\0'; |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 262 | return output; |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc, |
| 266 | const UUID& instance) const { |
| 267 | static const char templateString[] = |
| 268 | "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" |
| 269 | "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" |
| 270 | " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " |
| 271 | "2012/08/23-13:03:03\">\n" |
| 272 | "<rdf:RDF " |
| 273 | "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" |
| 274 | "<rdf:Description rdf:about=\"\"\n" |
| 275 | " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" |
| 276 | " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" |
| 277 | " xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n" |
| 278 | " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"\n" |
| 279 | " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" |
| 280 | "<pdfaid:part>2</pdfaid:part>\n" |
| 281 | "<pdfaid:conformance>B</pdfaid:conformance>\n" |
| 282 | "%s" // ModifyDate |
| 283 | "%s" // CreateDate |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 284 | "%s" // xmp:CreatorTool |
| 285 | "<dc:format>application/pdf</dc:format>\n" |
| 286 | "%s" // dc:title |
| 287 | "%s" // dc:description |
| 288 | "%s" // author |
| 289 | "%s" // keywords |
| 290 | "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n" |
| 291 | "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" |
halcanary | 8cd4a24 | 2016-04-07 08:56:15 -0700 | [diff] [blame] | 292 | "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Producer>\n" |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 293 | "%s" // pdf:Keywords |
| 294 | "</rdf:Description>\n" |
| 295 | "</rdf:RDF>\n" |
| 296 | "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. |
| 297 | "<?xpacket end=\"w\"?>\n"; |
| 298 | |
| 299 | SkString creationDate; |
| 300 | SkString modificationDate; |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 301 | if (fCreation) { |
| 302 | SkString tmp; |
| 303 | fCreation->toISO8601(&tmp); |
| 304 | SkASSERT(0 == count_xml_escape_size(tmp)); |
| 305 | // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape |
| 306 | creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n", |
| 307 | tmp.c_str()); |
| 308 | } |
| 309 | if (fModified) { |
| 310 | SkString tmp; |
| 311 | fModified->toISO8601(&tmp); |
| 312 | SkASSERT(0 == count_xml_escape_size(tmp)); |
| 313 | modificationDate = sk_string_printf( |
| 314 | "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 315 | } |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 316 | SkString title = escape_xml( |
| 317 | get(fInfo, "Title"), |
| 318 | "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 319 | "</rdf:li></rdf:Alt></dc:title>\n"); |
| 320 | SkString author = escape_xml( |
| 321 | get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", |
| 322 | "</rdf:li></rdf:Bag></dc:creator>\n"); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 323 | // TODO: in theory, XMP can support multiple authors. Split on a delimiter? |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 324 | SkString subject = escape_xml( |
| 325 | get(fInfo, "Subject"), |
| 326 | "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 327 | "</rdf:li></rdf:Alt></dc:description>\n"); |
| 328 | SkString keywords1 = escape_xml( |
| 329 | get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", |
| 330 | "</rdf:li></rdf:Bag></dc:subject>\n"); |
| 331 | SkString keywords2 = escape_xml( |
| 332 | get(fInfo, "Keywords"), "<pdf:Keywords>", |
| 333 | "</pdf:Keywords>\n"); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 334 | |
| 335 | // TODO: in theory, keywords can be a list too. |
| 336 | SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", |
| 337 | "</xmp:CreatorTool>\n"); |
| 338 | SkString documentID = uuid_to_string(doc); // no need to escape |
| 339 | SkASSERT(0 == count_xml_escape_size(documentID)); |
| 340 | SkString instanceID = uuid_to_string(instance); |
| 341 | SkASSERT(0 == count_xml_escape_size(instanceID)); |
| 342 | return new PDFXMLObject(sk_string_printf( |
| 343 | templateString, modificationDate.c_str(), creationDate.c_str(), |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 344 | creator.c_str(), title.c_str(), |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 345 | subject.c_str(), author.c_str(), keywords1.c_str(), |
| 346 | documentID.c_str(), instanceID.c_str(), keywords2.c_str())); |
| 347 | } |
| 348 | |
halcanary | 8cd4a24 | 2016-04-07 08:56:15 -0700 | [diff] [blame] | 349 | #undef SKPDF_STRING |
| 350 | #undef SKPDF_STRING_IMPL |
| 351 | |