简体   繁体   English

为什么xxd添加字符?

[英]Why does xxd add chars?

I am trying to reverse an od command from a system where I have no hexdump or base64 tools. 我正在尝试从没有hexdumpbase64工具的系统中撤消od命令。

I do this like that (of course, in reality, the encoding takes place at the "small" system, the decoding is done on my workstation, but to test it, I try the whole way in one line first): 我这样做是这样的(当然,实际上,编码是在“小型”系统上进行的,解码是在我的工作站上完成的,但是要进行测试,我首先尝试在一行中进行整体测试):

echo TEST | od -tx1 | xxd -r

Of course, echo TEST is just a placeholder here for eg. 当然,例如, echo TEST只是一个占位符。 cat test.bmp or anything else. cat test.bmp或其他任何东西。

> echo TEST
TEST

> echo TEST | od -tx1
0000000 54 45 53 54 0a
0000005 

> echo TEST | od -tx1 | xxd -r
TEST

That looks right, but it is different, as we can see here if we give it to od again: 看起来不错,但这是不同的,因为如果再次将其赋予od ,我们可以在这里看到:

> echo TEST | od -tx1 | xxd -r | od -tx1
0000000 54 45 53 54 0a 00 00 00
0000010 

Why does xxd -r add those 00 s? 为什么xxd -r将那00秒相加?

You're getting those three nul bytes because of xxd -r trying and failing to parse input that's in a different format than what it expects. 由于xxd -r尝试并且无法解析格式与期望格式不同的输入,因此得到了这三个nul字节。 od -tx1 adds an extra line with an offset but no data bytes. od -tx1添加了一个带偏移但没有数据字节的额外行。 Plus the offsets in xxd have a colon after them, and are printed with a different width, and printable bytes are displayed as well as the hex dump, and possibly in a different base... Something about that doesn't play well with xxd , and it adds the extra bytes as a result. 加上xxd的偏移量在它们之后有一个冒号,并以不同的宽度打印,并且显示了可打印的字节以及十六进制转储,并且可能在不同的基数中…… xxd不能很好地解决这一问题,并因此增加了多余的字节。

Examples: 例子:

$ echo TEST | xxd
00000000: 5445 5354 0a                             TEST.
$ echo TEST | xxd | xxd -r
TEST
$ echo TEST | xxd | xxd -r | xxd    
00000000: 5445 5354 0a                             TEST.
$ echo TEST | xxd | xxd -r | od -tx1
0000000 54 45 53 54 0a
0000005
$ echo TEST | od -tx1 | head -1 | xxd -r | od -tx1 
0000000 54 45 53 54 0a
0000005

See how they're not present when giving xxd -r its expected xxd style input? 看看给xxd -r预期的xxd样式输入时它们不存在吗? And how they're not there when you prune that extra line from od 's output? 当您从od的输出中od多余的行时,它们又如何不存在? Don't mix and match incompatible data formats. 不要混合和匹配不兼容的数据格式。

It seems to work if I remove the offsets at all: 如果我完全删除偏移量,它似乎可以工作:

> echo TEST | od -tx1 -An
 54 45 53 54 0a
> echo TEST | od -tx1 -An | xxd -r -p
TEST
> echo TEST | od -tx1 -An | xxd -r -p | od -tx1 -An
 54 45 53 54 0a

Bingo! 答对了! Note the extra " " in front of the bytes. 请注意字节前面的多余“”。 It seems to have no effect. 似乎没有作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM