blob: 49e077fc2af6ed137ea108615cceb3170ad65c4e [file] [log] [blame]
Kenny Root6e7ac5f2010-07-19 10:31:34 -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#include <utils/ObbFile.h>
18#include <utils/String8.h>
19
20#include <getopt.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24using namespace android;
25
26static const char* gProgName = "obbtool";
27static const char* gProgVersion = "1.0";
28
29static int wantUsage = 0;
30static int wantVersion = 0;
31
Kenny Root02ca31f2010-08-12 07:36:02 -070032#define ADD_OPTS "n:v:o"
Kenny Root6e7ac5f2010-07-19 10:31:34 -070033static const struct option longopts[] = {
34 {"help", no_argument, &wantUsage, 1},
35 {"version", no_argument, &wantVersion, 1},
36
37 /* Args for "add" */
38 {"name", required_argument, NULL, 'n'},
39 {"version", required_argument, NULL, 'v'},
Kenny Root02ca31f2010-08-12 07:36:02 -070040 {"overlay", optional_argument, NULL, 'o'},
Kenny Root6e7ac5f2010-07-19 10:31:34 -070041
42 {NULL, 0, NULL, '\0'}
43};
44
45struct package_info_t {
46 char* packageName;
47 int packageVersion;
Kenny Root02ca31f2010-08-12 07:36:02 -070048 bool overlay;
Kenny Root6e7ac5f2010-07-19 10:31:34 -070049};
50
51/*
52 * Print usage info.
53 */
54void usage(void)
55{
56 fprintf(stderr, "Opaque Binary Blob (OBB) Tool\n\n");
57 fprintf(stderr, "Usage:\n");
58 fprintf(stderr,
59 " %s a[dd] [ OPTIONS ] FILENAME\n"
60 " Adds an OBB signature to the file.\n\n", gProgName);
61 fprintf(stderr,
62 " %s r[emove] FILENAME\n"
63 " Removes the OBB signature from the file.\n\n", gProgName);
64 fprintf(stderr,
65 " %s i[nfo] FILENAME\n"
66 " Prints the OBB signature information of a file.\n\n", gProgName);
67}
68
69void doAdd(const char* filename, struct package_info_t* info) {
70 ObbFile *obb = new ObbFile();
71 if (obb->readFrom(filename)) {
72 fprintf(stderr, "ERROR: %s: OBB signature already present\n", filename);
73 return;
74 }
75
76 obb->setPackageName(String8(info->packageName));
77 obb->setVersion(info->packageVersion);
Kenny Root02ca31f2010-08-12 07:36:02 -070078 obb->setOverlay(info->overlay);
Kenny Root6e7ac5f2010-07-19 10:31:34 -070079
80 if (!obb->writeTo(filename)) {
81 fprintf(stderr, "ERROR: %s: couldn't write OBB signature: %s\n",
82 filename, strerror(errno));
83 return;
84 }
85
86 fprintf(stderr, "OBB signature successfully written\n");
87}
88
89void doRemove(const char* filename) {
90 ObbFile *obb = new ObbFile();
91 if (!obb->readFrom(filename)) {
92 fprintf(stderr, "ERROR: %s: no OBB signature present\n", filename);
93 return;
94 }
95
96 if (!obb->removeFrom(filename)) {
97 fprintf(stderr, "ERROR: %s: couldn't remove OBB signature\n", filename);
98 return;
99 }
100
101 fprintf(stderr, "OBB signature successfully removed\n");
102}
103
104void doInfo(const char* filename) {
105 ObbFile *obb = new ObbFile();
106 if (!obb->readFrom(filename)) {
107 fprintf(stderr, "ERROR: %s: couldn't read OBB signature\n", filename);
108 return;
109 }
110
111 printf("OBB info for '%s':\n", filename);
112 printf("Package name: %s\n", obb->getPackageName().string());
113 printf(" Version: %d\n", obb->getVersion());
Kenny Root02ca31f2010-08-12 07:36:02 -0700114 printf(" Flags: 0x%08x\n", obb->getFlags());
115 printf(" Overlay: %s\n", obb->isOverlay() ? "true" : "false");
Kenny Root6e7ac5f2010-07-19 10:31:34 -0700116}
117
118/*
119 * Parse args.
120 */
121int main(int argc, char* const argv[])
122{
123 const char *prog = argv[0];
124 struct options *options;
125 int opt;
126 int option_index = 0;
127 struct package_info_t package_info;
128
129 int result = 1; // pessimistically assume an error.
130
131 if (argc < 2) {
132 wantUsage = 1;
133 goto bail;
134 }
135
136 while ((opt = getopt_long(argc, argv, ADD_OPTS, longopts, &option_index)) != -1) {
137 switch (opt) {
138 case 0:
139 if (longopts[option_index].flag)
140 break;
141 fprintf(stderr, "'%s' requires an argument\n", longopts[option_index].name);
142 wantUsage = 1;
143 goto bail;
144 case 'n':
145 package_info.packageName = optarg;
146 break;
Kenny Root02ca31f2010-08-12 07:36:02 -0700147 case 'v': {
Kenny Root6e7ac5f2010-07-19 10:31:34 -0700148 char *end;
149 package_info.packageVersion = strtol(optarg, &end, 10);
150 if (*optarg == '\0' || *end != '\0') {
151 fprintf(stderr, "ERROR: invalid version; should be integer!\n\n");
152 wantUsage = 1;
153 goto bail;
154 }
155 break;
Kenny Root02ca31f2010-08-12 07:36:02 -0700156 }
157 case 'o':
158 package_info.overlay = true;
Kenny Root6e7ac5f2010-07-19 10:31:34 -0700159 break;
160 case '?':
161 wantUsage = 1;
162 goto bail;
163 }
164 }
165
166 if (wantVersion) {
167 fprintf(stderr, "%s %s\n", gProgName, gProgVersion);
168 }
169
170 if (wantUsage) {
171 goto bail;
172 }
173
174#define CHECK_OP(name) \
175 if (strncmp(op, name, opsize)) { \
176 fprintf(stderr, "ERROR: unknown function '%s'!\n\n", op); \
177 wantUsage = 1; \
178 goto bail; \
179 }
180
181 if (optind < argc) {
182 const char* op = argv[optind++];
183 const int opsize = strlen(op);
184
185 if (optind >= argc) {
186 fprintf(stderr, "ERROR: filename required!\n\n");
187 wantUsage = 1;
188 goto bail;
189 }
190
191 const char* filename = argv[optind++];
192
193 switch (op[0]) {
194 case 'a':
195 CHECK_OP("add");
196 if (package_info.packageName == NULL) {
197 fprintf(stderr, "ERROR: arguments required 'packageName' and 'version'\n");
198 goto bail;
199 }
200 doAdd(filename, &package_info);
201 break;
202 case 'r':
203 CHECK_OP("remove");
204 doRemove(filename);
205 break;
206 case 'i':
207 CHECK_OP("info");
208 doInfo(filename);
209 break;
210 default:
211 fprintf(stderr, "ERROR: unknown command '%s'!\n\n", op);
212 wantUsage = 1;
213 goto bail;
214 }
215 }
216
217bail:
218 if (wantUsage) {
219 usage();
220 result = 2;
221 }
222
223 return result;
224}