1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "callbacks.h" // NOLINT(build/include)
16
17 #include <cstdlib>
18 #include <cstring>
19
20 #include "sandboxed_api/vars.h"
21
WriteToMemory(char * contents,size_t size,size_t num_bytes,void * userp)22 size_t WriteToMemory(char* contents, size_t size, size_t num_bytes,
23 void* userp) {
24 size_t real_size = size * num_bytes;
25 auto* mem = static_cast<sapi::LenValStruct*>(userp);
26
27 char* ptr = static_cast<char*>(realloc(mem->data, mem->size + real_size + 1));
28 if (ptr == nullptr) return 0;
29
30 mem->data = ptr;
31 auto data = static_cast<char*>(mem->data);
32 memcpy(&(data[mem->size]), contents, real_size);
33 mem->size += real_size;
34 data[mem->size] = 0;
35
36 return real_size;
37 }
38