blob: 5243f50051fd3b45a275761d01708c756bef2ca9 [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)
30
Kenny Root7cee34a2010-06-01 10:34:29 -070031class ObbFile : public RefBase {
32protected:
33 virtual ~ObbFile();
34
35public:
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 Root6e7ac5f2010-07-19 10:31:34 -070042 bool removeFrom(const char* filename);
43 bool removeFrom(int fd);
Kenny Root7cee34a2010-06-01 10:34:29 -070044
45 const char* getFileName() const {
46 return mFileName;
47 }
48
49 const String8 getPackageName() const {
50 return mPackageName;
51 }
52
Kenny Root7cee34a2010-06-01 10:34:29 -070053 void setPackageName(String8 packageName) {
54 mPackageName = packageName;
55 }
56
Kenny Root02ca31f2010-08-12 07:36:02 -070057 int32_t getVersion() const {
58 return mVersion;
59 }
60
Kenny Root7cee34a2010-06-01 10:34:29 -070061 void setVersion(int32_t version) {
62 mVersion = version;
63 }
64
Kenny Root02ca31f2010-08-12 07:36:02 -070065 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 Root7cee34a2010-06-01 10:34:29 -070085 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
96private:
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 Root02ca31f2010-08-12 07:36:02 -0700103 /* Flags for this OBB type. */
104 int32_t mFlags;
105
Kenny Root7cee34a2010-06-01 10:34:29 -0700106 const char* mFileName;
107
108 size_t mFileSize;
109
Kenny Root6e7ac5f2010-07-19 10:31:34 -0700110 size_t mFooterStart;
111
Kenny Root7cee34a2010-06-01 10:34:29 -0700112 unsigned char* mReadBuf;
113
114 bool parseObbFile(int fd);
115};
116
117}
118#endif /* OBBFILE_H_ */