blob: be2f24f97d0218944b29a9f2ee187652d73af7f9 [file] [log] [blame]
Joe Onorato99598ee2019-02-11 15:55:13 +00001/*
2 * Copyright (C) 2017 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 "proto_util.h"
18
19#include <google/protobuf/io/zero_copy_stream_impl.h>
20
21#include <android/util/protobuf.h>
22#include <android/util/ProtoOutputStream.h>
23#include <android-base/file.h>
24
25namespace android {
26namespace os {
27namespace incidentd {
28
29using namespace android::base;
30using namespace android::util;
31using google::protobuf::io::FileOutputStream;
32
33// special section ids
34const int FIELD_ID_INCIDENT_HEADER = 1;
35
36status_t write_header_section(int fd, const vector<uint8_t>& buf) {
37 status_t err;
38 const size_t bufSize = buf.size();
39
40 if (buf.empty()) {
41 return NO_ERROR;
42 }
43
44 err = write_section_header(fd, FIELD_ID_INCIDENT_HEADER, bufSize);
45 if (err != NO_ERROR) {
46 return err;
47 }
48
49 err = WriteFully(fd, (uint8_t const*)buf.data(), bufSize);
50 if (err != NO_ERROR) {
51 return err;
52 }
53
54 return NO_ERROR;
55}
56
57status_t write_section_header(int fd, int sectionId, size_t size) {
58 uint8_t buf[20];
59 uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size);
60 return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno;
61}
62
63status_t write_section(int fd, int sectionId, const MessageLite& message) {
64 status_t err;
65
66 err = write_section_header(fd, sectionId, message.ByteSize());
67 if (err != NO_ERROR) {
68 return err;
69 }
70
71 FileOutputStream stream(fd);
72 if (!message.SerializeToZeroCopyStream(&stream)) {
73 return stream.GetErrno();
74 } else {
75 return NO_ERROR;
76 }
77}
78
79
80
81} // namespace incidentd
82} // namespace os
83} // namespace android
84