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) |
| 30 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 31 | class ObbFile : public RefBase { |
| 32 | protected: |
| 33 | virtual ~ObbFile(); |
| 34 | |
| 35 | public: |
| 36 | ObbFile(); |
| 37 | |
| 38 | bool readFrom(const char* filename); |
| 39 | bool readFrom(int fd); |
| 40 | bool writeTo(const char* filename); |
| 41 | bool writeTo(int fd); |
Kenny Root | 6e7ac5f | 2010-07-19 10:31:34 -0700 | [diff] [blame] | 42 | bool removeFrom(const char* filename); |
| 43 | bool removeFrom(int fd); |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 44 | |
| 45 | const char* getFileName() const { |
| 46 | return mFileName; |
| 47 | } |
| 48 | |
| 49 | const String8 getPackageName() const { |
| 50 | return mPackageName; |
| 51 | } |
| 52 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 53 | void setPackageName(String8 packageName) { |
| 54 | mPackageName = packageName; |
| 55 | } |
| 56 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 57 | int32_t getVersion() const { |
| 58 | return mVersion; |
| 59 | } |
| 60 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 61 | void setVersion(int32_t version) { |
| 62 | mVersion = version; |
| 63 | } |
| 64 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 65 | int32_t getFlags() const { |
| 66 | return mFlags; |
| 67 | } |
| 68 | |
| 69 | void setFlags(int32_t flags) { |
| 70 | mFlags = flags; |
| 71 | } |
| 72 | |
| 73 | bool isOverlay() { |
| 74 | return (mFlags & OBB_OVERLAY) == OBB_OVERLAY; |
| 75 | } |
| 76 | |
| 77 | void setOverlay(bool overlay) { |
| 78 | if (overlay) { |
| 79 | mFlags |= OBB_OVERLAY; |
| 80 | } else { |
| 81 | mFlags &= ~OBB_OVERLAY; |
| 82 | } |
| 83 | } |
| 84 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 85 | static inline uint32_t get4LE(const unsigned char* buf) { |
| 86 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 87 | } |
| 88 | |
| 89 | static inline void put4LE(unsigned char* buf, uint32_t val) { |
| 90 | buf[0] = val & 0xFF; |
| 91 | buf[1] = (val >> 8) & 0xFF; |
| 92 | buf[2] = (val >> 16) & 0xFF; |
| 93 | buf[3] = (val >> 24) & 0xFF; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | /* Package name this ObbFile is associated with */ |
| 98 | String8 mPackageName; |
| 99 | |
| 100 | /* Package version this ObbFile is associated with */ |
| 101 | int32_t mVersion; |
| 102 | |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 103 | /* Flags for this OBB type. */ |
| 104 | int32_t mFlags; |
| 105 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 106 | const char* mFileName; |
| 107 | |
| 108 | size_t mFileSize; |
| 109 | |
Kenny Root | 6e7ac5f | 2010-07-19 10:31:34 -0700 | [diff] [blame] | 110 | size_t mFooterStart; |
| 111 | |
Kenny Root | 7cee34a | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 112 | unsigned char* mReadBuf; |
| 113 | |
| 114 | bool parseObbFile(int fd); |
| 115 | }; |
| 116 | |
| 117 | } |
| 118 | #endif /* OBBFILE_H_ */ |