1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#ifndef ArgumentDecoder_h 27#define ArgumentDecoder_h 28 29#include "ArgumentCoder.h" 30#include "Attachment.h" 31#include <wtf/PassOwnPtr.h> 32#include <wtf/TypeTraits.h> 33#include <wtf/Vector.h> 34 35namespace CoreIPC { 36 37class DataReference; 38 39class ArgumentDecoder { 40public: 41 static PassOwnPtr<ArgumentDecoder> create(const uint8_t* buffer, size_t bufferSize); 42 virtual ~ArgumentDecoder(); 43 44 uint64_t destinationID() const { return m_destinationID; } 45 size_t length() const { return m_bufferEnd - m_buffer; } 46 47 bool isInvalid() const { return m_bufferPos > m_bufferEnd; } 48 void markInvalid() { m_bufferPos = m_bufferEnd + 1; } 49 50 bool decodeFixedLengthData(uint8_t*, size_t, unsigned alignment); 51 52 // The data in the data reference here will only be valid for the lifetime of the ArgumentDecoder object. 53 bool decodeVariableLengthByteArray(DataReference&); 54 55 bool decode(bool&); 56 bool decode(uint8_t&); 57 bool decode(uint16_t&); 58 bool decode(uint32_t&); 59 bool decode(uint64_t&); 60 bool decode(int32_t&); 61 bool decode(int64_t&); 62 bool decode(float&); 63 bool decode(double&); 64 65 template<typename T> bool decodeEnum(T& result) 66 { 67 COMPILE_ASSERT(sizeof(T) <= sizeof(uint64_t), enum_type_must_not_be_larger_than_64_bits); 68 69 uint64_t value; 70 if (!decode(value)) 71 return false; 72 73 result = static_cast<T>(value); 74 return true; 75 } 76 77 template<typename T> 78 bool bufferIsLargeEnoughToContain(size_t numElements) const 79 { 80 COMPILE_ASSERT(WTF::IsArithmetic<T>::value, type_must_have_known_encoded_size); 81 82 if (numElements > std::numeric_limits<size_t>::max() / sizeof(T)) 83 return false; 84 85 return bufferIsLargeEnoughToContain(__alignof(T), numElements * sizeof(T)); 86 } 87 88 // Generic type decode function. 89 template<typename T> bool decode(T& t) 90 { 91 return ArgumentCoder<T>::decode(*this, t); 92 } 93 94 bool removeAttachment(Attachment&); 95 96protected: 97 ArgumentDecoder(const uint8_t* buffer, size_t bufferSize, Vector<Attachment>&); 98 99 void initialize(const uint8_t* buffer, size_t bufferSize); 100 101 bool alignBufferPosition(unsigned alignment, size_t size); 102 bool bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const; 103 104// FIXME: Move m_destinationID to MessageDecoder. 105protected: 106 uint64_t m_destinationID; 107 108private: 109 uint8_t* m_allocatedBase; 110 uint8_t* m_buffer; 111 uint8_t* m_bufferPos; 112 uint8_t* m_bufferEnd; 113 114 Vector<Attachment> m_attachments; 115}; 116 117} // namespace CoreIPC 118 119#endif // ArgumentDecoder_h 120