[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] md5sum, sha*sum: only escape file names containing newlines
From: |
Pádraig Brady |
Subject: |
[PATCH] md5sum, sha*sum: only escape file names containing newlines |
Date: |
Fri, 1 Nov 2013 16:29:25 +0000 |
This should be fully backwards and forwards compatible with previous
escaping, since we'll never have a file name at the start of a line,
thus '\' at the start of a line always means the file name is escaped.
* src/md5sum.c (main): Only escape the output (with a leading '\')
when a '\n' character is present in the file name. There is no
need to do this in the more likely case where a '\' character is
present in the name. Note this is faster also as we only scan the
file name once (for '\n') rather than four times.
(print_filename): Use the predetermined boolean as to whether to
escape or not, so that in the common case we can output the
file name directly, rather than inspecting each character.
* tests/misc/md5sum.pl: Adjust accordingly.
* tests/misc/sha1sum.pl: Likewise.
---
src/md5sum.c | 22 +++++++++++++++-------
tests/misc/md5sum.pl | 6 ++++--
tests/misc/sha1sum.pl | 6 ++++--
3 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/src/md5sum.c b/src/md5sum.c
index b437811..43fabe2 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -657,11 +657,17 @@ digest_check (const char *checkfile_name)
&& (!strict || n_improperly_formatted_lines == 0));
}
+/* If ESCAPE is true, then translate each NEWLINE byte to the string, "\\n",
+ and each backslash to "\\\\". */
static void
-print_filename (char const *file)
+print_filename (char const *file, bool escape)
{
- /* Translate each NEWLINE byte to the string, "\\n",
- and each backslash to "\\\\". */
+ if (! escape)
+ {
+ fputs (file, stdout);
+ return;
+ }
+
while (*file)
{
switch (*file)
@@ -823,14 +829,16 @@ main (int argc, char **argv)
ok = false;
else
{
+ bool needs_escape = strchr (file, '\n');
+
if (prefix_tag)
{
- if (strchr (file, '\n') || strchr (file, '\\'))
+ if (needs_escape)
putchar ('\\');
fputs (DIGEST_TYPE_STRING, stdout);
fputs (" (", stdout);
- print_filename (file);
+ print_filename (file, needs_escape);
fputs (") = ", stdout);
}
@@ -838,7 +846,7 @@ main (int argc, char **argv)
/* Output a leading backslash if the file name contains
a newline or backslash. */
- if (!prefix_tag && (strchr (file, '\n') || strchr (file, '\\')))
+ if (!prefix_tag && needs_escape)
putchar ('\\');
for (i = 0; i < (digest_hex_bytes / 2); ++i)
@@ -850,7 +858,7 @@ main (int argc, char **argv)
putchar (file_is_binary ? '*' : ' ');
- print_filename (file);
+ print_filename (file, needs_escape);
}
putchar ('\n');
diff --git a/tests/misc/md5sum.pl b/tests/misc/md5sum.pl
index 5192d55..556f819 100755
--- a/tests/misc/md5sum.pl
+++ b/tests/misc/md5sum.pl
@@ -38,8 +38,10 @@ my @Tests =
{OUT=>"d174ab98d277d9f5a5611c2c9f419d9f
f\n"}],
['7', {IN=> {f=> '1234567890' x 8}},
{OUT=>"57edf4a22be3c955ac49da2e2107b67a
f\n"}],
- ['backslash', {IN=> {".\\foo"=> ''}},
- {OUT=>"\\$degenerate .\\\\foo\n"}],
+ ['backslash', {IN=> {".\nfoo"=> ''}},
+ {OUT=>"\\$degenerate .\\nfoo\n"}],
+ ['nbackslash', {IN=> {".\\foo"=> ''}},
+ {OUT=>"$degenerate .\\foo\n"}],
['check-1', '--check', {AUX=> {f=> ''}},
{IN=> {'f.md5' => "$degenerate f\n"}},
{OUT=>"f: OK\n"}],
diff --git a/tests/misc/sha1sum.pl b/tests/misc/sha1sum.pl
index 327c4dd..cda453f 100755
--- a/tests/misc/sha1sum.pl
+++ b/tests/misc/sha1sum.pl
@@ -44,8 +44,10 @@ my @Tests =
{OUT=>"50abf5706a150990a08b2c5ea40fa0e585554732
f\n"}],
['million-a', {IN=> {f=> 'a' x 1000000}},
{OUT=>"34aa973cd4c4daa4f61eeb2bdbad27316534016f
f\n"}],
- ['bs-sha', {IN=> {".\\foo"=> ''}},
- {OUT=>"\\$sha_degenerate .\\\\foo\n"}],
+ ['bs-sha', {IN=> {".\nfoo"=> ''}},
+ {OUT=>"\\$sha_degenerate .\\nfoo\n"}],
+ ['nbs-sha', {IN=> {".\\foo"=> ''}},
+ {OUT=>"$sha_degenerate .\\foo\n"}],
# The sha1sum and md5sum drivers share a lot of code.
# Ensure that sha1sum does *not* share the part that makes
# md5sum accept BSD format.
--
1.7.7.6
- [PATCH] md5sum, sha*sum: only escape file names containing newlines,
Pádraig Brady <=
Re: [PATCH] md5sum, sha*sum: only escape file names containing newlines, Eric Blake, 2013/11/01