#define BASE32_ONIONLEN 16
#define BASE32_ALPHABET "abcdefghijklmnopqrstuvwxyz234567"
void base32_onion(char *dst, unsigned char *src) { // base32-encode hash
uint8_t byte = 0, // dst location
offset = 0; // bit offset
for(; byte < BASE32_ONIONLEN; offset += 5) {
if(offset > 7) {
offset -= 8;
src++;
}
dst[byte++] = BASE32_ALPHABET[(htobe16(*(uint16_t*)src) >> (11-offset))
& (uint16_t)0x001F];
}
dst[byte] = '\0';
}
我无法理解以dst[byte++]
开头的部分。我是一个python程序员,我不确定所有的类型转换是如何工作的。 src
指向一个字节对吗?并且(uint16_t*)
将其转换为指向2字节值的指针?这会使src
处的字节成为两个字节值的开头,还是结束?什么是>> (11-offset)
的东西?
答案 0 :(得分:0)
uint16_t t1, t2, t3;
uint8_t t4, t5, t6;
t1 = *(uint16_t*)src; // Whatever src is pointing to, treat the next
// 16 bits as an unsigned number
// Safer would be memcpy(&t1, src, sizeof(t1));
t2 = htobe(t1); // Guessing this changes the value to have big-endian
// byte order
t3 = t2 >> (11-offset); // Shift t2 to the right by (11-offset) bits
t4 = t3 & (uint16_t)0x001F; // t4 contains the lower 5 bits of t3
t5 = BASE32_ALPHABET[t4]; // t5 gets the t4-th byte of the base32 alphabet
t6 = byte++; // t6 gets the value of byte, byte's value is then
// incremented, so t6 + 1 == byte
dest[t6] = t5; // t5 is assigned to dest[t6]
向右移位n位的值相当于将值除以2 n 。