1 /*
2 * Copyright 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
18 #include "tuningfork/protobuf_util.h"
19 #include "tuningfork/protobuf_nano_util.h"
20
21 #include <pb_encode.h>
22 #include <pb_decode.h>
23
24 namespace tuningfork {
25
Read(pb_istream_t * stream,uint8_t * buf,size_t count)26 bool VectorStream::Read(pb_istream_t *stream, uint8_t *buf, size_t count) {
27 VectorStream* str = (VectorStream*)(stream->state);
28 if (buf==NULL) {
29 if(count > str->vec->size() - str->it) {
30 str->it = str->vec->size();
31 return false;
32 }
33 else {
34 str->it += count;
35 return true;
36 }
37 }
38 auto p = &(*str->vec)[str->it];
39 auto n = std::min(count, str->vec->size() - str->it);
40 std::copy(p, p + n, buf);
41 str->it += n;
42 return n==count;
43 }
Write(pb_ostream_t * stream,const uint8_t * buf,size_t count)44 bool VectorStream::Write(pb_ostream_t *stream, const uint8_t *buf, size_t count) {
45 if(buf==NULL)
46 return true;
47 VectorStream* str = (VectorStream*)(stream->state);
48 auto vec = str->vec;
49 int b = buf[0];
50 auto sz = vec->size();
51 vec->resize(sz+count);
52 std::copy(buf, buf+count, &(*vec)[sz]);
53 return true;
54 }
55
CProtobufSerialization_Free(CProtobufSerialization * ser)56 void CProtobufSerialization_Free(CProtobufSerialization* ser) {
57 if(ser && ser->dealloc && ser->bytes) {
58 ser->dealloc(ser->bytes);
59 ser->bytes = 0;
60 ser->size = 0;
61 ser->dealloc = 0;
62 }
63 }
64
65 } // namespace tuningfork {
66