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

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: 2016-04-25 (Monday) 11:33:51 EDT Created 4 years, 7 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
« no previous file with comments | « src/core/SkString.cpp ('k') | tests/StringTest.cpp » ('j') | 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 "SkMD5.h" 8 #include "SkMD5.h"
9 #include "SkMilestone.h" 9 #include "SkMilestone.h"
10 #include "SkPDFMetadata.h" 10 #include "SkPDFMetadata.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // <81b14aafa313db63dbd6f981e49f94f4> ] 88 // <81b14aafa313db63dbd6f981e49f94f4> ]
89 auto array = sk_make_sp<SkPDFArray>(); 89 auto array = sk_make_sp<SkPDFArray>();
90 static_assert(sizeof(UUID) == 16, "uuid_size"); 90 static_assert(sizeof(UUID) == 16, "uuid_size");
91 array->appendString( 91 array->appendString(
92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); 92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID)));
93 array->appendString( 93 array->appendString(
94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); 94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID)));
95 return array.release(); 95 return array.release();
96 } 96 }
97 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');
121 return string;
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');
142 return string;
143 #endif
144 }
145
146 static const SkString get(const SkTArray<SkDocument::Attribute>& info, 98 static const SkString get(const SkTArray<SkDocument::Attribute>& info,
147 const char* key) { 99 const char* key) {
148 for (const auto& keyValue : info) { 100 for (const auto& keyValue : info) {
149 if (keyValue.fKey.equals(key)) { 101 if (keyValue.fKey.equals(key)) {
150 return keyValue.fValue; 102 return keyValue.fValue;
151 } 103 }
152 } 104 }
153 return SkString(); 105 return SkString();
154 } 106 }
155 107
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. 248 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding.
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 if (fCreation) { 253 if (fCreation) {
302 SkString tmp; 254 SkString tmp;
303 fCreation->toISO8601(&tmp); 255 fCreation->toISO8601(&tmp);
304 SkASSERT(0 == count_xml_escape_size(tmp)); 256 SkASSERT(0 == count_xml_escape_size(tmp));
305 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape 257 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape
306 creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n", 258 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n",
307 tmp.c_str()); 259 tmp.c_str());
308 } 260 }
309 if (fModified) { 261 if (fModified) {
310 SkString tmp; 262 SkString tmp;
311 fModified->toISO8601(&tmp); 263 fModified->toISO8601(&tmp);
312 SkASSERT(0 == count_xml_escape_size(tmp)); 264 SkASSERT(0 == count_xml_escape_size(tmp));
313 modificationDate = sk_string_printf( 265 modificationDate = SkStringPrintf(
314 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); 266 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str());
315 } 267 }
316 SkString title = escape_xml( 268 SkString title = escape_xml(
317 get(fInfo, "Title"), 269 get(fInfo, "Title"),
318 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", 270 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">",
319 "</rdf:li></rdf:Alt></dc:title>\n"); 271 "</rdf:li></rdf:Alt></dc:title>\n");
320 SkString author = escape_xml( 272 SkString author = escape_xml(
321 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", 273 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>",
322 "</rdf:li></rdf:Bag></dc:creator>\n"); 274 "</rdf:li></rdf:Bag></dc:creator>\n");
323 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? 275 // TODO: in theory, XMP can support multiple authors. Split on a delimiter?
324 SkString subject = escape_xml( 276 SkString subject = escape_xml(
325 get(fInfo, "Subject"), 277 get(fInfo, "Subject"),
326 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", 278 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">",
327 "</rdf:li></rdf:Alt></dc:description>\n"); 279 "</rdf:li></rdf:Alt></dc:description>\n");
328 SkString keywords1 = escape_xml( 280 SkString keywords1 = escape_xml(
329 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", 281 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>",
330 "</rdf:li></rdf:Bag></dc:subject>\n"); 282 "</rdf:li></rdf:Bag></dc:subject>\n");
331 SkString keywords2 = escape_xml( 283 SkString keywords2 = escape_xml(
332 get(fInfo, "Keywords"), "<pdf:Keywords>", 284 get(fInfo, "Keywords"), "<pdf:Keywords>",
333 "</pdf:Keywords>\n"); 285 "</pdf:Keywords>\n");
334 286
335 // TODO: in theory, keywords can be a list too. 287 // TODO: in theory, keywords can be a list too.
336 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", 288 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>",
337 "</xmp:CreatorTool>\n"); 289 "</xmp:CreatorTool>\n");
338 SkString documentID = uuid_to_string(doc); // no need to escape 290 SkString documentID = uuid_to_string(doc); // no need to escape
339 SkASSERT(0 == count_xml_escape_size(documentID)); 291 SkASSERT(0 == count_xml_escape_size(documentID));
340 SkString instanceID = uuid_to_string(instance); 292 SkString instanceID = uuid_to_string(instance);
341 SkASSERT(0 == count_xml_escape_size(instanceID)); 293 SkASSERT(0 == count_xml_escape_size(instanceID));
342 return new PDFXMLObject(sk_string_printf( 294 return new PDFXMLObject(SkStringPrintf(
343 templateString, modificationDate.c_str(), creationDate.c_str(), 295 templateString, modificationDate.c_str(), creationDate.c_str(),
344 creator.c_str(), title.c_str(), 296 creator.c_str(), title.c_str(),
345 subject.c_str(), author.c_str(), keywords1.c_str(), 297 subject.c_str(), author.c_str(), keywords1.c_str(),
346 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); 298 documentID.c_str(), instanceID.c_str(), keywords2.c_str()));
347 } 299 }
348 300
349 #undef SKPDF_STRING 301 #undef SKPDF_STRING
350 #undef SKPDF_STRING_IMPL 302 #undef SKPDF_STRING_IMPL
351 303
OLDNEW
« no previous file with comments | « src/core/SkString.cpp ('k') | tests/StringTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698