]> wimlib.net Git - wimlib/blob - src/symlink.c
42a771e65978171a8f0fff35e8a2a52570fd16ec
[wimlib] / src / symlink.c
1 #include "dentry.h"
2 #include "io.h"
3 #include "lookup_table.h"
4
5 /*
6  * Find the symlink target of a symbolic link or junction point in the WIM.
7  *
8  * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx
9  * Except the first 8 bytes aren't included in the resource (presumably because
10  * we already know the reparse tag from the dentry, and we already know the
11  * reparse tag len from the lookup table entry resource length).
12  */
13 static ssize_t get_symlink_name(const u8 *resource, size_t resource_len,
14                                 char *buf, size_t buf_len,
15                                 bool is_junction_point)
16 {
17         const u8 *p = resource;
18         u16 substitute_name_offset;
19         u16 substitute_name_len;
20         u16 print_name_offset;
21         u16 print_name_len;
22         char *link_target;
23         size_t link_target_len;
24         ssize_t ret;
25         unsigned header_size;
26         char *translated_target;
27         bool is_absolute;
28
29         if (resource_len < 12)
30                 return -EIO;
31         p = get_u16(p, &substitute_name_offset);
32         p = get_u16(p, &substitute_name_len);
33         p = get_u16(p, &print_name_offset);
34         p = get_u16(p, &print_name_len);
35         if (is_junction_point) {
36                 header_size = 8;
37         } else {
38                 u32 flags;
39                 p = get_u32(p, &flags);
40                 is_absolute = (flags & 1) ? false : true;
41                 header_size = 12;
42         }
43         if (header_size + substitute_name_offset + substitute_name_len > resource_len)
44                 return -EIO;
45         link_target = utf16_to_utf8(p + substitute_name_offset,
46                                     substitute_name_len,
47                                     &link_target_len);
48
49         if (!link_target)
50                 return -EIO;
51
52         if (link_target_len + 1 > buf_len) {
53                 ret = -ENAMETOOLONG;
54                 goto out;
55         }
56
57         translated_target = link_target;
58         if (is_junction_point || is_absolute) {
59                 if (link_target_len < 7
60                       || memcmp(translated_target, "\\??\\", 4) != 0
61                       || translated_target[4] == '\0'
62                       || translated_target[5] != ':'
63                       || translated_target[6] != '\\') {
64                         ret = -EIO;
65                         goto out;
66                 }
67                 translated_target += 4;
68                 link_target_len -= 4;
69         }
70         for (size_t i = 0; i < link_target_len; i++)
71                 if (translated_target[i] == '\\')
72                         translated_target[i] = '/';
73
74         memcpy(buf, translated_target, link_target_len + 1);
75         ret = link_target_len;
76 out:
77         FREE(link_target);
78         return ret;
79 }
80
81 void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
82 {
83         size_t utf8_len = strlen(symlink_target);
84         size_t utf16_len;
85         char *name_utf16 = utf8_to_utf16(symlink_target, utf8_len, &utf16_len);
86         if (!name_utf16)
87                 return NULL;
88         /*DEBUG("utf16_len = %zu", utf16_len);*/
89         for (size_t i = 0; i < utf16_len / 2; i++)
90                 if (((u16*)name_utf16)[i] == to_le16('/'))
91                         ((u16*)name_utf16)[i] = to_le16('\\');
92         size_t len = 12 + utf16_len * 2;
93         void *buf = MALLOC(len);
94         if (!buf)
95                 goto out;
96
97         u8 *p = buf;
98         p = put_u16(p, 0); /* Substitute name offset */
99         p = put_u16(p, utf16_len); /* Substitute name length */
100         p = put_u16(p, utf16_len); /* Print name offset */
101         p = put_u16(p, utf16_len); /* Print name length */
102         p = put_u32(p, (symlink_target[0] == '/') ?  0 : 1);
103         p = put_bytes(p, utf16_len, name_utf16);
104         p = put_bytes(p, utf16_len, name_utf16);
105         /*DEBUG("utf16_len = %zu, len = %zu", utf16_len, len);*/
106         *len_ret = len;
107 out:
108         FREE(name_utf16);
109         return buf;
110 }
111
112 /* Get the symlink target from a dentry that's already checked to be either a
113  * "real" symlink or a junction point. */
114 ssize_t dentry_readlink(const struct dentry *dentry, char *buf, size_t buf_len,
115                         const WIMStruct *w)
116 {
117         struct ads_entry *ads;
118         struct lookup_table_entry *entry;
119         struct resource_entry *res_entry;
120         bool is_junction_point;
121
122         wimlib_assert(dentry_is_symlink(dentry));
123
124         if (dentry->reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK) {
125                 is_junction_point = false;
126                 /* 
127                  * This is of course not actually documented, but what I think is going
128                  * on here is that the symlink dentries have 2 alternate data streams;
129                  * one is the default data stream, which is not used and is empty, and
130                  * one is the symlink buffer data stream, which is confusingly also
131                  * unnamed, but isn't empty as it contains the symlink target within the
132                  * resource.
133                  */
134                 if (dentry->num_ads != 2)
135                         return -EIO;
136                 if ((entry = lookup_resource(w->lookup_table, dentry->ads_entries[0].hash)))
137                         goto do_readlink;
138                 if ((entry = lookup_resource(w->lookup_table, dentry->ads_entries[1].hash)))
139                         goto do_readlink;
140         } else {
141                 wimlib_assert(dentry->reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
142
143                 is_junction_point = true;
144
145                 if ((entry = lookup_resource(w->lookup_table, dentry->hash)))
146                         goto do_readlink;
147         }
148         return -EIO;
149 do_readlink:
150         res_entry = &entry->resource_entry;
151         if (res_entry->original_size > 10000)
152                 return -EIO;
153         char res_buf[res_entry->original_size];
154         if (read_full_resource(w->fp, res_entry->size, 
155                                res_entry->original_size,
156                                res_entry->offset,
157                                wim_resource_compression_type(w, res_entry),
158                                res_buf) != 0)
159                 return -EIO;
160         return get_symlink_name(res_buf, res_entry->original_size, buf,
161                                 buf_len, is_junction_point);
162 }