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 #include "byte_value.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <vector>
22 
23 #include "fpdfview.h"
24 
25 namespace pdfClient_utils {
26 namespace {
27 constexpr size_t kBufferSize = 128;
28 }  // namespace
29 
30 template <class T>
GetBytes(std::vector<char> * result,const std::function<size_t (T *,size_t)> & f)31 void GetBytes(std::vector<char>* result, const std::function<size_t(T*, size_t)>& f) {
32     result->resize(kBufferSize);
33     size_t result_bytes = f(reinterpret_cast<T*>(result->data()), kBufferSize);
34 
35     result->resize(result_bytes);
36 
37     if (result_bytes > kBufferSize) {
38         size_t result_bytes_2 = f(reinterpret_cast<T*>(result->data()), result_bytes);
39         DCHECK_EQ(result_bytes, result_bytes_2) << "Pdfium function called with "
40                                                    "the same arguments returned a "
41                                                    "value that's a different size.";
42     }
43 }
44 
45 // Explicitly instantiate all specializations here:
46 template void GetBytes<void>(std::vector<char>* result,
47                              const std::function<size_t(void*, size_t)>& f);
48 template void GetBytes<FPDF_WCHAR>(std::vector<char>* result,
49                                    const std::function<size_t(FPDF_WCHAR*, size_t)>& f);
50 
51 }  // namespace pdfClient_utils