• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <snapuserd/snapuserd_buffer.h>
18 #include <snapuserd/snapuserd_kernel.h>
19 
20 namespace android {
21 namespace snapshot {
22 
Initialize(size_t size)23 void BufferSink::Initialize(size_t size) {
24     buffer_size_ = size;
25     buffer_offset_ = 0;
26     buffer_ = std::make_unique<uint8_t[]>(size);
27 }
28 
GetPayloadBuffer(size_t size)29 void* BufferSink::GetPayloadBuffer(size_t size) {
30     if ((buffer_size_ - buffer_offset_) < size) return nullptr;
31 
32     char* buffer = reinterpret_cast<char*>(GetBufPtr());
33     struct dm_user_message* msg = (struct dm_user_message*)(&(buffer[0]));
34     return (char*)msg->payload.buf + buffer_offset_;
35 }
36 
GetBuffer(size_t requested,size_t * actual)37 void* BufferSink::GetBuffer(size_t requested, size_t* actual) {
38     void* buf = GetPayloadBuffer(requested);
39     if (!buf) {
40         *actual = 0;
41         return nullptr;
42     }
43     *actual = requested;
44     return buf;
45 }
46 
GetHeaderPtr()47 struct dm_user_header* BufferSink::GetHeaderPtr() {
48     if (!(sizeof(struct dm_user_header) <= buffer_size_)) {
49         return nullptr;
50     }
51     char* buf = reinterpret_cast<char*>(GetBufPtr());
52     struct dm_user_header* header = (struct dm_user_header*)(&(buf[0]));
53     return header;
54 }
55 
GetPayloadBufPtr()56 void* BufferSink::GetPayloadBufPtr() {
57     char* buffer = reinterpret_cast<char*>(GetBufPtr());
58     struct dm_user_message* msg = reinterpret_cast<struct dm_user_message*>(&(buffer[0]));
59     return msg->payload.buf;
60 }
61 
Initialize(BufferSink * sink,size_t size)62 void XorSink::Initialize(BufferSink* sink, size_t size) {
63     bufsink_ = sink;
64     buffer_size_ = size;
65     returned_ = 0;
66     buffer_ = std::make_unique<uint8_t[]>(size);
67 }
68 
Reset()69 void XorSink::Reset() {
70     returned_ = 0;
71 }
72 
GetBuffer(size_t requested,size_t * actual)73 void* XorSink::GetBuffer(size_t requested, size_t* actual) {
74     if (requested > buffer_size_) {
75         *actual = buffer_size_;
76     } else {
77         *actual = requested;
78     }
79     return buffer_.get();
80 }
81 
ReturnData(void * buffer,size_t len)82 bool XorSink::ReturnData(void* buffer, size_t len) {
83     uint8_t* xor_data = reinterpret_cast<uint8_t*>(buffer);
84     uint8_t* buff = reinterpret_cast<uint8_t*>(bufsink_->GetPayloadBuffer(len + returned_));
85     if (buff == nullptr) {
86         return false;
87     }
88     for (size_t i = 0; i < len; i++) {
89         buff[returned_ + i] ^= xor_data[i];
90     }
91     returned_ += len;
92     return true;
93 }
94 
95 }  // namespace snapshot
96 }  // namespace android
97