From 90fc3833203192ff5a6009d339454b0265b4efc1 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 4 Feb 2022 04:21:28 +0100 Subject: Add support for Apple's OPACK encoding and TLV format --- include/Makefile.am | 7 +- include/endianness.h | 123 ++++++++++++++++++++++++++++++++++ include/libimobiledevice-glue/opack.h | 29 ++++++++ include/libimobiledevice-glue/tlv.h | 42 ++++++++++++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 include/endianness.h create mode 100644 include/libimobiledevice-glue/opack.h create mode 100644 include/libimobiledevice-glue/tlv.h (limited to 'include') diff --git a/include/Makefile.am b/include/Makefile.am index 3684b8f..a071583 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,7 +1,12 @@ +EXTRA_DIST = \ + endianness.h + nobase_include_HEADERS = \ libimobiledevice-glue/socket.h \ libimobiledevice-glue/thread.h \ libimobiledevice-glue/utils.h \ libimobiledevice-glue/collection.h \ libimobiledevice-glue/termcolors.h \ - libimobiledevice-glue/cbuf.h + libimobiledevice-glue/cbuf.h \ + libimobiledevice-glue/opack.h \ + libimobiledevice-glue/tlv.h diff --git a/include/endianness.h b/include/endianness.h new file mode 100644 index 0000000..099877a --- /dev/null +++ b/include/endianness.h @@ -0,0 +1,123 @@ +#ifndef ENDIANNESS_H +#define ENDIANNESS_H + +#ifndef __LITTLE_ENDIAN +#define __LITTLE_ENDIAN 1234 +#endif + +#ifndef __BIG_ENDIAN +#define __BIG_ENDIAN 4321 +#endif + +#ifndef __BYTE_ORDER +#ifdef __LITTLE_ENDIAN__ +#define __BYTE_ORDER __LITTLE_ENDIAN +#else +#ifdef __BIG_ENDIAN__ +#define __BYTE_ORDER __BIG_ENDIAN +#endif +#endif +#endif + +#ifndef be16toh +#if __BYTE_ORDER == __BIG_ENDIAN +#define be16toh(x) (x) +#else +#define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) +#endif +#endif + +#ifndef htobe16 +#define htobe16 be16toh +#endif + +#ifndef le16toh +#if __BYTE_ORDER == __BIG_ENDIAN +#define le16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) +#else +#define le16toh(x) (x) +#endif +#endif + +#ifndef htole16 +#define htole16 le16toh +#endif + +#ifndef __bswap_32 +#define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \ + | (((x) & 0x00FF0000) >> 8) \ + | (((x) & 0x0000FF00) << 8) \ + | (((x) & 0x000000FF) << 24)) +#endif + +#ifndef be32toh +#if __BYTE_ORDER == __BIG_ENDIAN +#define be32toh(x) (x) +#else +#define be32toh(x) __bswap_32(x) +#endif +#endif + +#ifndef htobe32 +#define htobe32 be32toh +#endif + +#ifndef le32toh +#if __BYTE_ORDER == __BIG_ENDIAN +#define le32toh(x) __bswap_32(x) +#else +#define le32toh(x) (x) +#endif +#endif + +#ifndef htole32 +#define htole32 le32toh +#endif + +#ifndef __bswap_64 +#define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \ + | (((x) & 0x00FF000000000000ull) >> 40) \ + | (((x) & 0x0000FF0000000000ull) >> 24) \ + | (((x) & 0x000000FF00000000ull) >> 8) \ + | (((x) & 0x00000000FF000000ull) << 8) \ + | (((x) & 0x0000000000FF0000ull) << 24) \ + | (((x) & 0x000000000000FF00ull) << 40) \ + | (((x) & 0x00000000000000FFull) << 56)) +#endif + +#ifndef htobe64 +#if __BYTE_ORDER == __BIG_ENDIAN +#define htobe64(x) (x) +#else +#define htobe64(x) __bswap_64(x) +#endif +#endif + +#ifndef be64toh +#define be64toh htobe64 +#endif + +#ifndef le64toh +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define le64toh(x) (x) +#else +#define le64toh(x) __bswap_64(x) +#endif +#endif + +#ifndef htole64 +#define htole64 le64toh +#endif + +#if (defined(__BIG_ENDIAN__) \ + && !defined(__FLOAT_WORD_ORDER__)) \ + || (defined(__FLOAT_WORD_ORDER__) \ + && __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__) +#define float_bswap64(x) bswap64(x) +#define float_bswap32(x) bswap32(x) +#else +#define float_bswap64(x) (x) +#define float_bswap32(x) (x) +#endif + +#endif /* ENDIANNESS_H */ diff --git a/include/libimobiledevice-glue/opack.h b/include/libimobiledevice-glue/opack.h new file mode 100644 index 0000000..43b1d1d --- /dev/null +++ b/include/libimobiledevice-glue/opack.h @@ -0,0 +1,29 @@ +/* + * opack.h + * "opack" format encoder/decoder implementation. + * + * Copyright (c) 2021 Nikias Bassen, All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __OPACK_H +#define __OPACK_H + +#include + +void opack_encode_from_plist(plist_t plist, unsigned char** out, unsigned int* out_len); +int opack_decode_to_plist(unsigned char* buf, unsigned int buf_len, plist_t* plist_out); + +#endif /* __OPACK_H */ diff --git a/include/libimobiledevice-glue/tlv.h b/include/libimobiledevice-glue/tlv.h new file mode 100644 index 0000000..895c883 --- /dev/null +++ b/include/libimobiledevice-glue/tlv.h @@ -0,0 +1,42 @@ +/* + * tlv.h + * Simple TLV implementation. + * + * Copyright (c) 2021 Nikias Bassen, All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __TLV_H +#define __TLV_H + +#include + +struct tlv_buf { + unsigned char* data; + unsigned int length; + unsigned int capacity; +}; +typedef struct tlv_buf* tlv_buf_t; + +tlv_buf_t tlv_buf_new(); +void tlv_buf_free(tlv_buf_t tlv); + +void tlv_buf_append(tlv_buf_t tlv, uint8_t tag, unsigned int length, void* data); +unsigned char* tlv_get_data_ptr(const void* tlv_data, void* tlv_end, uint8_t tag, uint8_t* length); +int tlv_data_get_uint(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint64_t* value); +int tlv_data_get_uint8(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint8_t* value); +int tlv_data_copy_data(const void* tlv_data, unsigned int tlv_length, uint8_t tag, void** out, unsigned int* out_len); + +#endif /* __TLV_H */ -- cgit v1.1-32-gdbae