dex++ 0.3.1
C++ and C API reference
Loading...
Searching...
No Matches
dex::apk::Apk Class Reference

A loaded Android package. More...

#include <apk/apk.hpp>

Public Member Functions

std::vector< std::string > entries () const
 Names of every entry in the archive, in central-directory order.
 
std::optional< std::vector< uint8_t > > read (std::string_view name) const
 Raw (decompressed) bytes of one archive entry, or nullopt if absent.
 
const Manifest * manifest () const
 The decoded AndroidManifest.xml.
 
const ResourceTable * resources () const
 The decoded resources.arsc table.
 
std::optional< std::string_view > resolve_string (uint32_t reference_id) const
 Convenience: resolve a resource reference id (e.g. the value of android:label="@string/app_name") to its string, via resources().
 
const SigningInfo & signing () const
 Signing schemes and certificates.
 
std::expected< AnalysisContext, Error > analysis (const ApkLoadOptions &options={}) const
 A code-analysis context over every classes*.dex in the APK, in multidex order, loaded from the bytes the Apk already holds.
 

Static Public Member Functions

static std::expected< Apk, Error > open (const std::string &path)
 Open an APK (ZIP) from disk.
 
static std::expected< Apk, Error > from_buffer (std::vector< uint8_t > bytes, const std::string &label="<apk>")
 Open an APK (ZIP) whose bytes are already in memory, for example bytes received over the network or uploaded by a user.
 
static std::expected< Apk, Error > from_buffer (std::span< const uint8_t > bytes, const std::string &label="<apk>")
 Open an APK (ZIP) from a view of bytes in memory.
 

Detailed Description

A loaded Android package.

Owns the archive bytes; lazily decodes the manifest and lazily builds the code-analysis context on first request. Cheap to copy (shared ownership of the impl).

Member Function Documentation

◆ open()

static std::expected< Apk, Error > dex::apk::Apk::open ( const std::string &  path)
static

Open an APK (ZIP) from disk.

Returns
The Apk on success. On failure, an Error with code FileNotFound if the path does not exist, CannotRead if it cannot be read (a directory, no permission), or InvalidApk if it is not a ZIP archive.

◆ from_buffer() [1/2]

static std::expected< Apk, Error > dex::apk::Apk::from_buffer ( std::vector< uint8_t >  bytes,
const std::string &  label = "<apk>" 
)
static

Open an APK (ZIP) whose bytes are already in memory, for example bytes received over the network or uploaded by a user.

The bytes go through the same checks as the bytes open() reads from a file, and every accessor then works the same.

The Apk owns its bytes. This overload takes the vector by value, so a caller that passes it with std::move gives its memory to the Apk and nothing is copied. An lvalue vector is copied.

Parameters
bytesThe content of an APK file.
labelNames the bytes in error messages, where open() puts the path, for example the name of the file they came from. It is also used for the errors of analysis().
Returns
The Apk on success. On failure, an Error with code InvalidApk if the bytes are not a ZIP archive.

◆ from_buffer() [2/2]

static std::expected< Apk, Error > dex::apk::Apk::from_buffer ( std::span< const uint8_t >  bytes,
const std::string &  label = "<apk>" 
)
static

Open an APK (ZIP) from a view of bytes in memory.

The bytes are copied, so the caller may free them as soon as this returns. Otherwise the same as the overload that takes a vector.

◆ entries()

std::vector< std::string > dex::apk::Apk::entries ( ) const

Names of every entry in the archive, in central-directory order.

◆ read()

std::optional< std::vector< uint8_t > > dex::apk::Apk::read ( std::string_view  name) const

Raw (decompressed) bytes of one archive entry, or nullopt if absent.

◆ manifest()

const Manifest * dex::apk::Apk::manifest ( ) const

The decoded AndroidManifest.xml.

Parsed once on first call and cached. nullptr if the APK has no manifest or it fails to decode. Boolean and integer attributes given as resource references are resolved through resources() (see manifest.hpp).

◆ resources()

const ResourceTable * dex::apk::Apk::resources ( ) const

The decoded resources.arsc table.

Parsed once on first call and cached. nullptr if the APK has no resource table or it fails to decode.

◆ resolve_string()

std::optional< std::string_view > dex::apk::Apk::resolve_string ( uint32_t  reference_id) const

Convenience: resolve a resource reference id (e.g. the value of android:label="@string/app_name") to its string, via resources().

nullopt if there is no resource table or the id is not a string.

◆ signing()

const SigningInfo & dex::apk::Apk::signing ( ) const

Signing schemes and certificates.

Identification only — digests and signatures are NOT verified. Computed once on first call and cached; an unsigned APK yields a default SigningInfo (is_signed() == false).

◆ analysis()

std::expected< AnalysisContext, Error > dex::apk::Apk::analysis ( const ApkLoadOptions &  options = {}) const

A code-analysis context over every classes*.dex in the APK, in multidex order, loaded from the bytes the Apk already holds.

options works as for AnalysisContext::from_apk: pass {.threads = 1} to keep all the work, the call graph included, on the calling thread.

The result is cached per value of options. The first call with a given value builds the context, and every later call with an equal value returns that same context, so its lazy structures (call graph, class hierarchy, cross-references) are built once. A call with different options builds and keeps a second, separate context, which holds its own copy of the DEX files; the two give the same answers. So call it with one setting in a program. A failure is cached the same way.

Returns
The context on success. On failure, an Error with code InvalidApk if the APK has no classes.dex or an entry is damaged, or InvalidDex if a DEX file is malformed. The message starts with the path given to open() or the label given to from_buffer().

The documentation for this class was generated from the following file: