Skip to content

Commit

Permalink
Fix handling for directories with extended characters
Browse files Browse the repository at this point in the history
* Closes #3
  • Loading branch information
VitaSmith committed Apr 10, 2021
1 parent 35794da commit 318147f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
6 changes: 3 additions & 3 deletions cdecrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cdecrypt - Decrypt Wii U NUS content files
Copyright © 2013-2015 crediar <https://code.google.com/p/cdecrypt/>
Copyright © 2020 VitaSmith <https://github.com/VitaSmith/cdecrypt>
Copyright © 2020-2021 VitaSmith <https://github.com/VitaSmith/cdecrypt>
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
Expand Down Expand Up @@ -366,13 +366,13 @@ int main_utf8(int argc, char** argv)

if (argc < 2) {
printf("%s %s - Wii U NUS content file decrypter\n"
"Copyright (c) 2020 VitaSmith, Copyright (c) 2013-2015 crediar\n"
"Copyright (c) 2020-2021 VitaSmith, Copyright (c) 2013-2015 crediar\n"
"Visit https://github.com/VitaSmith/cdecrypt for official source and downloads.\n\n"
"Usage: %s <file or directory>\n\n"
"This program is free software; you can redistribute it and/or modify it under\n"
"the terms of the GNU General Public License as published by the Free Software\n"
"Foundation; either version 3 of the License or any later version.\n",
appname(argv[0]), APP_VERSION_STR, appname(argv[0]));
_appname(argv[0]), APP_VERSION_STR, _appname(argv[0]));
return EXIT_SUCCESS;
}

Expand Down
9 changes: 9 additions & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ static __inline int stat64_utf8(const char* path, struct stat64* buffer)
return r;
}

static __inline BOOL CreateDirectory_utf8(const char* path, LPSECURITY_ATTRIBUTES attrs)
{
BOOL r;
wchar_t* path16 = utf8_to_utf16(path);
r = CreateDirectoryW(path16, attrs);
free(path16);
return r;
}

