1 /*
2  * Copyright (C) 2010 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 // DO NOT USE: please use parcelable instead
20 // This code is deprecated and will not be supported via AIDL code gen. For data
21 // to be sent over binder, please use parcelables.
22 
23 #include <stdint.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <utils/Errors.h>
27 
28 #include <type_traits>
29 
30 namespace android {
31 
32 // DO NOT USE: please use parcelable instead
33 // This code is deprecated and will not be supported via AIDL code gen. For data
34 // to be sent over binder, please use parcelables.
35 class FlattenableUtils {
36 public:
37     template<size_t N>
align(size_t size)38     static size_t align(size_t size) {
39         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
40         return (size + (N-1)) & ~(N-1);
41     }
42 
43     template<size_t N>
align(void const * & buffer)44     static size_t align(void const*& buffer) {
45         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
46         uintptr_t b = uintptr_t(buffer);
47         buffer = reinterpret_cast<void*>((uintptr_t(buffer) + (N-1)) & ~(N-1));
48         return size_t(uintptr_t(buffer) - b);
49     }
50 
51     template<size_t N>
align(void * & buffer)52     static size_t align(void*& buffer) {
53         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
54         void* b = buffer;
55         buffer = reinterpret_cast<void*>((uintptr_t(buffer) + (N-1)) & ~(N-1));
56         size_t delta = size_t(uintptr_t(buffer) - uintptr_t(b));
57         memset(b, 0, delta);
58         return delta;
59     }
60 
advance(void * & buffer,size_t & size,size_t offset)61     static void advance(void*& buffer, size_t& size, size_t offset) {
62         buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset );
63         size -= offset;
64     }
65 
advance(void const * & buffer,size_t & size,size_t offset)66     static void advance(void const*& buffer, size_t& size, size_t offset) {
67         buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset );
68         size -= offset;
69     }
70 
71     // write a POD structure
72     template<typename T>
write(void * & buffer,size_t & size,const T & value)73     static void write(void*& buffer, size_t& size, const T& value) {
74         static_assert(std::is_trivially_copyable<T>::value,
75                       "Cannot flatten a non-trivially-copyable type");
76         memcpy(buffer, &value, sizeof(T));
77         advance(buffer, size, sizeof(T));
78     }
79 
80     // read a POD structure
81     template<typename T>
read(void const * & buffer,size_t & size,T & value)82     static void read(void const*& buffer, size_t& size, T& value) {
83         static_assert(std::is_trivially_copyable<T>::value,
84                       "Cannot unflatten a non-trivially-copyable type");
85         memcpy(&value, buffer, sizeof(T));
86         advance(buffer, size, sizeof(T));
87     }
88 };
89 
90 // DO NOT USE: please use parcelable instead
91 // This code is deprecated and will not be supported via AIDL code gen. For data
92 // to be sent over binder, please use parcelables.
93 /*
94  * The Flattenable protocol allows an object to serialize itself out
95  * to a byte-buffer and an array of file descriptors.
96  * Flattenable objects must implement this protocol.
97  */
98 
99 template <typename T>
100 class Flattenable {
101 public:
102     // size in bytes of the flattened object
103     inline size_t getFlattenedSize() const;
104 
105     // number of file descriptors to flatten
106     inline size_t getFdCount() const;
107 
108     // flattens the object into buffer.
109     // size should be at least of getFlattenedSize()
110     // file descriptors are written in the fds[] array but ownership is
111     // not transfered (ie: they must be dupped by the caller of
112     // flatten() if needed).
113     inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
114 
115     // unflattens the object from buffer.
116     // size should be equal to the value of getFlattenedSize() when the
117     // object was flattened.
118     // unflattened file descriptors are found in the fds[] array and
119     // don't need to be dupped(). ie: the caller of unflatten doesn't
120     // keep ownership. If a fd is not retained by unflatten() it must be
121     // explicitly closed.
122     inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
123 };
124 
125 template<typename T>
getFlattenedSize()126 inline size_t Flattenable<T>::getFlattenedSize() const {
127     return static_cast<T const*>(this)->T::getFlattenedSize();
128 }
129 template<typename T>
getFdCount()130 inline size_t Flattenable<T>::getFdCount() const {
131     return static_cast<T const*>(this)->T::getFdCount();
132 }
133 template<typename T>
flatten(void * & buffer,size_t & size,int * & fds,size_t & count)134 inline status_t Flattenable<T>::flatten(
135         void*& buffer, size_t& size, int*& fds, size_t& count) const {
136     return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
137 }
138 template<typename T>
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)139 inline status_t Flattenable<T>::unflatten(
140         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
141     return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
142 }
143 
144 // DO NOT USE: please use parcelable instead
145 // This code is deprecated and will not be supported via AIDL code gen. For data
146 // to be sent over binder, please use parcelables.
147 /*
148  * LightFlattenable is a protocol allowing object to serialize themselves out
149  * to a byte-buffer. Because it doesn't handle file-descriptors,
150  * LightFlattenable is usually more size efficient than Flattenable.
151  * LightFlattenable objects must implement this protocol.
152  */
153 template <typename T>
154 class LightFlattenable {
155 public:
156     // returns whether this object always flatten into the same size.
157     // for efficiency, this should always be inline.
158     inline bool isFixedSize() const;
159 
160     // returns size in bytes of the flattened object. must be a constant.
161     inline size_t getFlattenedSize() const;
162 
163     // flattens the object into buffer.
164     inline status_t flatten(void* buffer, size_t size) const;
165 
166     // unflattens the object from buffer of given size.
167     inline status_t unflatten(void const* buffer, size_t size);
168 };
169 
170 template <typename T>
isFixedSize()171 inline bool LightFlattenable<T>::isFixedSize() const {
172     return static_cast<T const*>(this)->T::isFixedSize();
173 }
174 template <typename T>
getFlattenedSize()175 inline size_t LightFlattenable<T>::getFlattenedSize() const {
176     return static_cast<T const*>(this)->T::getFlattenedSize();
177 }
178 template <typename T>
flatten(void * buffer,size_t size)179 inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
180     return static_cast<T const*>(this)->T::flatten(buffer, size);
181 }
182 template <typename T>
unflatten(void const * buffer,size_t size)183 inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
184     return static_cast<T*>(this)->T::unflatten(buffer, size);
185 }
186 
187 // DO NOT USE: please use parcelable instead
188 // This code is deprecated and will not be supported via AIDL code gen. For data
189 // to be sent over binder, please use parcelables.
190 /*
191  * LightFlattenablePod is an implementation of the LightFlattenable protocol
192  * for POD (plain-old-data) objects.
193  * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
194  * need to implement any methods; obviously Foo must be a POD structure.
195  */
196 template <typename T>
197 class LightFlattenablePod : public LightFlattenable<T> {
198 public:
isFixedSize()199     inline bool isFixedSize() const {
200         return true;
201     }
202 
getFlattenedSize()203     inline size_t getFlattenedSize() const {
204         return sizeof(T);
205     }
flatten(void * buffer,size_t size)206     inline status_t flatten(void* buffer, size_t size) const {
207         if (size < sizeof(T)) return NO_MEMORY;
208         memcpy(buffer, static_cast<T const*>(this), sizeof(T));
209         return OK;
210     }
unflatten(void const * buffer,size_t)211     inline status_t unflatten(void const* buffer, size_t) {
212         memcpy(static_cast<T*>(this), buffer, sizeof(T));
213         return OK;
214     }
215 };
216 
217 }  // namespace android
218