drgn/libdrgn/serialize.h
Omar Sandoval 75c3679147 Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:

- It's too slow for some common use cases, like iterating over large
  data structures.
- It can't be reused in utilities written in other languages.

This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:

- Types are now represented by a single Type class rather than the messy
  polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
  functions.

The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.

Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-04-02 14:12:07 -07:00

98 lines
2.9 KiB
C

// Copyright 2018-2019 - Omar Sandoval
// SPDX-License-Identifier: GPL-3.0+
/**
* @file
*
* Serialization and deserialization to and from memory.
*
* See @ref SerializationDeserialization.
*/
#ifndef DRGN_SERIALIZE_H
#define DRGN_SERIALIZE_H
#include <stdbool.h>
#include <stdint.h>
/**
* @ingroup Internals
*
* @defgroup SerializationDeserialization Serialization/deserialization
*
* Serialization and deserialization of bits to and from memory.
*
* @{
*/
/** Copy the @p bit_size%th bit to the more-significant bits in an integer. */
static inline int64_t sign_extend(int64_t svalue, uint64_t bit_size)
{
if (bit_size < 64) {
int64_t mask;
mask = INT64_C(1) << (bit_size - 1);
svalue = (svalue ^ mask) - mask;
}
return svalue;
}
/** Truncate a signed integer to @p bit_size bits. */
static inline int64_t truncate_signed(int64_t svalue, uint64_t bit_size)
{
if (bit_size < 64) {
/* INT64_C(1) << 63 is undefined, so this must be UINT64_C. */
svalue &= (UINT64_C(1) << bit_size) - 1;
}
return sign_extend(svalue, bit_size);
}
/** Truncate an unsigned integer to @p bit_size bits. */
static inline uint64_t truncate_unsigned(uint64_t uvalue, uint64_t bit_size)
{
if (bit_size < 64)
uvalue &= (UINT64_C(1) << bit_size) - 1;
return uvalue;
}
/**
* Serialize bits to a memory buffer.
*
* Note that this does not perform any bounds checking, so the caller must check
* that <tt>bit_offset + bit_size</tt> is within the buffer.
*
* @param[in] buf Memory buffer to write to.
* @param[in] bit_offset Offset in bits from the beginning of @p buf to where to
* write. This is interpreted differently based on @p little_endian.
* @param[in] uvalue Bits to write, in host order.
* @param[in] bit_size Number of bits in @p uvalue. This must be grather than
* zero and no more than 64. Note that this is not checked or truncated, so if
* @p uvalue has more than this many bits, the results will likely be incorrect.
* @param[in] little_endian Whether the bits should be written out in
* little-endian order.
*/
void serialize_bits(void *buf, uint64_t bit_offset, uint64_t uvalue,
uint8_t bit_size, bool little_endian);
/**
* Deserialize bits from a memory buffer.
*
* Note that this does not perform any bounds checking, so the caller must check
* that <tt>bit_offset + bit_size</tt> is within the buffer.
*
* @param[in] buf Memory buffer to read from.
* @param[in] bit_offset Offset in bits from the beginning of @p buf to where to
* read from. This is interpreted differently based on @p little_endian.
* @param[in] bit_size Number of bits to read. This must be grather than zero
* and no more than 64.
* @param[in] little_endian Whether the bits should be interpreted in
* little-endian order.
* @return The read bits in host order.
*/
uint64_t deserialize_bits(const void *buf, uint64_t bit_offset,
uint8_t bit_size, bool little_endian);
/** @} */
#endif /* DRGN_SERIALIZE_H */