题目给出encode和部分decode代码, 让补全decode代码。
#include <stdio.h> typedef unsigned int uint32_t; typedef unsigned char uint8_t; int encode(const void* raw_in, void* raw_out, uint32_t password, size_t len) { const uint8_t* in = (const uint8_t*)raw_in; uint8_t* out = (uint8_t*)raw_out; uint32_t seed = password ^ 0xd11b5438u; for (size_t i = 0 ; i < len; ++i) { uint8_t a = ( in[i] ^ seed ) >> 1; // in[i]的高7位异或后放入a的低7位 uint8_t b = ( ( ((uint32_t)in[i]) << 16 ) ^ seed ) >> (16-7); // in[i]的低1位异或后放入b的高1位 a &= 127; // 清零无效位 b &= 128; // 清零 无效位 a = 127 & ( a ^ (b << 3)); // 该句没有用 out[i] = a | b; seed = (seed * 608347 ^ seed ^ out[i]); } return 0; } int decode(const void* raw_in, void* raw_out, uint32_t password, size_t len) { const uint8_t* in = (const uint8_t*)raw_in; uint8_t* out = (uint8_t*)raw_out; uint32_t seed = password ^ 0xd11b5438u; for (size_t i = 0 ; i < len; ++i) { // 请在此处补全代码 uint8_t a = in[i] & 127;//取出in[i]的低7位放入a中 uint8_t b = in[i] & 128;//取出in[i]的高1位放入b中 uint8_t c = ((a<<1)^seed)&254;//运用c=a^b;a=c^b;的原理,将原来的数据还原,并清空无效的数据位 uint8_t d = (((((uint32_t)b)<<9)^seed)>>16)&1;//运用c=a^b;a=c^b;的原理,将原来的数据还原,并清空无效的数据位 out[i] = c | d;//将还原后的高位和低位相或 seed = (seed * 608347 ^ seed ^ in[i]); } return 0; } int main() { const uint8_t buf1[] = {0x7f, 0xa6, 0xa6, 0xac, 0x30, 0x7f, 0xfa, 0x3c, 0x4c, 0xc5, 0xf6, 0xf2, 0xee, 0x1e, 0xd3, 0x85, 0x07, 0x5b, 0x66, 0xdb, 0xf0, 0x90, 0x4f, 0x80, 0x63, 0x45, 0xf7, 0x2a, 0x9b, 0x41, 0x88, 0xb3, 0xe1, 0x96, 0x93, 0x9f, 0x5f, 0x56, 0xb3, 0x3d, }; uint8_t buf2[100] = {0}; const uint32_t password = 0xb6127c0du; const size_t len = sizeof(buf1); decode(buf1, buf2, password, len); printf("%s\n", buf2); return 0; }
encode过程:运行结果: