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

Unified Diff: pdf/pdfium/pdfium_api_string_buffer_adapter.h

Issue 848073003: PDF: Yet another stab at getting WriteInto() buffer sizes correct. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: document Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: pdf/pdfium/pdfium_api_string_buffer_adapter.h
diff --git a/pdf/pdfium/pdfium_api_string_buffer_adapter.h b/pdf/pdfium/pdfium_api_string_buffer_adapter.h
new file mode 100644
index 0000000000000000000000000000000000000000..bd28f4cf60d9bfc7f1dc03712cc9eca3e72df522
--- /dev/null
+++ b/pdf/pdfium/pdfium_api_string_buffer_adapter.h
@@ -0,0 +1,53 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
+#define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
+
+#include "base/basictypes.h"
+
+namespace chrome_pdf {
+
+// Helper to deal with the fact that many PDFium APIs write the null-terminator
+// into string buffers that are passed to them, but the PDF plugin likes to pass
+// in std::strings / base::string16s, where one should not count on the internal
+// string buffers to be null-terminated.
+
+template <typename STRING_TYPE>
+class PDFiumAPIStringBufferAdapter {
+ public:
+ // |str| is the string to write into.
+ // |expected_size| is the number of characters the PDFium API will write,
+ // including the null-terminator. It should be at least 1.
+ // |check_expected_size| whether to check the actual number of characters
+ // written into |str| against |expected_size| when calling Close().
+ PDFiumAPIStringBufferAdapter(STRING_TYPE* str,
+ size_t expected_size,
+ bool check_expected_size);
+ ~PDFiumAPIStringBufferAdapter();
+
+ // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
+ // hold |expected_size_| + 1 characters, so teh PDFium API that uses the
raymes 2015/01/15 04:33:24 nit: teh->the
+ // pointer has space to write a null-terminator.
+ void* GetData();
+
+ // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra
+ // null-terminator. This must be called prior to the adapter's destruction.
+ // The pointer returned by GetData() should be considered invalid.
+ void Close(int actual_size);
+ void Close(size_t actual_size);
+
+ private:
+ STRING_TYPE* const str_;
+ void* const data_;
+ const size_t expected_size_;
+ const bool check_expected_size_;
+ bool is_closed_;
+
+ DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter);
+};
+
+} // namespace chrome_pdf
+
+#endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_

Powered by Google App Engine
This is Rietveld 408576698