+static int read_ads_entries(const u8 *p, struct dentry *dentry,
+ u64 remaining_size)
+{
+ u16 num_ads = dentry->num_ads;
+ struct ads_entry *ads_entries = CALLOC(num_ads, sizeof(struct ads_entry));
+ int ret;
+ if (!ads_entries) {
+ ERROR("Could not allocate memory for %"PRIu16" "
+ "alternate data stream entries", num_ads);
+ return WIMLIB_ERR_NOMEM;
+ }
+ DEBUG2("Reading %"PRIu16" alternate data streams "
+ "(remaining size = %"PRIu64")", num_ads, remaining_size);
+
+ for (u16 i = 0; i < num_ads; i++) {
+ struct ads_entry *cur_entry = &ads_entries[i];
+ u64 length;
+ size_t utf8_len;
+ const char *p_save = p;
+ /* Read the base stream entry, excluding the stream name. */
+ if (remaining_size < WIM_ADS_ENTRY_DISK_SIZE) {
+ ERROR("Stream entries go past end of metadata resource");
+ ERROR("(remaining_size = %"PRIu64")", remaining_size);
+ ret = WIMLIB_ERR_INVALID_DENTRY;
+ goto out_free_ads_entries;
+ }
+ remaining_size -= WIM_ADS_ENTRY_DISK_SIZE;
+
+ p = get_u64(p, &length); /* ADS entry length */
+
+ DEBUG2("ADS length = %"PRIu64, length);
+
+ p += 8; /* Unused */
+ p = get_bytes(p, SHA1_HASH_SIZE, (u8*)cur_entry->hash);
+ p = get_u16(p, &cur_entry->stream_name_len);
+
+ DEBUG2("Stream name length = %u", cur_entry->stream_name_len);
+
+ cur_entry->stream_name = NULL;
+ cur_entry->stream_name_utf8 = NULL;
+
+ if (remaining_size < cur_entry->stream_name_len + 2) {
+ ERROR("Stream entries go past end of metadata resource");
+ ERROR("(remaining_size = %"PRIu64" bytes, stream_name_len "
+ "= %"PRIu16" bytes", remaining_size,
+ cur_entry->stream_name_len);
+ ret = WIMLIB_ERR_INVALID_DENTRY;
+ goto out_free_ads_entries;
+ }
+ remaining_size -= cur_entry->stream_name_len + 2;
+
+ cur_entry->stream_name = MALLOC(cur_entry->stream_name_len);
+ if (!cur_entry->stream_name) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_ads_entries;
+ }
+ get_bytes(p, cur_entry->stream_name_len,
+ (u8*)cur_entry->stream_name);
+ cur_entry->stream_name_utf8 = utf16_to_utf8(cur_entry->stream_name,
+ cur_entry->stream_name_len,
+ &utf8_len);
+ cur_entry->stream_name_utf8_len = utf8_len;
+
+ if (!cur_entry->stream_name_utf8) {
+ ret = WIMLIB_ERR_NOMEM;
+ goto out_free_ads_entries;
+ }
+ p = p_save + ads_entry_length(cur_entry);
+ }
+ dentry->ads_entries = ads_entries;
+ return 0;
+out_free_ads_entries:
+ for (u16 i = 0; i < num_ads; i++) {
+ FREE(ads_entries[i].stream_name);
+ FREE(ads_entries[i].stream_name_utf8);
+ }
+ FREE(ads_entries);
+ return ret;
+}
+