MD5_state MD5_from_string(XString msg) { MD5_state state; MD5_init(&state); MD5_block block; const uint64_t msg_full_len = msg.size(); while (msg.size() >= 64) { for (int i = 0; i < 0x10; i++) X[i] = msg[4 * i + 0] | msg[4 * i + 1] << 8 | msg[4 * i + 2] << 16 | msg[4 * i + 3] << 24; MD5_do_block(&state, block); msg = msg.xslice_t(64); } // now pad 1-512 bits + the 64-bit length - may be two blocks uint8_t buf[0x40] = {}; really_memcpy(buf, reinterpret_cast<const uint8_t *>(msg.data()), msg.size()); buf[msg.size()] = 0x80; // a single one bit if (64 - msg.size() > 8) { for (int i = 0; i < 8; i++) buf[0x38 + i] = (msg_full_len * 8) >> (i * 8); } for (int i = 0; i < 0x10; i++) X[i] = buf[4 * i + 0] | buf[4 * i + 1] << 8 | buf[4 * i + 2] << 16 | buf[4 * i + 3] << 24; MD5_do_block(&state, block); if (64 - msg.size() <= 8) { really_memset0(buf, 0x38); for (int i = 0; i < 8; i++) buf[0x38 + i] = (msg_full_len * 8) >> (i * 8); for (int i = 0; i < 0x10; i++) X[i] = buf[4 * i + 0] | buf[4 * i + 1] << 8 | buf[4 * i + 2] << 16 | buf[4 * i + 3] << 24; MD5_do_block(&state, block); }
void WBUF_STRUCT(uint8_t *p, size_t pos, T& structure) { really_memcpy(p + pos, pod_addressof_c(structure), sizeof(T)); }
inline void RFIFO_WFIFO_CLONE(int rfd, int wfd, size_t len) { really_memcpy(static_cast<uint8_t *>(WFIFOP(wfd, 0)), static_cast<const uint8_t *>(RFIFOP(rfd, 0)), len); }
inline void WFIFO_BUF_CLONE(int fd, const uint8_t *buf, size_t len) { really_memcpy(static_cast<uint8_t *>(WFIFOP(fd, 0)), buf, len); }
void WFIFO_STRUCT(int fd, size_t pos, T& structure) { really_memcpy(static_cast<uint8_t *>(WFIFOP(fd, pos)), pod_addressof_c(structure), sizeof(T)); }
void RBUF_STRUCT(const uint8_t *p, size_t pos, T& structure) { really_memcpy(pod_addressof_m(structure), p + pos, sizeof(T)); }
inline void RFIFO_BUF_CLONE(int fd, uint8_t *buf, size_t len) { really_memcpy(buf, static_cast<const uint8_t *>(RFIFOP(fd, 0)), len); }
void RFIFO_STRUCT(int fd, size_t pos, T& structure) { really_memcpy(pod_addressof_m(structure), static_cast<const uint8_t *>(RFIFOP(fd, pos)), sizeof(T)); }