dex++ 0.3.1
C++ and C API reference
Loading...
Searching...
No Matches
dexpp_model.h File Reference

C API for dex::model, the editable DEX model. More...

#include <stddef.h>
#include <stdint.h>
#include "dexpp.h"

Classes

struct  DexppPoolStats
 How many entries each pool of the written file would have. More...
 
struct  DexppProblem
 One rule of the format a model breaks. More...
 
struct  DexppClassInfo
 Facts about one class of a model. More...
 

Typedefs

typedef struct DexppModelDex DexppModelDex
 An editable DEX file in memory.
 
typedef struct DexppPoolStats DexppPoolStats
 How many entries each pool of the written file would have.
 
typedef struct DexppProblem DexppProblem
 One rule of the format a model breaks.
 
typedef struct DexppClassInfo DexppClassInfo
 Facts about one class of a model.
 

Functions

DexppModelDex * dexpp_model_read (const uint8_t *bytes, size_t len, int debug_info, DexppError **err)
 Read DEX bytes into a model.
 
DexppModelDex * dexpp_model_read_file (const char *path, int debug_info, DexppError **err)
 Read a DEX file into a model.
 
DexppModelDex * dexpp_model_new (uint32_t version)
 An empty model of the given DEX version (35, 37, 38, 39 or 40).
 
DexppModelDex * dexpp_model_clone (const DexppModelDex *dex)
 A deep copy of a model.
 
void dexpp_model_free (DexppModelDex *dex)
 Free a model.
 
uint8_t * dexpp_model_write (const DexppModelDex *dex, int debug_info, size_t *out_len, DexppError **err)
 Write the model to DEX bytes.
 
int dexpp_model_write_file (const DexppModelDex *dex, const char *path, int debug_info, DexppError **err)
 Write straight to a file.
 
uint32_t dexpp_model_version (const DexppModelDex *dex)
 The DEX format version dexpp_model_write() uses.
 
void dexpp_model_set_version (DexppModelDex *dex, uint32_t version)
 Set the DEX format version.
 
uint32_t dexpp_model_required_version (const DexppModelDex *dex)
 The lowest DEX version that has every feature the model uses.
 
DexppPoolStats dexpp_model_pool_stats (const DexppModelDex *dex)
 The pool sizes the model would write.
 
DexppProblem * dexpp_model_validate (const DexppModelDex *dex, size_t *count)
 Every rule of the format the model breaks.
 
void dexpp_problem_array_free (DexppProblem *problems, size_t count)
 Free an array of problems.
 
size_t dexpp_model_class_count (const DexppModelDex *dex)
 The number of classes in the model.
 
char ** dexpp_model_class_descriptors (const DexppModelDex *dex, size_t *count)
 The descriptors of every class, in model order.
 
int dexpp_model_has_class (const DexppModelDex *dex, const char *descriptor)
 1 when the model has a class with this descriptor, otherwise 0.
 
int dexpp_model_class_info (const DexppModelDex *dex, const char *descriptor, DexppClassInfo *out)
 Fill out for the class with this descriptor (release with dexpp_class_info_clear()).
 
void dexpp_class_info_clear (DexppClassInfo *info)
 Free the strings and the interface array of a DexppClassInfo (not the struct), and set them to NULL and the interface count to 0.
 
int dexpp_model_move_class (DexppModelDex *dst, DexppModelDex *src, const char *descriptor, DexppError **err)
 Move the class descriptor out of src and into dst.
 
int dexpp_model_copy_class (DexppModelDex *dst, const DexppModelDex *src, const char *descriptor, DexppError **err)
 Copy the class descriptor from src into dst, leaving src unchanged.
 
int dexpp_model_remove_class (DexppModelDex *dex, const char *descriptor)
 Remove the class descriptor from the model.
 
void dexpp_model_rename_types (DexppModelDex *dex, const char *const *from, const char *const *to, size_t count)
 Rename classes everywhere they are referenced: class definitions, supertypes, member ids, prototypes, array types, annotations and values, call sites, catch types, debug local types, and the class names inside generic signatures.
 

Detailed Description

C API for dex::model, the editable DEX model.

Read a DEX file into an in-memory model, change it, and write a DEX file again. The C++ model is a tree of plain structs (src/model/types.hpp); this API keeps it behind one opaque handle, DexppModelDex, and works at the granularity of whole classes:

  • read a DEX into a model, or make an empty model;
  • inspect its classes (descriptor, access flags, superclass, interfaces, source file, field and method counts);
  • build a new model by moving or copying whole classes between models, which is how a DEX file is split into several files or several merged;
  • rename every reference to a class;
  • validate it, ask the lowest DEX version it needs, count its pools;
  • write it back to DEX bytes.

Editing inside a method is not exposed: a class crosses this boundary whole. Building classes instruction by instruction stays in the C++ API.

The conventions are those of dexpp.h (errors, strings, arrays, _clear functions), which this header includes. A failed write reports DEXPP_ERROR_INVALID_MODEL with the list of problems, DEXPP_ERROR_POOL_OVERFLOW or DEXPP_ERROR_VERSION_TOO_LOW; a failed move or copy reports DEXPP_ERROR_MODEL.

Typedef Documentation

◆ DexppModelDex

typedef struct DexppModelDex DexppModelDex

An editable DEX file in memory.

◆ DexppPoolStats

How many entries each pool of the written file would have.

Use it to decide how to split classes across files: a DEX file holds at most 65535 types and prototypes, and at most 65536 fields, methods, method handles and call sites.

◆ DexppProblem

typedef struct DexppProblem DexppProblem

One rule of the format a model breaks.

◆ DexppClassInfo

Facts about one class of a model.

