Files
SimpleRemoter/common/sha256_posix.h
T
yuanyuanxianganddeepseek-flash 3a158a241c Feature: Support upload_file and download_file on Linux and macOS hosts
Both tools used to reject LNX/MAC targets outright. The server now gates
on the target client type instead: a POSIX target must pass an absolute
POSIX remote path without a backslash or a ".." segment (-32602
otherwise), while Windows targets keep their exact previous behaviour.

upload_file needed no new client command; the POSIX receiver already
handles COMMAND_SEND_FILE_V2 and derives the destination from the packet
name. It now also reports a real receiver_ack: the upload session
records dstClientID/transferID, and OnC2CRecvResult matches the 176
acknowledgement from Linux targets, so a destination write failure
becomes -32011 and a missing ack (with bytes in flight) becomes -32001.

download_file needed two pieces on the POSIX side, both shared by Linux
and macOS through common/FileTransferV2.h: a FileCompletePacketV2 per
file with a streaming SHA-256 (new self-contained common/sha256_posix.h,
since BCrypt is Windows-only) so the server can count completed files
and verify digests, and a TOKEN_DOWNLOAD_ERROR reply when the requested
paths do not exist or are denied, which the server maps to
-32009/-32010 within milliseconds instead of waiting for the stall
watchdog.

Zero-byte files were lost when uploading to POSIX targets because the
worker sends no data chunk for them and the complete packet carries no
path, so the receiver had nothing to create. The server now injects a
zero-length final chunk for each empty file, only for POSIX targets;
Windows targets keep their existing behaviour.

Also fixes a download completion edge case found while testing: the
client decides entry order, so a directory may arrive after all files
(an empty subdirectory, for example), and the directory branch only
completed when the directory count alone covered totalFiles. The check
is now filesDone + directoryCount >= totalFiles.

Verified end to end against a WSL Linux client and a Windows client:
upload and download of a tree with a 3 MB binary, a Chinese filename, an
empty file and an empty subdirectory all round-trip with matching
SHA-256; the documented error paths (-32602, -32009, -32011, -32001) and
the Windows upload/download/C2C regressions were exercised as well.

Co-Authored-By: deepseek-flash
2026-10-27 20:39:22 +01:00

149 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* sha256_posix.h - 最小实现 SHA-256(POSIX 侧)
*
* 为什么不用现成的:Windows 侧的 SHA-256 走 BCrypt(common/VerifyV2.h),
* Linux/macOS 没有 BCrypt,也没有 OpenSSL 依赖,而 V2 文件发送要向服务端
* 回报 FileCompletePacketV2.sha256,必须自己算。
*
* 只实现 FileTransferV2 需要的流式接口(update/final),不做别的事。
*/
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#error "sha256_posix.h is POSIX-only; Windows uses BCrypt via VerifyV2.h."
#endif
#include <cstddef>
#include <cstdint>
#include <cstring>
class Sha256
{
public:
Sha256() { reset(); }
void reset()
{
static const uint32_t init[8] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
memcpy(m_h, init, sizeof(m_h));
m_bufLen = 0;
m_totalLen = 0;
}
void update(const void* data, size_t len)
{
const uint8_t* p = (const uint8_t*)data;
m_totalLen += len;
if (m_bufLen > 0) {
size_t take = 64 - m_bufLen;
if (take > len) take = len;
memcpy(m_buf + m_bufLen, p, take);
m_bufLen += take;
p += take;
len -= take;
if (m_bufLen == 64) {
transform(m_buf);
m_bufLen = 0;
}
}
while (len >= 64) {
transform(p);
p += 64;
len -= 64;
}
if (len > 0) {
memcpy(m_buf, p, len);
m_bufLen = len;
}
}
// 输出 32 字节摘要;调用后对象可 reset() 复用(不自动 reset,避免隐式行为)
void final(uint8_t out[32])
{
uint64_t bitLen = m_totalLen * 8;
// 0x80 + 零填充 + 64 位长度,凑满整块
uint8_t pad[72];
memset(pad, 0, sizeof(pad));
pad[0] = 0x80;
size_t padLen = (m_bufLen < 56) ? (56 - m_bufLen) : (120 - m_bufLen);
update(pad, padLen);
uint8_t lenBytes[8];
for (int i = 0; i < 8; ++i) {
lenBytes[i] = (uint8_t)(bitLen >> (56 - 8 * i));
}
update(lenBytes, sizeof(lenBytes));
for (int i = 0; i < 8; ++i) {
out[4 * i] = (uint8_t)(m_h[i] >> 24);
out[4 * i + 1] = (uint8_t)(m_h[i] >> 16);
out[4 * i + 2] = (uint8_t)(m_h[i] >> 8);
out[4 * i + 3] = (uint8_t)(m_h[i]);
}
}
static void hash(const void* data, size_t len, uint8_t out[32])
{
Sha256 s;
s.update(data, len);
s.final(out);
}
private:
static uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
void transform(const uint8_t block[64])
{
static const uint32_t K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
uint32_t w[64];
for (int i = 0; i < 16; ++i) {
w[i] = ((uint32_t)block[4 * i] << 24) | ((uint32_t)block[4 * i + 1] << 16) |
((uint32_t)block[4 * i + 2] << 8) | (uint32_t)block[4 * i + 3];
}
for (int i = 16; i < 64; ++i) {
uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint32_t a = m_h[0], b = m_h[1], c = m_h[2], d = m_h[3];
uint32_t e = m_h[4], f = m_h[5], g = m_h[6], h = m_h[7];
for (int i = 0; i < 64; ++i) {
uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
uint32_t ch = (e & f) ^ (~e & g);
uint32_t t1 = h + S1 + ch + K[i] + w[i];
uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
uint32_t t2 = S0 + maj;
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
m_h[0] += a; m_h[1] += b; m_h[2] += c; m_h[3] += d;
m_h[4] += e; m_h[5] += f; m_h[6] += g; m_h[7] += h;
}
uint32_t m_h[8];
uint8_t m_buf[64];
size_t m_bufLen;
uint64_t m_totalLen;
};