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