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