#define CALL_MAIN int wmain(int argc, wchar_t** argv16) { \
SetConsoleOutputCP(CP_UTF8); \
char** argv = calloc(argc, sizeof(char*)); \
Expand Down
77 changes: 74 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Common code for Gust (Koei/Tecmo) PC games tools
Copyright © 2019-2020 VitaSmith
Copyright © 2019-2021 VitaSmith
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
Expand Down Expand Up @@ -28,12 +28,18 @@ bool create_path(char* path)
{
bool result = true;
struct stat64_t st;
#if defined(_WIN32)
// Ignore Windows drive names
if ((strlen(path) == 2) && (path[1] == ':'))
return true;
#endif
if (stat64_utf8(path, &st) != 0) {
// Directory doesn't exist, create it
size_t pos = 0;
for (size_t n = strlen(path); n > 0; n--) {
if (path[n] == PATH_SEP) {
pos = n;
while ((n > 0) && (path[--n] == PATH_SEP));
pos = n + 1;
break;
}
}
Expand Down Expand Up @@ -61,6 +67,71 @@ bool create_path(char* path)
return result;
}

// dirname/basename, that *PRESERVE* the string parameter.
// Note that these calls are not concurrent, meaning that you MUST be done
// using the returned string from a previous call before invoking again.
#if defined(_WIN32)
char* _basename_win32(const char* path, bool remove_extension)
{
static char basename[128];
static char ext[64];
ext[0] = 0;
_splitpath_s(path, NULL, 0, NULL, 0, basename, sizeof(basename), ext, sizeof(ext));
if ((ext[0] != 0) && !remove_extension)
strncat(basename, ext, sizeof(basename) - strlen(basename));
return basename;
}

// This call should behave pretty similar to UNIX' dirname
char* _dirname_win32(const char* path)
{
static char dir[PATH_MAX];
static char drive[4];
int found_sep = 0;
memset(drive, 0, sizeof(drive));
_splitpath_s(path, drive, sizeof(drive), dir, sizeof(dir) - 3, NULL, 0, NULL, 0);
// Only deal with drives that are one letter
drive[2] = 0;
drive[3] = 0;
if (drive[1] != ':')
drive[0] = 0;
// Removing trailing path separators
for (int32_t n = (int32_t)strlen(dir) - 1; (n > 0) && ((dir[n] == '/') || (dir[n] == '\\')); n--) {
dir[n] = 0;
found_sep++;
}
if (dir[0] == 0) {
if (drive[0] == 0)
return found_sep ? "\\" : ".";
drive[2] = '\\';
return drive;
}
if (drive[0] != 0) {
// Add the drive
memmove(&dir[2], dir, strlen(dir) + 1);
memcpy(dir, drive, strlen(drive));
dir[2] = '\\';
}
return dir;
}
#else
char* _basename_unix(const char* path)
{
static char path_copy[PATH_MAX];
strncpy(path_copy, path, sizeof(path_copy));
path_copy[PATH_MAX - 1] = 0;
return basename(path_copy);
}

char* _dirname_unix(const char* path)
{
static char path_copy[PATH_MAX];
strncpy(path_copy, path, sizeof(path_copy));
path_copy[PATH_MAX - 1] = 0;
return dirname(path_copy);
}
#endif

bool is_file(const char* path)
{
struct stat64_t st;
Expand All @@ -76,7 +147,7 @@ bool is_directory(const char* path)
char* change_extension(const char* path, const char* extension)
{
static char new_path[PATH_MAX];
strncpy(new_path, basename((char*)path), sizeof(new_path) - 1);
strncpy(new_path, _basename((char*)path), sizeof(new_path) - 1);
for (size_t i = 0; i < sizeof(new_path); i++) {
if (new_path[i] == '.')
new_path[i] = 0;
Expand Down
29 changes: 13 additions & 16 deletions util.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Common code for Gust (Koei/Tecmo) PC games tools
Copyright © 2019-2020 VitaSmith
Copyright © 2019-2021 VitaSmith
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
Expand All @@ -22,7 +22,7 @@
#include <crtdbg.h>
#endif

#if defined(__APPLE__)
#if !defined(_WIN32)
#include <libgen.h>
#endif
#include <stdbool.h>
Expand Down Expand Up @@ -50,7 +50,7 @@
#if !defined(S_ISREG)
#define S_ISREG(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFREG)
#endif
#define CREATE_DIR(path) CreateDirectoryA(path, NULL)
#define CREATE_DIR(path) CreateDirectory_utf8(path, NULL)
#define PATH_SEP '\\'
#else
#if defined(__APPLE__)
Expand Down Expand Up @@ -85,20 +85,17 @@
#endif

#if defined(_WIN32)
static __inline char* _basename(const char* path, bool remove_extension)
{
static char basename[128];
static char ext[64];
ext[0] = 0;
_splitpath_s(path, NULL, 0, NULL, 0, basename, sizeof(basename), ext, sizeof(ext));
if ((ext[0] != 0) && !remove_extension)
strncat(basename, ext, sizeof(basename) - strlen(basename));
return basename;
}
#define basename(path) _basename(path, false)
#define appname(path) _basename(path, true)
char* _basename_win32(const char* path, bool remove_extension);
char* _dirname_win32(const char* path);
#define _basename(path) _basename_win32(path, false)
#define _appname(path) _basename_win32(path, true)
#define _dirname(path) _dirname_win32(path)
#else
#define appname(path) basename(path)
char* _basename_unix(const char* path);
char* _dirname_unix(const char* path);
#define _basename(path) _basename_unix(path)
#define _appname(path) _basename_unix(path)
#define _dirname(path) _dirname_unix(path)
#endif

#if defined (_MSC_VER)
Expand Down

0 comments on commit 318147f

Please sign in to comment.