DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memorystream.h Source File

memorystream.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_MEMORYSTREAM_H_
00016 #define RAPIDJSON_MEMORYSTREAM_H_
00017 
00018 #include "stream.h"
00019 
00020 #ifdef __clang__
00021 RAPIDJSON_DIAG_PUSH
00022 RAPIDJSON_DIAG_OFF(unreachable-code)
00023 RAPIDJSON_DIAG_OFF(missing-noreturn)
00024 #endif
00025 
00026 RAPIDJSON_NAMESPACE_BEGIN
00027 
00028 //! Represents an in-memory input byte stream.
00029 /*!
00030     This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream.
00031 
00032     It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file.
00033 
00034     Differences between MemoryStream and StringStream:
00035     1. StringStream has encoding but MemoryStream is a byte stream.
00036     2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source.
00037     3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4().
00038     \note implements Stream concept
00039 */
00040 struct MemoryStream {
00041     typedef char Ch; // byte
00042 
00043     MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
00044 
00045     Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; }
00046     Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; }
00047     size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
00048 
00049     Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
00050     void Put(Ch) { RAPIDJSON_ASSERT(false); }
00051     void Flush() { RAPIDJSON_ASSERT(false); }
00052     size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
00053 
00054     // For encoding detection only.
00055     const Ch* Peek4() const {
00056         return Tell() + 4 <= size_ ? src_ : 0;
00057     }
00058 
00059     const Ch* src_;     //!< Current read position.
00060     const Ch* begin_;   //!< Original head of the string.
00061     const Ch* end_;     //!< End of stream.
00062     size_t size_;       //!< Size of the stream.
00063 };
00064 
00065 RAPIDJSON_NAMESPACE_END
00066 
00067 #ifdef __clang__
00068 RAPIDJSON_DIAG_POP
00069 #endif
00070 
00071 #endif // RAPIDJSON_MEMORYBUFFER_H_