+static inline unsigned bytes(uint64_t value) {
+ return (64 - __builtin_clzll(value) + 7) / 8;
+}
+
+static void put(std::streambuf &stream, uint64_t value, size_t length) {
+ length *= 8;
+ do put(stream, uint8_t(value >> (length -= 8)));
+ while (length != 0);
+}
+
+static void der(std::streambuf &stream, uint64_t value) {
+ if (value < 128)
+ put(stream, value);
+ else {
+ unsigned length(bytes(value));
+ put(stream, 0x80 | length);
+ put(stream, value, length);
+ }
+}
+
+static std::string der(uint8_t tag, const char *value, size_t length) {
+ std::stringbuf data;
+ put(data, tag);
+ der(data, length);
+ put(data, value, length);
+ return data.str();
+}
+
+static std::string der(uint8_t tag, const char *value) {
+ return der(tag, value, strlen(value)); }
+static std::string der(uint8_t tag, const std::string &value) {
+ return der(tag, value.data(), value.size()); }
+
+template <typename Type_>
+static void der_(std::stringbuf &data, const Type_ &values) {
+ size_t size(0);
+ for (const auto &value : values)
+ size += value.size();
+ der(data, size);
+ for (const auto &value : values)
+ put(data, value);
+}
+
+static std::string der(const std::vector<std::string> &values) {
+ std::stringbuf data;
+ put(data, 0x30);
+ der_(data, values);
+ return data.str();
+}
+
+static std::string der(const std::multiset<std::string> &values) {
+ std::stringbuf data;
+ put(data, 0x31);
+ der_(data, values);
+ return data.str();
+}
+
+static std::string der(const std::pair<std::string, std::string> &value) {
+ const auto key(der(0x0c, value.first));
+ std::stringbuf data;
+ put(data, 0x30);
+ der(data, key.size() + value.second.size());
+ put(data, key);
+ put(data, value.second);
+ return data.str();
+}
+
+#ifndef LDID_NOPLIST
+static std::string der(plist_t data) {
+ switch (const auto type = plist_get_node_type(data)) {
+ case PLIST_BOOLEAN: {
+ uint8_t value(0);
+ plist_get_bool_val(data, &value);
+
+ std::stringbuf data;
+ put(data, 0x01);
+ der(data, 1);
+ put(data, value != 0 ? 1 : 0);
+ return data.str();
+ } break;
+
+ case PLIST_UINT: {
+ uint64_t value;
+ plist_get_uint_val(data, &value);
+ const auto length(bytes(value));
+
+ std::stringbuf data;
+ put(data, 0x02);
+ der(data, length);
+ put(data, value, length);
+ return data.str();
+ } break;
+
+ case PLIST_REAL: {
+ fprintf(stderr, "ldid: Invalid plist entry type\n");
+ exit(1);
+ } break;
+
+ case PLIST_DATE: {
+ fprintf(stderr, "ldid: Invalid plist entry type\n");
+ exit(1);
+ } break;
+
+ case PLIST_DATA: {
+ char *value;
+ uint64_t length;
+ plist_get_data_val(data, &value, &length);
+ _scope({ free(value); });
+ return der(0x04, value, length);
+ } break;
+
+ case PLIST_STRING: {
+ char *value;
+ plist_get_string_val(data, &value);
+ _scope({ free(value); });
+ return der(0x0c, value);
+ } break;
+
+ case PLIST_ARRAY: {
+ std::vector<std::string> values;
+ for (auto e(plist_array_get_size(data)), i(decltype(e)(0)); i != e; ++i)
+ values.push_back(der(plist_array_get_item(data, i)));
+ return der(values);
+ } break;
+
+ case PLIST_DICT: {
+ std::multiset<std::string> values;
+
+ plist_dict_iter iterator(NULL);
+ plist_dict_new_iter(data, &iterator);
+ _scope({ free(iterator); });
+
+ for (;;) {
+ char *key(NULL);
+ plist_t value(NULL);
+ plist_dict_next_item(data, iterator, &key, &value);
+ if (key == NULL)
+ break;
+ _scope({ free(key); });
+ values.insert(der(std::make_pair(key, der(value))));
+ }
+
+ return der(values);
+ } break;
+
+ default: {
+ fprintf(stderr, "ldid: Unsupported plist type %d", type);
+ exit(1);
+ } break;
+ }
+}
+#endif
+