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