2012-01-31 01:40:22 +00:00
|
|
|
/*
|
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
|
|
|
* ISO file extraction
|
|
|
|
* Copyright (c) 2011-2012 Pete Batard <pete@akeo.ie>
|
|
|
|
* Based on libcdio's iso & udf samples:
|
|
|
|
* Copyright (c) 2003-2011 Rocky Bernstein <rocky@gnu.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <direct.h>
|
2012-03-27 22:31:58 +00:00
|
|
|
#include <ctype.h>
|
2012-01-31 01:40:22 +00:00
|
|
|
|
|
|
|
#include <cdio/cdio.h>
|
|
|
|
#include <cdio/logging.h>
|
|
|
|
#include <cdio/iso9660.h>
|
|
|
|
#include <cdio/udf.h>
|
|
|
|
|
|
|
|
#include "rufus.h"
|
2012-02-01 14:26:36 +00:00
|
|
|
#include "msapi_utf8.h"
|
|
|
|
#include "resource.h"
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-01 14:26:36 +00:00
|
|
|
// How often should we update the progress bar (in 2K blocks) as updating
|
|
|
|
// the progress bar for every block will bring extraction to a crawl
|
2012-03-29 19:27:53 +00:00
|
|
|
#define PROGRESS_THRESHOLD 128
|
2012-02-07 02:05:58 +00:00
|
|
|
#define FOUR_GIGABYTES 4294967296LL
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-01 14:26:36 +00:00
|
|
|
// Needed for UDF ISO access
|
2012-03-16 18:30:44 +00:00
|
|
|
CdIo_t* cdio_open (const char* psz_source, driver_id_t driver_id) {return NULL;}
|
|
|
|
void cdio_destroy (CdIo_t* p_cdio) {}
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-01 14:26:36 +00:00
|
|
|
RUFUS_ISO_REPORT iso_report;
|
2012-02-07 16:17:14 +00:00
|
|
|
int64_t iso_blocking_status = -1;
|
|
|
|
#define ISO_BLOCKING(x) do {x; iso_blocking_status++; } while(0)
|
2012-03-16 18:30:44 +00:00
|
|
|
static const char* psz_extract_dir;
|
|
|
|
static const char* bootmgr_name = "bootmgr";
|
|
|
|
static const char* ldlinux_name = "ldlinux.sys";
|
|
|
|
static const char* isolinux_name[] = { "isolinux.cfg", "syslinux.cfg", "extlinux.conf"};
|
|
|
|
static const char* vesamenu_name = "vesamenu.c32";
|
|
|
|
static const char* pe_dirname[] = { "/i386", "/minint" };
|
|
|
|
static const char* pe_file[] = { "ntdetect.com", "setupldr.bin", "txtsetup.sif" };
|
2012-03-11 01:55:25 +00:00
|
|
|
static int64_t old_vesamenu_threshold = 145000;
|
2012-02-15 21:55:41 +00:00
|
|
|
static uint8_t i_joliet_level = 0;
|
2012-02-01 14:26:36 +00:00
|
|
|
static uint64_t total_blocks, nb_blocks;
|
|
|
|
static BOOL scan_only = FALSE;
|
2012-02-21 00:08:31 +00:00
|
|
|
static StrArray config_path;
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-07 02:05:58 +00:00
|
|
|
// TODO: Timestamp & permissions preservation
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-07 02:05:58 +00:00
|
|
|
// Convert a file size to human readable
|
|
|
|
static __inline char* size_to_hr(int64_t size)
|
|
|
|
{
|
|
|
|
int suffix = 0;
|
|
|
|
static char str_size[24];
|
2012-02-08 00:26:37 +00:00
|
|
|
const char* sizes[] = { "", "KB", "MB", "GB", "TB" };
|
2012-02-07 02:05:58 +00:00
|
|
|
double hr_size = (double)size;
|
|
|
|
while ((suffix < ARRAYSIZE(sizes)) && (hr_size >= 1024.0)) {
|
|
|
|
hr_size /= 1024.0;
|
|
|
|
suffix++;
|
|
|
|
}
|
2012-02-08 00:26:37 +00:00
|
|
|
if (suffix == 0) {
|
|
|
|
safe_sprintf(str_size, sizeof(str_size), " (%d bytes)", (int)hr_size);
|
|
|
|
} else {
|
|
|
|
safe_sprintf(str_size, sizeof(str_size), " (%0.1f %s)", hr_size, sizes[suffix]);
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
return str_size;
|
|
|
|
}
|
|
|
|
|
2012-02-14 20:52:39 +00:00
|
|
|
static void log_handler (cdio_log_level_t level, const char *message)
|
2012-02-07 02:05:58 +00:00
|
|
|
{
|
2012-02-14 20:52:39 +00:00
|
|
|
switch(level) {
|
|
|
|
case CDIO_LOG_DEBUG:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
uprintf("libcdio: %s\n", message);
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 19:46:28 +00:00
|
|
|
/*
|
|
|
|
* Scan and set ISO properties
|
|
|
|
* Returns true if the the current file does not need to be processed further
|
|
|
|
*/
|
2012-03-16 18:30:44 +00:00
|
|
|
static __inline BOOL check_iso_props(const char* psz_dirname, BOOL* is_syslinux_cfg, BOOL* is_old_vesamenu,
|
|
|
|
int64_t i_file_length, const char* psz_basename, const char* psz_fullpath)
|
2012-02-21 19:46:28 +00:00
|
|
|
{
|
2012-03-16 18:30:44 +00:00
|
|
|
size_t i, j;
|
2012-02-21 19:46:28 +00:00
|
|
|
|
|
|
|
// Check for an isolinux/syslinux config file anywhere
|
|
|
|
*is_syslinux_cfg = FALSE;
|
|
|
|
for (i=0; i<ARRAYSIZE(isolinux_name); i++) {
|
2012-03-16 18:30:44 +00:00
|
|
|
if (safe_stricmp(psz_basename, isolinux_name[i]) == 0)
|
2012-02-21 19:46:28 +00:00
|
|
|
*is_syslinux_cfg = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-11 01:55:25 +00:00
|
|
|
// Check for an old vesamenu.c32 file anywhere
|
|
|
|
*is_old_vesamenu = FALSE;
|
2012-03-16 18:30:44 +00:00
|
|
|
if ((safe_stricmp(psz_basename, vesamenu_name) == 0) && (i_file_length <= old_vesamenu_threshold)) {
|
2012-03-11 01:55:25 +00:00
|
|
|
*is_old_vesamenu = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-02-21 19:46:28 +00:00
|
|
|
if (scan_only) {
|
|
|
|
// Check for a "bootmgr" file in root (psz_path = "")
|
2012-03-16 18:30:44 +00:00
|
|
|
if ((*psz_dirname == 0) && (safe_stricmp(psz_basename, bootmgr_name) == 0))
|
2012-02-21 19:46:28 +00:00
|
|
|
iso_report.has_bootmgr = TRUE;
|
2012-03-16 18:30:44 +00:00
|
|
|
|
|
|
|
// Check for PE (XP) specific files in "/i386" or "/minint"
|
|
|
|
for (i=0; i<ARRAYSIZE(pe_dirname); i++)
|
|
|
|
if (safe_stricmp(psz_dirname, pe_dirname[i]) == 0)
|
|
|
|
for (j=0; j<ARRAYSIZE(pe_file); j++)
|
|
|
|
if (safe_stricmp(psz_basename, pe_file[j]) == 0)
|
|
|
|
iso_report.winpe |= (1<<i)<<(ARRAYSIZE(pe_dirname)*j);
|
|
|
|
|
2012-02-21 19:46:28 +00:00
|
|
|
if (*is_syslinux_cfg) {
|
|
|
|
iso_report.has_isolinux = TRUE;
|
|
|
|
// Maintain a list of all the isolinux/syslinux configs identified so far
|
|
|
|
StrArrayAdd(&config_path, psz_fullpath);
|
|
|
|
}
|
2012-03-11 01:55:25 +00:00
|
|
|
if (*is_old_vesamenu)
|
|
|
|
iso_report.has_old_vesamenu = TRUE;
|
2012-02-21 19:46:28 +00:00
|
|
|
if (i_file_length >= FOUR_GIGABYTES)
|
|
|
|
iso_report.has_4GB_file = TRUE;
|
|
|
|
// Compute projected size needed
|
|
|
|
total_blocks += i_file_length/UDF_BLOCKSIZE;
|
|
|
|
// NB: ISO_BLOCKSIZE = UDF_BLOCKSIZE
|
|
|
|
if ((i_file_length != 0) && (i_file_length%ISO_BLOCKSIZE == 0)) //
|
|
|
|
total_blocks++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
// In case there's an ldlinux.sys on the ISO, prevent it from overwriting ours
|
2012-03-16 18:30:44 +00:00
|
|
|
if ((*psz_dirname == 0) && (safe_strcmp(psz_basename, ldlinux_name) == 0)) {
|
2012-02-21 19:46:28 +00:00
|
|
|
uprintf("skipping % file from ISO image\n", ldlinux_name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-07 02:05:58 +00:00
|
|
|
// Returns 0 on success, nonzero on error
|
2012-01-31 01:40:22 +00:00
|
|
|
static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const char *psz_path)
|
|
|
|
{
|
2012-02-07 16:17:14 +00:00
|
|
|
HANDLE file_handle = NULL;
|
2012-02-07 02:05:58 +00:00
|
|
|
DWORD buf_size, wr_size;
|
2012-03-11 01:55:25 +00:00
|
|
|
BOOL r, is_syslinux_cfg, is_old_vesamenu;
|
2012-01-31 01:40:22 +00:00
|
|
|
int i_length;
|
2012-02-07 02:05:58 +00:00
|
|
|
size_t i, nul_pos;
|
2012-02-15 00:52:40 +00:00
|
|
|
char* psz_fullpath = NULL;
|
2012-01-31 01:40:22 +00:00
|
|
|
const char* psz_basename;
|
|
|
|
udf_dirent_t *p_udf_dirent2;
|
|
|
|
uint8_t buf[UDF_BLOCKSIZE];
|
|
|
|
int64_t i_read, i_file_length;
|
|
|
|
|
|
|
|
if ((p_udf_dirent == NULL) || (psz_path == NULL))
|
|
|
|
return 1;
|
|
|
|
|
2012-02-01 14:26:36 +00:00
|
|
|
while ((p_udf_dirent = udf_readdir(p_udf_dirent)) != NULL) {
|
|
|
|
if (FormatStatus) goto out;
|
2012-01-31 01:40:22 +00:00
|
|
|
psz_basename = udf_get_filename(p_udf_dirent);
|
2012-02-07 02:05:58 +00:00
|
|
|
i_length = (int)(3 + strlen(psz_path) + strlen(psz_basename) + strlen(psz_extract_dir) + 24);
|
2012-01-31 01:40:22 +00:00
|
|
|
psz_fullpath = (char*)calloc(sizeof(char), i_length);
|
|
|
|
if (psz_fullpath == NULL) {
|
|
|
|
uprintf("Error allocating file name\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
i_length = safe_sprintf(psz_fullpath, i_length, "%s%s/%s", psz_extract_dir, psz_path, psz_basename);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (i_length < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (udf_is_dir(p_udf_dirent)) {
|
2012-03-03 23:12:48 +00:00
|
|
|
if (!scan_only) _mkdirU(psz_fullpath);
|
2012-01-31 01:40:22 +00:00
|
|
|
p_udf_dirent2 = udf_opendir(p_udf_dirent);
|
|
|
|
if (p_udf_dirent2 != NULL) {
|
|
|
|
if (udf_extract_files(p_udf, p_udf_dirent2, &psz_fullpath[strlen(psz_extract_dir)]))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-01 14:26:36 +00:00
|
|
|
i_file_length = udf_get_file_length(p_udf_dirent);
|
2012-03-16 18:30:44 +00:00
|
|
|
if (check_iso_props(psz_path, &is_syslinux_cfg, &is_old_vesamenu, i_file_length, psz_basename, psz_fullpath)) {
|
2012-02-01 14:26:36 +00:00
|
|
|
safe_free(psz_fullpath);
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
// Replace slashes with backslashes and append the size to the path for UI display
|
|
|
|
nul_pos = safe_strlen(psz_fullpath);
|
2012-02-08 00:26:37 +00:00
|
|
|
for (i=0; i<nul_pos; i++) if (psz_fullpath[i] == '/') psz_fullpath[i] = '\\';
|
2012-02-07 02:05:58 +00:00
|
|
|
safe_strcpy(&psz_fullpath[nul_pos], 24, size_to_hr(i_file_length));
|
2012-02-01 14:26:36 +00:00
|
|
|
uprintf("Extracting: %s\n", psz_fullpath);
|
|
|
|
SetWindowTextU(hISOFileName, psz_fullpath);
|
2012-02-07 02:05:58 +00:00
|
|
|
// Remove the appended size for extraction
|
|
|
|
psz_fullpath[nul_pos] = 0;
|
2012-03-11 01:55:25 +00:00
|
|
|
if (is_old_vesamenu && use_own_vesamenu) {
|
|
|
|
if (CopyFileA("vesamenu.c32", psz_fullpath, FALSE)) {
|
|
|
|
uprintf(" Replaced with local version\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uprintf(" Could not replace file: %s\n", WindowsErrorString());
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
file_handle = CreateFileU(psz_fullpath, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (file_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf(" Unable to create file: %s\n", WindowsErrorString());
|
2012-01-31 01:40:22 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
while (i_file_length > 0) {
|
2012-02-01 14:26:36 +00:00
|
|
|
if (FormatStatus) goto out;
|
2012-01-31 01:40:22 +00:00
|
|
|
memset(buf, 0, UDF_BLOCKSIZE);
|
|
|
|
i_read = udf_read_block(p_udf_dirent, buf, 1);
|
|
|
|
if (i_read < 0) {
|
|
|
|
uprintf(" Error reading UDF file %s\n", &psz_fullpath[strlen(psz_extract_dir)]);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
buf_size = (DWORD)MIN(i_file_length, i_read);
|
2012-02-07 16:17:14 +00:00
|
|
|
ISO_BLOCKING(r = WriteFile(file_handle, buf, buf_size, &wr_size, NULL));
|
|
|
|
if ((!r) || (buf_size != wr_size)) {
|
2012-02-07 02:05:58 +00:00
|
|
|
uprintf(" Error writing file: %s\n", WindowsErrorString());
|
2012-01-31 01:40:22 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
i_file_length -= i_read;
|
2012-02-07 02:05:58 +00:00
|
|
|
if (nb_blocks++ % PROGRESS_THRESHOLD == 0) {
|
2012-02-01 14:26:36 +00:00
|
|
|
SendMessage(hISOProgressBar, PBM_SETPOS, (WPARAM)((MAX_PROGRESS*nb_blocks)/total_blocks), 0);
|
2012-03-29 19:27:53 +00:00
|
|
|
UpdateProgress(OP_DOS, 100.0f*nb_blocks/total_blocks);
|
2012-02-07 02:05:58 +00:00
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
// If you have a fast USB 3.0 device, the default Windows buffering does an
|
|
|
|
// excellent job at compensating for our small blocks read/writes to max out the
|
|
|
|
// device's bandwidth.
|
|
|
|
// The drawback however is with cancellation. With a large file, CloseHandle()
|
2012-02-07 16:17:14 +00:00
|
|
|
// may take forever to complete and is not interruptible. We try to detect this.
|
|
|
|
ISO_BLOCKING(safe_closehandle(file_handle));
|
2012-03-01 19:19:12 +00:00
|
|
|
if (is_syslinux_cfg) {
|
|
|
|
// Workaround for isolinux config files requiring an ISO label for kernel
|
|
|
|
// append that may be different from our USB label.
|
2012-03-27 19:31:15 +00:00
|
|
|
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label, TRUE) != NULL)
|
2012-03-01 19:19:12 +00:00
|
|
|
uprintf("Patched %s: '%s' -> '%s'\n", psz_fullpath, iso_report.label, iso_report.usb_label);
|
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
2012-02-01 14:26:36 +00:00
|
|
|
safe_free(psz_fullpath);
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
2012-02-01 14:26:36 +00:00
|
|
|
if (p_udf_dirent != NULL)
|
|
|
|
udf_dirent_free(p_udf_dirent);
|
2012-02-07 16:17:14 +00:00
|
|
|
ISO_BLOCKING(safe_closehandle(file_handle));
|
2012-02-01 14:26:36 +00:00
|
|
|
safe_free(psz_fullpath);
|
2012-01-31 01:40:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-07 02:05:58 +00:00
|
|
|
// Returns 0 on success, nonzero on error
|
2012-01-31 01:40:22 +00:00
|
|
|
static int iso_extract_files(iso9660_t* p_iso, const char *psz_path)
|
|
|
|
{
|
2012-02-07 16:17:14 +00:00
|
|
|
HANDLE file_handle = NULL;
|
2012-02-07 02:05:58 +00:00
|
|
|
DWORD buf_size, wr_size;
|
2012-03-11 01:55:25 +00:00
|
|
|
BOOL s, is_syslinux_cfg, is_old_vesamenu;
|
2012-01-31 01:40:22 +00:00
|
|
|
int i_length, r = 1;
|
2012-02-07 02:05:58 +00:00
|
|
|
char psz_fullpath[1024], *psz_basename;
|
2012-01-31 01:40:22 +00:00
|
|
|
const char *psz_iso_name = &psz_fullpath[strlen(psz_extract_dir)];
|
|
|
|
unsigned char buf[ISO_BLOCKSIZE];
|
|
|
|
CdioListNode_t* p_entnode;
|
|
|
|
iso9660_stat_t *p_statbuf;
|
|
|
|
CdioList_t* p_entlist;
|
2012-02-07 02:05:58 +00:00
|
|
|
size_t i, nul_pos;
|
2012-01-31 01:40:22 +00:00
|
|
|
lsn_t lsn;
|
|
|
|
int64_t i_file_length;
|
|
|
|
|
|
|
|
if ((p_iso == NULL) || (psz_path == NULL))
|
|
|
|
return 1;
|
|
|
|
|
2012-02-07 02:05:58 +00:00
|
|
|
i_length = safe_sprintf(psz_fullpath, sizeof(psz_fullpath), "%s%s/", psz_extract_dir, psz_path);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (i_length < 0)
|
|
|
|
return 1;
|
|
|
|
psz_basename = &psz_fullpath[i_length];
|
|
|
|
|
|
|
|
p_entlist = iso9660_ifs_readdir(p_iso, psz_path);
|
2012-02-16 20:44:16 +00:00
|
|
|
if (!p_entlist) {
|
|
|
|
uprintf("Could not access directory %s\n", psz_path);
|
2012-01-31 01:40:22 +00:00
|
|
|
return 1;
|
2012-02-16 20:44:16 +00:00
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-15 21:55:41 +00:00
|
|
|
_CDIO_LIST_FOREACH(p_entnode, p_entlist) {
|
2012-02-01 14:26:36 +00:00
|
|
|
if (FormatStatus) goto out;
|
2012-01-31 01:40:22 +00:00
|
|
|
p_statbuf = (iso9660_stat_t*) _cdio_list_node_data(p_entnode);
|
|
|
|
/* Eliminate . and .. entries */
|
|
|
|
if ( (strcmp(p_statbuf->filename, ".") == 0)
|
|
|
|
|| (strcmp(p_statbuf->filename, "..") == 0) )
|
|
|
|
continue;
|
2012-02-15 21:55:41 +00:00
|
|
|
iso9660_name_translate_ext(p_statbuf->filename, psz_basename, i_joliet_level);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (p_statbuf->type == _STAT_DIR) {
|
2012-03-03 23:12:48 +00:00
|
|
|
if (!scan_only) _mkdirU(psz_fullpath);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (iso_extract_files(p_iso, psz_iso_name))
|
|
|
|
goto out;
|
|
|
|
} else {
|
2012-02-01 14:26:36 +00:00
|
|
|
i_file_length = p_statbuf->size;
|
2012-03-16 18:30:44 +00:00
|
|
|
if (check_iso_props(psz_path, &is_syslinux_cfg, &is_old_vesamenu, i_file_length, psz_basename, psz_fullpath)) {
|
2012-02-01 14:26:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
// Replace slashes with backslashes and append the size to the path for UI display
|
|
|
|
nul_pos = safe_strlen(psz_fullpath);
|
2012-02-08 00:26:37 +00:00
|
|
|
for (i=0; i<nul_pos; i++) if (psz_fullpath[i] == '/') psz_fullpath[i] = '\\';
|
2012-02-07 02:05:58 +00:00
|
|
|
safe_strcpy(&psz_fullpath[nul_pos], 24, size_to_hr(i_file_length));
|
2012-01-31 01:40:22 +00:00
|
|
|
uprintf("Extracting: %s\n", psz_fullpath);
|
2012-02-07 02:05:58 +00:00
|
|
|
SetWindowTextU(hISOFileName, psz_fullpath);
|
2012-02-08 00:26:37 +00:00
|
|
|
// ISO9660 cannot handle backslashes
|
|
|
|
for (i=0; i<nul_pos; i++) if (psz_fullpath[i] == '\\') psz_fullpath[i] = '/';
|
2012-02-07 02:05:58 +00:00
|
|
|
psz_fullpath[nul_pos] = 0;
|
2012-03-11 01:55:25 +00:00
|
|
|
if (is_old_vesamenu && use_own_vesamenu) {
|
|
|
|
if (CopyFileA("vesamenu.c32", psz_fullpath, FALSE)) {
|
|
|
|
uprintf(" Replaced with local version\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uprintf(" Could not replace file: %s\n", WindowsErrorString());
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
file_handle = CreateFileU(psz_fullpath, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (file_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf(" Unable to create file: %s\n", WindowsErrorString());
|
2012-01-31 01:40:22 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i_file_length > 0; i++) {
|
2012-02-01 14:26:36 +00:00
|
|
|
if (FormatStatus) goto out;
|
2012-01-31 01:40:22 +00:00
|
|
|
memset(buf, 0, ISO_BLOCKSIZE);
|
|
|
|
lsn = p_statbuf->lsn + (lsn_t)i;
|
|
|
|
if (iso9660_iso_seek_read(p_iso, buf, lsn, 1) != ISO_BLOCKSIZE) {
|
|
|
|
uprintf(" Error reading ISO9660 file %s at LSN %lu\n",
|
|
|
|
psz_iso_name, (long unsigned int)lsn);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-07 02:05:58 +00:00
|
|
|
buf_size = (DWORD)MIN(i_file_length, ISO_BLOCKSIZE);
|
2012-02-07 16:17:14 +00:00
|
|
|
ISO_BLOCKING(s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL));
|
|
|
|
if ((!s) || (buf_size != wr_size)) {
|
2012-02-07 02:05:58 +00:00
|
|
|
uprintf(" Error writing file: %s\n", WindowsErrorString());
|
2012-01-31 01:40:22 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
i_file_length -= ISO_BLOCKSIZE;
|
2012-02-07 02:05:58 +00:00
|
|
|
if (nb_blocks++ % PROGRESS_THRESHOLD == 0) {
|
2012-02-01 14:26:36 +00:00
|
|
|
SendMessage(hISOProgressBar, PBM_SETPOS, (WPARAM)((MAX_PROGRESS*nb_blocks)/total_blocks), 0);
|
2012-03-29 19:27:53 +00:00
|
|
|
UpdateProgress(OP_DOS, 100.0f*nb_blocks/total_blocks);
|
2012-02-07 02:05:58 +00:00
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
2012-02-07 16:17:14 +00:00
|
|
|
ISO_BLOCKING(safe_closehandle(file_handle));
|
2012-03-01 19:19:12 +00:00
|
|
|
if (is_syslinux_cfg) {
|
2012-03-27 19:31:15 +00:00
|
|
|
if (replace_in_token_data(psz_fullpath, "append", iso_report.label, iso_report.usb_label, TRUE) != NULL)
|
2012-03-01 19:19:12 +00:00
|
|
|
uprintf("Patched %s: '%s' -> '%s'\n", psz_fullpath, iso_report.label, iso_report.usb_label);
|
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
out:
|
2012-02-07 16:17:14 +00:00
|
|
|
ISO_BLOCKING(safe_closehandle(file_handle));
|
2012-01-31 01:40:22 +00:00
|
|
|
_cdio_list_free(p_entlist, true);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-11-04 01:33:54 +00:00
|
|
|
BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan)
|
2012-01-31 01:40:22 +00:00
|
|
|
{
|
2012-02-21 00:08:31 +00:00
|
|
|
size_t i;
|
2012-05-30 23:49:28 +00:00
|
|
|
int j;
|
2012-02-21 00:08:31 +00:00
|
|
|
FILE* fd;
|
2012-01-31 01:40:22 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
iso9660_t* p_iso = NULL;
|
|
|
|
udf_t* p_udf = NULL;
|
|
|
|
udf_dirent_t* p_udf_root;
|
2012-02-01 14:26:36 +00:00
|
|
|
LONG progress_style;
|
2012-03-27 19:31:15 +00:00
|
|
|
char* tmp;
|
|
|
|
char path[64];
|
2012-02-21 00:08:31 +00:00
|
|
|
const char* scan_text = "Scanning ISO image...";
|
2012-03-27 19:31:15 +00:00
|
|
|
const char* basedir[] = { "i386", "minint" };
|
|
|
|
const char* tmp_sif = ".\\txtsetup.sif~";
|
2012-02-01 14:26:36 +00:00
|
|
|
|
|
|
|
if ((src_iso == NULL) || (dest_dir == NULL))
|
|
|
|
return FALSE;
|
2012-01-31 01:40:22 +00:00
|
|
|
|
2012-02-01 14:26:36 +00:00
|
|
|
scan_only = scan;
|
2012-02-14 20:52:39 +00:00
|
|
|
cdio_log_set_handler(log_handler);
|
2012-02-01 14:26:36 +00:00
|
|
|
psz_extract_dir = dest_dir;
|
|
|
|
progress_style = GetWindowLong(hISOProgressBar, GWL_STYLE);
|
|
|
|
if (scan_only) {
|
|
|
|
total_blocks = 0;
|
2012-03-11 01:55:25 +00:00
|
|
|
memset(&iso_report, 0, sizeof(iso_report));
|
2012-02-21 00:08:31 +00:00
|
|
|
// String array of all isolinux/syslinux locations
|
|
|
|
StrArrayCreate(&config_path, 8);
|
2012-02-01 14:26:36 +00:00
|
|
|
// Change the Window title and static text
|
|
|
|
SetWindowTextU(hISOProgressDlg, scan_text);
|
|
|
|
SetWindowTextU(hISOFileName, scan_text);
|
|
|
|
// Change progress style to marquee for scanning
|
|
|
|
SetWindowLong(hISOProgressBar, GWL_STYLE, progress_style | PBS_MARQUEE);
|
|
|
|
SendMessage(hISOProgressBar, PBM_SETMARQUEE, TRUE, 0);
|
|
|
|
} else {
|
|
|
|
uprintf("Extracting files...\n");
|
|
|
|
if (total_blocks == 0) {
|
|
|
|
uprintf("Error: ISO has not been properly scanned.\n");
|
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_ISO_SCAN);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
nb_blocks = 0;
|
2012-02-07 16:17:14 +00:00
|
|
|
iso_blocking_status = 0;
|
2012-02-01 14:26:36 +00:00
|
|
|
SetWindowLong(hISOProgressBar, GWL_STYLE, progress_style & (~PBS_MARQUEE));
|
|
|
|
SendMessage(hISOProgressBar, PBM_SETPOS, 0, 0);
|
|
|
|
}
|
|
|
|
ShowWindow(hISOProgressDlg, SW_SHOW);
|
|
|
|
UpdateWindow(hISOProgressDlg);
|
2012-01-31 01:40:22 +00:00
|
|
|
|
|
|
|
/* First try to open as UDF - fallback to ISO if it failed */
|
|
|
|
p_udf = udf_open(src_iso);
|
|
|
|
if (p_udf == NULL)
|
|
|
|
goto try_iso;
|
2012-02-07 02:05:58 +00:00
|
|
|
uprintf("Disc image is an UDF image\n");
|
2012-01-31 01:40:22 +00:00
|
|
|
|
|
|
|
p_udf_root = udf_get_root(p_udf, true, 0);
|
|
|
|
if (p_udf_root == NULL) {
|
|
|
|
uprintf("Couldn't locate UDF root directory\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-14 20:52:39 +00:00
|
|
|
if (scan_only) {
|
|
|
|
if (udf_get_logical_volume_id(p_udf, iso_report.label, sizeof(iso_report.label)) <= 0)
|
|
|
|
iso_report.label[0] = 0;
|
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
r = udf_extract_files(p_udf, p_udf_root, "");
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
try_iso:
|
2012-02-16 20:44:16 +00:00
|
|
|
p_iso = iso9660_open_ext(src_iso, ISO_EXTENSION_ALL);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (p_iso == NULL) {
|
|
|
|
uprintf("Unable to open image '%s'.\n", src_iso);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-15 21:55:41 +00:00
|
|
|
i_joliet_level = iso9660_ifs_get_joliet_level(p_iso);
|
2012-02-16 23:40:31 +00:00
|
|
|
uprintf("Disc image is an ISO9660 image\n");
|
2012-02-14 20:52:39 +00:00
|
|
|
if (scan_only) {
|
2012-03-27 19:31:15 +00:00
|
|
|
if (iso9660_ifs_get_volume_id(p_iso, &tmp)) {
|
|
|
|
safe_strcpy(iso_report.label, sizeof(iso_report.label), tmp);
|
|
|
|
safe_free(tmp);
|
2012-02-16 20:44:16 +00:00
|
|
|
} else
|
2012-02-14 20:52:39 +00:00
|
|
|
iso_report.label[0] = 0;
|
|
|
|
}
|
2012-01-31 01:40:22 +00:00
|
|
|
r = iso_extract_files(p_iso, "");
|
|
|
|
|
|
|
|
out:
|
2012-02-07 16:17:14 +00:00
|
|
|
iso_blocking_status = -1;
|
2012-02-01 14:26:36 +00:00
|
|
|
if (scan_only) {
|
2012-05-30 23:49:28 +00:00
|
|
|
// Remove trailing spaces from the label
|
2012-11-04 00:29:24 +00:00
|
|
|
for (j=(int)safe_strlen(iso_report.label)-1; ((j>=0)&&(isspace(iso_report.label[j]))); j--)
|
2012-05-30 23:49:28 +00:00
|
|
|
iso_report.label[j] = 0;
|
2012-02-01 14:26:36 +00:00
|
|
|
// We use the fact that UDF_BLOCKSIZE and ISO_BLOCKSIZE are the same here
|
|
|
|
iso_report.projected_size = total_blocks * ISO_BLOCKSIZE;
|
2012-02-21 00:08:31 +00:00
|
|
|
// We will link the existing isolinux.cfg from a syslinux.cfg we create
|
|
|
|
// If multiple config file exist, choose the one with the shortest path
|
|
|
|
if (iso_report.has_isolinux) {
|
|
|
|
safe_strcpy(iso_report.cfg_path, sizeof(iso_report.cfg_path), config_path.Table[0]);
|
|
|
|
for (i=1; i<config_path.Index; i++) {
|
|
|
|
if (safe_strlen(iso_report.cfg_path) > safe_strlen(config_path.Table[i]))
|
|
|
|
safe_strcpy(iso_report.cfg_path, sizeof(iso_report.cfg_path), config_path.Table[i]);
|
|
|
|
}
|
|
|
|
uprintf("Will use %s for Syslinux\n", iso_report.cfg_path);
|
|
|
|
}
|
2012-03-27 19:31:15 +00:00
|
|
|
if (IS_WINPE(iso_report.winpe)) {
|
|
|
|
// In case we have a WinPE 1.x based iso, we extract and parse txtsetup.sif
|
|
|
|
// during scan, to see if /minint was provided for OsLoadOptions, as it decides
|
|
|
|
// whether we should use 0x80 or 0x81 as the disk ID in the MBR
|
|
|
|
safe_sprintf(path, sizeof(path), "/%s/txtsetup.sif",
|
|
|
|
basedir[((iso_report.winpe&WINPE_I386) == WINPE_I386)?0:1]);
|
|
|
|
ExtractISOFile(src_iso, path, tmp_sif);
|
2012-11-12 01:53:34 +00:00
|
|
|
tmp = get_token_data_file("OsLoadOptions", tmp_sif);
|
2012-03-27 19:31:15 +00:00
|
|
|
if (tmp != NULL) {
|
|
|
|
for (i=0; i<strlen(tmp); i++)
|
|
|
|
tmp[i] = (char)tolower(tmp[i]);
|
|
|
|
uprintf("Checking txtsetup.sif:\n OsLoadOptions = %s\n", tmp);
|
|
|
|
iso_report.uses_minint = (strstr(tmp, "/minint") != NULL);
|
|
|
|
}
|
|
|
|
_unlink(tmp_sif);
|
|
|
|
safe_free(tmp);
|
|
|
|
}
|
2012-02-21 00:08:31 +00:00
|
|
|
StrArrayDestroy(&config_path);
|
|
|
|
} else if (iso_report.has_isolinux) {
|
2012-03-27 19:31:15 +00:00
|
|
|
safe_sprintf(path, sizeof(path), "%s\\syslinux.cfg", dest_dir);
|
2012-02-21 00:08:31 +00:00
|
|
|
// Create a /syslinux.cfg (if none exists) that points to the existing isolinux cfg
|
2012-03-27 19:31:15 +00:00
|
|
|
fd = fopen(path, "r");
|
2012-02-21 00:08:31 +00:00
|
|
|
if (fd == NULL) {
|
2012-03-27 19:31:15 +00:00
|
|
|
fd = fopen(path, "w"); // No "/syslinux.cfg" => create a new one
|
2012-02-21 00:08:31 +00:00
|
|
|
if (fd == NULL) {
|
2012-03-27 19:31:15 +00:00
|
|
|
uprintf("Unable to create %s - booting from USB will not work\n", path);
|
2012-02-21 00:08:31 +00:00
|
|
|
r = 1;
|
|
|
|
} else {
|
|
|
|
fprintf(fd, "DEFAULT loadconfig\n\nLABEL loadconfig\n CONFIG %s\n", iso_report.cfg_path);
|
|
|
|
for (i=safe_strlen(iso_report.cfg_path); (i>0)&&(iso_report.cfg_path[i]!='/'); i--);
|
|
|
|
if (i>0) {
|
|
|
|
iso_report.cfg_path[i] = 0;
|
|
|
|
fprintf(fd, " APPEND %s/\n", iso_report.cfg_path);
|
|
|
|
iso_report.cfg_path[i] = '/';
|
|
|
|
}
|
2012-03-27 19:31:15 +00:00
|
|
|
uprintf("Created: %s\n", path);
|
2012-02-21 00:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 19:19:12 +00:00
|
|
|
if (fd != NULL)
|
|
|
|
fclose(fd);
|
2012-02-01 14:26:36 +00:00
|
|
|
}
|
2012-03-11 01:55:25 +00:00
|
|
|
ShowWindow(hISOProgressDlg, SW_HIDE);
|
2012-01-31 01:40:22 +00:00
|
|
|
if (p_iso != NULL)
|
|
|
|
iso9660_close(p_iso);
|
|
|
|
if (p_udf != NULL)
|
|
|
|
udf_close(p_udf);
|
2012-02-01 14:26:36 +00:00
|
|
|
if ((r != 0) && (FormatStatus == 0))
|
2012-02-07 02:05:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR((scan_only?ERROR_ISO_SCAN:ERROR_ISO_EXTRACT));
|
|
|
|
return (r == 0);
|
2012-01-31 01:40:22 +00:00
|
|
|
}
|
2012-03-27 19:31:15 +00:00
|
|
|
|
|
|
|
BOOL ExtractISOFile(const char* iso, const char* iso_file, const char* dest_file)
|
|
|
|
{
|
|
|
|
size_t i;
|
2012-03-27 22:31:58 +00:00
|
|
|
ssize_t read_size;
|
2012-03-27 19:31:15 +00:00
|
|
|
int64_t file_length;
|
|
|
|
char buf[UDF_BLOCKSIZE];
|
|
|
|
DWORD buf_size, wr_size;
|
|
|
|
BOOL s, r = FALSE;
|
|
|
|
iso9660_t* p_iso = NULL;
|
|
|
|
udf_t* p_udf = NULL;
|
|
|
|
udf_dirent_t *p_udf_root = NULL, *p_udf_file = NULL;
|
|
|
|
iso9660_stat_t *p_statbuf = NULL;
|
|
|
|
lsn_t lsn;
|
|
|
|
HANDLE file_handle = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
file_handle = CreateFileU(dest_file, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (file_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf(" Unable to create file %s: %s\n", dest_file, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First try to open as UDF - fallback to ISO if it failed */
|
|
|
|
p_udf = udf_open(iso);
|
|
|
|
if (p_udf == NULL)
|
|
|
|
goto try_iso;
|
|
|
|
|
|
|
|
p_udf_root = udf_get_root(p_udf, true, 0);
|
|
|
|
if (p_udf_root == NULL) {
|
|
|
|
uprintf("Couldn't locate UDF root directory\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
p_udf_file = udf_fopen(p_udf_root, iso_file);
|
|
|
|
if (!p_udf_file) {
|
|
|
|
uprintf("Couldn't locate file %s in ISO image\n", iso_file);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
file_length = udf_get_file_length(p_udf_file);
|
|
|
|
while (file_length > 0) {
|
|
|
|
memset(buf, 0, UDF_BLOCKSIZE);
|
2012-03-27 22:31:58 +00:00
|
|
|
read_size = udf_read_block(p_udf_file, buf, 1);
|
|
|
|
if (read_size < 0) {
|
2012-03-27 19:31:15 +00:00
|
|
|
uprintf("Error reading UDF file %s\n", iso_file);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-03-27 22:31:58 +00:00
|
|
|
buf_size = (DWORD)MIN(file_length, read_size);
|
2012-03-27 19:31:15 +00:00
|
|
|
s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL);
|
|
|
|
if ((!s) || (buf_size != wr_size)) {
|
|
|
|
uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2012-03-27 22:31:58 +00:00
|
|
|
file_length -= read_size;
|
2012-03-27 19:31:15 +00:00
|
|
|
}
|
|
|
|
r = TRUE;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
try_iso:
|
|
|
|
p_iso = iso9660_open(iso);
|
|
|
|
if (p_iso == NULL) {
|
|
|
|
uprintf("Unable to open image '%s'.\n", iso);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_statbuf = iso9660_ifs_stat_translate(p_iso, iso_file);
|
|
|
|
if (p_statbuf == NULL) {
|
|
|
|
uprintf("Could not get ISO-9660 file information for file %s\n", iso_file);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_length = p_statbuf->size;
|
|
|
|
for (i = 0; file_length > 0; i++) {
|
|
|
|
memset(buf, 0, ISO_BLOCKSIZE);
|
|
|
|
lsn = p_statbuf->lsn + (lsn_t)i;
|
|
|
|
if (iso9660_iso_seek_read(p_iso, buf, lsn, 1) != ISO_BLOCKSIZE) {
|
|
|
|
uprintf(" Error reading ISO9660 file %s at LSN %lu\n", iso_file, (long unsigned int)lsn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
buf_size = (DWORD)MIN(file_length, ISO_BLOCKSIZE);
|
|
|
|
s = WriteFile(file_handle, buf, buf_size, &wr_size, NULL);
|
|
|
|
if ((!s) || (buf_size != wr_size)) {
|
|
|
|
uprintf(" Error writing file %s: %s\n", dest_file, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
file_length -= ISO_BLOCKSIZE;
|
|
|
|
}
|
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
safe_closehandle(file_handle);
|
2012-03-27 22:31:58 +00:00
|
|
|
if (p_statbuf != NULL)
|
|
|
|
safe_free(p_statbuf->rr.psz_symlink);
|
2012-03-27 19:31:15 +00:00
|
|
|
safe_free(p_statbuf);
|
|
|
|
if (p_udf_root != NULL)
|
|
|
|
udf_dirent_free(p_udf_root);
|
|
|
|
if (p_udf_file != NULL)
|
|
|
|
udf_dirent_free(p_udf_file);
|
|
|
|
if (p_iso != NULL)
|
|
|
|
iso9660_close(p_iso);
|
|
|
|
if (p_udf != NULL)
|
|
|
|
udf_close(p_udf);
|
|
|
|
return (r == 0);
|
|
|
|
}
|