The member names follow the analysis API (descriptor, access_flags), not the C++ model's ClassDef (type, access). Release with dexpp_class_info_clear().

Function Documentation

◆ dexpp_model_read()

DexppModelDex * dexpp_model_read ( const uint8_t *  bytes,
size_t  len,
int  debug_info,
DexppError **  err 
)

Read DEX bytes into a model.

debug_info nonzero keeps line numbers and local variables. Errors: INVALID_DEX, UNSUPPORTED_DEX.

◆ dexpp_model_read_file()

DexppModelDex * dexpp_model_read_file ( const char *  path,
int  debug_info,
DexppError **  err 
)

Read a DEX file into a model.

Errors: FILE_NOT_FOUND, CANNOT_READ, INVALID_DEX, UNSUPPORTED_DEX.

◆ dexpp_model_new()

DexppModelDex * dexpp_model_new ( uint32_t  version)

An empty model of the given DEX version (35, 37, 38, 39 or 40).

◆ dexpp_model_clone()

DexppModelDex * dexpp_model_clone ( const DexppModelDex *  dex)

A deep copy of a model.

◆ dexpp_model_free()

void dexpp_model_free ( DexppModelDex *  dex)

Free a model.

Accepts NULL.

◆ dexpp_model_write()

uint8_t * dexpp_model_write ( const DexppModelDex *  dex,
int  debug_info,
size_t *  out_len,
DexppError **  err 
)

Write the model to DEX bytes.

debug_info nonzero writes debug info. On success returns a malloc'd buffer of *out_len bytes (free with dexpp_buffer_free()). Errors: INVALID_MODEL (with the problems), POOL_OVERFLOW, VERSION_TOO_LOW.

◆ dexpp_model_write_file()

int dexpp_model_write_file ( const DexppModelDex *  dex,
const char *  path,
int  debug_info,
DexppError **  err 
)

Write straight to a file.

Returns 1 on success, 0 on failure. Errors as dexpp_model_write(), plus CANNOT_WRITE.

◆ dexpp_model_version()

uint32_t dexpp_model_version ( const DexppModelDex *  dex)

The DEX format version dexpp_model_write() uses.

◆ dexpp_model_set_version()

void dexpp_model_set_version ( DexppModelDex *  dex,
uint32_t  version 
)

Set the DEX format version.

Writing never raises it by itself.

◆ dexpp_model_required_version()

uint32_t dexpp_model_required_version ( const DexppModelDex *  dex)

The lowest DEX version that has every feature the model uses.

◆ dexpp_model_pool_stats()

DexppPoolStats dexpp_model_pool_stats ( const DexppModelDex *  dex)

The pool sizes the model would write.

◆ dexpp_model_validate()

DexppProblem * dexpp_model_validate ( const DexppModelDex *  dex,
size_t *  count 
)

Every rule of the format the model breaks.

Free with dexpp_problem_array_free(). An empty array (NULL, *count 0) means dexpp_model_write() can encode the model.

◆ dexpp_problem_array_free()

void dexpp_problem_array_free ( DexppProblem *  problems,
size_t  count 
)

Free an array of problems.

Accepts NULL.

◆ dexpp_model_class_count()

size_t dexpp_model_class_count ( const DexppModelDex *  dex)

The number of classes in the model.

It copies nothing, unlike dexpp_model_class_descriptors().

◆ dexpp_model_class_descriptors()

char ** dexpp_model_class_descriptors ( const DexppModelDex *  dex,
size_t *  count 
)

The descriptors of every class, in model order.

Free with dexpp_string_array_free().

◆ dexpp_model_has_class()

int dexpp_model_has_class ( const DexppModelDex *  dex,
const char *  descriptor 
)

1 when the model has a class with this descriptor, otherwise 0.

It copies nothing, unlike dexpp_model_class_info().

◆ dexpp_model_class_info()

int dexpp_model_class_info ( const DexppModelDex *  dex,
const char *  descriptor,
DexppClassInfo *  out 
)

Fill out for the class with this descriptor (release with dexpp_class_info_clear()).

Returns 0 when there is no such class; then out is untouched.

◆ dexpp_class_info_clear()

void dexpp_class_info_clear ( DexppClassInfo *  info)

Free the strings and the interface array of a DexppClassInfo (not the struct), and set them to NULL and the interface count to 0.

◆ dexpp_model_move_class()

int dexpp_model_move_class ( DexppModelDex *  dst,
DexppModelDex *  src,
const char *  descriptor,
DexppError **  err 
)

Move the class descriptor out of src and into dst.

The class's data is moved, not copied, so this is cheap for a large class. Returns 1 on success; 0 and sets *err (DEXPP_ERROR_MODEL) if src has no such class, or dst already has one with that descriptor.

◆ dexpp_model_copy_class()

int dexpp_model_copy_class ( DexppModelDex *  dst,
const DexppModelDex *  src,
const char *  descriptor,
DexppError **  err 
)

Copy the class descriptor from src into dst, leaving src unchanged.

Returns 1 on success; 0 and sets *err as dexpp_model_move_class does.

◆ dexpp_model_remove_class()

int dexpp_model_remove_class ( DexppModelDex *  dex,
const char *  descriptor 
)

Remove the class descriptor from the model.

Returns 1 if it was removed, 0 if there was no such class.

◆ dexpp_model_rename_types()

void dexpp_model_rename_types ( DexppModelDex *  dex,
const char *const *  from,
const char *const *  to,
size_t  count 
)

Rename classes everywhere they are referenced: class definitions, supertypes, member ids, prototypes, array types, annotations and values, call sites, catch types, debug local types, and the class names inside generic signatures.

from[i] -> to[i] for each of the count pairs; a descriptor not listed is left unchanged. String constants are never renamed.