File indexing completed on 2024-04-28 16:59:44

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef ARGUMENTS_P_H
0025 #define ARGUMENTS_P_H
0026 
0027 #include "arguments.h"
0028 
0029 #include "error.h"
0030 
0031 class Arguments::Private
0032 {
0033 public:
0034     Private()
0035        : m_isByteSwapped(false),
0036          m_memOwnership(nullptr)
0037     {}
0038 
0039     static inline Private *get(Arguments *args) { return args->d; }
0040 
0041     Private(const Private &other);
0042     Private &operator=(const Private &other);
0043     void initFrom(const Private &other);
0044     ~Private();
0045 
0046     chunk m_data;
0047     bool m_isByteSwapped;
0048     byte *m_memOwnership;
0049     cstring m_signature;
0050     std::vector<int> m_fileDescriptors;
0051     Error m_error;
0052 };
0053 
0054 struct TypeInfo
0055 {
0056     inline Arguments::IoState state() const { return static_cast<Arguments::IoState>(_state); }
0057     byte _state;
0058     byte alignment : 6;
0059     bool isPrimitive : 1;
0060     bool isString : 1;
0061 };
0062 
0063 // helper to verify the max nesting requirements of the d-bus spec
0064 struct Nesting
0065 {
0066     inline Nesting() : array(0), paren(0), variant(0) {}
0067     static const int arrayMax = 32;
0068     static const int parenMax = 32;
0069     static const int totalMax = 64;
0070 
0071     inline bool beginArray() { array++; return likely(array <= arrayMax && total() <= totalMax); }
0072     inline void endArray() { assert(array >= 1); array--; }
0073     inline bool beginParen() { paren++; return likely(paren <= parenMax && total() <= totalMax); }
0074     inline void endParen() { assert(paren >= 1); paren--; }
0075     inline bool beginVariant() { variant++; return likely(total() <= totalMax); }
0076     inline void endVariant() { assert(variant >= 1); variant--; }
0077     inline uint32 total() { return array + paren + variant; }
0078 
0079     uint32 array;
0080     uint32 paren;
0081     uint32 variant;
0082 };
0083 
0084 cstring printableState(Arguments::IoState state);
0085 bool parseSingleCompleteType(cstring *s, Nesting *nest);
0086 
0087 inline bool isAligned(uint32 value, uint32 alignment)
0088 {
0089     assert(alignment == 8 || alignment == 4 || alignment == 2 || alignment == 1);
0090     return (value & (alignment - 1)) == 0;
0091 }
0092 
0093 enum {
0094     StructAlignment = 8
0095 };
0096 
0097 const TypeInfo &typeInfo(char letterCode);
0098 
0099 // Macros are icky, but here every use saves three lines.
0100 // Funny condition to avoid the dangling-else problem.
0101 #define VALID_IF(cond, errCode) if (likely(cond)) {} else { \
0102     m_state = InvalidData; d->m_error.setCode(errCode); return; }
0103 
0104 #endif // ARGUMENTS_P_H