Chris@64: // Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #include Chris@64: #include Chris@64: #include Chris@64: #include Chris@64: #include "common.h" Chris@64: #include "layout.h" Chris@64: #include "any.h" Chris@64: Chris@64: #ifndef CAPNP_MESSAGE_H_ Chris@64: #define CAPNP_MESSAGE_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: namespace capnp { Chris@64: Chris@64: namespace _ { // private Chris@64: class ReaderArena; Chris@64: class BuilderArena; Chris@64: } Chris@64: Chris@64: class StructSchema; Chris@64: class Orphanage; Chris@64: template Chris@64: class Orphan; Chris@64: Chris@64: // ======================================================================================= Chris@64: Chris@64: struct ReaderOptions { Chris@64: // Options controlling how data is read. Chris@64: Chris@64: uint64_t traversalLimitInWords = 8 * 1024 * 1024; Chris@64: // Limits how many total words of data are allowed to be traversed. Traversal is counted when Chris@64: // a new struct or list builder is obtained, e.g. from a get() accessor. This means that calling Chris@64: // the getter for the same sub-struct multiple times will cause it to be double-counted. Once Chris@64: // the traversal limit is reached, an error will be reported. Chris@64: // Chris@64: // This limit exists for security reasons. It is possible for an attacker to construct a message Chris@64: // in which multiple pointers point at the same location. This is technically invalid, but hard Chris@64: // to detect. Using such a message, an attacker could cause a message which is small on the wire Chris@64: // to appear much larger when actually traversed, possibly exhausting server resources leading to Chris@64: // denial-of-service. Chris@64: // Chris@64: // It makes sense to set a traversal limit that is much larger than the underlying message. Chris@64: // Together with sensible coding practices (e.g. trying to avoid calling sub-object getters Chris@64: // multiple times, which is expensive anyway), this should provide adequate protection without Chris@64: // inconvenience. Chris@64: // Chris@64: // The default limit is 64 MiB. This may or may not be a sensible number for any given use case, Chris@64: // but probably at least prevents easy exploitation while also avoiding causing problems in most Chris@64: // typical cases. Chris@64: Chris@64: int nestingLimit = 64; Chris@64: // Limits how deeply-nested a message structure can be, e.g. structs containing other structs or Chris@64: // lists of structs. Chris@64: // Chris@64: // Like the traversal limit, this limit exists for security reasons. Since it is common to use Chris@64: // recursive code to traverse recursive data structures, an attacker could easily cause a stack Chris@64: // overflow by sending a very-deeply-nested (or even cyclic) message, without the message even Chris@64: // being very large. The default limit of 64 is probably low enough to prevent any chance of Chris@64: // stack overflow, yet high enough that it is never a problem in practice. Chris@64: }; Chris@64: Chris@64: class MessageReader { Chris@64: // Abstract interface for an object used to read a Cap'n Proto message. Subclasses of Chris@64: // MessageReader are responsible for reading the raw, flat message content. Callers should Chris@64: // usually call `messageReader.getRoot()` to get a `MyStructType::Reader` Chris@64: // representing the root of the message, then use that to traverse the message content. Chris@64: // Chris@64: // Some common subclasses of `MessageReader` include `SegmentArrayMessageReader`, whose Chris@64: // constructor accepts pointers to the raw data, and `StreamFdMessageReader` (from Chris@64: // `serialize.h`), which reads the message from a file descriptor. One might implement other Chris@64: // subclasses to handle things like reading from shared memory segments, mmap()ed files, etc. Chris@64: Chris@64: public: Chris@64: MessageReader(ReaderOptions options); Chris@64: // It is suggested that subclasses take ReaderOptions as a constructor parameter, but give it a Chris@64: // default value of "ReaderOptions()". The base class constructor doesn't have a default value Chris@64: // in order to remind subclasses that they really need to give the user a way to provide this. Chris@64: Chris@64: virtual ~MessageReader() noexcept(false); Chris@64: Chris@64: virtual kj::ArrayPtr getSegment(uint id) = 0; Chris@64: // Gets the segment with the given ID, or returns null if no such segment exists. This method Chris@64: // will be called at most once for each segment ID. Chris@64: Chris@64: inline const ReaderOptions& getOptions(); Chris@64: // Get the options passed to the constructor. Chris@64: Chris@64: template Chris@64: typename RootType::Reader getRoot(); Chris@64: // Get the root struct of the message, interpreting it as the given struct type. Chris@64: Chris@64: template Chris@64: typename RootType::Reader getRoot(SchemaType schema); Chris@64: // Dynamically interpret the root struct of the message using the given schema (a StructSchema). Chris@64: // RootType in this case must be DynamicStruct, and you must #include to Chris@64: // use this. Chris@64: Chris@64: bool isCanonical(); Chris@64: // Returns whether the message encoded in the reader is in canonical form. Chris@64: Chris@64: private: Chris@64: ReaderOptions options; Chris@64: Chris@64: // Space in which we can construct a ReaderArena. We don't use ReaderArena directly here Chris@64: // because we don't want clients to have to #include arena.h, which itself includes a bunch of Chris@64: // big STL headers. We don't use a pointer to a ReaderArena because that would require an Chris@64: // extra malloc on every message which could be expensive when processing small messages. Chris@64: void* arenaSpace[15 + sizeof(kj::MutexGuarded) / sizeof(void*)]; Chris@64: bool allocatedArena; Chris@64: Chris@64: _::ReaderArena* arena() { return reinterpret_cast<_::ReaderArena*>(arenaSpace); } Chris@64: AnyPointer::Reader getRootInternal(); Chris@64: }; Chris@64: Chris@64: class MessageBuilder { Chris@64: // Abstract interface for an object used to allocate and build a message. Subclasses of Chris@64: // MessageBuilder are responsible for allocating the space in which the message will be written. Chris@64: // The most common subclass is `MallocMessageBuilder`, but other subclasses may be used to do Chris@64: // tricky things like allocate messages in shared memory or mmap()ed files. Chris@64: // Chris@64: // Creating a new message ususually means allocating a new MessageBuilder (ideally on the stack) Chris@64: // and then calling `messageBuilder.initRoot()` to get a `MyStructType::Builder`. Chris@64: // That, in turn, can be used to fill in the message content. When done, you can call Chris@64: // `messageBuilder.getSegmentsForOutput()` to get a list of flat data arrays containing the Chris@64: // message. Chris@64: Chris@64: public: Chris@64: MessageBuilder(); Chris@64: virtual ~MessageBuilder() noexcept(false); Chris@64: KJ_DISALLOW_COPY(MessageBuilder); Chris@64: Chris@64: struct SegmentInit { Chris@64: kj::ArrayPtr space; Chris@64: Chris@64: size_t wordsUsed; Chris@64: // Number of words in `space` which are used; the rest are free space in which additional Chris@64: // objects may be allocated. Chris@64: }; Chris@64: Chris@64: explicit MessageBuilder(kj::ArrayPtr segments); Chris@64: // Create a MessageBuilder backed by existing memory. This is an advanced interface that most Chris@64: // people should not use. THIS METHOD IS INSECURE; see below. Chris@64: // Chris@64: // This allows a MessageBuilder to be constructed to modify an in-memory message without first Chris@64: // making a copy of the content. This is especially useful in conjunction with mmap(). Chris@64: // Chris@64: // The contents of each segment must outlive the MessageBuilder, but the SegmentInit array itself Chris@64: // only need outlive the constructor. Chris@64: // Chris@64: // SECURITY: Do not use this in conjunction with untrusted data. This constructor assumes that Chris@64: // the input message is valid. This constructor is designed to be used with data you control, Chris@64: // e.g. an mmap'd file which is owned and accessed by only one program. When reading data you Chris@64: // do not trust, you *must* load it into a Reader and then copy into a Builder as a means of Chris@64: // validating the content. Chris@64: // Chris@64: // WARNING: It is NOT safe to initialize a MessageBuilder in this way from memory that is Chris@64: // currently in use by another MessageBuilder or MessageReader. Other readers/builders will Chris@64: // not observe changes to the segment sizes nor newly-allocated segments caused by allocating Chris@64: // new objects in this message. Chris@64: Chris@64: virtual kj::ArrayPtr allocateSegment(uint minimumSize) = 0; Chris@64: // Allocates an array of at least the given number of words, throwing an exception or crashing if Chris@64: // this is not possible. It is expected that this method will usually return more space than Chris@64: // requested, and the caller should use that extra space as much as possible before allocating Chris@64: // more. The returned space remains valid at least until the MessageBuilder is destroyed. Chris@64: // Chris@64: // Cap'n Proto will only call this once at a time, so the subclass need not worry about Chris@64: // thread-safety. Chris@64: Chris@64: template Chris@64: typename RootType::Builder initRoot(); Chris@64: // Initialize the root struct of the message as the given struct type. Chris@64: Chris@64: template Chris@64: void setRoot(Reader&& value); Chris@64: // Set the root struct to a deep copy of the given struct. Chris@64: Chris@64: template Chris@64: typename RootType::Builder getRoot(); Chris@64: // Get the root struct of the message, interpreting it as the given struct type. Chris@64: Chris@64: template Chris@64: typename RootType::Builder getRoot(SchemaType schema); Chris@64: // Dynamically interpret the root struct of the message using the given schema (a StructSchema). Chris@64: // RootType in this case must be DynamicStruct, and you must #include to Chris@64: // use this. Chris@64: Chris@64: template Chris@64: typename RootType::Builder initRoot(SchemaType schema); Chris@64: // Dynamically init the root struct of the message using the given schema (a StructSchema). Chris@64: // RootType in this case must be DynamicStruct, and you must #include to Chris@64: // use this. Chris@64: Chris@64: template Chris@64: void adoptRoot(Orphan&& orphan); Chris@64: // Like setRoot() but adopts the orphan without copying. Chris@64: Chris@64: kj::ArrayPtr> getSegmentsForOutput(); Chris@64: // Get the raw data that makes up the message. Chris@64: Chris@64: Orphanage getOrphanage(); Chris@64: Chris@64: bool isCanonical(); Chris@64: // Check whether the message builder is in canonical form Chris@64: Chris@64: private: Chris@64: void* arenaSpace[22]; Chris@64: // Space in which we can construct a BuilderArena. We don't use BuilderArena directly here Chris@64: // because we don't want clients to have to #include arena.h, which itself includes a bunch of Chris@64: // big STL headers. We don't use a pointer to a BuilderArena because that would require an Chris@64: // extra malloc on every message which could be expensive when processing small messages. Chris@64: Chris@64: bool allocatedArena = false; Chris@64: // We have to initialize the arena lazily because when we do so we want to allocate the root Chris@64: // pointer immediately, and this will allocate a segment, which requires a virtual function Chris@64: // call on the MessageBuilder. We can't do such a call in the constructor since the subclass Chris@64: // isn't constructed yet. This is kind of annoying because it means that getOrphanage() is Chris@64: // not thread-safe, but that shouldn't be a huge deal... Chris@64: Chris@64: _::BuilderArena* arena() { return reinterpret_cast<_::BuilderArena*>(arenaSpace); } Chris@64: _::SegmentBuilder* getRootSegment(); Chris@64: AnyPointer::Builder getRootInternal(); Chris@64: }; Chris@64: Chris@64: template Chris@64: typename RootType::Reader readMessageUnchecked(const word* data); Chris@64: // IF THE INPUT IS INVALID, THIS MAY CRASH, CORRUPT MEMORY, CREATE A SECURITY HOLE IN YOUR APP, Chris@64: // MURDER YOUR FIRST-BORN CHILD, AND/OR BRING ABOUT ETERNAL DAMNATION ON ALL OF HUMANITY. DO NOT Chris@64: // USE UNLESS YOU UNDERSTAND THE CONSEQUENCES. Chris@64: // Chris@64: // Given a pointer to a known-valid message located in a single contiguous memory segment, Chris@64: // returns a reader for that message. No bounds-checking will be done while traversing this Chris@64: // message. Use this only if you have already verified that all pointers are valid and in-bounds, Chris@64: // and there are no far pointers in the message. Chris@64: // Chris@64: // To create a message that can be passed to this function, build a message using a MallocAllocator Chris@64: // whose preferred segment size is larger than the message size. This guarantees that the message Chris@64: // will be allocated as a single segment, meaning getSegmentsForOutput() returns a single word Chris@64: // array. That word array is your message; you may pass a pointer to its first word into Chris@64: // readMessageUnchecked() to read the message. Chris@64: // Chris@64: // This can be particularly handy for embedding messages in generated code: you can Chris@64: // embed the raw bytes (using AlignedData) then make a Reader for it using this. This is the way Chris@64: // default values are embedded in code generated by the Cap'n Proto compiler. E.g., if you have Chris@64: // a message MyMessage, you can read its default value like so: Chris@64: // MyMessage::Reader reader = Message::readMessageUnchecked(MyMessage::DEFAULT.words); Chris@64: // Chris@64: // To sanitize a message from an untrusted source such that it can be safely passed to Chris@64: // readMessageUnchecked(), use copyToUnchecked(). Chris@64: Chris@64: template Chris@64: void copyToUnchecked(Reader&& reader, kj::ArrayPtr uncheckedBuffer); Chris@64: // Copy the content of the given reader into the given buffer, such that it can safely be passed to Chris@64: // readMessageUnchecked(). The buffer's size must be exactly reader.totalSizeInWords() + 1, Chris@64: // otherwise an exception will be thrown. The buffer must be zero'd before calling. Chris@64: Chris@64: template Chris@64: typename RootType::Reader readDataStruct(kj::ArrayPtr data); Chris@64: // Interprets the given data as a single, data-only struct. Only primitive fields (booleans, Chris@64: // numbers, and enums) will be readable; all pointers will be null. This is useful if you want Chris@64: // to use Cap'n Proto as a language/platform-neutral way to pack some bits. Chris@64: // Chris@64: // The input is a word array rather than a byte array to enforce alignment. If you have a byte Chris@64: // array which you know is word-aligned (or if your platform supports unaligned reads and you don't Chris@64: // mind the performance penalty), then you can use `reinterpret_cast` to convert a byte array into Chris@64: // a word array: Chris@64: // Chris@64: // kj::arrayPtr(reinterpret_cast(bytes.begin()), Chris@64: // reinterpret_cast(bytes.end())) Chris@64: Chris@64: template Chris@64: typename kj::ArrayPtr writeDataStruct(BuilderType builder); Chris@64: // Given a struct builder, get the underlying data section as a word array, suitable for passing Chris@64: // to `readDataStruct()`. Chris@64: // Chris@64: // Note that you may call `.toBytes()` on the returned value to convert to `ArrayPtr`. Chris@64: Chris@64: template Chris@64: static typename Type::Reader defaultValue(); Chris@64: // Get a default instance of the given struct or list type. Chris@64: // Chris@64: // TODO(cleanup): Find a better home for this function? Chris@64: Chris@64: // ======================================================================================= Chris@64: Chris@64: class SegmentArrayMessageReader: public MessageReader { Chris@64: // A simple MessageReader that reads from an array of word arrays representing all segments. Chris@64: // In particular you can read directly from the output of MessageBuilder::getSegmentsForOutput() Chris@64: // (although it would probably make more sense to call builder.getRoot().asReader() in that case). Chris@64: Chris@64: public: Chris@64: SegmentArrayMessageReader(kj::ArrayPtr> segments, Chris@64: ReaderOptions options = ReaderOptions()); Chris@64: // Creates a message pointing at the given segment array, without taking ownership of the Chris@64: // segments. All arrays passed in must remain valid until the MessageReader is destroyed. Chris@64: Chris@64: KJ_DISALLOW_COPY(SegmentArrayMessageReader); Chris@64: ~SegmentArrayMessageReader() noexcept(false); Chris@64: Chris@64: virtual kj::ArrayPtr getSegment(uint id) override; Chris@64: Chris@64: private: Chris@64: kj::ArrayPtr> segments; Chris@64: }; Chris@64: Chris@64: enum class AllocationStrategy: uint8_t { Chris@64: FIXED_SIZE, Chris@64: // The builder will prefer to allocate the same amount of space for each segment with no Chris@64: // heuristic growth. It will still allocate larger segments when the preferred size is too small Chris@64: // for some single object. This mode is generally not recommended, but can be particularly useful Chris@64: // for testing in order to force a message to allocate a predictable number of segments. Note Chris@64: // that you can force every single object in the message to be located in a separate segment by Chris@64: // using this mode with firstSegmentWords = 0. Chris@64: Chris@64: GROW_HEURISTICALLY Chris@64: // The builder will heuristically decide how much space to allocate for each segment. Each Chris@64: // allocated segment will be progressively larger than the previous segments on the assumption Chris@64: // that message sizes are exponentially distributed. The total number of segments that will be Chris@64: // allocated for a message of size n is O(log n). Chris@64: }; Chris@64: Chris@64: constexpr uint SUGGESTED_FIRST_SEGMENT_WORDS = 1024; Chris@64: constexpr AllocationStrategy SUGGESTED_ALLOCATION_STRATEGY = AllocationStrategy::GROW_HEURISTICALLY; Chris@64: Chris@64: class MallocMessageBuilder: public MessageBuilder { Chris@64: // A simple MessageBuilder that uses malloc() (actually, calloc()) to allocate segments. This Chris@64: // implementation should be reasonable for any case that doesn't require writing the message to Chris@64: // a specific location in memory. Chris@64: Chris@64: public: Chris@64: explicit MallocMessageBuilder(uint firstSegmentWords = SUGGESTED_FIRST_SEGMENT_WORDS, Chris@64: AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY); Chris@64: // Creates a BuilderContext which allocates at least the given number of words for the first Chris@64: // segment, and then uses the given strategy to decide how much to allocate for subsequent Chris@64: // segments. When choosing a value for firstSegmentWords, consider that: Chris@64: // 1) Reading and writing messages gets slower when multiple segments are involved, so it's good Chris@64: // if most messages fit in a single segment. Chris@64: // 2) Unused bytes will not be written to the wire, so generally it is not a big deal to allocate Chris@64: // more space than you need. It only becomes problematic if you are allocating many messages Chris@64: // in parallel and thus use lots of memory, or if you allocate so much extra space that just Chris@64: // zeroing it out becomes a bottleneck. Chris@64: // The defaults have been chosen to be reasonable for most people, so don't change them unless you Chris@64: // have reason to believe you need to. Chris@64: Chris@64: explicit MallocMessageBuilder(kj::ArrayPtr firstSegment, Chris@64: AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY); Chris@64: // This version always returns the given array for the first segment, and then proceeds with the Chris@64: // allocation strategy. This is useful for optimization when building lots of small messages in Chris@64: // a tight loop: you can reuse the space for the first segment. Chris@64: // Chris@64: // firstSegment MUST be zero-initialized. MallocMessageBuilder's destructor will write new zeros Chris@64: // over any space that was used so that it can be reused. Chris@64: Chris@64: KJ_DISALLOW_COPY(MallocMessageBuilder); Chris@64: virtual ~MallocMessageBuilder() noexcept(false); Chris@64: Chris@64: virtual kj::ArrayPtr allocateSegment(uint minimumSize) override; Chris@64: Chris@64: private: Chris@64: uint nextSize; Chris@64: AllocationStrategy allocationStrategy; Chris@64: Chris@64: bool ownFirstSegment; Chris@64: bool returnedFirstSegment; Chris@64: Chris@64: void* firstSegment; Chris@64: Chris@64: struct MoreSegments; Chris@64: kj::Maybe> moreSegments; Chris@64: }; Chris@64: Chris@64: class FlatMessageBuilder: public MessageBuilder { Chris@64: // THIS IS NOT THE CLASS YOU'RE LOOKING FOR. Chris@64: // Chris@64: // If you want to write a message into already-existing scratch space, use `MallocMessageBuilder` Chris@64: // and pass the scratch space to its constructor. It will then only fall back to malloc() if Chris@64: // the scratch space is not large enough. Chris@64: // Chris@64: // Do NOT use this class unless you really know what you're doing. This class is problematic Chris@64: // because it requires advance knowledge of the size of your message, which is usually impossible Chris@64: // to determine without actually building the message. The class was created primarily to Chris@64: // implement `copyToUnchecked()`, which itself exists only to support other internal parts of Chris@64: // the Cap'n Proto implementation. Chris@64: Chris@64: public: Chris@64: explicit FlatMessageBuilder(kj::ArrayPtr array); Chris@64: KJ_DISALLOW_COPY(FlatMessageBuilder); Chris@64: virtual ~FlatMessageBuilder() noexcept(false); Chris@64: Chris@64: void requireFilled(); Chris@64: // Throws an exception if the flat array is not exactly full. Chris@64: Chris@64: virtual kj::ArrayPtr allocateSegment(uint minimumSize) override; Chris@64: Chris@64: private: Chris@64: kj::ArrayPtr array; Chris@64: bool allocated; Chris@64: }; Chris@64: Chris@64: // ======================================================================================= Chris@64: // implementation details Chris@64: Chris@64: inline const ReaderOptions& MessageReader::getOptions() { Chris@64: return options; Chris@64: } Chris@64: Chris@64: template Chris@64: inline typename RootType::Reader MessageReader::getRoot() { Chris@64: return getRootInternal().getAs(); Chris@64: } Chris@64: Chris@64: template Chris@64: inline typename RootType::Builder MessageBuilder::initRoot() { Chris@64: return getRootInternal().initAs(); Chris@64: } Chris@64: Chris@64: template Chris@64: inline void MessageBuilder::setRoot(Reader&& value) { Chris@64: getRootInternal().setAs>(value); Chris@64: } Chris@64: Chris@64: template Chris@64: inline typename RootType::Builder MessageBuilder::getRoot() { Chris@64: return getRootInternal().getAs(); Chris@64: } Chris@64: Chris@64: template Chris@64: void MessageBuilder::adoptRoot(Orphan&& orphan) { Chris@64: return getRootInternal().adopt(kj::mv(orphan)); Chris@64: } Chris@64: Chris@64: template Chris@64: typename RootType::Reader MessageReader::getRoot(SchemaType schema) { Chris@64: return getRootInternal().getAs(schema); Chris@64: } Chris@64: Chris@64: template Chris@64: typename RootType::Builder MessageBuilder::getRoot(SchemaType schema) { Chris@64: return getRootInternal().getAs(schema); Chris@64: } Chris@64: Chris@64: template Chris@64: typename RootType::Builder MessageBuilder::initRoot(SchemaType schema) { Chris@64: return getRootInternal().initAs(schema); Chris@64: } Chris@64: Chris@64: template Chris@64: typename RootType::Reader readMessageUnchecked(const word* data) { Chris@64: return AnyPointer::Reader(_::PointerReader::getRootUnchecked(data)).getAs(); Chris@64: } Chris@64: Chris@64: template Chris@64: void copyToUnchecked(Reader&& reader, kj::ArrayPtr uncheckedBuffer) { Chris@64: FlatMessageBuilder builder(uncheckedBuffer); Chris@64: builder.setRoot(kj::fwd(reader)); Chris@64: builder.requireFilled(); Chris@64: } Chris@64: Chris@64: template Chris@64: typename RootType::Reader readDataStruct(kj::ArrayPtr data) { Chris@64: return typename RootType::Reader(_::StructReader(data)); Chris@64: } Chris@64: Chris@64: template Chris@64: typename kj::ArrayPtr writeDataStruct(BuilderType builder) { Chris@64: auto bytes = _::PointerHelpers>::getInternalBuilder(kj::mv(builder)) Chris@64: .getDataSectionAsBlob(); Chris@64: return kj::arrayPtr(reinterpret_cast(bytes.begin()), Chris@64: reinterpret_cast(bytes.end())); Chris@64: } Chris@64: Chris@64: template Chris@64: static typename Type::Reader defaultValue() { Chris@64: return typename Type::Reader(_::StructReader()); Chris@64: } Chris@64: Chris@64: template Chris@64: kj::Array canonicalize(T&& reader) { Chris@64: return _::PointerHelpers>::getInternalReader(reader).canonicalize(); Chris@64: } Chris@64: Chris@64: } // namespace capnp Chris@64: Chris@64: #endif // CAPNP_MESSAGE_H_