+ return security_id;
+}
+
+/* Initialize a `struct sd_set' mapping from SHA1 message digests of security
+ * descriptors to indices into the security descriptors table of the WIM image
+ * (security IDs). */
+int
+init_sd_set(struct wim_sd_set *sd_set, struct wim_security_data *sd)
+{
+ int ret;
+
+ sd_set->sd = sd;
+ sd_set->rb_root.rb_node = NULL;
+
+ /* Remember the original number of security descriptors so that newly
+ * added ones can be rolled back if needed. */
+ sd_set->orig_num_entries = sd->num_entries;
+ for (u32 i = 0; i < sd->num_entries; i++) {
+ struct sd_node *new;
+
+ new = MALLOC(sizeof(struct sd_node));
+ if (!new) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_destroy_sd_set;
+ }
+ sha1_buffer(sd->descriptors[i], sd->sizes[i], new->hash);
+ new->security_id = i;
+ if (!insert_sd_node(sd_set, new))
+ FREE(new); /* Ignore duplicate security descriptor */
+ }
+ ret = 0;
+ goto out;
+out_destroy_sd_set:
+ destroy_sd_set(sd_set, false);
+out:
+ return ret;