Skip to content

Commit

Permalink
Display and handle filenames with invalid multibyte sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlopezroche committed Sep 24, 2019
1 parent 26f9c26 commit a708010
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bin_PROGRAMS = fdupes

if NO_NCURSES
fdupes_SOURCES = fdupes.c errormsg.c dir.c log.c fmatch.c sigint.c flags.c md5/md5.c fdupes.h errormsg.h dir.h log.h fmatch.h sigint.h flags.h md5/md5.h
fdupes_SOURCES = fdupes.c errormsg.c dir.c log.c fmatch.c sigint.c flags.c mbstowcs_escape_invalid.c positive_wcwidth.c md5/md5.c fdupes.h errormsg.h dir.h log.h fmatch.h sigint.h flags.h mbstowcs_escape_invalid.h positive_wcwidth.h md5/md5.h
notrans_dist_man1_MANS = fdupes.1

else
fdupes_SOURCES = fdupes.c ncurses-commands.c ncurses-getcommand.c ncurses-interface.c ncurses-print.c ncurses-prompt.c ncurses-status.c commandidentifier.c errormsg.c wcs.c dir.c log.c fmatch.c sigint.c flags.c md5/md5.c fdupes.h ncurses-commands.h ncurses-getcommand.h ncurses-interface.h ncurses-print.h ncurses-prompt.h ncurses-status.h commandidentifier.h errormsg.h wcs.h filegroup.h dir.h log.h fmatch.h sigint.h flags.h md5/md5.h
fdupes_SOURCES = fdupes.c ncurses-commands.c ncurses-getcommand.c ncurses-interface.c ncurses-print.c ncurses-prompt.c ncurses-status.c commandidentifier.c errormsg.c wcs.c dir.c log.c fmatch.c sigint.c flags.c mbstowcs_escape_invalid.c positive_wcwidth.c md5/md5.c fdupes.h ncurses-commands.h ncurses-getcommand.h ncurses-interface.h ncurses-print.h ncurses-prompt.h ncurses-status.h commandidentifier.h errormsg.h wcs.h filegroup.h dir.h log.h fmatch.h sigint.h flags.h mbstowcs_escape_invalid.h positive_wcwidth.h md5/md5.h
dist_man1_MANS = fdupes.1 fdupes-help.1

endif
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ AS_IF([test x"$with_ncurses" != x"no"],
[AC_DEFINE([_XOPEN_SOURCE], [], [enable certain functions in wchar.h])]
[AC_DEFINE([_XOPEN_SOURCE_EXTENDED], [], [enable certain functions in curses.h])]
[AC_DEFINE([_ISOC99_SOURCE], [], [enable strtoll])]
[AC_SEARCH_LIBS([pcre2_match_8], [pcre2-8], [], [AC_ERROR([pcre2 library not found])])]
[AC_DEFINE([PCRE2_CODE_UNIT_WIDTH], [8], [PCRE2 Code Unit Width])],
[AC_SEARCH_LIBS([pcre2_match_32], [pcre2-32], [], [AC_ERROR([pcre2 library not found])])]
[AC_DEFINE([PCRE2_CODE_UNIT_WIDTH], [32], [PCRE2 Code Unit Width])],

[AC_DEFINE([NO_NCURSES], [], [Do not compile against ncurses])]
)
Expand Down
147 changes: 147 additions & 0 deletions mbstowcs_escape_invalid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* Copyright (c) 2019 Adrian Lopez
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. */

#include "config.h"
#include "mbstowcs_escape_invalid.h"
#include <wchar.h>
#include <string.h>
#include <stdio.h>

void reset_mbstate(mbstate_t *state)
{
memset(state, 0, sizeof(mbstate_t));
}

size_t putoctal(wchar_t *dest, char c)
{
swprintf(dest, 5, L"\\%03o", (unsigned char) c);
return 4;
}

size_t put_invalid_sequence(wchar_t *dest, const char *src, size_t *destination_index, size_t count, size_t max)
{
size_t x;

for (x = 0; x < count; ++x)
{
if (dest != 0)
{
if (*destination_index + 5 > max)
return x;

putoctal(dest + *destination_index, src[x]);
}

*destination_index += 4;
}

return count;
}

