1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_BYTE_VALUE_H_
18 #define MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_BYTE_VALUE_H_
19 
20 #include <functional>
21 #include <vector>
22 
23 namespace pdfClient_utils {
24 // Wrapper around a Pdfium function to make it easier to get a bytestring.
25 // Many Pdfium functions have the following form:
26 // size_t FPDF_GetFooString(other_args ..., void* buffer, size_t buffer_len);
27 // These functions return the number of bytes in the result, regardless of the
28 // value of buffer_len. If buffer_len >= the number of bytes in the result,
29 // buffer is also filled in with the result value.
30 //
31 // GetBytes accepts a |result| vector, where the result will be stored, and a
32 // std::function that accepts a void* buffer and size_t buffer_len (with any
33 // other arguments being pre-bound using, e.g., BindFront, a lambda, or a
34 // wrapper).
35 // |result| will be filled in with the bytes returned, and be resized to match
36 // the size of the returned bytestring.
37 // Any data already in |result| will be overridden or deleted.
38 template <class T>
39 void GetBytes(std::vector<char>* result, const std::function<size_t(T*, size_t)>& f);
40 }  // namespace pdfClient_utils
41 #endif  // MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_BYTE_VALUE_H_