+ if (stream_idx_ret)
+ *stream_idx_ret = ads_entry - inode->i_ads_entries;
+
+ lte = ads_entry->lte;
+ if (!lte)
+ return NO_UNIX_DATA;
+
+ size = wim_resource_size(lte);
+ if (size != sizeof(struct wimlib_unix_data))
+ return BAD_UNIX_DATA;
+
+ ret = read_full_resource_into_buf(lte, unix_data);
+ if (ret)
+ return ret;
+
+ if (unix_data->version != 0)
+ return BAD_UNIX_DATA;
+ return 0;
+}
+
+int
+inode_set_unix_data(struct wim_inode *inode, uid_t uid, gid_t gid, mode_t mode,
+ struct wim_lookup_table *lookup_table, int which)
+{
+ struct wimlib_unix_data unix_data;
+ int ret;
+ bool have_good_unix_data = false;
+ bool have_unix_data = false;
+ u16 stream_idx;
+
+ if (!(which & UNIX_DATA_CREATE)) {
+ ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
+ if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
+ have_unix_data = true;
+ if (ret == 0)
+ have_good_unix_data = true;
+ }
+ unix_data.version = 0;
+ if (which & UNIX_DATA_UID || !have_good_unix_data)
+ unix_data.uid = uid;
+ if (which & UNIX_DATA_GID || !have_good_unix_data)
+ unix_data.gid = gid;
+ if (which & UNIX_DATA_MODE || !have_good_unix_data)
+ unix_data.mode = mode;
+ ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
+ &unix_data,
+ sizeof(struct wimlib_unix_data),
+ lookup_table);
+ if (ret == 0 && have_unix_data)
+ inode_remove_ads(inode, stream_idx, lookup_table);
+ return ret;
+}
+#endif /* !__WIN32__ */
+
+/* Replace weird characters in filenames and alternate data stream names.
+ *
+ * In particular we do not want the path separator to appear in any names, as
+ * that would make it possible for a "malicious" WIM to extract itself to any
+ * location it wanted to. */
+static void
+replace_forbidden_characters(utf16lechar *name)
+{
+ utf16lechar *p;
+
+ for (p = name; *p; p++) {
+ #ifdef __WIN32__
+ if (wcschr(L"<>:\"/\\|?*", (wchar_t)*p))
+ #else
+ if (*p == cpu_to_le16('/'))
+ #endif
+ {
+ #ifdef __WIN32__
+ *p = cpu_to_le16(0xfffd);
+ #else
+ *p = cpu_to_le16('?');
+ #endif
+ if (name) {
+ WARNING("File, directory, or stream name \"%"WS"\"\n"
+ " contains forbidden characters; "
+ "substituting replacement characters.",
+ name);
+ name = NULL;
+ }
+ }
+ }
+}