1 /*
2  * Copyright (C) 2018 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 #pragma once
18 
19 #include <cstdint>
20 #include <cstdio>
21 #include <cstdlib>
22 
23 class IOStream {
24   public:
IOStream(unsigned char * buf,size_t bufSize)25     IOStream(unsigned char* buf, size_t bufSize) : m_buf(buf), m_bufSize(bufSize) {
26     }
27 
alloc(size_t len)28     unsigned char* alloc(size_t len) {
29         if (m_allocSize + len > m_bufSize) {
30             printf("Command response is too large.\n");
31             return nullptr;
32         }
33         unsigned char* buf = m_buf + m_allocSize;
34         m_allocSize += len;
35         return buf;
36     }
37 
flush()38     int flush() {
39         m_flushSize = m_allocSize;
40         return 0;
41     }
42 
getDmaForReading(uint64_t guest_paddr)43     void* getDmaForReading(uint64_t guest_paddr) {
44         // GLDMA is not supported, so we don't have to implement this. Just keep
45         // a stub here to keep the generated decoder stubs happy
46         return nullptr;
47     }
48 
unlockDma(uint64_t guest_paddr)49     void unlockDma(uint64_t guest_paddr) {
50         // GLDMA is not supported, so we don't have to implement this. Just keep
51         // a stub here to keep the generated decoder stubs happy
52     }
53 
getFlushSize()54     size_t getFlushSize() {
55         return m_flushSize;
56     }
57 
58   private:
59     size_t m_allocSize = 0U;
60     size_t m_flushSize = 0U;
61     unsigned char* m_buf;
62     size_t m_bufSize;
63 };
64