Implement PII Stripper, part 2

Implement EncodedBuffer that strip pii based on given privacy request.
The reason to implement another buffer is the length-delimited field's
size could change when its submessage gets stripped. It also intends to
keep the orignal data around for other requests to consume it.

In addition, the section implementation has adapted EncodedBuffer so
write out to each request's fd could be request-specific. The next step
is allow requests to set its privacy spec.

Notice the current design set the privacy spec of dropbox to AUTOMATIC,
this behavior might change in the future.

Bug: 64687253
Test: unit tests are writtern, see README.md for how to run unit tests.
Change-Id: I7ac236b8265ba9289dc6e17a8a5bf7f67ffb6bf5
diff --git a/cmds/incidentd/src/protobuf.cpp b/cmds/incidentd/src/protobuf.cpp
index b865339..05de831 100644
--- a/cmds/incidentd/src/protobuf.cpp
+++ b/cmds/incidentd/src/protobuf.cpp
@@ -16,8 +16,17 @@
 
 #include "protobuf.h"
 
+uint8_t read_wire_type(uint32_t varint)
+{
+    return (uint8_t) (varint & 0x07);
+}
 
-uint8_t* 
+uint32_t read_field_id(uint32_t varint)
+{
+    return varint >> 3;
+}
+
+uint8_t*
 write_raw_varint(uint8_t* buf, uint32_t val)
 {
     uint8_t* p = buf;
@@ -32,7 +41,7 @@
     }
 }
 
-uint8_t* 
+uint8_t*
 write_length_delimited_tag_header(uint8_t* buf, uint32_t fieldId, size_t size)
 {
     buf = write_raw_varint(buf, (fieldId << 3) | 2);
@@ -40,3 +49,24 @@
     return buf;
 }
 
+size_t
+write_raw_varint(vector<uint8_t> &buf, uint32_t val)
+{
+    size_t size = 0;
+    while (true) {
+        size++;
+        if ((val & ~0x7F) == 0) {
+            buf.push_back((uint8_t) val);
+            return size;
+        } else {
+            buf.push_back((uint8_t)((val & 0x7F) | 0x80));
+            val >>= 7;
+        }
+    }
+}
+
+size_t
+write_header(vector<uint8_t> &buf, uint32_t fieldId, uint8_t wireType)
+{
+    return write_raw_varint(buf, (fieldId << 3) | wireType);
+}
\ No newline at end of file