1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de> 4 * Distributed under the terms of the MIT License. 5 */ 6 #ifndef _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_ 7 #define _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_ 8 9 10 #include <SupportDefs.h> 11 12 #include <util/SinglyLinkedList.h> 13 14 #include <package/hpkg/PackageAttributeValue.h> 15 #include <package/hpkg/PackageContentHandler.h> 16 #include <package/hpkg/PackageInfoAttributeValue.h> 17 18 19 namespace BPackageKit { 20 21 namespace BHPKG { 22 23 24 class BErrorOutput; 25 26 27 namespace BPrivate { 28 29 30 class ReaderImplBase { 31 protected: 32 ReaderImplBase( 33 BErrorOutput* errorOutput); 34 virtual ~ReaderImplBase(); 35 36 virtual status_t Init(int fd, bool keepFD); 37 38 int FD() const; 39 40 BErrorOutput* ErrorOutput() const; 41 42 protected: 43 struct AttributeHandlerContext { 44 BErrorOutput* errorOutput; 45 union { 46 BPackageContentHandler* packageContentHandler; 47 BLowLevelPackageContentHandler* lowLevelHandler; 48 }; 49 bool hasLowLevelHandler; 50 51 uint64 heapOffset; 52 uint64 heapSize; 53 54 BHPKGPackageSectionID section; 55 56 AttributeHandlerContext(BErrorOutput* errorOutput, 57 BPackageContentHandler* packageContentHandler, 58 BHPKGPackageSectionID section); 59 60 AttributeHandlerContext(BErrorOutput* errorOutput, 61 BLowLevelPackageContentHandler* lowLevelHandler, 62 BHPKGPackageSectionID section); 63 64 void ErrorOccurred(); 65 }; 66 67 68 typedef BPackageAttributeValue AttributeValue; 69 70 struct AttributeHandler 71 : SinglyLinkedListLinkImpl<AttributeHandler> { 72 virtual ~AttributeHandler(); 73 74 void SetLevel(int level); 75 virtual status_t HandleAttribute( 76 AttributeHandlerContext* context, uint8 id, 77 const AttributeValue& value, AttributeHandler** _handler); 78 79 virtual status_t Delete(AttributeHandlerContext* context); 80 81 protected: 82 int fLevel; 83 }; 84 85 86 struct IgnoreAttributeHandler : AttributeHandler { 87 }; 88 89 90 struct PackageVersionAttributeHandler : AttributeHandler { 91 PackageVersionAttributeHandler( 92 BPackageInfoAttributeValue& packageInfoValue, 93 BPackageVersionData& versionData, bool notify); 94 95 virtual status_t HandleAttribute( 96 AttributeHandlerContext* context, uint8 id, 97 const AttributeValue& value, AttributeHandler** _handler); 98 99 virtual status_t Delete(AttributeHandlerContext* context); 100 101 private: 102 BPackageInfoAttributeValue& fPackageInfoValue; 103 BPackageVersionData& fPackageVersionData; 104 bool fNotify; 105 }; 106 107 108 struct PackageResolvableAttributeHandler : AttributeHandler { 109 PackageResolvableAttributeHandler( 110 BPackageInfoAttributeValue& packageInfoValue); 111 112 virtual status_t HandleAttribute( 113 AttributeHandlerContext* context, uint8 id, 114 const AttributeValue& value, AttributeHandler** _handler); 115 116 virtual status_t Delete(AttributeHandlerContext* context); 117 118 private: 119 BPackageInfoAttributeValue& fPackageInfoValue; 120 }; 121 122 123 struct PackageResolvableExpressionAttributeHandler 124 : AttributeHandler { 125 PackageResolvableExpressionAttributeHandler( 126 BPackageInfoAttributeValue& packageInfoValue); 127 128 virtual status_t HandleAttribute( 129 AttributeHandlerContext* context, uint8 id, 130 const AttributeValue& value, AttributeHandler** _handler); 131 132 virtual status_t Delete(AttributeHandlerContext* context); 133 134 private: 135 BPackageInfoAttributeValue& fPackageInfoValue; 136 }; 137 138 139 struct PackageAttributeHandler : AttributeHandler { 140 virtual status_t HandleAttribute( 141 AttributeHandlerContext* context, uint8 id, 142 const AttributeValue& value, AttributeHandler** _handler); 143 144 private: 145 BPackageInfoAttributeValue fPackageInfoValue; 146 }; 147 148 149 struct LowLevelAttributeHandler : AttributeHandler { 150 LowLevelAttributeHandler(); 151 LowLevelAttributeHandler(uint8 id, 152 const BPackageAttributeValue& value, void* parentToken, 153 void* token); 154 155 virtual status_t HandleAttribute( 156 AttributeHandlerContext* context, uint8 id, 157 const AttributeValue& value, AttributeHandler** _handler); 158 virtual status_t Delete(AttributeHandlerContext* context); 159 160 private: 161 void* fParentToken; 162 void* fToken; 163 uint8 fID; 164 AttributeValue fValue; 165 }; 166 167 168 struct SectionInfo { 169 uint32 compression; 170 uint32 compressedLength; 171 uint32 uncompressedLength; 172 uint8* data; 173 uint64 offset; 174 uint64 currentOffset; 175 uint64 stringsLength; 176 uint64 stringsCount; 177 char** strings; 178 const char* name; 179 180 SectionInfo(const char* _name) 181 : 182 data(NULL), 183 strings(NULL), 184 name(_name) 185 { 186 } 187 188 ~SectionInfo() 189 { 190 delete[] strings; 191 delete[] data; 192 } 193 }; 194 195 typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList; 196 197 protected: 198 const char* CheckCompression( 199 const SectionInfo& section) const; 200 201 status_t ParseStrings(); 202 203 status_t ParsePackageAttributesSection( 204 AttributeHandlerContext* context, 205 AttributeHandler* rootAttributeHandler); 206 status_t ParseAttributeTree( 207 AttributeHandlerContext* context, 208 bool& _sectionHandled); 209 210 virtual status_t ReadAttributeValue(uint8 type, uint8 encoding, 211 AttributeValue& _value); 212 213 status_t ReadUnsignedLEB128(uint64& _value); 214 215 status_t ReadBuffer(off_t offset, void* buffer, 216 size_t size); 217 status_t ReadCompressedBuffer( 218 const SectionInfo& section); 219 220 inline AttributeHandler* CurrentAttributeHandler() const; 221 inline void PushAttributeHandler( 222 AttributeHandler* handler); 223 inline AttributeHandler* PopAttributeHandler(); 224 inline void ClearAttributeHandlerStack(); 225 226 inline SectionInfo* CurrentSection(); 227 inline void SetCurrentSection(SectionInfo* section); 228 229 protected: 230 SectionInfo fPackageAttributesSection; 231 232 private: 233 status_t _ParseAttributeTree( 234 AttributeHandlerContext* context); 235 236 template<typename Type> 237 inline status_t _Read(Type& _value); 238 239 status_t _ReadSectionBuffer(void* buffer, size_t size); 240 241 status_t _ReadAttribute(uint8& _id, 242 AttributeValue& _value, 243 bool* _hasChildren = NULL, 244 uint64* _tag = NULL); 245 246 status_t _ReadString(const char*& _string, 247 size_t* _stringLength = NULL); 248 249 private: 250 BErrorOutput* fErrorOutput; 251 int fFD; 252 bool fOwnsFD; 253 254 SectionInfo* fCurrentSection; 255 256 AttributeHandlerList fAttributeHandlerStack; 257 258 uint8* fScratchBuffer; 259 size_t fScratchBufferSize; 260 }; 261 262 263 inline int 264 ReaderImplBase::FD() const 265 { 266 return fFD; 267 } 268 269 270 inline BErrorOutput* 271 ReaderImplBase::ErrorOutput() const 272 { 273 return fErrorOutput; 274 } 275 276 277 ReaderImplBase::SectionInfo* 278 ReaderImplBase::CurrentSection() 279 { 280 return fCurrentSection; 281 } 282 283 284 void 285 ReaderImplBase::SetCurrentSection(SectionInfo* section) 286 { 287 fCurrentSection = section; 288 } 289 290 291 template<typename Type> 292 status_t 293 ReaderImplBase::_Read(Type& _value) 294 { 295 return _ReadSectionBuffer(&_value, sizeof(Type)); 296 } 297 298 299 inline ReaderImplBase::AttributeHandler* 300 ReaderImplBase::CurrentAttributeHandler() const 301 { 302 return fAttributeHandlerStack.Head(); 303 } 304 305 306 inline void 307 ReaderImplBase::PushAttributeHandler(AttributeHandler* handler) 308 { 309 fAttributeHandlerStack.Add(handler); 310 } 311 312 313 inline ReaderImplBase::AttributeHandler* 314 ReaderImplBase::PopAttributeHandler() 315 { 316 return fAttributeHandlerStack.RemoveHead(); 317 } 318 319 320 inline void 321 ReaderImplBase::ClearAttributeHandlerStack() 322 { 323 fAttributeHandlerStack.MakeEmpty(); 324 } 325 326 327 } // namespace BPrivate 328 329 } // namespace BHPKG 330 331 } // namespace BPackageKit 332 333 334 #endif // _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_ 335