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 
getFileName()46     const char* getFileName() const {
47         return mFileName;
48     }
49 
getPackageName()50     const String8 getPackageName() const {
51         return mPackageName;
52     }
53 
setPackageName(String8 packageName)54     void setPackageName(String8 packageName) {
55         mPackageName = packageName;
56     }
57 
getVersion()58     int32_t getVersion() const {
59         return mVersion;
60     }
61 
setVersion(int32_t version)62     void setVersion(int32_t version) {
63         mVersion = version;
64     }
65 
getFlags()66     int32_t getFlags() const {
67         return mFlags;
68     }
69 
setFlags(int32_t flags)70     void setFlags(int32_t flags) {
71         mFlags = flags;
72     }
73 
getSalt(size_t * length)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 
setSalt(const unsigned char * salt,size_t length)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 
isOverlay()94     bool isOverlay() {
95         return (mFlags & OBB_OVERLAY) == OBB_OVERLAY;
96     }
97 
setOverlay(bool overlay)98     void setOverlay(bool overlay) {
99         if (overlay) {
100             mFlags |= OBB_OVERLAY;
101         } else {
102             mFlags &= ~OBB_OVERLAY;
103         }
104     }
105 
get4LE(const unsigned char * buf)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 
put4LE(unsigned char * buf,uint32_t val)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 
124     /* Flags for this OBB type. */
125     int32_t mFlags;
126 
127     /* Whether the file is salted. */
128     bool mSalted;
129 
130     /* The encryption salt. */
131     unsigned char mSalt[8];
132 
133     const char* mFileName;
134 
135     size_t mFileSize;
136 
137     size_t mFooterStart;
138 
139     unsigned char* mReadBuf;
140 
141     bool parseObbFile(int fd);
142 };
143 
144 }
145 #endif /* OBBFILE_H_ */
146