blob: 2765d4d189b99b0ae0042b0ff531f12797762370 [file] [log] [blame]
halcanary34422612015-10-12 10:11:18 -07001/*
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
halcanary488165e2016-04-22 06:10:21 -07008#include "SkMD5.h"
halcanaryffe54002016-03-29 09:09:29 -07009#include "SkMilestone.h"
halcanary34422612015-10-12 10:11:18 -070010#include "SkPDFMetadata.h"
11#include "SkPDFTypes.h"
bungeman221524d2016-01-05 14:59:40 -080012#include <utility>
halcanary34422612015-10-12 10:11:18 -070013
halcanary34422612015-10-12 10:11:18 -070014static 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
halcanaryffe54002016-03-29 09:09:29 -070028#define SKPDF_STRING(X) SKPDF_STRING_IMPL(X)
29#define SKPDF_STRING_IMPL(X) #X
30
halcanary34422612015-10-12 10:11:18 -070031SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const {
halcanaryece83922016-03-08 08:32:12 -080032 auto dict = sk_make_sp<SkPDFDict>();
halcanary34422612015-10-12 10:11:18 -070033 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 }
halcanaryffe54002016-03-29 09:09:29 -070042 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE));
halcanary34422612015-10-12 10:11:18 -070043 if (fCreation) {
44 dict->insertString("CreationDate", pdf_date(*fCreation.get()));
45 }
46 if (fModified) {
47 dict->insertString("ModDate", pdf_date(*fModified.get()));
48 }
halcanaryfcad44b2016-03-06 14:47:10 -080049 return dict.release();
halcanary34422612015-10-12 10:11:18 -070050}
51
halcanary34422612015-10-12 10:11:18 -070052SkPDFMetadata::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));
benjaminwagnerec4d4d72016-03-25 12:59:53 -070058 double msec = SkTime::GetMSecs();
halcanary34422612015-10-12 10:11:18 -070059 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
86SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) {
87 // /ID [ <81b14aafa313db63dbd6f981e49f94f4>
88 // <81b14aafa313db63dbd6f981e49f94f4> ]
halcanaryece83922016-03-08 08:32:12 -080089 auto array = sk_make_sp<SkPDFArray>();
halcanary34422612015-10-12 10:11:18 -070090 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)));
halcanaryfcad44b2016-03-06 14:47:10 -080095 return array.release();
halcanary34422612015-10-12 10:11:18 -070096}
97
98// Improvement on SkStringPrintf to allow for arbitrarily long output.
99// TODO: replace SkStringPrintf.
100static 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');
halcanary050ab5d2016-04-08 09:55:20 -0700121 return string;
halcanary34422612015-10-12 10:11:18 -0700122#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');
halcanary050ab5d2016-04-08 09:55:20 -0700142 return string;
halcanary34422612015-10-12 10:11:18 -0700143#endif
144}
145
146static 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)
164static 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
186namespace {
halcanary70d15542015-11-22 12:55:04 -0800187class PDFXMLObject final : public SkPDFObject {
halcanary34422612015-10-12 10:11:18 -0700188public:
bungeman221524d2016-01-05 14:59:40 -0800189 PDFXMLObject(SkString xml) : fXML(std::move(xml)) {}
halcanary34422612015-10-12 10:11:18 -0700190 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
207private:
208 const SkString fXML;
209};
210} // namespace
211
212static 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("&amp;") - strlen("&")
217 } else if (input[i] == '<') {
218 extra += 3; // strlen("&lt;") - strlen("<")
219 }
220 }
221 return extra;
222}
223
224const 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 // "&" --> "&amp;" and "<" --> "&lt;"
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[] = "&amp;";
243 static const char kLt[] = "&lt;";
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';
halcanary78daeff2016-04-07 12:34:59 -0700262 return output;
halcanary34422612015-10-12 10:11:18 -0700263}
264
265SkPDFObject* 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
halcanary34422612015-10-12 10:11:18 -0700284 "%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"
halcanary8cd4a242016-04-07 08:56:15 -0700292 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Producer>\n"
halcanary34422612015-10-12 10:11:18 -0700293 "%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;
halcanary34422612015-10-12 10:11:18 -0700301 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());
halcanary34422612015-10-12 10:11:18 -0700315 }
halcanary78daeff2016-04-07 12:34:59 -0700316 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");
halcanary34422612015-10-12 10:11:18 -0700323 // TODO: in theory, XMP can support multiple authors. Split on a delimiter?
halcanary78daeff2016-04-07 12:34:59 -0700324 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");
halcanary34422612015-10-12 10:11:18 -0700334
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(),
halcanary78daeff2016-04-07 12:34:59 -0700344 creator.c_str(), title.c_str(),
halcanary34422612015-10-12 10:11:18 -0700345 subject.c_str(), author.c_str(), keywords1.c_str(),
346 documentID.c_str(), instanceID.c_str(), keywords2.c_str()));
347}
348
halcanary8cd4a242016-04-07 08:56:15 -0700349#undef SKPDF_STRING
350#undef SKPDF_STRING_IMPL
351