+struct wim_ads_entry *
+inode_add_ads_utf16le(struct wim_inode *inode,
+ const utf16lechar *stream_name,
+ size_t stream_name_nbytes)
+{
+ DEBUG("Add alternate data stream \"%"WS"\"", stream_name);
+ return do_inode_add_ads(inode, stream_name, stream_name_nbytes, true);
+}
+
+/*
+ * Add an alternate stream entry to a WIM inode and return a pointer to it, or
+ * NULL if memory could not be allocated.
+ */
+struct wim_ads_entry *
+inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
+{
+ DEBUG("Add alternate data stream \"%"TS"\"", stream_name);
+ return do_inode_add_ads(inode, stream_name,
+ tstrlen(stream_name) * sizeof(tchar),
+ TCHAR_IS_UTF16LE);
+}
+
+int
+inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
+ const void *value, size_t size,
+ struct wim_lookup_table *lookup_table)
+{
+ int ret = WIMLIB_ERR_NOMEM;
+ struct wim_ads_entry *new_ads_entry;
+ struct wim_lookup_table_entry *existing_lte;
+ struct wim_lookup_table_entry *lte;
+ u8 value_hash[SHA1_HASH_SIZE];
+
+ wimlib_assert(inode->i_resolved);
+ new_ads_entry = inode_add_ads(inode, name);
+ if (!new_ads_entry)
+ goto out;
+ sha1_buffer((const u8*)value, size, value_hash);
+ existing_lte = __lookup_resource(lookup_table, value_hash);
+ if (existing_lte) {
+ lte = existing_lte;
+ lte->refcnt++;
+ } else {
+ u8 *value_copy;
+ lte = new_lookup_table_entry();
+ if (!lte)
+ goto out_remove_ads_entry;
+ value_copy = MALLOC(size);
+ if (!value_copy) {
+ FREE(lte);
+ goto out_remove_ads_entry;
+ }
+ memcpy(value_copy, value, size);
+ lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
+ lte->attached_buffer = value_copy;
+ lte->resource_entry.original_size = size;
+ lte->resource_entry.size = size;
+ copy_hash(lte->hash, value_hash);
+ lookup_table_insert(lookup_table, lte);
+ }
+ new_ads_entry->lte = lte;
+ ret = 0;
+ goto out;
+out_remove_ads_entry:
+ inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
+ lookup_table);
+out:
+ return ret;
+}
+
+/* Set the unnamed stream of a WIM inode, given a data buffer containing the
+ * stream contents. */
+int
+inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
+ struct wim_lookup_table *lookup_table)
+{
+ struct wim_lookup_table_entry *lte, *existing_lte;
+ u8 hash[SHA1_HASH_SIZE];
+ void *buf;
+
+ sha1_buffer(data, len, hash);
+ existing_lte = __lookup_resource(lookup_table, hash);
+ if (existing_lte) {
+ wimlib_assert(wim_resource_size(existing_lte) == len);
+ lte = existing_lte;
+ lte->refcnt++;
+ } else {
+ lte = new_lookup_table_entry();
+ if (!lte)
+ return WIMLIB_ERR_NOMEM;
+ buf = MALLOC(len);
+ if (!buf) {
+ free_lookup_table_entry(lte);
+ return WIMLIB_ERR_NOMEM;
+ }
+ memcpy(buf, data, len);
+ lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
+ lte->attached_buffer = buf;
+ lte->resource_entry.original_size = len;
+ copy_hash(lte->hash, hash);
+ lookup_table_insert(lookup_table, lte);
+ }
+ inode->i_lte = lte;
+ inode->i_resolved = 1;
+ return 0;
+}
+
+/* Remove an alternate data stream from a WIM inode */
+void
+inode_remove_ads(struct wim_inode *inode, u16 idx,
+ struct wim_lookup_table *lookup_table)