Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: src/pdf/SkPDFMetadata.cpp

Issue 1403803002: SkStringPrintf and SkString::printf now are no longer limted by a static buffer (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-10-12 (Monday) 15:52:58 EDT Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/core/SkString.cpp ('K') | « src/core/SkString.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkPDFMetadata.h" 8 #include "SkPDFMetadata.h"
9 #include "SkPDFTypes.h" 9 #include "SkPDFTypes.h"
10 10
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 // <81b14aafa313db63dbd6f981e49f94f4> ] 87 // <81b14aafa313db63dbd6f981e49f94f4> ]
88 SkAutoTUnref<SkPDFArray> array(new SkPDFArray); 88 SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
89 static_assert(sizeof(UUID) == 16, "uuid_size"); 89 static_assert(sizeof(UUID) == 16, "uuid_size");
90 array->appendString( 90 array->appendString(
91 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); 91 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID)));
92 array->appendString( 92 array->appendString(
93 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); 93 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID)));
94 return array.detach(); 94 return array.detach();
95 } 95 }
96 96
97 // Improvement on SkStringPrintf to allow for arbitrarily long output.
98 // TODO: replace SkStringPrintf.
99 static SkString sk_string_printf(const char* format, ...) {
100 #ifdef SK_BUILD_FOR_WIN
101 va_list args;
102 va_start(args, format);
103 char buffer[1024];
104 int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args);
105 va_end(args);
106 if (length >= 0 && length < (int)sizeof(buffer)) {
107 return SkString(buffer, length);
108 }
109 va_start(args, format);
110 length = _vscprintf(format, args);
111 va_end(args);
112
113 SkString string((size_t)length);
114 va_start(args, format);
115 SkDEBUGCODE(int check = ) _vsnprintf_s(string.writable_str(), length + 1,
116 _TRUNCATE, format, args);
117 va_end(args);
118 SkASSERT(check == length);
119 SkASSERT(string[length] == '\0');
120 return skstd::move(string);
121 #else // C99/C++11 standard vsnprintf
122 // TODO: When all compilers support this, remove windows-specific code.
123 va_list args;
124 va_start(args, format);
125 char buffer[1024];
126 int length = vsnprintf(buffer, sizeof(buffer), format, args);
127 va_end(args);
128 if (length < 0) {
129 return SkString();
130 }
131 if (length < (int)sizeof(buffer)) {
132 return SkString(buffer, length);
133 }
134 SkString string((size_t)length);
135 va_start(args, format);
136 SkDEBUGCODE(int check = )
137 vsnprintf(string.writable_str(), length + 1, format, args);
138 va_end(args);
139 SkASSERT(check == length);
140 SkASSERT(string[length] == '\0');
141 return skstd::move(string);
142 #endif
143 }
144
145 static const SkString get(const SkTArray<SkDocument::Attribute>& info, 97 static const SkString get(const SkTArray<SkDocument::Attribute>& info,
146 const char* key) { 98 const char* key) {
147 for (const auto& keyValue : info) { 99 for (const auto& keyValue : info) {
148 if (keyValue.fKey.equals(key)) { 100 if (keyValue.fKey.equals(key)) {
149 return keyValue.fValue; 101 return keyValue.fValue;
150 } 102 }
151 } 103 }
152 return SkString(); 104 return SkString();
153 } 105 }
154 106
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 "<?xpacket end=\"w\"?>\n"; 249 "<?xpacket end=\"w\"?>\n";
298 250
299 SkString creationDate; 251 SkString creationDate;
300 SkString modificationDate; 252 SkString modificationDate;
301 SkString metadataDate; 253 SkString metadataDate;
302 if (fCreation) { 254 if (fCreation) {
303 SkString tmp; 255 SkString tmp;
304 fCreation->toISO8601(&tmp); 256 fCreation->toISO8601(&tmp);
305 SkASSERT(0 == count_xml_escape_size(tmp)); 257 SkASSERT(0 == count_xml_escape_size(tmp));
306 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape 258 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape
307 creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n", 259 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n",
308 tmp.c_str()); 260 tmp.c_str());
309 } 261 }
310 if (fModified) { 262 if (fModified) {
311 SkString tmp; 263 SkString tmp;
312 fModified->toISO8601(&tmp); 264 fModified->toISO8601(&tmp);
313 SkASSERT(0 == count_xml_escape_size(tmp)); 265 SkASSERT(0 == count_xml_escape_size(tmp));
314 modificationDate = sk_string_printf( 266 modificationDate = SkStringPrintf(
315 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); 267 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str());
316 metadataDate = sk_string_printf( 268 metadataDate = SkStringPrintf(
317 "<xmp:MetadataDate>%s</xmp:MetadataDate>\n", tmp.c_str()); 269 "<xmp:MetadataDate>%s</xmp:MetadataDate>\n", tmp.c_str());
318 } 270 }
319 271
320 SkString title = 272 SkString title =
321 escape_xml(get(fInfo, "Title"), "<dc:title><rdf:Alt><rdf:li>", 273 escape_xml(get(fInfo, "Title"), "<dc:title><rdf:Alt><rdf:li>",
322 "</rdf:li></rdf:Alt></dc:title>\n"); 274 "</rdf:li></rdf:Alt></dc:title>\n");
323 SkString author = 275 SkString author =
324 escape_xml(get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", 276 escape_xml(get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>",
325 "</rdf:li></rdf:Bag></dc:creator>\n"); 277 "</rdf:li></rdf:Bag></dc:creator>\n");
326 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? 278 // TODO: in theory, XMP can support multiple authors. Split on a delimiter?
327 SkString subject = escape_xml(get(fInfo, "Subject"), 279 SkString subject = escape_xml(get(fInfo, "Subject"),
328 "<dc:description><rdf:Alt><rdf:li>", 280 "<dc:description><rdf:Alt><rdf:li>",
329 "</rdf:li></rdf:Alt></dc:description>\n"); 281 "</rdf:li></rdf:Alt></dc:description>\n");
330 SkString keywords1 = 282 SkString keywords1 =
331 escape_xml(get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", 283 escape_xml(get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>",
332 "</rdf:li></rdf:Bag></dc:subject>\n"); 284 "</rdf:li></rdf:Bag></dc:subject>\n");
333 SkString keywords2 = escape_xml(get(fInfo, "Keywords"), "<pdf:Keywords>", 285 SkString keywords2 = escape_xml(get(fInfo, "Keywords"), "<pdf:Keywords>",
334 "</pdf:Keywords>\n"); 286 "</pdf:Keywords>\n");
335 287
336 // TODO: in theory, keywords can be a list too. 288 // TODO: in theory, keywords can be a list too.
337 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", 289 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>",
338 "</xmp:CreatorTool>\n"); 290 "</xmp:CreatorTool>\n");
339 SkString documentID = uuid_to_string(doc); // no need to escape 291 SkString documentID = uuid_to_string(doc); // no need to escape
340 SkASSERT(0 == count_xml_escape_size(documentID)); 292 SkASSERT(0 == count_xml_escape_size(documentID));
341 SkString instanceID = uuid_to_string(instance); 293 SkString instanceID = uuid_to_string(instance);
342 SkASSERT(0 == count_xml_escape_size(instanceID)); 294 SkASSERT(0 == count_xml_escape_size(instanceID));
343 return new PDFXMLObject(sk_string_printf( 295 return new PDFXMLObject(SkStringPrintf(
344 templateString, modificationDate.c_str(), creationDate.c_str(), 296 templateString, modificationDate.c_str(), creationDate.c_str(),
345 metadataDate.c_str(), creator.c_str(), title.c_str(), 297 metadataDate.c_str(), creator.c_str(), title.c_str(),
346 subject.c_str(), author.c_str(), keywords1.c_str(), 298 subject.c_str(), author.c_str(), keywords1.c_str(),
347 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); 299 documentID.c_str(), instanceID.c_str(), keywords2.c_str()));
348 } 300 }
349 301
350 #endif // SK_PDF_GENERATE_PDFA 302 #endif // SK_PDF_GENERATE_PDFA
OLDNEW
« src/core/SkString.cpp ('K') | « src/core/SkString.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698