-/* Scans an unnamed or named stream of a Win32 file (not a reparse point
- * stream); calculates its SHA1 message digest and either creates a `struct
- * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
- * wim_lookup_table_entry' for an identical stream.
- *
- * @path: Path to the file (UTF-16LE).
- *
- * @path_num_chars: Number of 2-byte characters in @path.
- *
- * @inode: WIM inode to save the stream into.
- *
- * @unhashed_streams: List of unhashed streams that have been added to the WIM
- * image.
- *
- * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
- * stream name.
- *
- * Returns 0 on success; nonzero on failure.
- */
+static bool
+get_data_stream_name(const wchar_t *raw_stream_name, size_t raw_stream_name_nchars,
+ const wchar_t **stream_name_ret, size_t *stream_name_nchars_ret)
+{
+ const wchar_t *sep, *type, *end;
+
+ /* The stream name should be returned as :NAME:TYPE */
+ if (raw_stream_name_nchars < 1)
+ return false;
+ if (raw_stream_name[0] != L':')
+ return false;
+
+ raw_stream_name++;
+ raw_stream_name_nchars--;
+
+ end = raw_stream_name + raw_stream_name_nchars;
+
+ sep = wmemchr(raw_stream_name, L':', raw_stream_name_nchars);
+ if (!sep)
+ return false;
+
+ type = sep + 1;
+ if (end - type != 5)
+ return false;
+
+ if (wmemcmp(type, L"$DATA", 5))
+ return false;
+
+ *stream_name_ret = raw_stream_name;
+ *stream_name_nchars_ret = sep - raw_stream_name;
+ return true;
+}
+
+static wchar_t *
+build_stream_path(const wchar_t *path, size_t path_nchars,
+ const wchar_t *stream_name, size_t stream_name_nchars)
+{
+ size_t stream_path_nchars;
+ wchar_t *stream_path;
+ wchar_t *p;
+
+ stream_path_nchars = path_nchars;
+ if (stream_name_nchars)
+ stream_path_nchars += 1 + stream_name_nchars;
+
+ stream_path = MALLOC((stream_path_nchars + 1) * sizeof(wchar_t));
+ if (stream_path) {
+ p = wmempcpy(stream_path, path, path_nchars);
+ if (stream_name_nchars) {
+ *p++ = L':';
+ p = wmempcpy(p, stream_name, stream_name_nchars);
+ }
+ *p++ = L'\0';
+ }
+ return stream_path;
+}
+