BitcodeWrapper.cpp revision 825b9aed7c01b050b2ee474327458b8958a28f39
1/*
2 * Copyright 2011, 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 "bcinfo/BitcodeWrapper.h"
18
19#define LOG_TAG "bcinfo"
20#include <cutils/log.h>
21
22#include "llvm/Bitcode/ReaderWriter.h"
23
24#include <cstdlib>
25#include <cstring>
26
27namespace bcinfo {
28
29BitcodeWrapper::BitcodeWrapper(const char *bitcode, size_t bitcodeSize)
30    : mFileType(BC_NOT_BC), mBitcode(bitcode),
31      mBitcodeEnd(bitcode + bitcodeSize - 1), mBitcodeSize(bitcodeSize) {
32  memset(&mBCHeader, 0, sizeof(mBCHeader));
33}
34
35
36BitcodeWrapper::~BitcodeWrapper() {
37  return;
38}
39
40
41bool BitcodeWrapper::unwrap() {
42  if (!mBitcode || !mBitcodeSize) {
43    LOGE("Invalid/empty bitcode");
44    return false;
45  }
46
47  if (llvm::isBitcodeWrapper((const unsigned char*) mBitcode,
48                             (const unsigned char*) mBitcodeEnd)) {
49    if (mBitcodeSize < sizeof(mBCHeader)) {
50      LOGE("Invalid bitcode size");
51      return false;
52    }
53
54    mFileType = BC_WRAPPER;
55    memcpy(&mBCHeader, mBitcode, sizeof(mBCHeader));
56    return true;
57  } else if (llvm::isRawBitcode((const unsigned char*) mBitcode,
58                                (const unsigned char*) mBitcodeEnd)) {
59    mFileType = BC_RAW;
60    return true;
61  } else {
62    LOGE("Not bitcode");
63    mFileType = BC_NOT_BC;
64    return false;
65  }
66}
67
68}  // namespace bcinfo
69
70