Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 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> |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 21 | #include <strings.h> |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/RefBase.h> |
| 24 | #include <utils/String8.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 28 | // OBB flags (bit 0) |
| 29 | #define OBB_OVERLAY (1 << 0) |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 30 | #define OBB_SALTED (1 << 1) |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 31 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 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); |
Kenny Root | 6e7ac5f | 2010-07-19 10:31:34 -0700 | [diff] [blame] | 43 | bool removeFrom(const char* filename); |
| 44 | bool removeFrom(int fd); |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 45 | |
| 46 | const char* getFileName() const { |
| 47 | return mFileName; |
| 48 | } |
| 49 | |
| 50 | const String8 getPackageName() const { |
| 51 | return mPackageName; |
| 52 | } |
| 53 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 54 | void setPackageName(String8 packageName) { |
| 55 | mPackageName = packageName; |
| 56 | } |
| 57 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 58 | int32_t getVersion() const { |
| 59 | return mVersion; |
| 60 | } |
| 61 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 62 | void setVersion(int32_t version) { |
| 63 | mVersion = version; |
| 64 | } |
| 65 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 66 | int32_t getFlags() const { |
| 67 | return mFlags; |
| 68 | } |
| 69 | |
| 70 | void setFlags(int32_t flags) { |
| 71 | mFlags = flags; |
| 72 | } |
| 73 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 74 | const unsigned char* getSalt(size_t* length) const { |
| 75 | if ((mFlags & OBB_SALTED) == 0) { |
| 76 | *length = 0; |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | *length = sizeof(mSalt); |
| 81 | return mSalt; |
| 82 | } |
| 83 | |
| 84 | bool setSalt(const unsigned char* salt, size_t length) { |
| 85 | if (length != sizeof(mSalt)) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | memcpy(mSalt, salt, sizeof(mSalt)); |
| 90 | mFlags |= OBB_SALTED; |
| 91 | return true; |
| 92 | } |
| 93 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 94 | bool isOverlay() { |
| 95 | return (mFlags & OBB_OVERLAY) == OBB_OVERLAY; |
| 96 | } |
| 97 | |
| 98 | void setOverlay(bool overlay) { |
| 99 | if (overlay) { |
| 100 | mFlags |= OBB_OVERLAY; |
| 101 | } else { |
| 102 | mFlags &= ~OBB_OVERLAY; |
| 103 | } |
| 104 | } |
| 105 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 106 | static inline uint32_t get4LE(const unsigned char* buf) { |
| 107 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 108 | } |
| 109 | |
| 110 | static inline void put4LE(unsigned char* buf, uint32_t val) { |
| 111 | buf[0] = val & 0xFF; |
| 112 | buf[1] = (val >> 8) & 0xFF; |
| 113 | buf[2] = (val >> 16) & 0xFF; |
| 114 | buf[3] = (val >> 24) & 0xFF; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | /* Package name this ObbFile is associated with */ |
| 119 | String8 mPackageName; |
| 120 | |
| 121 | /* Package version this ObbFile is associated with */ |
| 122 | int32_t mVersion; |
| 123 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 124 | /* Flags for this OBB type. */ |
| 125 | int32_t mFlags; |
| 126 | |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 127 | /* Whether the file is salted. */ |
| 128 | bool mSalted; |
| 129 | |
| 130 | /* The encryption salt. */ |
| 131 | unsigned char mSalt[8]; |
| 132 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 133 | const char* mFileName; |
| 134 | |
| 135 | size_t mFileSize; |
| 136 | |
Kenny Root | 6e7ac5f | 2010-07-19 10:31:34 -0700 | [diff] [blame] | 137 | size_t mFooterStart; |
| 138 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 139 | unsigned char* mReadBuf; |
| 140 | |
| 141 | bool parseObbFile(int fd); |
| 142 | }; |
| 143 | |
| 144 | } |
| 145 | #endif /* OBBFILE_H_ */ |