简体   繁体   English

C字符串分配向后

[英]C String assignment backwards

I have some decompiled C source code and saw this strange string assignment. 我有一些反编译的C源代码,看到了这个奇怪的字符串赋值。

char v1[21];
v1[0] = 0x75767778;
"tsrqponmlkjihgfedcba" = v1;

What is going on? 到底是怎么回事? How is this possible? 这怎么可能?

char v1[21];

No problem so far; 到目前为止没问题; that defines v1 as an array of 21 char elements. v1定义为包含21个char元素的数组。

v1[0] = 0x75767778;

Legal, but incorrect. 合法但不正确。 v1[0] is a char object. v1[0]是一个char对象。 Unless CHAR_BIT >= 32 (which is unlikely), it cannot hold the value 0x75767778 . 除非CHAR_BIT >= 32 (这不太可能),否则它不能保持值0x75767778 If plain char is unsigned and CHAR_BIT==8 , it would assign the value 0x78 ; 如果plain char是无符号的并且CHAR_BIT==8 ,则它将赋值0x78 ; if plain char is signed, it will probably do the same, but the value is implementation-defined. 如果普通char被签名,它可能会执行相同的操作,但该值是实现定义的。

It might be relevant that the bytes of 0x75767778 are the ASCII codes for the characters 'u' , 'v' , 'w' , and 'x' . 可能相关的是0x75767778的字节是字符'u''v''w''x'的ASCII码。

"tsrqponmlkjihgfedcba" = v1;

This is simply illegal. 这完全是非法的。 A string literal cannot be the left hand side of an assignment and C does not support array assignment. 字符串文字不能是赋值的左侧,C不支持数组赋值。

Apparently your "decompiler" (which you haven't identified) is generating something that more or less looks like C, but isn't. 显然你的“反编译器”(你还没有发现)正在生成或多或少看起来像C的东西,但却没有。

This might be partly explained if the left and right sides of the assignments are being reversed. 如果分配的左侧和右侧被反转,则可以部分解释这一点。 Even if that's the case, the output appears to be some sort of pseudo-code. 即使是这种情况,输出似乎也是某种伪代码。 (Did the code actually use = rather than, say, -> or => ?) (代码实际上是使用=而不是,比方说, ->=> ?)

Seeing the source code from which this was "decompiled" would be helpful -- as would knowing what "decompiler" you're using and seeing its documentation. 看到这个被“反编译”的源代码会有所帮助 - 就像知道你正在使用什么“反编译器”并查看其文档一样。

My guess is that the source code had a string literal like "xwvutsrqponmlkjihgfedcba" , and that generated code used an integer assignment to copy "xwvu" (represented as 0x75767778 ) and a separate operation for the remaining 20 characters, but that's only a guess. 我的猜测是源代码有一个字符串文字,如"xwvutsrqponmlkjihgfedcba" ,并且生成的代码使用整数赋值来复制"xwvu" (表示为0x75767778 )和另外20个字符的单独操作,但这只是猜测。 Or perhaps it was "abcdefghijklmnopqrstuvwx" and it was reversed due to byte ordering. 或者也许它是"abcdefghijklmnopqrstuvwx"并且由于字节排序而被颠倒了。

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

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