1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef OBBFILE_H_ 18 #define OBBFILE_H_ 19 20 #include <stdint.h> 21 #include <strings.h> 22 23 #include <utils/RefBase.h> 24 #include <utils/String8.h> 25 26 namespace android { 27 28 // OBB flags (bit 0) 29 #define OBB_OVERLAY (1 << 0) 30 #define OBB_SALTED (1 << 1) 31 32 class ObbFile : public RefBase { 33 protected: 34 virtual ~ObbFile(); 35 36 public: 37 ObbFile(); 38 39 bool readFrom(const char* filename); 40 bool readFrom(int fd); 41 bool writeTo(const char* filename); 42 bool writeTo(int fd); 43 bool removeFrom(const char* filename); 44 bool removeFrom(int fd); 45 getPackageName()46 const String8 getPackageName() const { 47 return mPackageName; 48 } 49 setPackageName(String8 packageName)50 void setPackageName(String8 packageName) { 51 mPackageName = packageName; 52 } 53 getVersion()54 int32_t getVersion() const { 55 return mVersion; 56 } 57 setVersion(int32_t version)58 void setVersion(int32_t version) { 59 mVersion = version; 60 } 61 getFlags()62 int32_t getFlags() const { 63 return mFlags; 64 } 65 setFlags(int32_t flags)66 void setFlags(int32_t flags) { 67 mFlags = flags; 68 } 69 getSalt(size_t * length)70 const unsigned char* getSalt(size_t* length) const { 71 if ((mFlags & OBB_SALTED) == 0) { 72 *length = 0; 73 return NULL; 74 } 75 76 *length = sizeof(mSalt); 77 return mSalt; 78 } 79 setSalt(const unsigned char * salt,size_t length)80 bool setSalt(const unsigned char* salt, size_t length) { 81 if (length != sizeof(mSalt)) { 82 return false; 83 } 84 85 memcpy(mSalt, salt, sizeof(mSalt)); 86 mFlags |= OBB_SALTED; 87 return true; 88 } 89 isOverlay()90 bool isOverlay() { 91 return (mFlags & OBB_OVERLAY) == OBB_OVERLAY; 92 } 93 setOverlay(bool overlay)94 void setOverlay(bool overlay) { 95 if (overlay) { 96 mFlags |= OBB_OVERLAY; 97 } else { 98 mFlags &= ~OBB_OVERLAY; 99 } 100 } 101 get4LE(const unsigned char * buf)102 static inline uint32_t get4LE(const unsigned char* buf) { 103 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); 104 } 105 put4LE(unsigned char * buf,uint32_t val)106 static inline void put4LE(unsigned char* buf, uint32_t val) { 107 buf[0] = val & 0xFF; 108 buf[1] = (val >> 8) & 0xFF; 109 buf[2] = (val >> 16) & 0xFF; 110 buf[3] = (val >> 24) & 0xFF; 111 } 112 113 private: 114 /* Package name this ObbFile is associated with */ 115 String8 mPackageName; 116 117 /* Package version this ObbFile is associated with */ 118 int32_t mVersion; 119 120 /* Flags for this OBB type. */ 121 int32_t mFlags; 122 123 /* The encryption salt. */ 124 unsigned char mSalt[8]; 125 126 size_t mFooterStart; 127 128 bool parseObbFile(int fd); 129 }; 130 131 } 132 #endif /* OBBFILE_H_ */ 133