size_t mbstowcs_escape_invalid(wchar_t *dest, const char *src, size_t n)
{
mbstate_t state;
wchar_t wc;
size_t x;
size_t i;
size_t dx;
size_t write;
size_t written;
size_t result;

reset_mbstate(&state);

x = 0;
i = 0;
dx = 0;

while (src[x] != '\0')
{
result = mbrtowc(&wc, src + x, 1, &state);

if (result == -2)
/* sequence is not yet complete */
{
++x;
++i;
}
else if (result == -1)
/* invalid sequence */
{
write = i == 0 ? 1 : i;

if (dest != 0)
{
written = put_invalid_sequence(dest, src + (x - i), &dx, write, n);

if (written != write)
return -1;
}
else
put_invalid_sequence(0, src + (x - i), &dx, write, 0);

if (i == 0)
++x;

i = 0;

reset_mbstate(&state);
}
else if (result != 0)
/* OK, add character */
{
if (dest != 0)
{
if (dx < n)
dest[dx++] = wc;
else
return -1;
}
else
++dx;

++x;

i = 0;
}

if (src[x] == L'\0' && i > 0)
/* output final incomplete sequence */
{
if (dest != 0)
{
written = put_invalid_sequence(dest, src + (x - i), &dx, i, n);

if (written != i)
return -1;
}
else
put_invalid_sequence(0, src + (x - i), &dx, i, 0);
}
}

if (dest != 0)
{
if (dx < n)
dest[dx] = L'\0';
else
return -1;
}

return dx + 1;
}
26 changes: 26 additions & 0 deletions mbstowcs_escape_invalid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2019 Adrian Lopez
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. */

#ifndef MBSTOWCS_ESCAPE_INVALID_H
#define MBSTOWCS_ESCAPE_INVALID_H

#include <wchar.h>

size_t mbstowcs_escape_invalid(wchar_t *dest, const char *src, size_t n);

#endif
57 changes: 32 additions & 25 deletions ncurses-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ncurses-status.h"
#include "ncurses-commands.h"
#include "wcs.h"
#include "mbstowcs_escape_invalid.h"
#include <wchar.h>
#include <pcre2.h>

