Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 956ab6ac authored by Yi Jin's avatar Yi Jin Committed by Android (Google) Code Review
Browse files

Merge "Implement go/streaming-proto native libs, part 2"

parents ee1e5ab2 974a9c28
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -33,18 +33,18 @@ write_field_or_skip(EncodedBuffer::iterator* iter, EncodedBuffer* buf, uint8_t w
{
    EncodedBuffer::Pointer snapshot = iter->rp()->copy();
    size_t bytesToWrite = 0;
    uint32_t varint = 0;
    uint64_t varint = 0;
    switch (wireType) {
        case WIRE_TYPE_VARINT:
            varint = iter->readRawVarint();
            if(!skip) return buf->writeRawVarint(varint);
            if(!skip) return buf->writeRawVarint64(varint);
            break;
        case WIRE_TYPE_FIXED64:
            bytesToWrite = 8;
            break;
        case WIRE_TYPE_LENGTH_DELIMITED:
            bytesToWrite = iter->readRawVarint();
            if(!skip) buf->writeRawVarint(bytesToWrite);
            if(!skip) buf->writeRawVarint32(bytesToWrite);
            break;
        case WIRE_TYPE_FIXED32:
            bytesToWrite = 4;
@@ -76,7 +76,6 @@ stripField(EncodedBuffer::iterator* iter, EncodedBuffer* buf, const Privacy* par
    uint8_t wireType = read_wire_type(varint);
    uint32_t fieldId = read_field_id(varint);
    const Privacy* policy = parentPolicy->lookup(fieldId);

    if (policy == NULL || !policy->IsMessageType() || !policy->HasChildren()) {
        bool skip = !spec.CheckPremission(policy);
        size_t amt = buf->size();
@@ -99,7 +98,7 @@ stripField(EncodedBuffer::iterator* iter, EncodedBuffer* buf, const Privacy* par
    }

    buf->writeHeader(fieldId, wireType);
    buf->writeRawVarint(finalSize);
    buf->writeRawVarint32(finalSize);
    while (!q.empty()) {
        EncodedBuffer* subField = q.front();
        EncodedBuffer::iterator it = subField->begin();
+2 −2
Original line number Diff line number Diff line
@@ -22,15 +22,15 @@ LOCAL_CFLAGS := \
        -Wall -Werror -Wno-missing-field-initializers -Wno-unused-variable -Wunused-parameter

LOCAL_SHARED_LIBRARIES := \
        libbinder \
        libcutils \
        liblog \
        libutils

LOCAL_C_INCLUDES := \
        $(LOCAL_PATH)/include

LOCAL_SRC_FILES := \
        src/EncodedBuffer.cpp \
        src/ProtoOutputStream.cpp \
        src/protobuf.cpp \

LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+65 −8
Original line number Diff line number Diff line
@@ -52,10 +52,10 @@ public:
        size_t index() const;
        size_t offset() const;

        void move(size_t amt);
        inline void move() { move(1); };
        Pointer* move(size_t amt);
        inline Pointer* move() { return move(1); };
        Pointer* rewind();

        void rewind();
        Pointer copy() const;

    private:
@@ -88,15 +88,71 @@ public:
    size_t currentToWrite();

    /**
     * Write a varint into a vector. Return the size of the varint.
     * Write a single byte to the buffer.
     */
    size_t writeRawVarint(uint32_t val);
    void writeRawByte(uint8_t val);

    /**
     * Write a varint32 into the buffer. Return the size of the varint.
     */
    size_t writeRawVarint32(uint32_t val);

    /**
     * Write a varint64 into the buffer. Return the size of the varint.
     */
    size_t writeRawVarint64(uint64_t val);

    /**
     * Write Fixed32 into the buffer.
     */
    void writeRawFixed32(uint32_t val);

    /**
     * Write Fixed64 into the buffer.
     */
    void writeRawFixed64(uint64_t val);

    /**
     * Write a protobuf header. Return the size of the header.
     */
    size_t writeHeader(uint32_t fieldId, uint8_t wireType);

    /********************************* Edit APIs ************************************************/
    /**
     * Returns the edit pointer.
     */
    Pointer* ep();

    /**
     * Read a single byte at ep, and move ep to next byte;
     */
    uint8_t readRawByte();

    /**
     * Read varint starting at ep, ep will move to pos of next byte.
     */
    uint64_t readRawVarint();

    /**
     * Read 4 bytes starting at ep, ep will move to pos of next byte.
     */
    uint32_t readRawFixed32();

    /**
     * Read 8 bytes starting at ep, ep will move to pos of next byte.
     */
    uint64_t readRawFixed64();

    /**
     * Edit 4 bytes starting at pos.
     */
    void editRawFixed32(size_t pos, uint32_t val);

    /**
     * Copy _size_ bytes of data starting at __srcPos__ to wp.
     */
    void copy(size_t srcPos, size_t size);

    /********************************* Read APIs ************************************************/
    class iterator;
    friend class iterator;
@@ -141,9 +197,8 @@ public:

        /**
         * Read varint from iterator, the iterator will point to next available byte.
         * Return the number of bytes of the varint.
         */
        uint32_t readRawVarint();
        uint64_t readRawVarint();

    private:
        const EncodedBuffer& mData;
@@ -160,6 +215,7 @@ private:
    vector<uint8_t*> mBuffers;

    Pointer mWp;
    Pointer mEp;

    inline uint8_t* at(const Pointer& p) const; // helper function to get value
};
@@ -168,3 +224,4 @@ private:
} // android

#endif // ANDROID_UTIL_ENCODED_BUFFER_H
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_UTIL_PROTOOUTPUT_STREAM_H
#define ANDROID_UTIL_PROTOOUTPUT_STREAM_H

#include <android/util/EncodedBuffer.h>

#include <stdint.h>
#include <string>

namespace android {
namespace util {

/**
 * Class to write to a protobuf stream.
 *
 * Each write method takes an ID code from the protoc generated classes
 * and the value to write.  To make a nested object, call start
 * and then end when you are done.
 *
 * See the java version implementation (ProtoOutputStream.java) for more infos.
 */
class ProtoOutputStream
{
public:
    ProtoOutputStream(int fd);
    ~ProtoOutputStream();

    /**
     * Write APIs for dumping protobuf data. Returns true if the write succeeds.
     */
    bool write(uint64_t fieldId, double val);
    bool write(uint64_t fieldId, float val);
    bool write(uint64_t fieldId, int val);
    bool write(uint64_t fieldId, long long val);
    bool write(uint64_t fieldId, bool val);
    bool write(uint64_t fieldId, std::string val);
    bool write(uint64_t fieldId, const char* val);

    /**
     * Starts a sub-message write session.
     * Returns a token of this write session.
     * Must call end(token) when finish write this sub-message.
     */
    long long start(uint64_t fieldId);
    void end(long long token);

    /**
     * Flushes the protobuf data out.
     */
    bool flush();

private:
    EncodedBuffer mBuffer;
    int mFd;
    size_t mCopyBegin;
    bool mCompact;
    int mDepth;
    int mObjectId;
    long long mExpectedObjectToken;

    inline void writeDoubleImpl(uint32_t id, double val);
    inline void writeFloatImpl(uint32_t id, float val);
    inline void writeInt64Impl(uint32_t id, long long val);
    inline void writeInt32Impl(uint32_t id, int val);
    inline void writeUint64Impl(uint32_t id, uint64_t val);
    inline void writeUint32Impl(uint32_t id, uint32_t val);
    inline void writeFixed64Impl(uint32_t id, uint64_t val);
    inline void writeFixed32Impl(uint32_t id, uint32_t val);
    inline void writeSFixed64Impl(uint32_t id, long long val);
    inline void writeSFixed32Impl(uint32_t id, int val);
    inline void writeZigzagInt64Impl(uint32_t id, long long val);
    inline void writeZigzagInt32Impl(uint32_t id, int val);
    inline void writeEnumImpl(uint32_t id, int val);
    inline void writeBoolImpl(uint32_t id, bool val);
    inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);

    bool compact();
    size_t editEncodedSize(size_t rawSize);
    bool compactSize(size_t rawSize);
};

}
}

#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H
 No newline at end of file
+11 −4
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ namespace util {

using namespace std;

const int FIELD_ID_SHIFT = 3;
const uint8_t WIRE_TYPE_MASK = (1 << FIELD_ID_SHIFT) - 1;

const uint8_t WIRE_TYPE_VARINT = 0;
const uint8_t WIRE_TYPE_FIXED64 = 1;
const uint8_t WIRE_TYPE_LENGTH_DELIMITED = 2;
@@ -35,16 +38,20 @@ const uint8_t WIRE_TYPE_FIXED32 = 5;
uint8_t read_wire_type(uint32_t varint);

/**
 * read field id from varint, it is varint >> 3;
 * Read field id from varint, it is varint >> 3;
 */
uint32_t read_field_id(uint32_t varint);

/**
 * Get the size of a varint.
 */
size_t get_varint_size(uint64_t varint);

/**
 * Write a varint into the buffer. Return the next position to write at.
 * There must be 10 bytes in the buffer. The same as
 * EncodedBuffer.writeRawVarint32
 * There must be 10 bytes in the buffer.
 */
uint8_t* write_raw_varint(uint8_t* buf, uint32_t val);
uint8_t* write_raw_varint(uint8_t* buf, uint64_t val);

/**
 * Write a protobuf WIRE_TYPE_LENGTH_DELIMITED header. Return the next position
Loading