blob: 47559cdd0d618db69128580fa44d58d7876611bb [file] [log] [blame]
Kenny Root7cee34a2010-06-01 10:34:29 -07001/*
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 Root02ca31f2010-08-12 07:36:02 -070021#include <strings.h>
Kenny Root7cee34a2010-06-01 10:34:29 -070022
23#include <utils/RefBase.h>
24#include <utils/String8.h>
25
26namespace android {
27
Kenny Root02ca31f2010-08-12 07:36:02 -070028// OBB flags (bit 0)
29#define OBB_OVERLAY (1 << 0)
Kenny Root3b1abba2010-10-13 15:00:07 -070030#define OBB_SALTED (1 << 1)
Kenny Root02ca31f2010-08-12 07:36:02 -070031
Kenny Root7cee34a2010-06-01 10:34:29 -070032class ObbFile : public RefBase {
33protected:
34 virtual ~ObbFile();
35
36public:
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 Root6e7ac5f2010-07-19 10:31:34 -070043 bool removeFrom(const char* filename);
44 bool removeFrom(int fd);
Kenny Root7cee34a2010-06-01 10:34:29 -070045
46 const char* getFileName() const {
47 return mFileName;
48 }
49
50 const String8 getPackageName() const {
51 return mPackageName;
52 }
53
Kenny Root7cee34a2010-06-01 10:34:29 -070054 void setPackageName(String8 packageName) {
55 mPackageName = packageName;
56 }
57
Kenny Root02ca31f2010-08-12 07:36:02 -070058 int32_t getVersion() const {
59 return mVersion;
60 }
61
Kenny Root7cee34a2010-06-01 10:34:29 -070062 void setVersion(int32_t version) {
63 mVersion = version;
64 }
65
Kenny Root02ca31f2010-08-12 07:36:02 -070066 int32_t getFlags() const {
67 return mFlags;
68 }
69
70 void setFlags(int32_t flags) {
71 mFlags = flags;
72 }
73
Kenny Root3b1abba2010-10-13 15:00:07 -070074 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 Root02ca31f2010-08-12 07:36:02 -070094 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 Root7cee34a2010-06-01 10:34:29 -0700106 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
117private:
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 Root02ca31f2010-08-12 07:36:02 -0700124 /* Flags for this OBB type. */
125 int32_t mFlags;
126
Kenny Root3b1abba2010-10-13 15:00:07 -0700127 /* Whether the file is salted. */
128 bool mSalted;
129
130 /* The encryption salt. */
131 unsigned char mSalt[8];
132
Kenny Root7cee34a2010-06-01 10:34:29 -0700133 const char* mFileName;
134
135 size_t mFileSize;
136
Kenny Root6e7ac5f2010-07-19 10:31:34 -0700137 size_t mFooterStart;
138
Kenny Root7cee34a2010-06-01 10:34:29 -0700139 unsigned char* mReadBuf;
140
141 bool parseObbFile(int fd);
142};
143
144}
145#endif /* OBBFILE_H_ */