| 1 | n/a | /* |
|---|
| 2 | n/a | IMPORTANT NOTE: IF THIS FILE IS CHANGED, PCBUILD\BDIST_WININST.VCXPROJ MUST |
|---|
| 3 | n/a | BE REBUILT AS WELL. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | IF CHANGES TO THIS FILE ARE CHECKED IN, THE RECOMPILED BINARIES MUST BE |
|---|
| 6 | n/a | CHECKED IN AS WELL! |
|---|
| 7 | n/a | */ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #include <windows.h> |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | #include "zlib.h" |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | #include <stdio.h> |
|---|
| 14 | n/a | #include <stdarg.h> |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | #include "archive.h" |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | /* Convert unix-path to dos-path */ |
|---|
| 19 | n/a | static void normpath(char *path) |
|---|
| 20 | n/a | { |
|---|
| 21 | n/a | while (path && *path) { |
|---|
| 22 | n/a | if (*path == '/') |
|---|
| 23 | n/a | *path = '\\'; |
|---|
| 24 | n/a | ++path; |
|---|
| 25 | n/a | } |
|---|
| 26 | n/a | } |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | BOOL ensure_directory(char *pathname, char *new_part, NOTIFYPROC notify) |
|---|
| 29 | n/a | { |
|---|
| 30 | n/a | while (new_part && *new_part && (new_part = strchr(new_part, '\\'))) { |
|---|
| 31 | n/a | DWORD attr; |
|---|
| 32 | n/a | *new_part = '\0'; |
|---|
| 33 | n/a | attr = GetFileAttributes(pathname); |
|---|
| 34 | n/a | if (attr == -1) { |
|---|
| 35 | n/a | /* nothing found */ |
|---|
| 36 | n/a | if (!CreateDirectory(pathname, NULL) && notify) |
|---|
| 37 | n/a | notify(SYSTEM_ERROR, |
|---|
| 38 | n/a | "CreateDirectory (%s)", pathname); |
|---|
| 39 | n/a | else |
|---|
| 40 | n/a | notify(DIR_CREATED, pathname); |
|---|
| 41 | n/a | } |
|---|
| 42 | n/a | if (attr & FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 43 | n/a | ; |
|---|
| 44 | n/a | } else { |
|---|
| 45 | n/a | SetLastError(183); |
|---|
| 46 | n/a | if (notify) |
|---|
| 47 | n/a | notify(SYSTEM_ERROR, |
|---|
| 48 | n/a | "CreateDirectory (%s)", pathname); |
|---|
| 49 | n/a | } |
|---|
| 50 | n/a | *new_part = '\\'; |
|---|
| 51 | n/a | ++new_part; |
|---|
| 52 | n/a | } |
|---|
| 53 | n/a | return TRUE; |
|---|
| 54 | n/a | } |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | /* XXX Should better explicitly specify |
|---|
| 57 | n/a | * uncomp_size and file_times instead of pfhdr! |
|---|
| 58 | n/a | */ |
|---|
| 59 | n/a | char *map_new_file(DWORD flags, char *filename, |
|---|
| 60 | n/a | char *pathname_part, int size, |
|---|
| 61 | n/a | WORD wFatDate, WORD wFatTime, |
|---|
| 62 | n/a | NOTIFYPROC notify) |
|---|
| 63 | n/a | { |
|---|
| 64 | n/a | HANDLE hFile, hFileMapping; |
|---|
| 65 | n/a | char *dst; |
|---|
| 66 | n/a | FILETIME ft; |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | try_again: |
|---|
| 69 | n/a | if (!flags) |
|---|
| 70 | n/a | flags = CREATE_NEW; |
|---|
| 71 | n/a | hFile = CreateFile(filename, |
|---|
| 72 | n/a | GENERIC_WRITE | GENERIC_READ, |
|---|
| 73 | n/a | 0, NULL, |
|---|
| 74 | n/a | flags, |
|---|
| 75 | n/a | FILE_ATTRIBUTE_NORMAL, NULL); |
|---|
| 76 | n/a | if (hFile == INVALID_HANDLE_VALUE) { |
|---|
| 77 | n/a | DWORD x = GetLastError(); |
|---|
| 78 | n/a | switch (x) { |
|---|
| 79 | n/a | case ERROR_FILE_EXISTS: |
|---|
| 80 | n/a | if (notify && notify(CAN_OVERWRITE, filename)) |
|---|
| 81 | n/a | hFile = CreateFile(filename, |
|---|
| 82 | n/a | GENERIC_WRITE|GENERIC_READ, |
|---|
| 83 | n/a | 0, NULL, |
|---|
| 84 | n/a | CREATE_ALWAYS, |
|---|
| 85 | n/a | FILE_ATTRIBUTE_NORMAL, |
|---|
| 86 | n/a | NULL); |
|---|
| 87 | n/a | else { |
|---|
| 88 | n/a | if (notify) |
|---|
| 89 | n/a | notify(FILE_OVERWRITTEN, filename); |
|---|
| 90 | n/a | return NULL; |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | break; |
|---|
| 93 | n/a | case ERROR_PATH_NOT_FOUND: |
|---|
| 94 | n/a | if (ensure_directory(filename, pathname_part, notify)) |
|---|
| 95 | n/a | goto try_again; |
|---|
| 96 | n/a | else |
|---|
| 97 | n/a | return FALSE; |
|---|
| 98 | n/a | break; |
|---|
| 99 | n/a | default: |
|---|
| 100 | n/a | SetLastError(x); |
|---|
| 101 | n/a | break; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | } |
|---|
| 104 | n/a | if (hFile == INVALID_HANDLE_VALUE) { |
|---|
| 105 | n/a | if (notify) |
|---|
| 106 | n/a | notify (SYSTEM_ERROR, "CreateFile (%s)", filename); |
|---|
| 107 | n/a | return NULL; |
|---|
| 108 | n/a | } |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | if (notify) |
|---|
| 111 | n/a | notify(FILE_CREATED, filename); |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | DosDateTimeToFileTime(wFatDate, wFatTime, &ft); |
|---|
| 114 | n/a | SetFileTime(hFile, &ft, &ft, &ft); |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | if (size == 0) { |
|---|
| 118 | n/a | /* We cannot map a zero-length file (Also it makes |
|---|
| 119 | n/a | no sense */ |
|---|
| 120 | n/a | CloseHandle(hFile); |
|---|
| 121 | n/a | return NULL; |
|---|
| 122 | n/a | } |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | hFileMapping = CreateFileMapping(hFile, |
|---|
| 125 | n/a | NULL, PAGE_READWRITE, 0, size, NULL); |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | CloseHandle(hFile); |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | if (hFileMapping == NULL) { |
|---|
| 130 | n/a | if (notify) |
|---|
| 131 | n/a | notify(SYSTEM_ERROR, |
|---|
| 132 | n/a | "CreateFileMapping (%s)", filename); |
|---|
| 133 | n/a | return NULL; |
|---|
| 134 | n/a | } |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | dst = MapViewOfFile(hFileMapping, |
|---|
| 137 | n/a | FILE_MAP_WRITE, 0, 0, 0); |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | CloseHandle(hFileMapping); |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | if (!dst) { |
|---|
| 142 | n/a | if (notify) |
|---|
| 143 | n/a | notify(SYSTEM_ERROR, "MapViewOfFile (%s)", filename); |
|---|
| 144 | n/a | return NULL; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | return dst; |
|---|
| 147 | n/a | } |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | BOOL |
|---|
| 151 | n/a | extract_file(char *dst, char *src, int method, int comp_size, |
|---|
| 152 | n/a | int uncomp_size, NOTIFYPROC notify) |
|---|
| 153 | n/a | { |
|---|
| 154 | n/a | z_stream zstream; |
|---|
| 155 | n/a | int result; |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | if (method == Z_DEFLATED) { |
|---|
| 158 | n/a | int x; |
|---|
| 159 | n/a | memset(&zstream, 0, sizeof(zstream)); |
|---|
| 160 | n/a | zstream.next_in = src; |
|---|
| 161 | n/a | zstream.avail_in = comp_size+1; |
|---|
| 162 | n/a | zstream.next_out = dst; |
|---|
| 163 | n/a | zstream.avail_out = uncomp_size; |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | /* Apparently an undocumented feature of zlib: Set windowsize |
|---|
| 166 | n/a | to negative values to suppress the gzip header and be compatible with |
|---|
| 167 | n/a | zip! */ |
|---|
| 168 | n/a | result = TRUE; |
|---|
| 169 | n/a | if (Z_OK != (x = inflateInit2(&zstream, -15))) { |
|---|
| 170 | n/a | if (notify) |
|---|
| 171 | n/a | notify(ZLIB_ERROR, |
|---|
| 172 | n/a | "inflateInit2 returns %d", x); |
|---|
| 173 | n/a | result = FALSE; |
|---|
| 174 | n/a | goto cleanup; |
|---|
| 175 | n/a | } |
|---|
| 176 | n/a | if (Z_STREAM_END != (x = inflate(&zstream, Z_FINISH))) { |
|---|
| 177 | n/a | if (notify) |
|---|
| 178 | n/a | notify(ZLIB_ERROR, |
|---|
| 179 | n/a | "inflate returns %d", x); |
|---|
| 180 | n/a | result = FALSE; |
|---|
| 181 | n/a | } |
|---|
| 182 | n/a | cleanup: |
|---|
| 183 | n/a | if (Z_OK != (x = inflateEnd(&zstream))) { |
|---|
| 184 | n/a | if (notify) |
|---|
| 185 | n/a | notify (ZLIB_ERROR, |
|---|
| 186 | n/a | "inflateEnd returns %d", x); |
|---|
| 187 | n/a | result = FALSE; |
|---|
| 188 | n/a | } |
|---|
| 189 | n/a | } else if (method == 0) { |
|---|
| 190 | n/a | memcpy(dst, src, uncomp_size); |
|---|
| 191 | n/a | result = TRUE; |
|---|
| 192 | n/a | } else |
|---|
| 193 | n/a | result = FALSE; |
|---|
| 194 | n/a | UnmapViewOfFile(dst); |
|---|
| 195 | n/a | return result; |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | /* Open a zip-compatible archive and extract all files |
|---|
| 199 | n/a | * into the specified directory (which is assumed to exist) |
|---|
| 200 | n/a | */ |
|---|
| 201 | n/a | BOOL |
|---|
| 202 | n/a | unzip_archive(SCHEME *scheme, char *dirname, char *data, DWORD size, |
|---|
| 203 | n/a | NOTIFYPROC notify) |
|---|
| 204 | n/a | { |
|---|
| 205 | n/a | int n; |
|---|
| 206 | n/a | char pathname[MAX_PATH]; |
|---|
| 207 | n/a | char *new_part; |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | /* read the end of central directory record */ |
|---|
| 210 | n/a | struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof |
|---|
| 211 | n/a | (struct eof_cdir)]; |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir - |
|---|
| 214 | n/a | pe->ofsCDir; |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | /* set position to start of central directory */ |
|---|
| 217 | n/a | int pos = arc_start + pe->ofsCDir; |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | /* make sure this is a zip file */ |
|---|
| 220 | n/a | if (pe->tag != 0x06054b50) |
|---|
| 221 | n/a | return FALSE; |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | /* Loop through the central directory, reading all entries */ |
|---|
| 224 | n/a | for (n = 0; n < pe->nTotalCDir; ++n) { |
|---|
| 225 | n/a | int i; |
|---|
| 226 | n/a | char *fname; |
|---|
| 227 | n/a | char *pcomp; |
|---|
| 228 | n/a | char *dst; |
|---|
| 229 | n/a | struct cdir *pcdir; |
|---|
| 230 | n/a | struct fhdr *pfhdr; |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | pcdir = (struct cdir *)&data[pos]; |
|---|
| 233 | n/a | pfhdr = (struct fhdr *)&data[pcdir->ofs_local_header + |
|---|
| 234 | n/a | arc_start]; |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | if (pcdir->tag != 0x02014b50) |
|---|
| 237 | n/a | return FALSE; |
|---|
| 238 | n/a | if (pfhdr->tag != 0x04034b50) |
|---|
| 239 | n/a | return FALSE; |
|---|
| 240 | n/a | pos += sizeof(struct cdir); |
|---|
| 241 | n/a | fname = (char *)&data[pos]; /* This is not null terminated! */ |
|---|
| 242 | n/a | pos += pcdir->fname_length + pcdir->extra_length + |
|---|
| 243 | n/a | pcdir->comment_length; |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | pcomp = &data[pcdir->ofs_local_header |
|---|
| 246 | n/a | + sizeof(struct fhdr) |
|---|
| 247 | n/a | + arc_start |
|---|
| 248 | n/a | + pfhdr->fname_length |
|---|
| 249 | n/a | + pfhdr->extra_length]; |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | /* dirname is the Python home directory (prefix) */ |
|---|
| 252 | n/a | strcpy(pathname, dirname); |
|---|
| 253 | n/a | if (pathname[strlen(pathname)-1] != '\\') |
|---|
| 254 | n/a | strcat(pathname, "\\"); |
|---|
| 255 | n/a | new_part = &pathname[lstrlen(pathname)]; |
|---|
| 256 | n/a | /* we must now match the first part of the pathname |
|---|
| 257 | n/a | * in the archive to a component in the installation |
|---|
| 258 | n/a | * scheme (PURELIB, PLATLIB, HEADERS, SCRIPTS, or DATA) |
|---|
| 259 | n/a | * and replace this part by the one in the scheme to use |
|---|
| 260 | n/a | */ |
|---|
| 261 | n/a | for (i = 0; scheme[i].name; ++i) { |
|---|
| 262 | n/a | if (0 == strnicmp(scheme[i].name, fname, |
|---|
| 263 | n/a | strlen(scheme[i].name))) { |
|---|
| 264 | n/a | char *rest; |
|---|
| 265 | n/a | int len; |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | /* length of the replaced part */ |
|---|
| 268 | n/a | int namelen = strlen(scheme[i].name); |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | strcat(pathname, scheme[i].prefix); |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | rest = fname + namelen; |
|---|
| 273 | n/a | len = pfhdr->fname_length - namelen; |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | if ((pathname[strlen(pathname)-1] != '\\') |
|---|
| 276 | n/a | && (pathname[strlen(pathname)-1] != '/')) |
|---|
| 277 | n/a | strcat(pathname, "\\"); |
|---|
| 278 | n/a | /* Now that pathname ends with a separator, |
|---|
| 279 | n/a | * we must make sure rest does not start with |
|---|
| 280 | n/a | * an additional one. |
|---|
| 281 | n/a | */ |
|---|
| 282 | n/a | if ((rest[0] == '\\') || (rest[0] == '/')) { |
|---|
| 283 | n/a | ++rest; |
|---|
| 284 | n/a | --len; |
|---|
| 285 | n/a | } |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | strncat(pathname, rest, len); |
|---|
| 288 | n/a | goto Done; |
|---|
| 289 | n/a | } |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | /* no prefix to replace found, go unchanged */ |
|---|
| 292 | n/a | strncat(pathname, fname, pfhdr->fname_length); |
|---|
| 293 | n/a | Done: |
|---|
| 294 | n/a | normpath(pathname); |
|---|
| 295 | n/a | if (pathname[strlen(pathname)-1] != '\\') { |
|---|
| 296 | n/a | /* |
|---|
| 297 | n/a | * The local file header (pfhdr) does not always |
|---|
| 298 | n/a | * contain the compressed and uncompressed sizes of |
|---|
| 299 | n/a | * the data depending on bit 3 of the flags field. So |
|---|
| 300 | n/a | * it seems better to use the data from the central |
|---|
| 301 | n/a | * directory (pcdir). |
|---|
| 302 | n/a | */ |
|---|
| 303 | n/a | dst = map_new_file(0, pathname, new_part, |
|---|
| 304 | n/a | pcdir->uncomp_size, |
|---|
| 305 | n/a | pcdir->last_mod_file_date, |
|---|
| 306 | n/a | pcdir->last_mod_file_time, notify); |
|---|
| 307 | n/a | if (dst) { |
|---|
| 308 | n/a | if (!extract_file(dst, pcomp, pfhdr->method, |
|---|
| 309 | n/a | pcdir->comp_size, |
|---|
| 310 | n/a | pcdir->uncomp_size, |
|---|
| 311 | n/a | notify)) |
|---|
| 312 | n/a | return FALSE; |
|---|
| 313 | n/a | } /* else ??? */ |
|---|
| 314 | n/a | } |
|---|
| 315 | n/a | if (notify) |
|---|
| 316 | n/a | notify(NUM_FILES, new_part, (int)pe->nTotalCDir, |
|---|
| 317 | n/a | (int)n+1); |
|---|
| 318 | n/a | } |
|---|
| 319 | n/a | return TRUE; |
|---|
| 320 | n/a | } |
|---|