aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-08-26 20:14:31 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-08-26 20:14:31 +0200
commitd3d7f085ebf2898b62d4bb75566122c65be96454 (patch)
treeed989658fe8d3f16eb069738eac9c400a5147283
parentb6355e2bb5c931413735b7df2964e20e050bbc07 (diff)
downloadbusybox-w32-d3d7f085ebf2898b62d4bb75566122c65be96454.tar.gz
busybox-w32-d3d7f085ebf2898b62d4bb75566122c65be96454.tar.bz2
busybox-w32-d3d7f085ebf2898b62d4bb75566122c65be96454.zip
hexdump: fix numerous bugs in handling of backslashes
Was: t=48\\ t=45\\ t=4c\\ t=4c\\ t=4f\\ t=0a\\ Now: =48=\n =45=\n =4c=\n =4c=\n =4f=\n =0a=\n Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/dump.c51
1 files changed, 31 insertions, 20 deletions
diff --git a/libbb/dump.c b/libbb/dump.c
index 566881a78..154be5d80 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -157,7 +157,7 @@ static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
157 /* 157 /*
158 * figure out the byte count for each conversion; 158 * figure out the byte count for each conversion;
159 * rewrite the format as necessary, set up blank- 159 * rewrite the format as necessary, set up blank-
160 * pbb_dump_adding for end of data. 160 * padding for end of data.
161 */ 161 */
162 if (*p1 == 'c') { 162 if (*p1 == 'c') {
163 pr->flags = F_CHAR; 163 pr->flags = F_CHAR;
@@ -466,14 +466,14 @@ static void bpad(PR *pr)
466} 466}
467 467
468static const char conv_str[] ALIGN1 = 468static const char conv_str[] ALIGN1 =
469 "\0\\0\0" 469 "\0" "\\""0""\0"
470 "\007\\a\0" /* \a */ 470 "\007""\\""a""\0" /* \a */
471 "\b\\b\0" 471 "\b" "\\""b""\0"
472 "\f\\b\0" 472 "\f" "\\""f""\0"
473 "\n\\n\0" 473 "\n" "\\""n""\0"
474 "\r\\r\0" 474 "\r" "\\""r""\0"
475 "\t\\t\0" 475 "\t" "\\""t""\0"
476 "\v\\v\0" 476 "\v" "\\""v""\0"
477 ; 477 ;
478 478
479 479
@@ -485,7 +485,7 @@ static void conv_c(PR *pr, unsigned char *p)
485 do { 485 do {
486 if (*p == *str) { 486 if (*p == *str) {
487 ++str; 487 ++str;
488 goto strpr; 488 goto strpr; /* map e.g. '\n' to "\\n" */
489 } 489 }
490 str += 4; 490 str += 4;
491 } while (*str); 491 } while (*str);
@@ -702,8 +702,6 @@ int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
702void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt) 702void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
703{ 703{
704 const char *p; 704 const char *p;
705 char *p1;
706 char *p2;
707 FS *tfs; 705 FS *tfs;
708 FU *tfu, **nextfupp; 706 FU *tfu, **nextfupp;
709 const char *savep; 707 const char *savep;
@@ -779,29 +777,42 @@ void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
779 } 777 }
780 } 778 }
781 tfu->fmt = xstrndup(savep, p - savep); 779 tfu->fmt = xstrndup(savep, p - savep);
782/* escape(tfu->fmt); */
783
784 p1 = tfu->fmt;
785 780
786 /* alphabetic escape sequences have to be done in place */ 781 /* alphabetic escape sequences have to be done in place */
782 strcpy_and_process_escape_sequences(tfu->fmt, tfu->fmt);
783 /* unknown mappings are not changed: "\z" -> '\\' 'z' */
784 /* trailing backslash, if any, is preserved */
785#if 0
786 char *p1;
787 char *p2;
788 p1 = tfu->fmt;
787 for (p2 = p1;; ++p1, ++p2) { 789 for (p2 = p1;; ++p1, ++p2) {
788 if (*p1 == '\0') { 790 *p2 = *p1;
789 *p2 = *p1; 791 if (*p1 == '\0')
790 break; 792 break;
791 } 793
792 if (*p1 == '\\') { 794 if (*p1 == '\\') {
793 const char *cs = conv_str + 4; 795 const char *cs;
794 ++p1; 796
797 p1++;
795 *p2 = *p1; 798 *p2 = *p1;
799 if (*p1 == '\0') {
800 /* "...\" trailing backslash. Eaten. */
801 break;
802 }
803 cs = conv_str + 4; /* skip NUL element */
796 do { 804 do {
805 /* map e.g. "\n" -> '\n' */
797 if (*p1 == cs[2]) { 806 if (*p1 == cs[2]) {
798 *p2 = cs[0]; 807 *p2 = cs[0];
799 break; 808 break;
800 } 809 }
801 cs += 4; 810 cs += 4;
802 } while (*cs); 811 } while (*cs);
812 /* unknown mappings remove bkslash: "\z" -> 'z' */
803 } 813 }
804 } 814 }
815#endif
805 816
806 p++; 817 p++;
807 } 818 }