2010-06-01 17:34:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "ObbFile"
|
2010-11-24 20:56:06 +00:00
|
|
|
|
|
|
|
#include <utils/Compat.h>
|
2010-06-01 17:34:29 +00:00
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/ObbFile.h>
|
|
|
|
|
|
|
|
//#define DEBUG 1
|
|
|
|
|
|
|
|
#define kFooterTagSize 8 /* last two 32-bit integers */
|
|
|
|
|
2010-10-13 22:00:07 +00:00
|
|
|
#define kFooterMinSize 33 /* 32-bit signature version (4 bytes)
|
2010-08-12 14:36:02 +00:00
|
|
|
* 32-bit package version (4 bytes)
|
|
|
|
* 32-bit flags (4 bytes)
|
2010-10-13 22:00:07 +00:00
|
|
|
* 64-bit salt (8 bytes)
|
|
|
|
* 32-bit package name size (4 bytes)
|
2010-08-12 14:36:02 +00:00
|
|
|
* >=1-character package name (1 byte)
|
|
|
|
* 32-bit footer size (4 bytes)
|
|
|
|
* 32-bit footer marker (4 bytes)
|
2010-06-01 17:34:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define kMaxBufSize 32768 /* Maximum file read buffer */
|
|
|
|
|
|
|
|
#define kSignature 0x01059983U /* ObbFile signature */
|
|
|
|
|
|
|
|
#define kSigVersion 1 /* We only know about signature version 1 */
|
|
|
|
|
|
|
|
/* offsets in version 1 of the header */
|
|
|
|
#define kPackageVersionOffset 4
|
2010-08-12 14:36:02 +00:00
|
|
|
#define kFlagsOffset 8
|
2010-10-13 22:00:07 +00:00
|
|
|
#define kSaltOffset 12
|
|
|
|
#define kPackageNameLenOffset 20
|
|
|
|
#define kPackageNameOffset 24
|
2010-06-01 17:34:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TEMP_FAILURE_RETRY is defined by some, but not all, versions of
|
|
|
|
* <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
|
|
|
|
* not already defined, then define it here.
|
|
|
|
*/
|
|
|
|
#ifndef TEMP_FAILURE_RETRY
|
|
|
|
/* Used to retry syscalls that can return EINTR. */
|
|
|
|
#define TEMP_FAILURE_RETRY(exp) ({ \
|
|
|
|
typeof (exp) _rc; \
|
|
|
|
do { \
|
|
|
|
_rc = (exp); \
|
|
|
|
} while (_rc == -1 && errno == EINTR); \
|
|
|
|
_rc; })
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2010-10-13 22:00:07 +00:00
|
|
|
ObbFile::ObbFile()
|
|
|
|
: mPackageName("")
|
|
|
|
, mVersion(-1)
|
|
|
|
, mFlags(0)
|
2010-08-12 14:36:02 +00:00
|
|
|
{
|
2010-10-13 22:00:07 +00:00
|
|
|
memset(mSalt, 0, sizeof(mSalt));
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObbFile::~ObbFile() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::readFrom(const char* filename)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
fd = ::open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't open file %s: %s", filename, strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
success = readFrom(fd);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (!success) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("failed to read from %s (fd=%d)\n", filename, fd);
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
2010-07-12 16:02:18 +00:00
|
|
|
|
|
|
|
out:
|
2010-06-01 17:34:29 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::readFrom(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("attempt to read from invalid fd\n");
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseObbFile(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::parseObbFile(int fd)
|
|
|
|
{
|
2010-11-24 20:56:06 +00:00
|
|
|
off64_t fileLength = lseek64(fd, 0, SEEK_END);
|
2010-06-01 17:34:29 +00:00
|
|
|
|
|
|
|
if (fileLength < kFooterMinSize) {
|
|
|
|
if (fileLength < 0) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("error seeking in ObbFile: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
} else {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("file is only %lld (less than %d minimum)\n", fileLength, kFooterMinSize);
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t actual;
|
|
|
|
size_t footerSize;
|
|
|
|
|
|
|
|
{
|
2010-11-24 20:56:06 +00:00
|
|
|
lseek64(fd, fileLength - kFooterTagSize, SEEK_SET);
|
2010-06-01 17:34:29 +00:00
|
|
|
|
|
|
|
char *footer = new char[kFooterTagSize];
|
|
|
|
actual = TEMP_FAILURE_RETRY(read(fd, footer, kFooterTagSize));
|
|
|
|
if (actual != kFooterTagSize) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't read footer signature: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int fileSig = get4LE((unsigned char*)footer + sizeof(int32_t));
|
|
|
|
if (fileSig != kSignature) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("footer didn't match magic string (expected 0x%08x; got 0x%08x)\n",
|
2010-06-01 17:34:29 +00:00
|
|
|
kSignature, fileSig);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
footerSize = get4LE((unsigned char*)footer);
|
|
|
|
if (footerSize > (size_t)fileLength - kFooterTagSize
|
|
|
|
|| footerSize > kMaxBufSize) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("claimed footer size is too large (0x%08zx; file size is 0x%08llx)\n",
|
2010-06-01 17:34:29 +00:00
|
|
|
footerSize, fileLength);
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-12 16:02:18 +00:00
|
|
|
|
2010-07-19 17:31:34 +00:00
|
|
|
if (footerSize < (kFooterMinSize - kFooterTagSize)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("claimed footer size is too small (0x%zx; minimum size is 0x%x)\n",
|
2010-07-19 17:31:34 +00:00
|
|
|
footerSize, kFooterMinSize - kFooterTagSize);
|
2010-07-12 16:02:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 20:56:06 +00:00
|
|
|
off64_t fileOffset = fileLength - footerSize - kFooterTagSize;
|
|
|
|
if (lseek64(fd, fileOffset, SEEK_SET) != fileOffset) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("seek %lld failed: %s\n", fileOffset, strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-19 17:31:34 +00:00
|
|
|
mFooterStart = fileOffset;
|
|
|
|
|
2010-07-12 16:02:18 +00:00
|
|
|
char* scanBuf = (char*)malloc(footerSize);
|
2010-06-01 17:34:29 +00:00
|
|
|
if (scanBuf == NULL) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't allocate scanBuf: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-12 16:02:18 +00:00
|
|
|
actual = TEMP_FAILURE_RETRY(read(fd, scanBuf, footerSize));
|
2010-06-01 17:34:29 +00:00
|
|
|
// readAmount is guaranteed to be less than kMaxBufSize
|
2010-07-12 16:02:18 +00:00
|
|
|
if (actual != (ssize_t)footerSize) {
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("couldn't read ObbFile footer: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
free(scanBuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-07-12 16:02:18 +00:00
|
|
|
for (int i = 0; i < footerSize; ++i) {
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("char: 0x%02x\n", scanBuf[i]);
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint32_t sigVersion = get4LE((unsigned char*)scanBuf);
|
|
|
|
if (sigVersion != kSigVersion) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Unsupported ObbFile version %d\n", sigVersion);
|
2010-06-01 17:34:29 +00:00
|
|
|
free(scanBuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVersion = (int32_t) get4LE((unsigned char*)scanBuf + kPackageVersionOffset);
|
2010-08-12 14:36:02 +00:00
|
|
|
mFlags = (int32_t) get4LE((unsigned char*)scanBuf + kFlagsOffset);
|
2010-06-01 17:34:29 +00:00
|
|
|
|
2010-10-13 22:00:07 +00:00
|
|
|
memcpy(&mSalt, (unsigned char*)scanBuf + kSaltOffset, sizeof(mSalt));
|
|
|
|
|
2010-11-24 20:56:06 +00:00
|
|
|
size_t packageNameLen = get4LE((unsigned char*)scanBuf + kPackageNameLenOffset);
|
|
|
|
if (packageNameLen == 0
|
2010-06-01 17:34:29 +00:00
|
|
|
|| packageNameLen > (footerSize - kPackageNameOffset)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("bad ObbFile package name length (0x%04zx; 0x%04zx possible)\n",
|
2010-07-12 16:02:18 +00:00
|
|
|
packageNameLen, footerSize - kPackageNameOffset);
|
2010-06-01 17:34:29 +00:00
|
|
|
free(scanBuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* packageName = reinterpret_cast<char*>(scanBuf + kPackageNameOffset);
|
|
|
|
mPackageName = String8(const_cast<char*>(packageName), packageNameLen);
|
|
|
|
|
|
|
|
free(scanBuf);
|
2010-07-12 16:02:18 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("Obb scan succeeded: packageName=%s, version=%d\n", mPackageName.string(), mVersion);
|
2010-07-12 16:02:18 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-01 17:34:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::writeTo(const char* filename)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
fd = ::open(filename, O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
success = writeTo(fd);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!success) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("failed to write to %s: %s\n", filename, strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::writeTo(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-24 20:56:06 +00:00
|
|
|
lseek64(fd, 0, SEEK_END);
|
2010-07-12 16:02:18 +00:00
|
|
|
|
2010-06-01 17:34:29 +00:00
|
|
|
if (mPackageName.size() == 0 || mVersion == -1) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("tried to write uninitialized ObbFile data\n");
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char intBuf[sizeof(uint32_t)+1];
|
|
|
|
memset(&intBuf, 0, sizeof(intBuf));
|
|
|
|
|
|
|
|
put4LE(intBuf, kSigVersion);
|
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write signature version: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
put4LE(intBuf, mVersion);
|
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write package version\n");
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-12 14:36:02 +00:00
|
|
|
put4LE(intBuf, mFlags);
|
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write package version\n");
|
2010-10-13 22:00:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, mSalt, sizeof(mSalt)) != (ssize_t)sizeof(mSalt)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write salt: %s\n", strerror(errno));
|
2010-08-12 14:36:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-01 17:34:29 +00:00
|
|
|
size_t packageNameLen = mPackageName.size();
|
|
|
|
put4LE(intBuf, packageNameLen);
|
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write package name length: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, mPackageName.string(), packageNameLen) != (ssize_t)packageNameLen) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write package name: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-12 14:36:02 +00:00
|
|
|
put4LE(intBuf, kPackageNameOffset + packageNameLen);
|
2010-06-01 17:34:29 +00:00
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write footer size: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
put4LE(intBuf, kSignature);
|
|
|
|
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("couldn't write footer magic signature: %s\n", strerror(errno));
|
2010-06-01 17:34:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-19 17:31:34 +00:00
|
|
|
bool ObbFile::removeFrom(const char* filename)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
fd = ::open(filename, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
success = removeFrom(fd);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!success) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("failed to remove signature from %s: %s\n", filename, strerror(errno));
|
2010-07-19 17:31:34 +00:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObbFile::removeFrom(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!readFrom(fd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftruncate(fd, mFooterStart);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-01 17:34:29 +00:00
|
|
|
}
|