Expand Down Expand Up @@ -211,7 +212,8 @@ int cmd_select_matching(struct filegroup *groups, int groupcount, wchar_t *comma
int cmd_select_regex(struct filegroup *groups, int groupcount, wchar_t *commandarguments, struct status_text *status)
{
size_t size;
char *mbs;
wchar_t *wcsfilename;
size_t needed;
int errorcode;
PCRE2_SIZE erroroffset;
pcre2_code *code;
Expand All @@ -223,16 +225,7 @@ int cmd_select_regex(struct filegroup *groups, int groupcount, wchar_t *commanda
int selectedfilecount = 0;
int groupselected;

size = wcstombs(0, commandarguments, 0) + 1;
mbs = (char*) malloc(size * sizeof(char));
if (mbs == 0)
return -1;

wcstombs(mbs, commandarguments, size);

code = pcre2_compile((PCRE2_SPTR8)mbs, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_UCP, &errorcode, &erroroffset, 0);

free(mbs);
code = pcre2_compile((PCRE2_SPTR)commandarguments, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_UCP, &errorcode, &erroroffset, 0);

if (code == 0)
return -1;
Expand All @@ -249,7 +242,18 @@ int cmd_select_regex(struct filegroup *groups, int groupcount, wchar_t *commanda

for (f = 0; f < groups[g].filecount; ++f)
{
matches = pcre2_match(code, (PCRE2_SPTR8)groups[g].files[f].file->d_name, PCRE2_ZERO_TERMINATED, 0, 0, md, 0);
needed = mbstowcs_escape_invalid(0, groups[g].files[f].file->d_name, 0);

wcsfilename = (wchar_t*) malloc(needed * sizeof(wchar_t));
if (wcsfilename == 0)
continue;

mbstowcs_escape_invalid(wcsfilename, groups[g].files[f].file->d_name, needed);

matches = pcre2_match(code, (PCRE2_SPTR)wcsfilename, PCRE2_ZERO_TERMINATED, 0, 0, md, 0);

free(wcsfilename);

if (matches > 0)
{
groups[g].selected = 1;
Expand Down Expand Up @@ -475,7 +479,8 @@ int cmd_clear_selections_matching(struct filegroup *groups, int groupcount, wcha
int cmd_clear_selections_regex(struct filegroup *groups, int groupcount, wchar_t *commandarguments, struct status_text *status)
{
size_t size;
char *mbs;
wchar_t *wcsfilename;
size_t needed;
int errorcode;
PCRE2_SIZE erroroffset;
pcre2_code *code;
Expand All @@ -489,16 +494,7 @@ int cmd_clear_selections_regex(struct filegroup *groups, int groupcount, wchar_t
int filedeselected;
int selectionsremaining;

size = wcstombs(0, commandarguments, 0) + 1;
mbs = (char*) malloc(size * sizeof(char));
if (mbs == 0)
return -1;

wcstombs(mbs, commandarguments, size);

code = pcre2_compile((PCRE2_SPTR8)mbs, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_UCP, &errorcode, &erroroffset, 0);

free(mbs);
code = pcre2_compile((PCRE2_SPTR)commandarguments, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_UCP, &errorcode, &erroroffset, 0);

if (code == 0)
return -1;
Expand All @@ -517,7 +513,18 @@ int cmd_clear_selections_regex(struct filegroup *groups, int groupcount, wchar_t

for (f = 0; f < groups[g].filecount; ++f)
{
matches = pcre2_match(code, (PCRE2_SPTR8)groups[g].files[f].file->d_name, PCRE2_ZERO_TERMINATED, 0, 0, md, 0);
needed = mbstowcs_escape_invalid(0, groups[g].files[f].file->d_name, 0);

wcsfilename = (wchar_t*) malloc(needed * sizeof(wchar_t));
if (wcsfilename == 0)
continue;

mbstowcs_escape_invalid(wcsfilename, groups[g].files[f].file->d_name, needed);

matches = pcre2_match(code, (PCRE2_SPTR)wcsfilename, PCRE2_ZERO_TERMINATED, 0, 0, md, 0);

free(wcsfilename);

if (matches > 0)
{
if (groups[g].files[f].selected)
Expand Down Expand Up @@ -666,4 +673,4 @@ int cmd_reset_selected(struct filegroup *groups, int groupcount, wchar_t *comman
format_status_left(status, L"Unmarked %d files.", resetfilecount);

return 1;
}
}
32 changes: 20 additions & 12 deletions ncurses-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "ncurses-prompt.h"
#include "ncurses-status.h"
#include "ncurses-print.h"
#include "mbstowcs_escape_invalid.h"
#include "positive_wcwidth.h"
#include "commandidentifier.h"
#include "filegroup.h"
#include "errormsg.h"
Expand Down Expand Up @@ -70,17 +72,25 @@ int filerowcount(file_t *file, const int columns, int group_file_count)
{
int lines;
int line_remaining;
int x = 0;
size_t x = 0;
size_t read;
size_t filename_bytes;
wchar_t ch;
mbstate_t mbstate;
int index_width;
int timestamp_width;
size_t needed;
wchar_t *wcfilename;

memset(&mbstate, 0, sizeof(mbstate));

filename_bytes = strlen(file->d_name);
needed = mbstowcs_escape_invalid(0, file->d_name, 0);

wcfilename = (wchar_t*)malloc(sizeof(wchar_t) * needed);
if (wcfilename == 0)
return 0;

mbstowcs_escape_invalid(wcfilename, file->d_name, needed);

index_width = get_num_digits(group_file_count);
if (index_width < FILE_INDEX_MIN_WIDTH)
Expand All @@ -92,25 +102,23 @@ int filerowcount(file_t *file, const int columns, int group_file_count)

line_remaining = columns - (index_width + timestamp_width + FILENAME_INDENT_EXTRA) % columns;

while (x < filename_bytes)
while (wcfilename[x] != L'\0')
{
read = mbrtowc(&ch, file->d_name + x, filename_bytes - x, &mbstate);
if (read < 0)
return lines;

x += read;

if (wcwidth(ch) <= line_remaining)
if (positive_wcwidth(wcfilename[x]) <= line_remaining)
{
line_remaining -= wcwidth(ch);
line_remaining -= positive_wcwidth(wcfilename[x]);
}
else
{
line_remaining = columns - wcwidth(ch);
line_remaining = columns - positive_wcwidth(wcfilename[x]);
++lines;
}

++x;
}

free(wcfilename);

return lines;
}

Expand Down
Loading

0 comments on commit a708010

Please sign in to comment.