1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

Adopt C99 <stdint.h> integer types.

The annoying int64.h is completely retired, since C99 guarantees a
64-bit integer type that you can actually treat like an ordinary
integer. Also, I've replaced the local typedefs uint32 and word32
(scattered through different parts of the crypto code) with the
standard uint32_t.
This commit is contained in:
Simon Tatham 2018-10-26 23:08:58 +01:00
parent 5cb56389bd
commit a647f2ba11
29 changed files with 432 additions and 721 deletions

4
Recipe
View File

@ -267,7 +267,7 @@ WINSSH = SSH winnoise wincapi winpgntc wingss winshare winnps winnpc
UXSSH = SSH uxnoise uxagentc uxgss uxshare UXSSH = SSH uxnoise uxagentc uxgss uxshare
# SFTP implementation (pscp, psftp). # SFTP implementation (pscp, psftp).
SFTP = sftp sftpcommon int64 logging cmdline SFTP = sftp sftpcommon logging cmdline
# Miscellaneous objects appearing in all the utilities, or all the # Miscellaneous objects appearing in all the utilities, or all the
# network ones, or the Unix or Windows subsets of those in turn. # network ones, or the Unix or Windows subsets of those in turn.
@ -283,7 +283,7 @@ UXMISC = MISCNET UXMISCCOMMON uxproxy
# SSH server. # SSH server.
SSHSERVER = SSHCOMMON sshserver settings be_none logging ssh2kex-server SSHSERVER = SSHCOMMON sshserver settings be_none logging ssh2kex-server
+ ssh2userauth-server sshrsag sshprime ssh2connection-server + ssh2userauth-server sshrsag sshprime ssh2connection-server
+ sesschan sftpcommon int64 sftpserver proxy cproxy ssh1login-server + sesschan sftpcommon sftpserver proxy cproxy ssh1login-server
+ ssh1connection-server scpserver + ssh1connection-server scpserver
# import.c and dependencies, for PuTTYgen-like utilities that have to # import.c and dependencies, for PuTTYgen-like utilities that have to

12
defs.h
View File

@ -12,6 +12,15 @@
#define PUTTY_DEFS_H #define PUTTY_DEFS_H
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#if defined _MSC_VER && _MSC_VER < 1800
/* Work around lack of inttypes.h in older MSVC */
#define PRIu64 "I64u"
#define SCNu64 "I64u"
#else
#include <inttypes.h>
#endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
@ -32,9 +41,6 @@ typedef struct strbuf strbuf;
struct RSAKey; struct RSAKey;
#include <stdint.h>
typedef uint32_t uint32;
typedef struct BinarySink BinarySink; typedef struct BinarySink BinarySink;
typedef struct BinarySource BinarySource; typedef struct BinarySource BinarySource;

175
int64.c
View File

@ -1,175 +0,0 @@
/*
* Handling of the int64 and uint64 types. Done in 32-bit integers,
* for (pre-C99) portability. Hopefully once C99 becomes widespread
* we can kiss this lot goodbye...
*/
#include <assert.h>
#include <string.h>
#include "int64.h"
uint64 uint64_div10(uint64 x, int *remainder)
{
uint64 y;
unsigned int rem, r2;
y.hi = x.hi / 10;
y.lo = x.lo / 10;
rem = x.lo % 10;
/*
* Now we have to add in the remainder left over from x.hi.
*/
r2 = x.hi % 10;
y.lo += r2 * 429496729;
rem += r2 * 6;
y.lo += rem / 10;
rem %= 10;
if (remainder)
*remainder = rem;
return y;
}
void uint64_decimal(uint64 x, char *buffer)
{
char buf[20];
int start = 20;
int d;
do {
x = uint64_div10(x, &d);
assert(start > 0);
buf[--start] = d + '0';
} while (x.hi || x.lo);
memcpy(buffer, buf + start, sizeof(buf) - start);
buffer[sizeof(buf) - start] = '\0';
}
uint64 uint64_make(unsigned long hi, unsigned long lo)
{
uint64 y;
y.hi = hi & 0xFFFFFFFFU;
y.lo = lo & 0xFFFFFFFFU;
return y;
}
uint64 uint64_add(uint64 x, uint64 y)
{
x.lo = (x.lo + y.lo) & 0xFFFFFFFFU;
x.hi += y.hi + (x.lo < y.lo ? 1 : 0);
return x;
}
uint64 uint64_add32(uint64 x, unsigned long y)
{
uint64 yy;
yy.hi = 0;
yy.lo = y;
return uint64_add(x, yy);
}
int uint64_compare(uint64 x, uint64 y)
{
if (x.hi != y.hi)
return x.hi < y.hi ? -1 : +1;
if (x.lo != y.lo)
return x.lo < y.lo ? -1 : +1;
return 0;
}
uint64 uint64_subtract(uint64 x, uint64 y)
{
x.lo = (x.lo - y.lo) & 0xFFFFFFFFU;
x.hi = (x.hi - y.hi - (x.lo > (y.lo ^ 0xFFFFFFFFU) ? 1 : 0)) & 0xFFFFFFFFU;
return x;
}
double uint64_to_double(uint64 x)
{
return (4294967296.0 * x.hi) + (double)x.lo;
}
uint64 uint64_shift_right(uint64 x, int shift)
{
if (shift < 32) {
x.lo >>= shift;
x.lo |= (x.hi << (32-shift)) & 0xFFFFFFFFU;
x.hi >>= shift;
} else {
x.lo = x.hi >> (shift-32);
x.hi = 0;
}
return x;
}
uint64 uint64_shift_left(uint64 x, int shift)
{
if (shift < 32) {
x.hi = (x.hi << shift) & 0xFFFFFFFFU;
x.hi |= (x.lo >> (32-shift));
x.lo = (x.lo << shift) & 0xFFFFFFFFU;
} else {
x.hi = (x.lo << (shift-32)) & 0xFFFFFFFFU;
x.lo = 0;
}
return x;
}
uint64 uint64_from_decimal(const char *str)
{
uint64 ret;
ret.hi = ret.lo = 0;
while (*str >= '0' && *str <= '9') {
ret = uint64_add(uint64_shift_left(ret, 3),
uint64_shift_left(ret, 1));
ret = uint64_add32(ret, *str - '0');
str++;
}
return ret;
}
#ifdef TESTMODE
#include <stdio.h>
int main(void)
{
uint64 x, y, z;
char buf[80];
x = uint64_make(0x3456789AUL, 0xDEF01234UL);
printf("%08lx.%08lx\n", x.hi, x.lo);
uint64_decimal(x, buf);
printf("%s\n", buf);
y = uint64_add32(x, 0xFFFFFFFFU);
printf("%08lx.%08lx\n", y.hi, y.lo);
uint64_decimal(y, buf);
printf("%s\n", buf);
z = uint64_subtract(y, x);
printf("%08lx.%08lx\n", z.hi, z.lo);
uint64_decimal(z, buf);
printf("%s\n", buf);
z = uint64_subtract(x, y);
printf("%08lx.%08lx\n", z.hi, z.lo);
uint64_decimal(z, buf);
printf("%s\n", buf);
y = uint64_shift_right(x, 4);
printf("%08lx.%08lx\n", y.hi, y.lo);
y = uint64_shift_right(x, 36);
printf("%08lx.%08lx\n", y.hi, y.lo);
y = uint64_shift_left(x, 4);
printf("%08lx.%08lx\n", x.hi, x.lo);
y = uint64_shift_left(x, 36);
printf("%08lx.%08lx\n", x.hi, x.lo);
return 0;
}
#endif

29
int64.h
View File

@ -1,29 +0,0 @@
/*
* Header for int64.c.
*/
#ifndef PUTTY_INT64_H
#define PUTTY_INT64_H
#include "defs.h"
typedef struct {
unsigned long hi, lo;
} uint64;
uint64 uint64_div10(uint64 x, int *remainder);
void uint64_decimal(uint64 x, char *buffer);
uint64 uint64_make(unsigned long hi, unsigned long lo);
uint64 uint64_add(uint64 x, uint64 y);
uint64 uint64_add32(uint64 x, unsigned long y);
int uint64_compare(uint64 x, uint64 y);
uint64 uint64_subtract(uint64 x, uint64 y);
double uint64_to_double(uint64 x);
uint64 uint64_shift_right(uint64 x, int shift);
uint64 uint64_shift_left(uint64 x, int shift);
uint64 uint64_from_decimal(const char *str);
void BinarySink_put_uint64(BinarySink *, uint64);
uint64 BinarySource_get_uint64(BinarySource *);
#endif

View File

@ -4,7 +4,6 @@
#include "marshal.h" #include "marshal.h"
#include "misc.h" #include "misc.h"
#include "int64.h"
void BinarySink_put_data(BinarySink *bs, const void *data, size_t len) void BinarySink_put_data(BinarySink *bs, const void *data, size_t len)
{ {
@ -47,10 +46,11 @@ void BinarySink_put_uint32(BinarySink *bs, unsigned long val)
bs->write(bs, data, sizeof(data)); bs->write(bs, data, sizeof(data));
} }
void BinarySink_put_uint64(BinarySink *bs, uint64 val) void BinarySink_put_uint64(BinarySink *bs, uint64_t val)
{ {
BinarySink_put_uint32(bs, val.hi); unsigned char data[8];
BinarySink_put_uint32(bs, val.lo); PUT_64BIT_MSB_FIRST(data, val);
bs->write(bs, data, sizeof(data));
} }
void BinarySink_put_string(BinarySink *bs, const void *data, size_t len) void BinarySink_put_string(BinarySink *bs, const void *data, size_t len)
@ -167,20 +167,15 @@ unsigned long BinarySource_get_uint32(BinarySource *src)
return GET_32BIT_MSB_FIRST(ucp); return GET_32BIT_MSB_FIRST(ucp);
} }
uint64 BinarySource_get_uint64(BinarySource *src) uint64_t BinarySource_get_uint64(BinarySource *src)
{ {
const unsigned char *ucp; const unsigned char *ucp;
uint64 toret;
if (!avail(8)) { if (!avail(8))
toret.hi = toret.lo = 0; return 0;
return toret;
}
ucp = consume(8); ucp = consume(8);
toret.hi = GET_32BIT_MSB_FIRST(ucp); return GET_64BIT_MSB_FIRST(ucp);
toret.lo = GET_32BIT_MSB_FIRST(ucp + 4);
return toret;
} }
ptrlen BinarySource_get_string(BinarySource *src) ptrlen BinarySource_get_string(BinarySource *src)

View File

@ -76,8 +76,7 @@ struct BinarySink {
* first parameter of any of these put_* macros. * first parameter of any of these put_* macros.
*/ */
/* Basic big-endian integer types. uint64 is the structure type /* Basic big-endian integer types. */
* defined in int64.h, not the C99 built-in type. */
#define put_byte(bs, val) \ #define put_byte(bs, val) \
BinarySink_put_byte(BinarySink_UPCAST(bs), val) BinarySink_put_byte(BinarySink_UPCAST(bs), val)
#define put_uint16(bs, val) \ #define put_uint16(bs, val) \
@ -146,6 +145,7 @@ void BinarySink_put_byte(BinarySink *, unsigned char);
void BinarySink_put_bool(BinarySink *, int); void BinarySink_put_bool(BinarySink *, int);
void BinarySink_put_uint16(BinarySink *, unsigned long); void BinarySink_put_uint16(BinarySink *, unsigned long);
void BinarySink_put_uint32(BinarySink *, unsigned long); void BinarySink_put_uint32(BinarySink *, unsigned long);
void BinarySink_put_uint64(BinarySink *, uint64_t);
void BinarySink_put_string(BinarySink *, const void *data, size_t len); void BinarySink_put_string(BinarySink *, const void *data, size_t len);
void BinarySink_put_stringpl(BinarySink *, ptrlen); void BinarySink_put_stringpl(BinarySink *, ptrlen);
void BinarySink_put_stringz(BinarySink *, const char *str); void BinarySink_put_stringz(BinarySink *, const char *str);
@ -277,6 +277,7 @@ unsigned char BinarySource_get_byte(BinarySource *);
int BinarySource_get_bool(BinarySource *); int BinarySource_get_bool(BinarySource *);
unsigned BinarySource_get_uint16(BinarySource *); unsigned BinarySource_get_uint16(BinarySource *);
unsigned long BinarySource_get_uint32(BinarySource *); unsigned long BinarySource_get_uint32(BinarySource *);
uint64_t BinarySource_get_uint64(BinarySource *);
ptrlen BinarySource_get_string(BinarySource *); ptrlen BinarySource_get_string(BinarySource *);
const char *BinarySource_get_asciz(BinarySource *); const char *BinarySource_get_asciz(BinarySource *);
ptrlen BinarySource_get_pstring(BinarySource *); ptrlen BinarySource_get_pstring(BinarySource *);

56
misc.h
View File

@ -166,11 +166,31 @@ void debug_memdump(const void *buf, int len, int L);
#define max(x,y) ( (x) > (y) ? (x) : (y) ) #define max(x,y) ( (x) > (y) ? (x) : (y) )
#endif #endif
#define GET_64BIT_LSB_FIRST(cp) \
(((uint64_t)(unsigned char)(cp)[0]) | \
((uint64_t)(unsigned char)(cp)[1] << 8) | \
((uint64_t)(unsigned char)(cp)[2] << 16) | \
((uint64_t)(unsigned char)(cp)[3] << 24) | \
((uint64_t)(unsigned char)(cp)[4] << 32) | \
((uint64_t)(unsigned char)(cp)[5] << 40) | \
((uint64_t)(unsigned char)(cp)[6] << 48) | \
((uint64_t)(unsigned char)(cp)[7] << 56))
#define PUT_64BIT_LSB_FIRST(cp, value) ( \
(cp)[0] = (unsigned char)(value), \
(cp)[1] = (unsigned char)((value) >> 8), \
(cp)[2] = (unsigned char)((value) >> 16), \
(cp)[3] = (unsigned char)((value) >> 24), \
(cp)[4] = (unsigned char)((value) >> 32), \
(cp)[5] = (unsigned char)((value) >> 40), \
(cp)[6] = (unsigned char)((value) >> 48), \
(cp)[7] = (unsigned char)((value) >> 56) )
#define GET_32BIT_LSB_FIRST(cp) \ #define GET_32BIT_LSB_FIRST(cp) \
(((unsigned long)(unsigned char)(cp)[0]) | \ (((uint32_t)(unsigned char)(cp)[0]) | \
((unsigned long)(unsigned char)(cp)[1] << 8) | \ ((uint32_t)(unsigned char)(cp)[1] << 8) | \
((unsigned long)(unsigned char)(cp)[2] << 16) | \ ((uint32_t)(unsigned char)(cp)[2] << 16) | \
((unsigned long)(unsigned char)(cp)[3] << 24)) ((uint32_t)(unsigned char)(cp)[3] << 24))
#define PUT_32BIT_LSB_FIRST(cp, value) ( \ #define PUT_32BIT_LSB_FIRST(cp, value) ( \
(cp)[0] = (unsigned char)(value), \ (cp)[0] = (unsigned char)(value), \
@ -187,10 +207,10 @@ void debug_memdump(const void *buf, int len, int L);
(cp)[1] = (unsigned char)((value) >> 8) ) (cp)[1] = (unsigned char)((value) >> 8) )
#define GET_32BIT_MSB_FIRST(cp) \ #define GET_32BIT_MSB_FIRST(cp) \
(((unsigned long)(unsigned char)(cp)[0] << 24) | \ (((uint32_t)(unsigned char)(cp)[0] << 24) | \
((unsigned long)(unsigned char)(cp)[1] << 16) | \ ((uint32_t)(unsigned char)(cp)[1] << 16) | \
((unsigned long)(unsigned char)(cp)[2] << 8) | \ ((uint32_t)(unsigned char)(cp)[2] << 8) | \
((unsigned long)(unsigned char)(cp)[3])) ((uint32_t)(unsigned char)(cp)[3]))
#define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp) #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
@ -202,6 +222,26 @@ void debug_memdump(const void *buf, int len, int L);
#define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value) #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
#define GET_64BIT_MSB_FIRST(cp) \
(((uint64_t)(unsigned char)(cp)[0] << 56) | \
((uint64_t)(unsigned char)(cp)[1] << 48) | \
((uint64_t)(unsigned char)(cp)[2] << 40) | \
((uint64_t)(unsigned char)(cp)[3] << 32) | \
((uint64_t)(unsigned char)(cp)[4] << 24) | \
((uint64_t)(unsigned char)(cp)[5] << 16) | \
((uint64_t)(unsigned char)(cp)[6] << 8) | \
((uint64_t)(unsigned char)(cp)[7]))
#define PUT_64BIT_MSB_FIRST(cp, value) ( \
(cp)[0] = (unsigned char)((value) >> 56), \
(cp)[1] = (unsigned char)((value) >> 48), \
(cp)[2] = (unsigned char)((value) >> 40), \
(cp)[3] = (unsigned char)((value) >> 32), \
(cp)[4] = (unsigned char)((value) >> 24), \
(cp)[5] = (unsigned char)((value) >> 16), \
(cp)[6] = (unsigned char)((value) >> 8), \
(cp)[7] = (unsigned char)(value) )
#define GET_16BIT_MSB_FIRST(cp) \ #define GET_16BIT_MSB_FIRST(cp) \
(((unsigned long)(unsigned char)(cp)[0] << 8) | \ (((unsigned long)(unsigned char)(cp)[0] << 8) | \
((unsigned long)(unsigned char)(cp)[1])) ((unsigned long)(unsigned char)(cp)[1]))

View File

@ -24,7 +24,7 @@ typedef gss_OID const_gss_OID; /* for our prototypes below */
******************************************************************************/ ******************************************************************************/
/* GSSAPI Type Definitions */ /* GSSAPI Type Definitions */
typedef uint32 OM_uint32; typedef uint32_t OM_uint32;
typedef struct gss_OID_desc_struct { typedef struct gss_OID_desc_struct {
OM_uint32 length; OM_uint32 length;

115
pscp.c
View File

@ -25,7 +25,6 @@
#include "ssh.h" #include "ssh.h"
#include "sftp.h" #include "sftp.h"
#include "storage.h" #include "storage.h"
#include "int64.h"
static int list = 0; static int list = 0;
static int verbose = 0; static int verbose = 0;
@ -506,7 +505,7 @@ static void do_cmd(char *host, char *user, char *cmd)
/* /*
* Update statistic information about current file. * Update statistic information about current file.
*/ */
static void print_stats(const char *name, uint64 size, uint64 done, static void print_stats(const char *name, uint64_t size, uint64_t done,
time_t start, time_t now) time_t start, time_t now)
{ {
float ratebs; float ratebs;
@ -515,42 +514,34 @@ static void print_stats(const char *name, uint64 size, uint64 done,
int pct; int pct;
int len; int len;
int elap; int elap;
double donedbl;
double sizedbl;
elap = (unsigned long) difftime(now, start); elap = (unsigned long) difftime(now, start);
if (now > start) if (now > start)
ratebs = (float) (uint64_to_double(done) / elap); ratebs = (float)done / elap;
else else
ratebs = (float) uint64_to_double(done); ratebs = (float)done;
if (ratebs < 1.0) if (ratebs < 1.0)
eta = (unsigned long) (uint64_to_double(uint64_subtract(size, done))); eta = size - done;
else { else
eta = (unsigned long) eta = (unsigned long)((size - done) / ratebs);
((uint64_to_double(uint64_subtract(size, done)) / ratebs));
}
etastr = dupprintf("%02ld:%02ld:%02ld", etastr = dupprintf("%02ld:%02ld:%02ld",
eta / 3600, (eta % 3600) / 60, eta % 60); eta / 3600, (eta % 3600) / 60, eta % 60);
donedbl = uint64_to_double(done); pct = (int) (100.0 * done / size);
sizedbl = uint64_to_double(size);
pct = (int) (100 * (donedbl * 1.0 / sizedbl));
{ {
char donekb[40];
/* divide by 1024 to provide kB */ /* divide by 1024 to provide kB */
uint64_decimal(uint64_shift_right(done, 10), donekb); len = printf("\r%-25.25s | %"PRIu64" kB | %5.1f kB/s | "
len = printf("\r%-25.25s | %s kB | %5.1f kB/s | ETA: %8s | %3d%%", "ETA: %8s | %3d%%", name, done >> 10,
name, ratebs / 1024.0, etastr, pct);
donekb, ratebs / 1024.0, etastr, pct);
if (len < prev_stats_len) if (len < prev_stats_len)
printf("%*s", prev_stats_len - len, ""); printf("%*s", prev_stats_len - len, "");
prev_stats_len = len; prev_stats_len = len;
if (uint64_compare(done, size) == 0) if (done == size)
abandon_stats(); abandon_stats();
fflush(stdout); fflush(stdout);
@ -743,7 +734,7 @@ static unsigned long scp_sftp_mtime, scp_sftp_atime;
static int scp_has_times; static int scp_has_times;
static struct fxp_handle *scp_sftp_filehandle; static struct fxp_handle *scp_sftp_filehandle;
static struct fxp_xfer *scp_sftp_xfer; static struct fxp_xfer *scp_sftp_xfer;
static uint64 scp_sftp_fileoffset; static uint64_t scp_sftp_fileoffset;
int scp_source_setup(const char *target, int shouldbedir) int scp_source_setup(const char *target, int shouldbedir)
{ {
@ -811,7 +802,7 @@ int scp_send_filetimes(unsigned long mtime, unsigned long atime)
} }
} }
int scp_send_filename(const char *name, uint64 size, int permissions) int scp_send_filename(const char *name, uint64_t size, int permissions)
{ {
if (using_sftp) { if (using_sftp) {
char *fullname; char *fullname;
@ -841,18 +832,16 @@ int scp_send_filename(const char *name, uint64 size, int permissions)
errs++; errs++;
return 1; return 1;
} }
scp_sftp_fileoffset = uint64_make(0, 0); scp_sftp_fileoffset = 0;
scp_sftp_xfer = xfer_upload_init(scp_sftp_filehandle, scp_sftp_xfer = xfer_upload_init(scp_sftp_filehandle,
scp_sftp_fileoffset); scp_sftp_fileoffset);
sfree(fullname); sfree(fullname);
return 0; return 0;
} else { } else {
char *buf; char *buf;
char sizestr[40];
uint64_decimal(size, sizestr);
if (permissions < 0) if (permissions < 0)
permissions = 0644; permissions = 0644;
buf = dupprintf("C%04o %s ", (int)(permissions & 07777), sizestr); buf = dupprintf("C%04o %"PRIu64" ", (int)(permissions & 07777), size);
backend_send(backend, buf, strlen(buf)); backend_send(backend, buf, strlen(buf));
sfree(buf); sfree(buf);
backend_send(backend, name, strlen(name)); backend_send(backend, name, strlen(name));
@ -885,7 +874,7 @@ int scp_send_filedata(char *data, int len)
xfer_upload_data(scp_sftp_xfer, data, len); xfer_upload_data(scp_sftp_xfer, data, len);
scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len); scp_sftp_fileoffset += len;
return 0; return 0;
} else { } else {
int bufsize = backend_send(backend, data, len); int bufsize = backend_send(backend, data, len);
@ -1143,7 +1132,7 @@ struct scp_sink_action {
char *buf; /* will need freeing after use */ char *buf; /* will need freeing after use */
char *name; /* filename or dirname (not ENDDIR) */ char *name; /* filename or dirname (not ENDDIR) */
long permissions; /* access permissions (not ENDDIR) */ long permissions; /* access permissions (not ENDDIR) */
uint64 size; /* file size (not ENDDIR) */ uint64_t size; /* file size (not ENDDIR) */
int settime; /* 1 if atime and mtime are filled */ int settime; /* 1 if atime and mtime are filled */
unsigned long atime, mtime; /* access times for the file */ unsigned long atime, mtime; /* access times for the file */
}; };
@ -1369,7 +1358,7 @@ int scp_get_sink_action(struct scp_sink_action *act)
act->action = SCP_SINK_DIR; act->action = SCP_SINK_DIR;
act->buf = dupstr(stripslashes(fname, 0)); act->buf = dupstr(stripslashes(fname, 0));
act->name = act->buf; act->name = act->buf;
act->size = uint64_make(0,0); /* duhh, it's a directory */ act->size = 0; /* duhh, it's a directory */
act->permissions = 07777 & attrs.permissions; act->permissions = 07777 & attrs.permissions;
if (scp_sftp_preserve && if (scp_sftp_preserve &&
(attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
@ -1391,7 +1380,7 @@ int scp_get_sink_action(struct scp_sink_action *act)
if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) { if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
act->size = attrs.size; act->size = attrs.size;
} else } else
act->size = uint64_make(ULONG_MAX,ULONG_MAX); /* no idea */ act->size = UINT64_MAX; /* no idea */
act->permissions = 07777 & attrs.permissions; act->permissions = 07777 & attrs.permissions;
if (scp_sftp_preserve && if (scp_sftp_preserve &&
(attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
@ -1476,12 +1465,9 @@ int scp_get_sink_action(struct scp_sink_action *act)
* SCP_SINK_DIR. * SCP_SINK_DIR.
*/ */
{ {
char sizestr[40]; if (sscanf(act->buf, "%lo %"SCNu64" %n", &act->permissions,
&act->size, &i) != 2)
if (sscanf(act->buf, "%lo %39s %n", &act->permissions,
sizestr, &i) != 2)
bump("Protocol error: Illegal file descriptor format"); bump("Protocol error: Illegal file descriptor format");
act->size = uint64_from_decimal(sizestr);
act->name = act->buf + i; act->name = act->buf + i;
return 0; return 0;
} }
@ -1504,7 +1490,7 @@ int scp_accept_filexfer(void)
errs++; errs++;
return 1; return 1;
} }
scp_sftp_fileoffset = uint64_make(0, 0); scp_sftp_fileoffset = 0;
scp_sftp_xfer = xfer_download_init(scp_sftp_filehandle, scp_sftp_xfer = xfer_download_init(scp_sftp_filehandle,
scp_sftp_fileoffset); scp_sftp_fileoffset);
sfree(scp_sftp_currentname); sfree(scp_sftp_currentname);
@ -1552,7 +1538,7 @@ int scp_recv_filedata(char *data, int len)
} else } else
actuallen = 0; actuallen = 0;
scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, actuallen); scp_sftp_fileoffset += actuallen;
return actuallen; return actuallen;
} else { } else {
@ -1625,14 +1611,14 @@ static void run_err(const char *fmt, ...)
*/ */
static void source(const char *src) static void source(const char *src)
{ {
uint64 size; uint64_t size;
unsigned long mtime, atime; unsigned long mtime, atime;
long permissions; long permissions;
const char *last; const char *last;
RFile *f; RFile *f;
int attr; int attr;
uint64 i; uint64_t i;
uint64 stat_bytes; uint64_t stat_bytes;
time_t stat_starttime, stat_lasttime; time_t stat_starttime, stat_lasttime;
attr = file_type(src); attr = file_type(src);
@ -1688,28 +1674,24 @@ static void source(const char *src)
} }
if (verbose) { if (verbose) {
char sizestr[40]; tell_user(stderr, "Sending file %s, size=%"PRIu64, last, size);
uint64_decimal(size, sizestr);
tell_user(stderr, "Sending file %s, size=%s", last, sizestr);
} }
if (scp_send_filename(last, size, permissions)) { if (scp_send_filename(last, size, permissions)) {
close_rfile(f); close_rfile(f);
return; return;
} }
stat_bytes = uint64_make(0,0); stat_bytes = 0;
stat_starttime = time(NULL); stat_starttime = time(NULL);
stat_lasttime = 0; stat_lasttime = 0;
#define PSCP_SEND_BLOCK 4096 #define PSCP_SEND_BLOCK 4096
for (i = uint64_make(0,0); for (i = 0; i < size; i += PSCP_SEND_BLOCK) {
uint64_compare(i,size) < 0;
i = uint64_add32(i,PSCP_SEND_BLOCK)) {
char transbuf[PSCP_SEND_BLOCK]; char transbuf[PSCP_SEND_BLOCK];
int j, k = PSCP_SEND_BLOCK; int j, k = PSCP_SEND_BLOCK;
if (uint64_compare(uint64_add32(i, k),size) > 0) /* i + k > size */ if (i + k > size)
k = (uint64_subtract(size, i)).lo; /* k = size - i; */ k = size - i;
if ((j = read_from_file(f, transbuf, k)) != k) { if ((j = read_from_file(f, transbuf, k)) != k) {
bump("%s: Read error", src); bump("%s: Read error", src);
} }
@ -1717,9 +1699,8 @@ static void source(const char *src)
bump("%s: Network error occurred", src); bump("%s: Network error occurred", src);
if (statistics) { if (statistics) {
stat_bytes = uint64_add32(stat_bytes, k); stat_bytes += k;
if (time(NULL) != stat_lasttime || if (time(NULL) != stat_lasttime || i + k == size) {
(uint64_compare(uint64_add32(i, k), size) == 0)) {
stat_lasttime = time(NULL); stat_lasttime = time(NULL);
print_stats(last, size, stat_bytes, print_stats(last, size, stat_bytes,
stat_starttime, stat_lasttime); stat_starttime, stat_lasttime);
@ -1786,9 +1767,9 @@ static void sink(const char *targ, const char *src)
int exists; int exists;
int attr; int attr;
WFile *f; WFile *f;
uint64 received; uint64_t received;
int wrerror = 0; int wrerror = 0;
uint64 stat_bytes; uint64_t stat_bytes;
time_t stat_starttime, stat_lasttime; time_t stat_starttime, stat_lasttime;
char *stat_name; char *stat_name;
@ -1926,24 +1907,24 @@ static void sink(const char *targ, const char *src)
return; return;
} }
stat_bytes = uint64_make(0, 0); stat_bytes = 0;
stat_starttime = time(NULL); stat_starttime = time(NULL);
stat_lasttime = 0; stat_lasttime = 0;
stat_name = stripslashes(destfname, 1); stat_name = stripslashes(destfname, 1);
received = uint64_make(0, 0); received = 0;
while (uint64_compare(received,act.size) < 0) { while (received < act.size) {
char transbuf[32768]; char transbuf[32768];
uint64 blksize; uint64_t blksize;
int read; int read;
blksize = uint64_make(0, 32768); blksize = 32768;
if (uint64_compare(blksize,uint64_subtract(act.size,received)) > 0) if (blksize > act.size - received)
blksize = uint64_subtract(act.size,received); blksize = act.size - received;
read = scp_recv_filedata(transbuf, (int)blksize.lo); read = scp_recv_filedata(transbuf, (int)blksize);
if (read <= 0) if (read <= 0)
bump("Lost connection"); bump("Lost connection");
if (wrerror) { if (wrerror) {
received = uint64_add32(received, read); received += read;
continue; continue;
} }
if (write_to_file(f, transbuf, read) != (int)read) { if (write_to_file(f, transbuf, read) != (int)read) {
@ -1953,19 +1934,19 @@ static void sink(const char *targ, const char *src)
printf("\r%-25.25s | %50s\n", printf("\r%-25.25s | %50s\n",
stat_name, stat_name,
"Write error.. waiting for end of file"); "Write error.. waiting for end of file");
received = uint64_add32(received, read); received += read;
continue; continue;
} }
if (statistics) { if (statistics) {
stat_bytes = uint64_add32(stat_bytes,read); stat_bytes += read;
if (time(NULL) > stat_lasttime || if (time(NULL) > stat_lasttime ||
uint64_compare(uint64_add32(received, read), act.size) == 0) { received + read == act.size) {
stat_lasttime = time(NULL); stat_lasttime = time(NULL);
print_stats(stat_name, act.size, stat_bytes, print_stats(stat_name, act.size, stat_bytes,
stat_starttime, stat_lasttime); stat_starttime, stat_lasttime);
} }
} }
received = uint64_add32(received, read); received += read;
} }
if (act.settime) { if (act.settime) {
set_file_times(f, act.mtime, act.atime); set_file_times(f, act.mtime, act.atime);

21
psftp.c
View File

@ -14,7 +14,6 @@
#include "storage.h" #include "storage.h"
#include "ssh.h" #include "ssh.h"
#include "sftp.h" #include "sftp.h"
#include "int64.h"
const char *const appname = "PSFTP"; const char *const appname = "PSFTP";
@ -234,7 +233,7 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
struct sftp_packet *pktin; struct sftp_packet *pktin;
struct sftp_request *req; struct sftp_request *req;
struct fxp_xfer *xfer; struct fxp_xfer *xfer;
uint64 offset; uint64_t offset;
WFile *file; WFile *file;
int ret, shown_err = FALSE; int ret, shown_err = FALSE;
struct fxp_attrs attrs; struct fxp_attrs attrs;
@ -434,8 +433,7 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
} }
if (restart) { if (restart) {
char decbuf[30]; if (seek_file(file, 0, FROM_END) == -1) {
if (seek_file(file, uint64_make(0,0) , FROM_END) == -1) {
close_wfile(file); close_wfile(file);
printf("reget: cannot restart %s - file too large\n", printf("reget: cannot restart %s - file too large\n",
outfname); outfname);
@ -447,10 +445,9 @@ int sftp_get_file(char *fname, char *outfname, int recurse, int restart)
} }
offset = get_file_posn(file); offset = get_file_posn(file);
uint64_decimal(offset, decbuf); printf("reget: restarting at file position %"PRIu64"\n", offset);
printf("reget: restarting at file position %s\n", decbuf);
} else { } else {
offset = uint64_make(0, 0); offset = 0;
} }
printf("remote:%s => local:%s\n", fname, outfname); printf("remote:%s => local:%s\n", fname, outfname);
@ -519,7 +516,7 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
struct fxp_xfer *xfer; struct fxp_xfer *xfer;
struct sftp_packet *pktin; struct sftp_packet *pktin;
struct sftp_request *req; struct sftp_request *req;
uint64 offset; uint64_t offset;
RFile *file; RFile *file;
int err = 0, eof; int err = 0, eof;
struct fxp_attrs attrs; struct fxp_attrs attrs;
@ -672,7 +669,6 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
} }
if (restart) { if (restart) {
char decbuf[30];
struct fxp_attrs attrs; struct fxp_attrs attrs;
int ret; int ret;
@ -691,13 +687,12 @@ int sftp_put_file(char *fname, char *outfname, int recurse, int restart)
goto cleanup; goto cleanup;
} }
offset = attrs.size; offset = attrs.size;
uint64_decimal(offset, decbuf); printf("reput: restarting at file position %"PRIu64"\n", offset);
printf("reput: restarting at file position %s\n", decbuf);
if (seek_file((WFile *)file, offset, FROM_START) != 0) if (seek_file((WFile *)file, offset, FROM_START) != 0)
seek_file((WFile *)file, uint64_make(0,0), FROM_END); /* *shrug* */ seek_file((WFile *)file, 0, FROM_END); /* *shrug* */
} else { } else {
offset = uint64_make(0, 0); offset = 0;
} }
printf("local:%s => remote:%s\n", fname, outfname); printf("local:%s => remote:%s\n", fname, outfname);

10
psftp.h
View File

@ -3,8 +3,6 @@
* platform-specific SFTP module. * platform-specific SFTP module.
*/ */
#include "int64.h"
#ifndef PUTTY_PSFTP_H #ifndef PUTTY_PSFTP_H
#define PUTTY_PSFTP_H #define PUTTY_PSFTP_H
@ -93,10 +91,10 @@ typedef struct RFile RFile;
typedef struct WFile WFile; typedef struct WFile WFile;
/* Output params size, perms, mtime and atime can all be NULL if /* Output params size, perms, mtime and atime can all be NULL if
* desired. perms will be -1 if the OS does not support POSIX permissions. */ * desired. perms will be -1 if the OS does not support POSIX permissions. */
RFile *open_existing_file(const char *name, uint64 *size, RFile *open_existing_file(const char *name, uint64_t *size,
unsigned long *mtime, unsigned long *atime, unsigned long *mtime, unsigned long *atime,
long *perms); long *perms);
WFile *open_existing_wfile(const char *name, uint64 *size); WFile *open_existing_wfile(const char *name, uint64_t *size);
/* Returns <0 on error, 0 on eof, or number of bytes read, as usual */ /* Returns <0 on error, 0 on eof, or number of bytes read, as usual */
int read_from_file(RFile *f, void *buffer, int length); int read_from_file(RFile *f, void *buffer, int length);
/* Closes and frees the RFile */ /* Closes and frees the RFile */
@ -109,9 +107,9 @@ void set_file_times(WFile *f, unsigned long mtime, unsigned long atime);
void close_wfile(WFile *f); void close_wfile(WFile *f);
/* Seek offset bytes through file */ /* Seek offset bytes through file */
enum { FROM_START, FROM_CURRENT, FROM_END }; enum { FROM_START, FROM_CURRENT, FROM_END };
int seek_file(WFile *f, uint64 offset, int whence); int seek_file(WFile *f, uint64_t offset, int whence);
/* Get file position */ /* Get file position */
uint64 get_file_posn(WFile *f); uint64_t get_file_posn(WFile *f);
/* /*
* Determine the type of a file: nonexistent, file, directory or * Determine the type of a file: nonexistent, file, directory or
* weird. `weird' covers anything else - named pipes, Unix sockets, * weird. `weird' covers anything else - named pipes, Unix sockets,

View File

@ -381,7 +381,7 @@ struct ScpSource {
strbuf *pending_commands[3]; strbuf *pending_commands[3];
int n_pending_commands; int n_pending_commands;
uint64 file_offset, file_size; uint64_t file_offset, file_size;
ScpReplyReceiver reply; ScpReplyReceiver reply;
@ -535,7 +535,7 @@ static void scp_source_send_E(ScpSource *scp)
static void scp_source_send_CD( static void scp_source_send_CD(
ScpSource *scp, char cmdchar, ScpSource *scp, char cmdchar,
struct fxp_attrs attrs, uint64 size, ptrlen name) struct fxp_attrs attrs, uint64_t size, ptrlen name)
{ {
strbuf *cmd; strbuf *cmd;
@ -547,18 +547,15 @@ static void scp_source_send_CD(
strbuf_catf(cmd, "T%lu 0 %lu 0\012", attrs.mtime, attrs.atime); strbuf_catf(cmd, "T%lu 0 %lu 0\012", attrs.mtime, attrs.atime);
} }
char sizebuf[32];
uint64_decimal(size, sizebuf);
const char *slash; const char *slash;
while ((slash = memchr(name.ptr, '/', name.len)) != NULL) while ((slash = memchr(name.ptr, '/', name.len)) != NULL)
name = make_ptrlen( name = make_ptrlen(
slash+1, name.len - (slash+1 - (const char *)name.ptr)); slash+1, name.len - (slash+1 - (const char *)name.ptr));
scp->pending_commands[scp->n_pending_commands++] = cmd = strbuf_new(); scp->pending_commands[scp->n_pending_commands++] = cmd = strbuf_new();
strbuf_catf(cmd, "%c%04o %s %.*s\012", cmdchar, strbuf_catf(cmd, "%c%04o %"PRIu64" %.*s\012", cmdchar,
(unsigned)(attrs.permissions & 07777), (unsigned)(attrs.permissions & 07777),
sizebuf, PTRLEN_PRINTF(name)); size, PTRLEN_PRINTF(name));
if (cmdchar == 'C') { if (cmdchar == 'C') {
/* We'll also wait for an ack before sending the file data, /* We'll also wait for an ack before sending the file data,
@ -626,8 +623,7 @@ static void scp_source_process_stack(ScpSource *scp)
/* /*
* Mostly, we start by waiting for an ack byte from the receiver. * Mostly, we start by waiting for an ack byte from the receiver.
*/ */
if (scp->head && scp->head->type == SCP_READFILE && if (scp->head && scp->head->type == SCP_READFILE && scp->file_offset) {
uint64_compare(scp->file_offset, uint64_make(0, 0)) != 0) {
/* /*
* Exception: if we're already in the middle of transferring a * Exception: if we're already in the middle of transferring a
* file, we'll be called back here because the channel backlog * file, we'll be called back here because the channel backlog
@ -717,14 +713,12 @@ static void scp_source_process_stack(ScpSource *scp)
* Transfer file data if our backlog hasn't filled up. * Transfer file data if our backlog hasn't filled up.
*/ */
int backlog; int backlog;
uint64 remaining = uint64_t limit = scp->file_size - scp->file_offset;
uint64_subtract(scp->file_size, scp->file_offset); if (limit > 4096)
uint64 limit = uint64_make(0, 4096); limit = 4096;
if (uint64_compare(remaining, limit) < 0) if (limit > 0) {
limit = remaining;
if (limit.lo > 0) {
sftpsrv_read(scp->sf, &scp->reply.srb, scp->head->handle, sftpsrv_read(scp->sf, &scp->reply.srb, scp->head->handle,
scp->file_offset, limit.lo); scp->file_offset, limit);
if (scp->reply.err) { if (scp->reply.err) {
scp_source_abort( scp_source_abort(
scp, "%.*s: unable to read: %s", scp, "%.*s: unable to read: %s",
@ -734,8 +728,7 @@ static void scp_source_process_stack(ScpSource *scp)
backlog = sshfwd_write( backlog = sshfwd_write(
scp->sc, scp->reply.data.ptr, scp->reply.data.len); scp->sc, scp->reply.data.ptr, scp->reply.data.len);
scp->file_offset = uint64_add( scp->file_offset += scp->reply.data.len;
scp->file_offset, uint64_make(0, scp->reply.data.len));
if (backlog < SCP_MAX_BACKLOG) if (backlog < SCP_MAX_BACKLOG)
scp_requeue(scp); scp_requeue(scp);
@ -819,8 +812,7 @@ static void scp_source_process_stack(ScpSource *scp)
assert(scp->recursive || node->wildcard); assert(scp->recursive || node->wildcard);
if (!node->wildcard) if (!node->wildcard)
scp_source_send_CD(scp, 'D', node->attrs, scp_source_send_CD(scp, 'D', node->attrs, 0, node->pathname);
uint64_make(0, 0), node->pathname);
sftpsrv_opendir(scp->sf, &scp->reply.srb, node->pathname); sftpsrv_opendir(scp->sf, &scp->reply.srb, node->pathname);
if (scp->reply.err) { if (scp->reply.err) {
scp_source_err( scp_source_err(
@ -855,7 +847,7 @@ static void scp_source_process_stack(ScpSource *scp)
scp_requeue(scp); scp_requeue(scp);
return; return;
} }
scp->file_offset = uint64_make(0, 0); scp->file_offset = 0;
scp->file_size = scp->reply.attrs.size; scp->file_size = scp->reply.attrs.size;
scp_source_send_CD(scp, 'C', node->attrs, scp_source_send_CD(scp, 'C', node->attrs,
scp->file_size, node->pathname); scp->file_size, node->pathname);
@ -943,7 +935,7 @@ struct ScpSink {
SshChannel *sc; SshChannel *sc;
ScpSinkStackEntry *head; ScpSinkStackEntry *head;
uint64 file_offset, file_size; uint64_t file_offset, file_size;
unsigned long atime, mtime; unsigned long atime, mtime;
int got_file_times; int got_file_times;
@ -1135,7 +1127,7 @@ static void scp_sink_coroutine(ScpSink *scp)
if (*p != ' ') if (*p != ' ')
goto parse_error; goto parse_error;
p++; p++;
scp->file_size = uint64_from_decimal(q); scp->file_size = strtoull(q, NULL, 10);
ptrlen leafname = make_ptrlen( ptrlen leafname = make_ptrlen(
p, scp->command->len - (p - scp->command->s)); p, scp->command->len - (p - scp->command->s));
@ -1185,11 +1177,11 @@ static void scp_sink_coroutine(ScpSink *scp)
* Now send an ack, and read the file data. * Now send an ack, and read the file data.
*/ */
sshfwd_write(scp->sc, "\0", 1); sshfwd_write(scp->sc, "\0", 1);
scp->file_offset = uint64_make(0, 0); scp->file_offset = 0;
while (uint64_compare(scp->file_offset, scp->file_size) < 0) { while (scp->file_offset < scp->file_size) {
void *vdata; void *vdata;
int len; int len;
uint64 this_len, remaining; uint64_t this_len, remaining;
crMaybeWaitUntilV( crMaybeWaitUntilV(
scp->input_eof || bufchain_size(&scp->data) > 0); scp->input_eof || bufchain_size(&scp->data) > 0);
@ -1200,22 +1192,21 @@ static void scp_sink_coroutine(ScpSink *scp)
} }
bufchain_prefix(&scp->data, &vdata, &len); bufchain_prefix(&scp->data, &vdata, &len);
this_len = uint64_make(0, len); this_len = len;
remaining = uint64_subtract( remaining = scp->file_size - scp->file_offset;
scp->file_size, scp->file_offset); if (this_len > remaining)
if (uint64_compare(this_len, remaining) > 0)
this_len = remaining; this_len = remaining;
sftpsrv_write(scp->sf, &scp->reply.srb, sftpsrv_write(scp->sf, &scp->reply.srb,
scp->reply.handle, scp->file_offset, scp->reply.handle, scp->file_offset,
make_ptrlen(vdata, this_len.lo)); make_ptrlen(vdata, this_len));
if (scp->reply.err) { if (scp->reply.err) {
scp->errmsg = dupprintf( scp->errmsg = dupprintf(
"'%.*s': unable to write to file: %s", "'%.*s': unable to write to file: %s",
PTRLEN_PRINTF(scp->filename), scp->reply.errmsg); PTRLEN_PRINTF(scp->filename), scp->reply.errmsg);
goto done; goto done;
} }
bufchain_consume(&scp->data, this_len.lo); bufchain_consume(&scp->data, this_len);
scp->file_offset = uint64_add(scp->file_offset, this_len); scp->file_offset += this_len;
} }
/* /*

45
sftp.c
View File

@ -9,7 +9,6 @@
#include <limits.h> #include <limits.h>
#include "misc.h" #include "misc.h"
#include "int64.h"
#include "tree234.h" #include "tree234.h"
#include "sftp.h" #include "sftp.h"
@ -683,7 +682,7 @@ int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
* error indicator. It might even depend on the SFTP server.) * error indicator. It might even depend on the SFTP server.)
*/ */
struct sftp_request *fxp_read_send(struct fxp_handle *handle, struct sftp_request *fxp_read_send(struct fxp_handle *handle,
uint64 offset, int len) uint64_t offset, int len)
{ {
struct sftp_request *req = sftp_alloc_request(); struct sftp_request *req = sftp_alloc_request();
struct sftp_packet *pktout; struct sftp_packet *pktout;
@ -812,7 +811,7 @@ struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
* Write to a file. Returns 0 on error, 1 on OK. * Write to a file. Returns 0 on error, 1 on OK.
*/ */
struct sftp_request *fxp_write_send(struct fxp_handle *handle, struct sftp_request *fxp_write_send(struct fxp_handle *handle,
void *buffer, uint64 offset, int len) void *buffer, uint64_t offset, int len)
{ {
struct sftp_request *req = sftp_alloc_request(); struct sftp_request *req = sftp_alloc_request();
struct sftp_packet *pktout; struct sftp_packet *pktout;
@ -894,18 +893,18 @@ void fxp_set_userdata(struct sftp_request *req, void *data)
struct req { struct req {
char *buffer; char *buffer;
int len, retlen, complete; int len, retlen, complete;
uint64 offset; uint64_t offset;
struct req *next, *prev; struct req *next, *prev;
}; };
struct fxp_xfer { struct fxp_xfer {
uint64 offset, furthestdata, filesize; uint64_t offset, furthestdata, filesize;
int req_totalsize, req_maxsize, eof, err; int req_totalsize, req_maxsize, eof, err;
struct fxp_handle *fh; struct fxp_handle *fh;
struct req *head, *tail; struct req *head, *tail;
}; };
static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset) static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64_t offset)
{ {
struct fxp_xfer *xfer = snew(struct fxp_xfer); struct fxp_xfer *xfer = snew(struct fxp_xfer);
@ -915,8 +914,8 @@ static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
xfer->req_totalsize = 0; xfer->req_totalsize = 0;
xfer->req_maxsize = 1048576; xfer->req_maxsize = 1048576;
xfer->err = 0; xfer->err = 0;
xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX); xfer->filesize = UINT64_MAX;
xfer->furthestdata = uint64_make(0, 0); xfer->furthestdata = 0;
return xfer; return xfer;
} }
@ -958,16 +957,16 @@ void xfer_download_queue(struct fxp_xfer *xfer)
sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len)); sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
fxp_set_userdata(req, rr); fxp_set_userdata(req, rr);
xfer->offset = uint64_add32(xfer->offset, rr->len); xfer->offset += rr->len;
xfer->req_totalsize += rr->len; xfer->req_totalsize += rr->len;
#ifdef DEBUG_DOWNLOAD #ifdef DEBUG_DOWNLOAD
{ char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); } printf("queueing read request %p at %"PRIu64"\n", rr, rr->offset);
#endif #endif
} }
} }
struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset) struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64_t offset)
{ {
struct fxp_xfer *xfer = xfer_init(fh, offset); struct fxp_xfer *xfer = xfer_init(fh, offset);
@ -1027,24 +1026,19 @@ int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
* I simply shouldn't have been queueing multiple requests in * I simply shouldn't have been queueing multiple requests in
* the first place... * the first place...
*/ */
if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) { if (rr->retlen > 0 && xfer->furthestdata < rr->offset) {
xfer->furthestdata = rr->offset; xfer->furthestdata = rr->offset;
#ifdef DEBUG_DOWNLOAD #ifdef DEBUG_DOWNLOAD
{ char buf[40]; printf("setting furthestdata = %"PRIu64"\n", xfer->furthestdata);
uint64_decimal(xfer->furthestdata, buf);
printf("setting furthestdata = %s\n", buf); }
#endif #endif
} }
if (rr->retlen < rr->len) { if (rr->retlen < rr->len) {
uint64 filesize = uint64_add32(rr->offset, uint64_t filesize = rr->offset + (rr->retlen < 0 ? 0 : rr->retlen);
(rr->retlen < 0 ? 0 : rr->retlen));
#ifdef DEBUG_DOWNLOAD #ifdef DEBUG_DOWNLOAD
{ char buf[40]; printf("short block! trying filesize = %"PRIu64"\n", filesize);
uint64_decimal(filesize, buf);
printf("short block! trying filesize = %s\n", buf); }
#endif #endif
if (uint64_compare(xfer->filesize, filesize) > 0) { if (xfer->filesize > filesize) {
xfer->filesize = filesize; xfer->filesize = filesize;
#ifdef DEBUG_DOWNLOAD #ifdef DEBUG_DOWNLOAD
printf("actually changing filesize\n"); printf("actually changing filesize\n");
@ -1052,7 +1046,7 @@ int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
} }
} }
if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) { if (xfer->furthestdata > xfer->filesize) {
fxp_error_message = "received a short buffer from FXP_READ, but not" fxp_error_message = "received a short buffer from FXP_READ, but not"
" at EOF"; " at EOF";
fxp_errtype = -1; fxp_errtype = -1;
@ -1109,7 +1103,7 @@ int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
return 0; return 0;
} }
struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset) struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64_t offset)
{ {
struct fxp_xfer *xfer = xfer_init(fh, offset); struct fxp_xfer *xfer = xfer_init(fh, offset);
@ -1157,11 +1151,12 @@ void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len)
sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len)); sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len));
fxp_set_userdata(req, rr); fxp_set_userdata(req, rr);
xfer->offset = uint64_add32(xfer->offset, rr->len); xfer->offset += rr->len;
xfer->req_totalsize += rr->len; xfer->req_totalsize += rr->len;
#ifdef DEBUG_UPLOAD #ifdef DEBUG_UPLOAD
{ char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); } printf("queueing write request %p at %"PRIu64" [len %d]\n",
rr, rr->offset, len);
#endif #endif
} }

15
sftp.h
View File

@ -3,7 +3,6 @@
*/ */
#include "defs.h" #include "defs.h"
#include "int64.h"
#define SSH_FXP_INIT 1 /* 0x1 */ #define SSH_FXP_INIT 1 /* 0x1 */
#define SSH_FXP_VERSION 2 /* 0x2 */ #define SSH_FXP_VERSION 2 /* 0x2 */
@ -81,7 +80,7 @@ void sftp_cleanup_request(void);
struct fxp_attrs { struct fxp_attrs {
unsigned long flags; unsigned long flags;
uint64 size; uint64_t size;
unsigned long uid; unsigned long uid;
unsigned long gid; unsigned long gid;
unsigned long permissions; unsigned long permissions;
@ -248,7 +247,7 @@ int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req);
* Read from a file. * Read from a file.
*/ */
struct sftp_request *fxp_read_send(struct fxp_handle *handle, struct sftp_request *fxp_read_send(struct fxp_handle *handle,
uint64 offset, int len); uint64_t offset, int len);
int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req, int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
char *buffer, int len); char *buffer, int len);
@ -256,7 +255,7 @@ int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
* Write to a file. Returns 0 on error, 1 on OK. * Write to a file. Returns 0 on error, 1 on OK.
*/ */
struct sftp_request *fxp_write_send(struct fxp_handle *handle, struct sftp_request *fxp_write_send(struct fxp_handle *handle,
void *buffer, uint64 offset, int len); void *buffer, uint64_t offset, int len);
int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req); int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req);
/* /*
@ -299,12 +298,12 @@ struct sftp_packet *sftp_recv(void);
struct fxp_xfer; struct fxp_xfer;
struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset); struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64_t offset);
void xfer_download_queue(struct fxp_xfer *xfer); void xfer_download_queue(struct fxp_xfer *xfer);
int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin); int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin);
int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len); int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len);
struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset); struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64_t offset);
int xfer_upload_ready(struct fxp_xfer *xfer); int xfer_upload_ready(struct fxp_xfer *xfer);
void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len); void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len);
int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin); int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin);
@ -378,11 +377,11 @@ struct SftpServerVtable {
/* Should call fxp_reply_error or fxp_reply_data */ /* Should call fxp_reply_error or fxp_reply_data */
void (*read)(SftpServer *srv, SftpReplyBuilder *reply, void (*read)(SftpServer *srv, SftpReplyBuilder *reply,
ptrlen handle, uint64 offset, unsigned length); ptrlen handle, uint64_t offset, unsigned length);
/* Should call fxp_reply_error or fxp_reply_ok */ /* Should call fxp_reply_error or fxp_reply_ok */
void (*write)(SftpServer *srv, SftpReplyBuilder *reply, void (*write)(SftpServer *srv, SftpReplyBuilder *reply,
ptrlen handle, uint64 offset, ptrlen data); ptrlen handle, uint64_t offset, ptrlen data);
/* Should call fxp_reply_error, or fxp_reply_name_count once and /* Should call fxp_reply_error, or fxp_reply_name_count once and
* then fxp_reply_full_name that many times */ * then fxp_reply_full_name that many times */

View File

@ -16,7 +16,7 @@ struct sftp_packet *sftp_handle_request(
struct sftp_packet *reply; struct sftp_packet *reply;
unsigned id; unsigned id;
ptrlen path, dstpath, handle, data; ptrlen path, dstpath, handle, data;
uint64 offset; uint64_t offset;
unsigned length; unsigned length;
struct fxp_attrs attrs; struct fxp_attrs attrs;
DefaultSftpReplyBuilder dsrb; DefaultSftpReplyBuilder dsrb;

23
ssh.h
View File

@ -4,7 +4,6 @@
#include "puttymem.h" #include "puttymem.h"
#include "tree234.h" #include "tree234.h"
#include "network.h" #include "network.h"
#include "int64.h"
#include "misc.h" #include "misc.h"
struct ssh_channel; struct ssh_channel;
@ -507,8 +506,6 @@ void rsa_ssh1_public_blob(BinarySink *bs, struct RSAKey *key,
int rsa_ssh1_public_blob_len(void *data, int maxlen); int rsa_ssh1_public_blob_len(void *data, int maxlen);
void freersakey(struct RSAKey *key); void freersakey(struct RSAKey *key);
typedef uint32 word32;
unsigned long crc32_compute(const void *s, size_t len); unsigned long crc32_compute(const void *s, size_t len);
unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len); unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
@ -516,7 +513,7 @@ unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len);
struct crcda_ctx; struct crcda_ctx;
struct crcda_ctx *crcda_make_context(void); struct crcda_ctx *crcda_make_context(void);
void crcda_free_context(struct crcda_ctx *ctx); void crcda_free_context(struct crcda_ctx *ctx);
int detect_attack(struct crcda_ctx *ctx, unsigned char *buf, uint32 len, int detect_attack(struct crcda_ctx *ctx, unsigned char *buf, uint32_t len,
unsigned char *IV); unsigned char *IV);
/* /*
@ -556,14 +553,14 @@ struct ssh2_cipheralg;
typedef const struct ssh2_cipheralg *ssh2_cipher; typedef const struct ssh2_cipheralg *ssh2_cipher;
typedef struct { typedef struct {
uint32 h[4]; uint32_t h[4];
} MD5_Core_State; } MD5_Core_State;
struct MD5Context { struct MD5Context {
MD5_Core_State core; MD5_Core_State core;
unsigned char block[64]; unsigned char block[64];
int blkused; int blkused;
uint32 lenhi, lenlo; uint64_t len;
BinarySink_IMPLEMENTATION; BinarySink_IMPLEMENTATION;
}; };
@ -581,10 +578,10 @@ void hmacmd5_do_hmac(struct hmacmd5_context *ctx,
int supports_sha_ni(void); int supports_sha_ni(void);
typedef struct SHA_State { typedef struct SHA_State {
uint32 h[5]; uint32_t h[5];
unsigned char block[64]; unsigned char block[64];
int blkused; int blkused;
uint32 lenhi, lenlo; uint64_t len;
void (*sha1)(struct SHA_State * s, const unsigned char *p, int len); void (*sha1)(struct SHA_State * s, const unsigned char *p, int len);
BinarySink_IMPLEMENTATION; BinarySink_IMPLEMENTATION;
} SHA_State; } SHA_State;
@ -596,10 +593,10 @@ void hmac_sha1_simple(const void *key, int keylen,
const void *data, int datalen, const void *data, int datalen,
unsigned char *output); unsigned char *output);
typedef struct SHA256_State { typedef struct SHA256_State {
uint32 h[8]; uint32_t h[8];
unsigned char block[64]; unsigned char block[64];
int blkused; int blkused;
uint32 lenhi, lenlo; uint64_t len;
void (*sha256)(struct SHA256_State * s, const unsigned char *p, int len); void (*sha256)(struct SHA256_State * s, const unsigned char *p, int len);
BinarySink_IMPLEMENTATION; BinarySink_IMPLEMENTATION;
} SHA256_State; } SHA256_State;
@ -608,10 +605,10 @@ void SHA256_Final(SHA256_State * s, unsigned char *output);
void SHA256_Simple(const void *p, int len, unsigned char *output); void SHA256_Simple(const void *p, int len, unsigned char *output);
typedef struct { typedef struct {
uint64 h[8]; uint64_t h[8];
unsigned char block[128]; unsigned char block[128];
int blkused; int blkused;
uint32 len[4]; uint64_t lenhi, lenlo;
BinarySink_IMPLEMENTATION; BinarySink_IMPLEMENTATION;
} SHA512_State; } SHA512_State;
#define SHA384_State SHA512_State #define SHA384_State SHA512_State
@ -898,7 +895,7 @@ extern const char sshver[];
*/ */
extern int ssh_fallback_cmd(Backend *backend); extern int ssh_fallback_cmd(Backend *backend);
void SHATransform(word32 * digest, word32 * data); void SHATransform(uint32_t *digest, uint32_t *data);
/* /*
* Check of compiler version * Check of compiler version

View File

@ -49,10 +49,10 @@
#endif #endif
struct AESContext { struct AESContext {
word32 keysched_buf[(MAX_NR + 1) * NB + 3]; uint32_t keysched_buf[(MAX_NR + 1) * NB + 3];
word32 invkeysched_buf[(MAX_NR + 1) * NB + 3]; uint32_t invkeysched_buf[(MAX_NR + 1) * NB + 3];
word32 *keysched, *invkeysched; uint32_t *keysched, *invkeysched;
word32 iv[NB]; uint32_t iv[NB];
int Nr; /* number of rounds */ int Nr; /* number of rounds */
void (*encrypt_cbc)(unsigned char*, int, AESContext*); void (*encrypt_cbc)(unsigned char*, int, AESContext*);
void (*decrypt_cbc)(unsigned char*, int, AESContext*); void (*decrypt_cbc)(unsigned char*, int, AESContext*);
@ -156,7 +156,7 @@ static const unsigned char Sboxinv[256] = {
0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
}; };
static const word32 E0[256] = { static const uint32_t E0[256] = {
0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d,
0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d,
@ -222,7 +222,7 @@ static const word32 E0[256] = {
0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11,
0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
}; };
static const word32 E1[256] = { static const uint32_t E1[256] = {
0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b,
0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b,
@ -288,7 +288,7 @@ static const word32 E1[256] = {
0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f,
0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616,
}; };
static const word32 E2[256] = { static const uint32_t E2[256] = {
0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b,
0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b,
@ -354,7 +354,7 @@ static const word32 E2[256] = {
0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f,
0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16,
}; };
static const word32 E3[256] = { static const uint32_t E3[256] = {
0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6,
0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56,
@ -420,7 +420,7 @@ static const word32 E3[256] = {
0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e,
0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c,
}; };
static const word32 D0[256] = { static const uint32_t D0[256] = {
0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96,
0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25,
@ -486,7 +486,7 @@ static const word32 D0[256] = {
0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190,
0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
}; };
static const word32 D1[256] = { static const uint32_t D1[256] = {
0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e,
0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c,
@ -552,7 +552,7 @@ static const word32 D1[256] = {
0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1,
0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857,
}; };
static const word32 D2[256] = { static const uint32_t D2[256] = {
0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27,
0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502,
@ -618,7 +618,7 @@ static const word32 D2[256] = {
0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456,
0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8,
}; };
static const word32 D3[256] = { static const uint32_t D3[256] = {
0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a,
0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5,
@ -700,11 +700,11 @@ static void aes_setup(AESContext * ctx, const unsigned char *key, int keylen)
/* Ensure the key schedule arrays are 16-byte aligned */ /* Ensure the key schedule arrays are 16-byte aligned */
bufaddr = (size_t)ctx->keysched_buf; bufaddr = (size_t)ctx->keysched_buf;
ctx->keysched = ctx->keysched_buf + ctx->keysched = ctx->keysched_buf +
(0xF & -bufaddr) / sizeof(word32); (0xF & -bufaddr) / sizeof(uint32_t);
assert((size_t)ctx->keysched % 16 == 0); assert((size_t)ctx->keysched % 16 == 0);
bufaddr = (size_t)ctx->invkeysched_buf; bufaddr = (size_t)ctx->invkeysched_buf;
ctx->invkeysched = ctx->invkeysched_buf + ctx->invkeysched = ctx->invkeysched_buf +
(0xF & -bufaddr) / sizeof(word32); (0xF & -bufaddr) / sizeof(uint32_t);
assert((size_t)ctx->invkeysched % 16 == 0); assert((size_t)ctx->invkeysched % 16 == 0);
ctx->isNI = supports_aes_ni(); ctx->isNI = supports_aes_ni();
@ -726,7 +726,7 @@ static void aes_setup(AESContext * ctx, const unsigned char *key, int keylen)
if (i < Nk) if (i < Nk)
ctx->keysched[i] = GET_32BIT_MSB_FIRST(key + 4 * i); ctx->keysched[i] = GET_32BIT_MSB_FIRST(key + 4 * i);
else { else {
word32 temp = ctx->keysched[i - 1]; uint32_t temp = ctx->keysched[i - 1];
if (i % Nk == 0) { if (i % Nk == 0) {
int a, b, c, d; int a, b, c, d;
a = (temp >> 16) & 0xFF; a = (temp >> 16) & 0xFF;
@ -758,7 +758,7 @@ static void aes_setup(AESContext * ctx, const unsigned char *key, int keylen)
*/ */
for (i = 0; i <= ctx->Nr; i++) { for (i = 0; i <= ctx->Nr; i++) {
for (j = 0; j < NB; j++) { for (j = 0; j < NB; j++) {
word32 temp; uint32_t temp;
temp = ctx->keysched[(ctx->Nr - i) * NB + j]; temp = ctx->keysched[(ctx->Nr - i) * NB + j];
if (i != 0 && i != ctx->Nr) { if (i != 0 && i != ctx->Nr) {
/* /*
@ -826,7 +826,7 @@ static void aes_setup(AESContext * ctx, const unsigned char *key, int keylen)
*/ */
static void aes_encrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx) static void aes_encrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx)
{ {
word32 block[4]; uint32_t block[4];
unsigned char* finish = blk + len; unsigned char* finish = blk + len;
int i; int i;
@ -835,8 +835,8 @@ static void aes_encrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx)
memcpy(block, ctx->iv, sizeof(block)); memcpy(block, ctx->iv, sizeof(block));
while (blk < finish) { while (blk < finish) {
word32 *keysched = ctx->keysched; uint32_t *keysched = ctx->keysched;
word32 newstate[4]; uint32_t newstate[4];
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
block[i] ^= GET_32BIT_MSB_FIRST(blk + 4 * i); block[i] ^= GET_32BIT_MSB_FIRST(blk + 4 * i);
ADD_ROUND_KEY; ADD_ROUND_KEY;
@ -872,7 +872,7 @@ static void aes_encrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx)
static void aes_sdctr_sw(unsigned char *blk, int len, AESContext *ctx) static void aes_sdctr_sw(unsigned char *blk, int len, AESContext *ctx)
{ {
word32 iv[4]; uint32_t iv[4];
unsigned char* finish = blk + len; unsigned char* finish = blk + len;
int i; int i;
@ -881,8 +881,8 @@ static void aes_sdctr_sw(unsigned char *blk, int len, AESContext *ctx)
memcpy(iv, ctx->iv, sizeof(iv)); memcpy(iv, ctx->iv, sizeof(iv));
while (blk < finish) { while (blk < finish) {
word32 *keysched = ctx->keysched; uint32_t *keysched = ctx->keysched;
word32 newstate[4], block[4], tmp; uint32_t newstate[4], block[4], tmp;
memcpy(block, iv, sizeof(block)); memcpy(block, iv, sizeof(block));
ADD_ROUND_KEY; ADD_ROUND_KEY;
switch (ctx->Nr) { switch (ctx->Nr) {
@ -922,7 +922,7 @@ static void aes_sdctr_sw(unsigned char *blk, int len, AESContext *ctx)
static void aes_decrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx) static void aes_decrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx)
{ {
word32 iv[4]; uint32_t iv[4];
unsigned char* finish = blk + len; unsigned char* finish = blk + len;
int i; int i;
@ -931,8 +931,8 @@ static void aes_decrypt_cbc_sw(unsigned char *blk, int len, AESContext * ctx)
memcpy(iv, ctx->iv, sizeof(iv)); memcpy(iv, ctx->iv, sizeof(iv));
while (blk < finish) { while (blk < finish) {
word32 *keysched = ctx->invkeysched; uint32_t *keysched = ctx->invkeysched;
word32 newstate[4], ct[4], block[4]; uint32_t newstate[4], ct[4], block[4];
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
block[i] = ct[i] = GET_32BIT_MSB_FIRST(blk + 4 * i); block[i] = ct[i] = GET_32BIT_MSB_FIRST(blk + 4 * i);
ADD_ROUND_KEY; ADD_ROUND_KEY;

View File

@ -10,8 +10,8 @@
#include "sshblowf.h" #include "sshblowf.h"
struct BlowfishContext { struct BlowfishContext {
word32 S0[256], S1[256], S2[256], S3[256], P[18]; uint32_t S0[256], S1[256], S2[256], S3[256], P[18];
word32 iv0, iv1; /* for CBC mode */ uint32_t iv0, iv1; /* for CBC mode */
}; };
/* /*
@ -27,7 +27,7 @@ struct BlowfishContext {
open my $spig, "spigot -n -B16 -d8336 pi |"; open my $spig, "spigot -n -B16 -d8336 pi |";
read $spig, $ignore, 2; # throw away the leading "3." read $spig, $ignore, 2; # throw away the leading "3."
for my $name ("parray", "sbox0".."sbox3") { for my $name ("parray", "sbox0".."sbox3") {
print "static const word32 ${name}[] = {\n"; print "static const uint32_t ${name}[] = {\n";
my $len = $name eq "parray" ? 18 : 256; my $len = $name eq "parray" ? 18 : 256;
for my $i (1..$len) { for my $i (1..$len) {
read $spig, $word, 8; read $spig, $word, 8;
@ -39,13 +39,13 @@ for my $name ("parray", "sbox0".."sbox3") {
close $spig; close $spig;
*/ */
static const word32 parray[] = { static const uint32_t parray[] = {
0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0,
0x082EFA98, 0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0x082EFA98, 0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C,
0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B,
}; };
static const word32 sbox0[] = { static const uint32_t sbox0[] = {
0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96, 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96,
0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16,
0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x636920D8, 0x71574E69, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658,
@ -91,7 +91,7 @@ static const word32 sbox0[] = {
0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A, 0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A,
}; };
static const word32 sbox1[] = { static const uint32_t sbox1[] = {
0x4B7A70E9, 0xB5B32944, 0xDB75092E, 0xC4192623, 0xAD6EA6B0, 0x49A7DF7D, 0x4B7A70E9, 0xB5B32944, 0xDB75092E, 0xC4192623, 0xAD6EA6B0, 0x49A7DF7D,
0x9CEE60B8, 0x8FEDB266, 0xECAA8C71, 0x699A17FF, 0x5664526C, 0xC2B19EE1, 0x9CEE60B8, 0x8FEDB266, 0xECAA8C71, 0x699A17FF, 0x5664526C, 0xC2B19EE1,
0x193602A5, 0x75094C29, 0xA0591340, 0xE4183A3E, 0x3F54989A, 0x5B429D65, 0x193602A5, 0x75094C29, 0xA0591340, 0xE4183A3E, 0x3F54989A, 0x5B429D65,
@ -137,7 +137,7 @@ static const word32 sbox1[] = {
0x153E21E7, 0x8FB03D4A, 0xE6E39F2B, 0xDB83ADF7, 0x153E21E7, 0x8FB03D4A, 0xE6E39F2B, 0xDB83ADF7,
}; };
static const word32 sbox2[] = { static const uint32_t sbox2[] = {
0xE93D5A68, 0x948140F7, 0xF64C261C, 0x94692934, 0x411520F7, 0x7602D4F7, 0xE93D5A68, 0x948140F7, 0xF64C261C, 0x94692934, 0x411520F7, 0x7602D4F7,
0xBCF46B2E, 0xD4A20068, 0xD4082471, 0x3320F46A, 0x43B7D4B7, 0x500061AF, 0xBCF46B2E, 0xD4A20068, 0xD4082471, 0x3320F46A, 0x43B7D4B7, 0x500061AF,
0x1E39F62E, 0x97244546, 0x14214F74, 0xBF8B8840, 0x4D95FC1D, 0x96B591AF, 0x1E39F62E, 0x97244546, 0x14214F74, 0xBF8B8840, 0x4D95FC1D, 0x96B591AF,
@ -183,7 +183,7 @@ static const word32 sbox2[] = {
0xD79A3234, 0x92638212, 0x670EFA8E, 0x406000E0, 0xD79A3234, 0x92638212, 0x670EFA8E, 0x406000E0,
}; };
static const word32 sbox3[] = { static const uint32_t sbox3[] = {
0x3A39CE37, 0xD3FAF5CF, 0xABC27737, 0x5AC52D1B, 0x5CB0679E, 0x4FA33742, 0x3A39CE37, 0xD3FAF5CF, 0xABC27737, 0x5AC52D1B, 0x5CB0679E, 0x4FA33742,
0xD3822740, 0x99BC9BBE, 0xD5118E9D, 0xBF0F7315, 0xD62D1C7E, 0xC700C47B, 0xD3822740, 0x99BC9BBE, 0xD5118E9D, 0xBF0F7315, 0xD62D1C7E, 0xC700C47B,
0xB78C1B6B, 0x21A19045, 0xB26EB1BE, 0x6A366EB4, 0x5748AB2F, 0xBC946E79, 0xB78C1B6B, 0x21A19045, 0xB26EB1BE, 0x6A366EB4, 0x5748AB2F, 0xBC946E79,
@ -233,15 +233,15 @@ static const word32 sbox3[] = {
#define F(x) Fprime( ((x>>24)&0xFF), ((x>>16)&0xFF), ((x>>8)&0xFF), (x&0xFF) ) #define F(x) Fprime( ((x>>24)&0xFF), ((x>>16)&0xFF), ((x>>8)&0xFF), (x&0xFF) )
#define ROUND(n) ( xL ^= P[n], t = xL, xL = F(xL) ^ xR, xR = t ) #define ROUND(n) ( xL ^= P[n], t = xL, xL = F(xL) ^ xR, xR = t )
static void blowfish_encrypt(word32 xL, word32 xR, word32 * output, static void blowfish_encrypt(uint32_t xL, uint32_t xR, uint32_t *output,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 *S0 = ctx->S0; uint32_t *S0 = ctx->S0;
word32 *S1 = ctx->S1; uint32_t *S1 = ctx->S1;
word32 *S2 = ctx->S2; uint32_t *S2 = ctx->S2;
word32 *S3 = ctx->S3; uint32_t *S3 = ctx->S3;
word32 *P = ctx->P; uint32_t *P = ctx->P;
word32 t; uint32_t t;
ROUND(0); ROUND(0);
ROUND(1); ROUND(1);
@ -266,15 +266,15 @@ static void blowfish_encrypt(word32 xL, word32 xR, word32 * output,
output[1] = xL; output[1] = xL;
} }
static void blowfish_decrypt(word32 xL, word32 xR, word32 * output, static void blowfish_decrypt(uint32_t xL, uint32_t xR, uint32_t *output,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 *S0 = ctx->S0; uint32_t *S0 = ctx->S0;
word32 *S1 = ctx->S1; uint32_t *S1 = ctx->S1;
word32 *S2 = ctx->S2; uint32_t *S2 = ctx->S2;
word32 *S3 = ctx->S3; uint32_t *S3 = ctx->S3;
word32 *P = ctx->P; uint32_t *P = ctx->P;
word32 t; uint32_t t;
ROUND(17); ROUND(17);
ROUND(16); ROUND(16);
@ -302,7 +302,7 @@ static void blowfish_decrypt(word32 xL, word32 xR, word32 * output,
static void blowfish_lsb_encrypt_cbc(unsigned char *blk, int len, static void blowfish_lsb_encrypt_cbc(unsigned char *blk, int len,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 xL, xR, out[2], iv0, iv1; uint32_t xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -330,7 +330,7 @@ static void blowfish_lsb_encrypt_cbc(unsigned char *blk, int len,
void blowfish_lsb_encrypt_ecb(void *vblk, int len, BlowfishContext * ctx) void blowfish_lsb_encrypt_ecb(void *vblk, int len, BlowfishContext * ctx)
{ {
unsigned char *blk = (unsigned char *)vblk; unsigned char *blk = (unsigned char *)vblk;
word32 xL, xR, out[2]; uint32_t xL, xR, out[2];
assert((len & 7) == 0); assert((len & 7) == 0);
@ -348,7 +348,7 @@ void blowfish_lsb_encrypt_ecb(void *vblk, int len, BlowfishContext * ctx)
static void blowfish_lsb_decrypt_cbc(unsigned char *blk, int len, static void blowfish_lsb_decrypt_cbc(unsigned char *blk, int len,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 xL, xR, out[2], iv0, iv1; uint32_t xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -376,7 +376,7 @@ static void blowfish_lsb_decrypt_cbc(unsigned char *blk, int len,
static void blowfish_msb_encrypt_cbc(unsigned char *blk, int len, static void blowfish_msb_encrypt_cbc(unsigned char *blk, int len,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 xL, xR, out[2], iv0, iv1; uint32_t xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -404,7 +404,7 @@ static void blowfish_msb_encrypt_cbc(unsigned char *blk, int len,
static void blowfish_msb_decrypt_cbc(unsigned char *blk, int len, static void blowfish_msb_decrypt_cbc(unsigned char *blk, int len,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 xL, xR, out[2], iv0, iv1; uint32_t xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -432,7 +432,7 @@ static void blowfish_msb_decrypt_cbc(unsigned char *blk, int len,
static void blowfish_msb_sdctr(unsigned char *blk, int len, static void blowfish_msb_sdctr(unsigned char *blk, int len,
BlowfishContext * ctx) BlowfishContext * ctx)
{ {
word32 b[2], iv0, iv1, tmp; uint32_t b[2], iv0, iv1, tmp;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -477,12 +477,12 @@ void blowfish_expandkey(BlowfishContext * ctx,
{ {
const unsigned char *key = (const unsigned char *)vkey; const unsigned char *key = (const unsigned char *)vkey;
const unsigned char *salt = (const unsigned char *)vsalt; const unsigned char *salt = (const unsigned char *)vsalt;
word32 *S0 = ctx->S0; uint32_t *S0 = ctx->S0;
word32 *S1 = ctx->S1; uint32_t *S1 = ctx->S1;
word32 *S2 = ctx->S2; uint32_t *S2 = ctx->S2;
word32 *S3 = ctx->S3; uint32_t *S3 = ctx->S3;
word32 *P = ctx->P; uint32_t *P = ctx->P;
word32 str[2]; uint32_t str[2];
int i, j; int i, j;
int saltpos; int saltpos;
unsigned char dummysalt[1]; unsigned char dummysalt[1];
@ -496,19 +496,19 @@ void blowfish_expandkey(BlowfishContext * ctx,
for (i = 0; i < 18; i++) { for (i = 0; i < 18; i++) {
P[i] ^= P[i] ^=
((word32) (unsigned char) (key[(i * 4 + 0) % keybytes])) << 24; ((uint32_t) (unsigned char) (key[(i * 4 + 0) % keybytes])) << 24;
P[i] ^= P[i] ^=
((word32) (unsigned char) (key[(i * 4 + 1) % keybytes])) << 16; ((uint32_t) (unsigned char) (key[(i * 4 + 1) % keybytes])) << 16;
P[i] ^= P[i] ^=
((word32) (unsigned char) (key[(i * 4 + 2) % keybytes])) << 8; ((uint32_t) (unsigned char) (key[(i * 4 + 2) % keybytes])) << 8;
P[i] ^= ((word32) (unsigned char) (key[(i * 4 + 3) % keybytes])); P[i] ^= ((uint32_t) (unsigned char) (key[(i * 4 + 3) % keybytes]));
} }
str[0] = str[1] = 0; str[0] = str[1] = 0;
for (i = 0; i < 18; i += 2) { for (i = 0; i < 18; i += 2) {
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
str[j/4] ^= ((word32)salt[saltpos++ % saltbytes]) << (24-8*(j%4)); str[j/4] ^= ((uint32_t)salt[saltpos++ % saltbytes]) << (24-8*(j%4));
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
P[i] = str[0]; P[i] = str[0];
@ -517,28 +517,28 @@ void blowfish_expandkey(BlowfishContext * ctx,
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
str[j/4] ^= ((word32)salt[saltpos++ % saltbytes]) << (24-8*(j%4)); str[j/4] ^= ((uint32_t)salt[saltpos++ % saltbytes]) << (24-8*(j%4));
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S0[i] = str[0]; S0[i] = str[0];
S0[i + 1] = str[1]; S0[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
str[j/4] ^= ((word32)salt[saltpos++ % saltbytes]) << (24-8*(j%4)); str[j/4] ^= ((uint32_t)salt[saltpos++ % saltbytes]) << (24-8*(j%4));
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S1[i] = str[0]; S1[i] = str[0];
S1[i + 1] = str[1]; S1[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
str[j/4] ^= ((word32)salt[saltpos++ % saltbytes]) << (24-8*(j%4)); str[j/4] ^= ((uint32_t)salt[saltpos++ % saltbytes]) << (24-8*(j%4));
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S2[i] = str[0]; S2[i] = str[0];
S2[i + 1] = str[1]; S2[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
str[j/4] ^= ((word32)salt[saltpos++ % saltbytes]) << (24-8*(j%4)); str[j/4] ^= ((uint32_t)salt[saltpos++ % saltbytes]) << (24-8*(j%4));
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S3[i] = str[0]; S3[i] = str[0];
S3[i + 1] = str[1]; S3[i + 1] = str[1];

View File

@ -45,7 +45,7 @@ struct chacha20 {
* 4-11 are the key * 4-11 are the key
* 12-13 are the counter * 12-13 are the counter
* 14-15 are the IV */ * 14-15 are the IV */
uint32 state[16]; uint32_t state[16];
/* The output of the state above ready to xor */ /* The output of the state above ready to xor */
unsigned char current[64]; unsigned char current[64];
/* The index of the above currently used to allow a true streaming cipher */ /* The index of the above currently used to allow a true streaming cipher */
@ -55,7 +55,7 @@ struct chacha20 {
static INLINE void chacha20_round(struct chacha20 *ctx) static INLINE void chacha20_round(struct chacha20 *ctx)
{ {
int i; int i;
uint32 copy[16]; uint32_t copy[16];
/* Take a copy */ /* Take a copy */
memcpy(copy, ctx->state, sizeof(copy)); memcpy(copy, ctx->state, sizeof(copy));
@ -114,7 +114,7 @@ static INLINE void chacha20_round(struct chacha20 *ctx)
/* Increment round counter */ /* Increment round counter */
++ctx->state[12]; ++ctx->state[12];
/* Check for overflow, not done in one line so the 32 bits are chopped by the type */ /* Check for overflow, not done in one line so the 32 bits are chopped by the type */
if (!(uint32)(ctx->state[12])) { if (!(uint32_t)(ctx->state[12])) {
++ctx->state[13]; ++ctx->state[13];
} }
} }

View File

@ -25,16 +25,13 @@
#include "misc.h" #include "misc.h"
#include "ssh.h" #include "ssh.h"
typedef unsigned char uchar;
typedef unsigned short uint16;
/* SSH Constants */ /* SSH Constants */
#define SSH_MAXBLOCKS (32 * 1024) #define SSH_MAXBLOCKS (32 * 1024)
#define SSH_BLOCKSIZE (8) #define SSH_BLOCKSIZE (8)
/* Hashing constants */ /* Hashing constants */
#define HASH_MINSIZE (8 * 1024) #define HASH_MINSIZE (8 * 1024)
#define HASH_ENTRYSIZE (sizeof(uint16)) #define HASH_ENTRYSIZE (sizeof(uint16_t))
#define HASH_FACTOR(x) ((x)*3/2) #define HASH_FACTOR(x) ((x)*3/2)
#define HASH_UNUSEDCHAR (0xff) #define HASH_UNUSEDCHAR (0xff)
#define HASH_UNUSED (0xffff) #define HASH_UNUSED (0xffff)
@ -47,12 +44,12 @@ typedef unsigned short uint16;
#define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE)) #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
uchar ONE[4] = { 1, 0, 0, 0 }; uint8_t ONE[4] = { 1, 0, 0, 0 };
uchar ZERO[4] = { 0, 0, 0, 0 }; uint8_t ZERO[4] = { 0, 0, 0, 0 };
struct crcda_ctx { struct crcda_ctx {
uint16 *h; uint16_t *h;
uint32 n; uint32_t n;
}; };
struct crcda_ctx *crcda_make_context(void) struct crcda_ctx *crcda_make_context(void)
@ -72,16 +69,16 @@ void crcda_free_context(struct crcda_ctx *ctx)
} }
} }
static void crc_update(uint32 *a, void *b) static void crc_update(uint32_t *a, void *b)
{ {
*a = crc32_update(*a, b, 4); *a = crc32_update(*a, b, 4);
} }
/* detect if a block is used in a particular pattern */ /* detect if a block is used in a particular pattern */
static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV) static int check_crc(uint8_t *S, uint8_t *buf, uint32_t len, uint8_t *IV)
{ {
uint32 crc; uint32_t crc;
uchar *c; uint8_t *c;
crc = 0; crc = 0;
if (IV && !CMP(S, IV)) { if (IV && !CMP(S, IV)) {
@ -101,12 +98,13 @@ static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV)
} }
/* Detect a crc32 compensation attack on a packet */ /* Detect a crc32 compensation attack on a packet */
int detect_attack(struct crcda_ctx *ctx, uchar *buf, uint32 len, uchar *IV) int detect_attack(
struct crcda_ctx *ctx, uint8_t *buf, uint32_t len, uint8_t *IV)
{ {
register uint32 i, j; register uint32_t i, j;
uint32 l; uint32_t l;
register uchar *c; register uint8_t *c;
uchar *d; uint8_t *d;
assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) || assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
len % SSH_BLOCKSIZE != 0)); len % SSH_BLOCKSIZE != 0));
@ -115,11 +113,11 @@ int detect_attack(struct crcda_ctx *ctx, uchar *buf, uint32 len, uchar *IV)
if (ctx->h == NULL) { if (ctx->h == NULL) {
ctx->n = l; ctx->n = l;
ctx->h = snewn(ctx->n, uint16); ctx->h = snewn(ctx->n, uint16_t);
} else { } else {
if (l > ctx->n) { if (l > ctx->n) {
ctx->n = l; ctx->n = l;
ctx->h = sresize(ctx->h, ctx->n, uint16); ctx->h = sresize(ctx->h, ctx->n, uint16_t);
} }
} }

View File

@ -277,16 +277,16 @@
*/ */
typedef struct { typedef struct {
word32 k0246[16], k1357[16]; uint32_t k0246[16], k1357[16];
word32 iv0, iv1; uint32_t iv0, iv1;
} DESContext; } DESContext;
#define rotl(x, c) ( (x << c) | (x >> (32-c)) ) #define rotl(x, c) ( (x << c) | (x >> (32-c)) )
#define rotl28(x, c) ( ( (x << c) | (x >> (28-c)) ) & 0x0FFFFFFF) #define rotl28(x, c) ( ( (x << c) | (x >> (28-c)) ) & 0x0FFFFFFF)
static word32 bitsel(word32 * input, const int *bitnums, int size) static uint32_t bitsel(uint32_t *input, const int *bitnums, int size)
{ {
word32 ret = 0; uint32_t ret = 0;
while (size--) { while (size--) {
int bitpos = *bitnums++; int bitpos = *bitnums++;
ret <<= 1; ret <<= 1;
@ -296,7 +296,8 @@ static word32 bitsel(word32 * input, const int *bitnums, int size)
return ret; return ret;
} }
static void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched) static void des_key_setup(
uint32_t key_msw, uint32_t key_lsw, DESContext *sched)
{ {
static const int PC1_Cbits[] = { static const int PC1_Cbits[] = {
@ -326,8 +327,8 @@ static void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
static const int leftshifts[] = static const int leftshifts[] =
{ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
word32 C, D; uint32_t C, D;
word32 buf[2]; uint32_t buf[2];
int i; int i;
buf[0] = key_lsw; buf[0] = key_lsw;
@ -348,7 +349,7 @@ static void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
sched->iv0 = sched->iv1 = 0; sched->iv0 = sched->iv1 = 0;
} }
static const word32 SPboxes[8][64] = { static const uint32_t SPboxes[8][64] = {
{0x01010400, 0x00000000, 0x00010000, 0x01010404, {0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
@ -520,10 +521,10 @@ static const word32 SPboxes[8][64] = {
bitswap(R, L, 16, 0x0000FFFF), \ bitswap(R, L, 16, 0x0000FFFF), \
bitswap(R, L, 4, 0x0F0F0F0F)) bitswap(R, L, 4, 0x0F0F0F0F))
static void des_encipher(word32 * output, word32 L, word32 R, static void des_encipher(
DESContext * sched) uint32_t *output, uint32_t L, uint32_t R, DESContext *sched)
{ {
word32 swap, s0246, s1357; uint32_t swap, s0246, s1357;
IP(L, R); IP(L, R);
@ -560,10 +561,10 @@ static void des_encipher(word32 * output, word32 L, word32 R,
output[1] = R; output[1] = R;
} }
static void des_decipher(word32 * output, word32 L, word32 R, static void des_decipher(
DESContext * sched) uint32_t *output, uint32_t L, uint32_t R, DESContext *sched)
{ {
word32 swap, s0246, s1357; uint32_t swap, s0246, s1357;
IP(L, R); IP(L, R);
@ -603,7 +604,7 @@ static void des_decipher(word32 * output, word32 L, word32 R,
static void des_cbc_encrypt(unsigned char *blk, static void des_cbc_encrypt(unsigned char *blk,
unsigned int len, DESContext * sched) unsigned int len, DESContext * sched)
{ {
word32 out[2], iv0, iv1; uint32_t out[2], iv0, iv1;
unsigned int i; unsigned int i;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -627,7 +628,7 @@ static void des_cbc_encrypt(unsigned char *blk,
static void des_cbc_decrypt(unsigned char *blk, static void des_cbc_decrypt(unsigned char *blk,
unsigned int len, DESContext * sched) unsigned int len, DESContext * sched)
{ {
word32 out[2], iv0, iv1, xL, xR; uint32_t out[2], iv0, iv1, xL, xR;
unsigned int i; unsigned int i;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -661,7 +662,7 @@ static void des_3cbc_encrypt(unsigned char *blk,
static void des_cbc3_encrypt(unsigned char *blk, static void des_cbc3_encrypt(unsigned char *blk,
unsigned int len, DESContext * scheds) unsigned int len, DESContext * scheds)
{ {
word32 out[2], iv0, iv1; uint32_t out[2], iv0, iv1;
unsigned int i; unsigned int i;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -695,7 +696,7 @@ static void des_3cbc_decrypt(unsigned char *blk,
static void des_cbc3_decrypt(unsigned char *blk, static void des_cbc3_decrypt(unsigned char *blk,
unsigned int len, DESContext * scheds) unsigned int len, DESContext * scheds)
{ {
word32 out[2], iv0, iv1, xL, xR; uint32_t out[2], iv0, iv1, xL, xR;
unsigned int i; unsigned int i;
assert((len & 7) == 0); assert((len & 7) == 0);
@ -723,7 +724,7 @@ static void des_cbc3_decrypt(unsigned char *blk,
static void des_sdctr3(unsigned char *blk, static void des_sdctr3(unsigned char *blk,
unsigned int len, DESContext * scheds) unsigned int len, DESContext * scheds)
{ {
word32 b[2], iv0, iv1, tmp; uint32_t b[2], iv0, iv1, tmp;
unsigned int i; unsigned int i;
assert((len & 7) == 0); assert((len & 7) == 0);

View File

@ -15,7 +15,7 @@
#define H(x,y,z) ( (x) ^ (y) ^ (z) ) #define H(x,y,z) ( (x) ^ (y) ^ (z) )
#define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) ) #define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) )
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) ) #define rol(x,y) ( ((x) << (y)) | (((uint32_t)x) >> (32-y)) )
#define subround(f,w,x,y,z,k,s,ti) \ #define subround(f,w,x,y,z,k,s,ti) \
w = x + rol(w + f(x,y,z) + block[k] + ti, s) w = x + rol(w + f(x,y,z) + block[k] + ti, s)
@ -28,9 +28,9 @@ static void MD5_Core_Init(MD5_Core_State * s)
s->h[3] = 0x10325476; s->h[3] = 0x10325476;
} }
static void MD5_Block(MD5_Core_State * s, uint32 * block) static void MD5_Block(MD5_Core_State *s, uint32_t *block)
{ {
uint32 a, b, c, d; uint32_t a, b, c, d;
a = s->h[0]; a = s->h[0];
b = s->h[1]; b = s->h[1];
@ -122,7 +122,7 @@ void MD5Init(struct MD5Context *s)
{ {
MD5_Core_Init(&s->core); MD5_Core_Init(&s->core);
s->blkused = 0; s->blkused = 0;
s->lenhi = s->lenlo = 0; s->len = 0;
BinarySink_INIT(s, MD5_BinarySink_write); BinarySink_INIT(s, MD5_BinarySink_write);
} }
@ -130,8 +130,8 @@ static void MD5_BinarySink_write(BinarySink *bs, const void *data, size_t len)
{ {
struct MD5Context *s = BinarySink_DOWNCAST(bs, struct MD5Context); struct MD5Context *s = BinarySink_DOWNCAST(bs, struct MD5Context);
const unsigned char *q = (const unsigned char *)data; const unsigned char *q = (const unsigned char *)data;
uint32 wordblock[16]; uint32_t wordblock[16];
uint32 lenw = len; uint32_t lenw = len;
int i; int i;
assert(lenw == len); assert(lenw == len);
@ -139,8 +139,7 @@ static void MD5_BinarySink_write(BinarySink *bs, const void *data, size_t len)
/* /*
* Update the length field. * Update the length field.
*/ */
s->lenlo += lenw; s->len += lenw;
s->lenhi += (s->lenlo < lenw);
if (s->blkused + len < BLKSIZE) { if (s->blkused + len < BLKSIZE) {
/* /*
@ -159,10 +158,10 @@ static void MD5_BinarySink_write(BinarySink *bs, const void *data, size_t len)
/* Now process the block. Gather bytes little-endian into words */ /* Now process the block. Gather bytes little-endian into words */
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {
wordblock[i] = wordblock[i] =
(((uint32) s->block[i * 4 + 3]) << 24) | (((uint32_t) s->block[i * 4 + 3]) << 24) |
(((uint32) s->block[i * 4 + 2]) << 16) | (((uint32_t) s->block[i * 4 + 2]) << 16) |
(((uint32) s->block[i * 4 + 1]) << 8) | (((uint32_t) s->block[i * 4 + 1]) << 8) |
(((uint32) s->block[i * 4 + 0]) << 0); (((uint32_t) s->block[i * 4 + 0]) << 0);
} }
MD5_Block(&s->core, wordblock); MD5_Block(&s->core, wordblock);
s->blkused = 0; s->blkused = 0;
@ -177,28 +176,20 @@ void MD5Final(unsigned char output[16], struct MD5Context *s)
int i; int i;
unsigned pad; unsigned pad;
unsigned char c[64]; unsigned char c[64];
uint32 lenhi, lenlo; uint64_t len;
if (s->blkused >= 56) if (s->blkused >= 56)
pad = 56 + 64 - s->blkused; pad = 56 + 64 - s->blkused;
else else
pad = 56 - s->blkused; pad = 56 - s->blkused;
lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3)); len = (s->len << 3);
lenlo = (s->lenlo << 3);
memset(c, 0, pad); memset(c, 0, pad);
c[0] = 0x80; c[0] = 0x80;
put_data(s, c, pad); put_data(s, c, pad);
c[7] = (lenhi >> 24) & 0xFF; PUT_64BIT_LSB_FIRST(c, len);
c[6] = (lenhi >> 16) & 0xFF;
c[5] = (lenhi >> 8) & 0xFF;
c[4] = (lenhi >> 0) & 0xFF;
c[3] = (lenlo >> 24) & 0xFF;
c[2] = (lenlo >> 16) & 0xFF;
c[1] = (lenlo >> 8) & 0xFF;
c[0] = (lenlo >> 0) & 0xFF;
put_data(s, c, 8); put_data(s, c, 8);

View File

@ -70,8 +70,8 @@ int random_diagnostics = 0;
static void random_stir(void) static void random_stir(void)
{ {
word32 block[HASHINPUT / sizeof(word32)]; uint32_t block[HASHINPUT / sizeof(uint32_t)];
word32 digest[HASHSIZE / sizeof(word32)]; uint32_t digest[HASHSIZE / sizeof(uint32_t)];
int i, j, k; int i, j, k;
/* /*
@ -91,24 +91,24 @@ static void random_stir(void)
for (p = 0; p < POOLSIZE; p += HASHSIZE) { for (p = 0; p < POOLSIZE; p += HASHSIZE) {
printf(" "); printf(" ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.pool + p + q)); printf(" %08x", *(uint32_t *)(pool.pool + p + q));
} }
printf("\n"); printf("\n");
} }
printf("incoming:\n "); printf("incoming:\n ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.incoming + q)); printf(" %08x", *(uint32_t *)(pool.incoming + q));
} }
printf("\nincomingb:\n "); printf("\nincomingb:\n ");
for (q = 0; q < HASHINPUT; q += 4) { for (q = 0; q < HASHINPUT; q += 4) {
printf(" %08x", *(word32 *)(pool.incomingb + q)); printf(" %08x", *(uint32_t *)(pool.incomingb + q));
} }
printf("\n"); printf("\n");
random_diagnostics++; random_diagnostics++;
} }
#endif #endif
SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb); SHATransform((uint32_t *) pool.incoming, (uint32_t *) pool.incomingb);
pool.incomingpos = 0; pool.incomingpos = 0;
/* /*
@ -144,7 +144,7 @@ static void random_stir(void)
*/ */
for (k = 0; k < sizeof(digest) / sizeof(*digest); k++) for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
digest[k] ^= ((word32 *) (pool.pool + j))[k]; digest[k] ^= ((uint32_t *) (pool.pool + j))[k];
/* /*
* Munge our unrevealed first block of the pool into * Munge our unrevealed first block of the pool into
@ -157,7 +157,7 @@ static void random_stir(void)
*/ */
for (k = 0; k < sizeof(digest) / sizeof(*digest); k++) for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
((word32 *) (pool.pool + j))[k] = digest[k]; ((uint32_t *) (pool.pool + j))[k] = digest[k];
} }
#ifdef RANDOM_DIAGNOSTICS #ifdef RANDOM_DIAGNOSTICS
@ -167,17 +167,17 @@ static void random_stir(void)
for (p = 0; p < POOLSIZE; p += HASHSIZE) { for (p = 0; p < POOLSIZE; p += HASHSIZE) {
printf(" "); printf(" ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.pool + p + q)); printf(" %08x", *(uint32_t *)(pool.pool + p + q));
} }
printf("\n"); printf("\n");
} }
printf("incoming:\n "); printf("incoming:\n ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.incoming + q)); printf(" %08x", *(uint32_t *)(pool.incoming + q));
} }
printf("\nincomingb:\n "); printf("\nincomingb:\n ");
for (q = 0; q < HASHINPUT; q += 4) { for (q = 0; q < HASHINPUT; q += 4) {
printf(" %08x", *(word32 *)(pool.incomingb + q)); printf(" %08x", *(uint32_t *)(pool.incomingb + q));
} }
printf("\n"); printf("\n");
} }
@ -202,17 +202,17 @@ static void random_stir(void)
for (p = 0; p < POOLSIZE; p += HASHSIZE) { for (p = 0; p < POOLSIZE; p += HASHSIZE) {
printf(" "); printf(" ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.pool + p + q)); printf(" %08x", *(uint32_t *)(pool.pool + p + q));
} }
printf("\n"); printf("\n");
} }
printf("incoming:\n "); printf("incoming:\n ");
for (q = 0; q < HASHSIZE; q += 4) { for (q = 0; q < HASHSIZE; q += 4) {
printf(" %08x", *(word32 *)(pool.incoming + q)); printf(" %08x", *(uint32_t *)(pool.incoming + q));
} }
printf("\nincomingb:\n "); printf("\nincomingb:\n ");
for (q = 0; q < HASHINPUT; q += 4) { for (q = 0; q < HASHINPUT; q += 4) {
printf(" %08x", *(word32 *)(pool.incomingb + q)); printf(" %08x", *(uint32_t *)(pool.incomingb + q));
} }
printf("\n"); printf("\n");
random_diagnostics--; random_diagnostics--;
@ -238,7 +238,7 @@ void random_add_noise(void *noise, int length)
HASHINPUT - pool.incomingpos); HASHINPUT - pool.incomingpos);
p += HASHINPUT - pool.incomingpos; p += HASHINPUT - pool.incomingpos;
length -= HASHINPUT - pool.incomingpos; length -= HASHINPUT - pool.incomingpos;
SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb); SHATransform((uint32_t *) pool.incoming, (uint32_t *) pool.incomingb);
for (i = 0; i < HASHSIZE; i++) { for (i = 0; i < HASHSIZE; i++) {
pool.pool[pool.poolpos++] ^= pool.incoming[i]; pool.pool[pool.poolpos++] ^= pool.incoming[i];
if (pool.poolpos >= POOLSIZE) if (pool.poolpos >= POOLSIZE)

View File

@ -11,8 +11,8 @@
* Core SHA256 algorithm: processes 16-word blocks into a message digest. * Core SHA256 algorithm: processes 16-word blocks into a message digest.
*/ */
#define ror(x,y) ( ((x) << (32-y)) | (((uint32)(x)) >> (y)) ) #define ror(x,y) ( ((x) << (32-y)) | (((uint32_t)(x)) >> (y)) )
#define shr(x,y) ( (((uint32)(x)) >> (y)) ) #define shr(x,y) ( (((uint32_t)(x)) >> (y)) )
#define Ch(x,y,z) ( ((x) & (y)) ^ (~(x) & (z)) ) #define Ch(x,y,z) ( ((x) & (y)) ^ (~(x) & (z)) )
#define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) ) #define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) )
#define bigsigma0(x) ( ror((x),2) ^ ror((x),13) ^ ror((x),22) ) #define bigsigma0(x) ( ror((x),2) ^ ror((x),13) ^ ror((x),22) )
@ -34,9 +34,9 @@ void SHA256_Core_Init(SHA256_State *s) {
s->h[7] = 0x5be0cd19; s->h[7] = 0x5be0cd19;
} }
void SHA256_Block(SHA256_State *s, uint32 *block) { void SHA256_Block(SHA256_State *s, uint32_t *block) {
uint32 w[80]; uint32_t w[80];
uint32 a,b,c,d,e,f,g,h; uint32_t a,b,c,d,e,f,g,h;
static const int k[] = { static const int k[] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
@ -68,7 +68,7 @@ void SHA256_Block(SHA256_State *s, uint32 *block) {
e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7]; e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
for (t = 0; t < 64; t+=8) { for (t = 0; t < 64; t+=8) {
uint32 t1, t2; uint32_t t1, t2;
#define ROUND(j,a,b,c,d,e,f,g,h) \ #define ROUND(j,a,b,c,d,e,f,g,h) \
t1 = h + bigsigma1(e) + Ch(e,f,g) + k[j] + w[j]; \ t1 = h + bigsigma1(e) + Ch(e,f,g) + k[j] + w[j]; \
@ -103,7 +103,7 @@ static void SHA256_BinarySink_write(BinarySink *bs,
void SHA256_Init(SHA256_State *s) { void SHA256_Init(SHA256_State *s) {
SHA256_Core_Init(s); SHA256_Core_Init(s);
s->blkused = 0; s->blkused = 0;
s->lenhi = s->lenlo = 0; s->len = 0;
if (supports_sha_ni()) if (supports_sha_ni())
s->sha256 = &SHA256_ni; s->sha256 = &SHA256_ni;
else else
@ -117,19 +117,16 @@ static void SHA256_BinarySink_write(BinarySink *bs,
struct SHA256_State *s = BinarySink_DOWNCAST(bs, struct SHA256_State); struct SHA256_State *s = BinarySink_DOWNCAST(bs, struct SHA256_State);
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
uint32 lenw = len;
assert(len == lenw);
/* /*
* Update the length field. * Update the length field.
*/ */
s->lenlo += lenw; s->len += len;
s->lenhi += (s->lenlo < lenw);
(*(s->sha256))(s, q, len); (*(s->sha256))(s, q, len);
} }
static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len) { static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len) {
uint32 wordblock[16]; uint32_t wordblock[16];
int i; int i;
if (s->blkused && s->blkused+len < BLKSIZE) { if (s->blkused && s->blkused+len < BLKSIZE) {
@ -149,10 +146,10 @@ static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len) {
/* Now process the block. Gather bytes big-endian into words */ /* Now process the block. Gather bytes big-endian into words */
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {
wordblock[i] = wordblock[i] =
( ((uint32)s->block[i*4+0]) << 24 ) | ( ((uint32_t)s->block[i*4+0]) << 24 ) |
( ((uint32)s->block[i*4+1]) << 16 ) | ( ((uint32_t)s->block[i*4+1]) << 16 ) |
( ((uint32)s->block[i*4+2]) << 8 ) | ( ((uint32_t)s->block[i*4+2]) << 8 ) |
( ((uint32)s->block[i*4+3]) << 0 ); ( ((uint32_t)s->block[i*4+3]) << 0 );
} }
SHA256_Block(s, wordblock); SHA256_Block(s, wordblock);
s->blkused = 0; s->blkused = 0;
@ -166,22 +163,20 @@ void SHA256_Final(SHA256_State *s, unsigned char *digest) {
int i; int i;
int pad; int pad;
unsigned char c[64]; unsigned char c[64];
uint32 lenhi, lenlo; uint64_t len;
if (s->blkused >= 56) if (s->blkused >= 56)
pad = 56 + 64 - s->blkused; pad = 56 + 64 - s->blkused;
else else
pad = 56 - s->blkused; pad = 56 - s->blkused;
lenhi = (s->lenhi << 3) | (s->lenlo >> (32-3)); len = (s->len << 3);
lenlo = (s->lenlo << 3);
memset(c, 0, pad); memset(c, 0, pad);
c[0] = 0x80; c[0] = 0x80;
put_data(s, &c, pad); put_data(s, &c, pad);
put_uint32(s, lenhi); put_uint64(s, len);
put_uint32(s, lenlo);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
digest[i*4+0] = (s->h[i] >> 24) & 0xFF; digest[i*4+0] = (s->h[i] >> 24) & 0xFF;

View File

@ -15,21 +15,17 @@
* Arithmetic implementations. Note that AND, XOR and NOT can * Arithmetic implementations. Note that AND, XOR and NOT can
* overlap destination with one source, but the others can't. * overlap destination with one source, but the others can't.
*/ */
#define add(r,x,y) ( r.lo = y.lo + x.lo, \ #define add(r,x,y) ( r = (x) + (y) )
r.hi = y.hi + x.hi + ((uint32)r.lo < (uint32)y.lo) ) #define rorB(r,x,y) ( r = ((x) >> (y)) | ((x) << (64-(y))) )
#define rorB(r,x,y) ( r.lo = ((uint32)x.hi >> ((y)-32)) | ((uint32)x.lo << (64-(y))), \ #define rorL(r,x,y) ( r = ((x) >> (y)) | ((x) << (64-(y))) )
r.hi = ((uint32)x.lo >> ((y)-32)) | ((uint32)x.hi << (64-(y))) ) #define shrB(r,x,y) ( r = (x) >> (y) )
#define rorL(r,x,y) ( r.lo = ((uint32)x.lo >> (y)) | ((uint32)x.hi << (32-(y))), \ #define shrL(r,x,y) ( r = (x) >> (y) )
r.hi = ((uint32)x.hi >> (y)) | ((uint32)x.lo << (32-(y))) ) #define and(r,x,y) ( r = (x) & (y) )
#define shrB(r,x,y) ( r.lo = (uint32)x.hi >> ((y)-32), r.hi = 0 ) #define xor(r,x,y) ( r = (x) ^ (y) )
#define shrL(r,x,y) ( r.lo = ((uint32)x.lo >> (y)) | ((uint32)x.hi << (32-(y))), \ #define not(r,x) ( r = ~(x) )
r.hi = (uint32)x.hi >> (y) ) #define INIT(h,l) ((((uint64_t)(h)) << 32) | (l))
#define and(r,x,y) ( r.lo = x.lo & y.lo, r.hi = x.hi & y.hi ) #define BUILD(r,h,l) ( r = ((((uint64_t)(h)) << 32) | (l)) )
#define xor(r,x,y) ( r.lo = x.lo ^ y.lo, r.hi = x.hi ^ y.hi ) #define EXTRACT(h,l,r) ( h = (r) >> 32, l = (r) & 0xFFFFFFFFU )
#define not(r,x) ( r.lo = ~x.lo, r.hi = ~x.hi )
#define INIT(h,l) { h, l }
#define BUILD(r,h,l) ( r.hi = h, r.lo = l )
#define EXTRACT(h,l,r) ( h = r.hi, l = r.lo )
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* Core SHA512 algorithm: processes 16-doubleword blocks into a * Core SHA512 algorithm: processes 16-doubleword blocks into a
@ -49,7 +45,7 @@
shrL(t,x,6), xor(r,r,t) ) shrL(t,x,6), xor(r,r,t) )
static void SHA512_Core_Init(SHA512_State *s) { static void SHA512_Core_Init(SHA512_State *s) {
static const uint64 iv[] = { static const uint64_t iv[] = {
INIT(0x6a09e667, 0xf3bcc908), INIT(0x6a09e667, 0xf3bcc908),
INIT(0xbb67ae85, 0x84caa73b), INIT(0xbb67ae85, 0x84caa73b),
INIT(0x3c6ef372, 0xfe94f82b), INIT(0x3c6ef372, 0xfe94f82b),
@ -65,7 +61,7 @@ static void SHA512_Core_Init(SHA512_State *s) {
} }
static void SHA384_Core_Init(SHA512_State *s) { static void SHA384_Core_Init(SHA512_State *s) {
static const uint64 iv[] = { static const uint64_t iv[] = {
INIT(0xcbbb9d5d, 0xc1059ed8), INIT(0xcbbb9d5d, 0xc1059ed8),
INIT(0x629a292a, 0x367cd507), INIT(0x629a292a, 0x367cd507),
INIT(0x9159015a, 0x3070dd17), INIT(0x9159015a, 0x3070dd17),
@ -80,10 +76,10 @@ static void SHA384_Core_Init(SHA512_State *s) {
s->h[i] = iv[i]; s->h[i] = iv[i];
} }
static void SHA512_Block(SHA512_State *s, uint64 *block) { static void SHA512_Block(SHA512_State *s, uint64_t *block) {
uint64 w[80]; uint64_t w[80];
uint64 a,b,c,d,e,f,g,h; uint64_t a,b,c,d,e,f,g,h;
static const uint64 k[] = { static const uint64_t k[] = {
INIT(0x428a2f98, 0xd728ae22), INIT(0x71374491, 0x23ef65cd), INIT(0x428a2f98, 0xd728ae22), INIT(0x71374491, 0x23ef65cd),
INIT(0xb5c0fbcf, 0xec4d3b2f), INIT(0xe9b5dba5, 0x8189dbbc), INIT(0xb5c0fbcf, 0xec4d3b2f), INIT(0xe9b5dba5, 0x8189dbbc),
INIT(0x3956c25b, 0xf348b538), INIT(0x59f111f1, 0xb605d019), INIT(0x3956c25b, 0xf348b538), INIT(0x59f111f1, 0xb605d019),
@ -132,7 +128,7 @@ static void SHA512_Block(SHA512_State *s, uint64 *block) {
w[t] = block[t]; w[t] = block[t];
for (t = 16; t < 80; t++) { for (t = 16; t < 80; t++) {
uint64 p, q, r, tmp; uint64_t p, q, r, tmp;
smallsigma1(p, tmp, w[t-2]); smallsigma1(p, tmp, w[t-2]);
smallsigma0(q, tmp, w[t-15]); smallsigma0(q, tmp, w[t-15]);
add(r, p, q); add(r, p, q);
@ -144,7 +140,7 @@ static void SHA512_Block(SHA512_State *s, uint64 *block) {
e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7]; e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
for (t = 0; t < 80; t+=8) { for (t = 0; t < 80; t+=8) {
uint64 tmp, p, q, r; uint64_t tmp, p, q, r;
#define ROUND(j,a,b,c,d,e,f,g,h) \ #define ROUND(j,a,b,c,d,e,f,g,h) \
bigsigma1(p, tmp, e); \ bigsigma1(p, tmp, e); \
@ -171,7 +167,7 @@ static void SHA512_Block(SHA512_State *s, uint64 *block) {
} }
{ {
uint64 tmp; uint64_t tmp;
#define UPDATE(state, local) ( tmp = state, add(state, tmp, local) ) #define UPDATE(state, local) ( tmp = state, add(state, tmp, local) )
UPDATE(s->h[0], a); UPDATE(s->h[1], b); UPDATE(s->h[0], a); UPDATE(s->h[1], b);
UPDATE(s->h[2], c); UPDATE(s->h[3], d); UPDATE(s->h[2], c); UPDATE(s->h[3], d);
@ -190,20 +186,16 @@ static void SHA512_BinarySink_write(BinarySink *bs,
const void *p, size_t len); const void *p, size_t len);
void SHA512_Init(SHA512_State *s) { void SHA512_Init(SHA512_State *s) {
int i;
SHA512_Core_Init(s); SHA512_Core_Init(s);
s->blkused = 0; s->blkused = 0;
for (i = 0; i < 4; i++) s->lenhi = s->lenlo = 0;
s->len[i] = 0;
BinarySink_INIT(s, SHA512_BinarySink_write); BinarySink_INIT(s, SHA512_BinarySink_write);
} }
void SHA384_Init(SHA512_State *s) { void SHA384_Init(SHA512_State *s) {
int i;
SHA384_Core_Init(s); SHA384_Core_Init(s);
s->blkused = 0; s->blkused = 0;
for (i = 0; i < 4; i++) s->lenhi = s->lenlo = 0;
s->len[i] = 0;
BinarySink_INIT(s, SHA512_BinarySink_write); BinarySink_INIT(s, SHA512_BinarySink_write);
} }
@ -212,19 +204,14 @@ static void SHA512_BinarySink_write(BinarySink *bs,
{ {
SHA512_State *s = BinarySink_DOWNCAST(bs, SHA512_State); SHA512_State *s = BinarySink_DOWNCAST(bs, SHA512_State);
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
uint64 wordblock[16]; uint64_t wordblock[16];
uint32 lenw = len;
int i; int i;
assert(lenw == len);
/* /*
* Update the length field. * Update the length field.
*/ */
for (i = 0; i < 4; i++) { s->lenlo += len;
s->len[i] += lenw; s->lenhi += (s->lenlo < len);
lenw = (s->len[i] < lenw);
}
if (s->blkused && s->blkused+len < BLKSIZE) { if (s->blkused && s->blkused+len < BLKSIZE) {
/* /*
@ -241,18 +228,8 @@ static void SHA512_BinarySink_write(BinarySink *bs,
q += BLKSIZE - s->blkused; q += BLKSIZE - s->blkused;
len -= BLKSIZE - s->blkused; len -= BLKSIZE - s->blkused;
/* Now process the block. Gather bytes big-endian into words */ /* Now process the block. Gather bytes big-endian into words */
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++)
uint32 h, l; wordblock[i] = GET_64BIT_MSB_FIRST(s->block + i*8);
h = ( ((uint32)s->block[i*8+0]) << 24 ) |
( ((uint32)s->block[i*8+1]) << 16 ) |
( ((uint32)s->block[i*8+2]) << 8 ) |
( ((uint32)s->block[i*8+3]) << 0 );
l = ( ((uint32)s->block[i*8+4]) << 24 ) |
( ((uint32)s->block[i*8+5]) << 16 ) |
( ((uint32)s->block[i*8+6]) << 8 ) |
( ((uint32)s->block[i*8+7]) << 0 );
BUILD(wordblock[i], h, l);
}
SHA512_Block(s, wordblock); SHA512_Block(s, wordblock);
s->blkused = 0; s->blkused = 0;
} }
@ -265,38 +242,25 @@ void SHA512_Final(SHA512_State *s, unsigned char *digest) {
int i; int i;
int pad; int pad;
unsigned char c[BLKSIZE]; unsigned char c[BLKSIZE];
uint32 len[4]; uint64_t lenhi, lenlo;
if (s->blkused >= BLKSIZE-16) if (s->blkused >= BLKSIZE-16)
pad = (BLKSIZE-16) + BLKSIZE - s->blkused; pad = (BLKSIZE-16) + BLKSIZE - s->blkused;
else else
pad = (BLKSIZE-16) - s->blkused; pad = (BLKSIZE-16) - s->blkused;
for (i = 4; i-- ;) { lenhi = (s->lenhi << 3) | (s->lenlo >> (32-3));
uint32 lenhi = s->len[i]; lenlo = (s->lenlo << 3);
uint32 lenlo = i > 0 ? s->len[i-1] : 0;
len[i] = (lenhi << 3) | (lenlo >> (32-3));
}
memset(c, 0, pad); memset(c, 0, pad);
c[0] = 0x80; c[0] = 0x80;
put_data(s, &c, pad); put_data(s, &c, pad);
for (i = 0; i < 4; i++) put_uint64(s, lenhi);
put_uint32(s, len[3-i]); put_uint64(s, lenlo);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++)
uint32 h, l; PUT_64BIT_MSB_FIRST(digest + i*8, s->h[i]);
EXTRACT(h, l, s->h[i]);
digest[i*8+0] = (h >> 24) & 0xFF;
digest[i*8+1] = (h >> 16) & 0xFF;
digest[i*8+2] = (h >> 8) & 0xFF;
digest[i*8+3] = (h >> 0) & 0xFF;
digest[i*8+4] = (l >> 24) & 0xFF;
digest[i*8+5] = (l >> 16) & 0xFF;
digest[i*8+6] = (l >> 8) & 0xFF;
digest[i*8+7] = (l >> 0) & 0xFF;
}
} }
void SHA384_Final(SHA512_State *s, unsigned char *digest) { void SHA384_Final(SHA512_State *s, unsigned char *digest) {

View File

@ -13,12 +13,12 @@
* Core SHA algorithm: processes 16-word blocks into a message digest. * Core SHA algorithm: processes 16-word blocks into a message digest.
*/ */
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) ) #define rol(x,y) ( ((x) << (y)) | (((uint32_t)x) >> (32-y)) )
static void sha1_sw(SHA_State * s, const unsigned char *q, int len); static void sha1_sw(SHA_State * s, const unsigned char *q, int len);
static void sha1_ni(SHA_State * s, const unsigned char *q, int len); static void sha1_ni(SHA_State * s, const unsigned char *q, int len);
static void SHA_Core_Init(uint32 h[5]) static void SHA_Core_Init(uint32_t h[5])
{ {
h[0] = 0x67452301; h[0] = 0x67452301;
h[1] = 0xefcdab89; h[1] = 0xefcdab89;
@ -27,10 +27,10 @@ static void SHA_Core_Init(uint32 h[5])
h[4] = 0xc3d2e1f0; h[4] = 0xc3d2e1f0;
} }
void SHATransform(word32 * digest, word32 * block) void SHATransform(uint32_t * digest, uint32_t * block)
{ {
word32 w[80]; uint32_t w[80];
word32 a, b, c, d, e; uint32_t a, b, c, d, e;
int t; int t;
#ifdef RANDOM_DIAGNOSTICS #ifdef RANDOM_DIAGNOSTICS
@ -52,7 +52,7 @@ void SHATransform(word32 * digest, word32 * block)
w[t] = block[t]; w[t] = block[t];
for (t = 16; t < 80; t++) { for (t = 16; t < 80; t++) {
word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]; uint32_t tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
w[t] = rol(tmp, 1); w[t] = rol(tmp, 1);
} }
@ -63,7 +63,7 @@ void SHATransform(word32 * digest, word32 * block)
e = digest[4]; e = digest[4];
for (t = 0; t < 20; t++) { for (t = 0; t < 20; t++) {
word32 tmp = uint32_t tmp =
rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999; rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
e = d; e = d;
d = c; d = c;
@ -72,7 +72,7 @@ void SHATransform(word32 * digest, word32 * block)
a = tmp; a = tmp;
} }
for (t = 20; t < 40; t++) { for (t = 20; t < 40; t++) {
word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1; uint32_t tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
e = d; e = d;
d = c; d = c;
c = rol(b, 30); c = rol(b, 30);
@ -80,7 +80,7 @@ void SHATransform(word32 * digest, word32 * block)
a = tmp; a = tmp;
} }
for (t = 40; t < 60; t++) { for (t = 40; t < 60; t++) {
word32 tmp = rol(a, uint32_t tmp = rol(a,
5) + ((b & c) | (b & d) | (c & d)) + e + w[t] + 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
0x8f1bbcdc; 0x8f1bbcdc;
e = d; e = d;
@ -90,7 +90,7 @@ void SHATransform(word32 * digest, word32 * block)
a = tmp; a = tmp;
} }
for (t = 60; t < 80; t++) { for (t = 60; t < 80; t++) {
word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6; uint32_t tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
e = d; e = d;
d = c; d = c;
c = rol(b, 30); c = rol(b, 30);
@ -130,7 +130,7 @@ void SHA_Init(SHA_State * s)
{ {
SHA_Core_Init(s->h); SHA_Core_Init(s->h);
s->blkused = 0; s->blkused = 0;
s->lenhi = s->lenlo = 0; s->len = 0;
if (supports_sha_ni()) if (supports_sha_ni())
s->sha1 = &sha1_ni; s->sha1 = &sha1_ni;
else else
@ -142,20 +142,18 @@ static void SHA_BinarySink_write(BinarySink *bs, const void *p, size_t len)
{ {
struct SHA_State *s = BinarySink_DOWNCAST(bs, struct SHA_State); struct SHA_State *s = BinarySink_DOWNCAST(bs, struct SHA_State);
const unsigned char *q = (const unsigned char *) p; const unsigned char *q = (const unsigned char *) p;
uint32 lenw = len;
assert(lenw == len);
/* /*
* Update the length field. * Update the length field.
*/ */
s->lenlo += lenw; s->len += len;
s->lenhi += (s->lenlo < lenw);
(*(s->sha1))(s, q, len); (*(s->sha1))(s, q, len);
} }
static void sha1_sw(SHA_State * s, const unsigned char *q, int len) static void sha1_sw(SHA_State * s, const unsigned char *q, int len)
{ {
uint32 wordblock[16]; uint32_t wordblock[16];
int i; int i;
if (s->blkused && s->blkused + len < 64) { if (s->blkused && s->blkused + len < 64) {
@ -175,10 +173,10 @@ static void sha1_sw(SHA_State * s, const unsigned char *q, int len)
/* Now process the block. Gather bytes big-endian into words */ /* Now process the block. Gather bytes big-endian into words */
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {
wordblock[i] = wordblock[i] =
(((uint32) s->block[i * 4 + 0]) << 24) | (((uint32_t) s->block[i * 4 + 0]) << 24) |
(((uint32) s->block[i * 4 + 1]) << 16) | (((uint32_t) s->block[i * 4 + 1]) << 16) |
(((uint32) s->block[i * 4 + 2]) << 8) | (((uint32_t) s->block[i * 4 + 2]) << 8) |
(((uint32) s->block[i * 4 + 3]) << 0); (((uint32_t) s->block[i * 4 + 3]) << 0);
} }
SHATransform(s->h, wordblock); SHATransform(s->h, wordblock);
s->blkused = 0; s->blkused = 0;
@ -193,22 +191,20 @@ void SHA_Final(SHA_State * s, unsigned char *output)
int i; int i;
int pad; int pad;
unsigned char c[64]; unsigned char c[64];
uint32 lenhi, lenlo; uint64_t len;
if (s->blkused >= 56) if (s->blkused >= 56)
pad = 56 + 64 - s->blkused; pad = 56 + 64 - s->blkused;
else else
pad = 56 - s->blkused; pad = 56 - s->blkused;
lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3)); len = (s->len << 3);
lenlo = (s->lenlo << 3);
memset(c, 0, pad); memset(c, 0, pad);
c[0] = 0x80; c[0] = 0x80;
put_data(s, &c, pad); put_data(s, &c, pad);
put_uint32(s, lenhi); put_uint64(s, len);
put_uint32(s, lenlo);
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
output[i * 4] = (s->h[i] >> 24) & 0xFF; output[i * 4] = (s->h[i] >> 24) & 0xFF;

View File

@ -20,7 +20,6 @@
#include "putty.h" #include "putty.h"
#include "ssh.h" #include "ssh.h"
#include "psftp.h" #include "psftp.h"
#include "int64.h"
/* /*
* In PSFTP our selects are synchronous, so these functions are * In PSFTP our selects are synchronous, so these functions are
@ -118,7 +117,7 @@ struct RFile {
int fd; int fd;
}; };
RFile *open_existing_file(const char *name, uint64 *size, RFile *open_existing_file(const char *name, uint64_t *size,
unsigned long *mtime, unsigned long *atime, unsigned long *mtime, unsigned long *atime,
long *perms) long *perms)
{ {
@ -140,8 +139,7 @@ RFile *open_existing_file(const char *name, uint64 *size,
} }
if (size) if (size)
*size = uint64_make((statbuf.st_size >> 16) >> 16, *size = statbuf.st_size;
statbuf.st_size);
if (mtime) if (mtime)
*mtime = statbuf.st_mtime; *mtime = statbuf.st_mtime;
@ -190,7 +188,7 @@ WFile *open_new_file(const char *name, long perms)
} }
WFile *open_existing_wfile(const char *name, uint64 *size) WFile *open_existing_wfile(const char *name, uint64_t *size)
{ {
int fd; int fd;
WFile *ret; WFile *ret;
@ -210,8 +208,7 @@ WFile *open_existing_wfile(const char *name, uint64 *size)
memset(&statbuf, 0, sizeof(statbuf)); memset(&statbuf, 0, sizeof(statbuf));
} }
*size = uint64_make((statbuf.st_size >> 16) >> 16, *size = statbuf.st_size;
statbuf.st_size);
} }
return ret; return ret;
@ -260,13 +257,10 @@ void close_wfile(WFile *f)
/* Seek offset bytes through file, from whence, where whence is /* Seek offset bytes through file, from whence, where whence is
FROM_START, FROM_CURRENT, or FROM_END */ FROM_START, FROM_CURRENT, or FROM_END */
int seek_file(WFile *f, uint64 offset, int whence) int seek_file(WFile *f, uint64_t offset, int whence)
{ {
off_t fileofft;
int lseek_whence; int lseek_whence;
fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
switch (whence) { switch (whence) {
case FROM_START: case FROM_START:
lseek_whence = SEEK_SET; lseek_whence = SEEK_SET;
@ -281,19 +275,12 @@ int seek_file(WFile *f, uint64 offset, int whence)
return -1; return -1;
} }
return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1; return lseek(f->fd, offset, lseek_whence) >= 0 ? 0 : -1;
} }
uint64 get_file_posn(WFile *f) uint64_t get_file_posn(WFile *f)
{ {
off_t fileofft; return lseek(f->fd, (off_t) 0, SEEK_CUR);
uint64 ret;
fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
ret = uint64_make((fileofft >> 16) >> 16, fileofft);
return ret;
} }
int file_type(const char *name) int file_type(const char *name)

View File

@ -356,11 +356,6 @@ static void uss_rename(SftpServer *srv, SftpReplyBuilder *reply,
} }
} }
static uint64 uint64_from_off_t(off_t off)
{
return uint64_make((off >> 16) >> 16, (off & 0xFFFFFFFFU));
}
static struct fxp_attrs uss_translate_struct_stat(const struct stat *st) static struct fxp_attrs uss_translate_struct_stat(const struct stat *st)
{ {
struct fxp_attrs attrs; struct fxp_attrs attrs;
@ -370,7 +365,7 @@ static struct fxp_attrs uss_translate_struct_stat(const struct stat *st)
SSH_FILEXFER_ATTR_UIDGID | SSH_FILEXFER_ATTR_UIDGID |
SSH_FILEXFER_ATTR_ACMODTIME); SSH_FILEXFER_ATTR_ACMODTIME);
attrs.size = uint64_from_off_t(st->st_size); attrs.size = st->st_size;
attrs.permissions = st->st_mode; attrs.permissions = st->st_mode;
attrs.uid = st->st_uid; attrs.uid = st->st_uid;
attrs.gid = st->st_gid; attrs.gid = st->st_gid;
@ -421,11 +416,6 @@ static void uss_fstat(SftpServer *srv, SftpReplyBuilder *reply,
} }
} }
static off_t uint64_to_off_t(uint64 u)
{
return ((((off_t)u.hi) << 16) << 16) | (off_t)u.lo;
}
/* /*
* The guts of setstat and fsetstat, macroised so that they can call * The guts of setstat and fsetstat, macroised so that they can call
* fchown(fd,...) or chown(path,...) depending on parameters. * fchown(fd,...) or chown(path,...) depending on parameters.
@ -433,8 +423,7 @@ static off_t uint64_to_off_t(uint64 u)
#define SETSTAT_GUTS(api_prefix, api_arg, attrs, success) do \ #define SETSTAT_GUTS(api_prefix, api_arg, attrs, success) do \
{ \ { \
if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) \ if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) \
if (api_prefix(truncate)( \ if (api_prefix(truncate)(api_arg, attrs.size) < 0) \
api_arg, uint64_to_off_t(attrs.size)) < 0) \
success = FALSE; \ success = FALSE; \
if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) \ if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) \
if (api_prefix(chown)(api_arg, attrs.uid, attrs.gid) < 0) \ if (api_prefix(chown)(api_arg, attrs.uid, attrs.gid) < 0) \
@ -492,7 +481,7 @@ static void uss_fsetstat(SftpServer *srv, SftpReplyBuilder *reply,
} }
static void uss_read(SftpServer *srv, SftpReplyBuilder *reply, static void uss_read(SftpServer *srv, SftpReplyBuilder *reply,
ptrlen handle, uint64 offset, unsigned length) ptrlen handle, uint64_t offset, unsigned length)
{ {
UnixSftpServer *uss = container_of(srv, UnixSftpServer, srv); UnixSftpServer *uss = container_of(srv, UnixSftpServer, srv);
int fd; int fd;
@ -513,7 +502,7 @@ static void uss_read(SftpServer *srv, SftpReplyBuilder *reply,
char *p = buf; char *p = buf;
int status = lseek(fd, uint64_to_off_t(offset), SEEK_SET); int status = lseek(fd, offset, SEEK_SET);
if (status >= 0 || errno == ESPIPE) { if (status >= 0 || errno == ESPIPE) {
int seekable = (status >= 0); int seekable = (status >= 0);
while (length > 0) { while (length > 0) {
@ -549,7 +538,7 @@ static void uss_read(SftpServer *srv, SftpReplyBuilder *reply,
} }
static void uss_write(SftpServer *srv, SftpReplyBuilder *reply, static void uss_write(SftpServer *srv, SftpReplyBuilder *reply,
ptrlen handle, uint64 offset, ptrlen data) ptrlen handle, uint64_t offset, ptrlen data)
{ {
UnixSftpServer *uss = container_of(srv, UnixSftpServer, srv); UnixSftpServer *uss = container_of(srv, UnixSftpServer, srv);
int fd; int fd;
@ -560,7 +549,7 @@ static void uss_write(SftpServer *srv, SftpReplyBuilder *reply,
const char *p = data.ptr; const char *p = data.ptr;
unsigned length = data.len; unsigned length = data.len;
int status = lseek(fd, uint64_to_off_t(offset), SEEK_SET); int status = lseek(fd, offset, SEEK_SET);
if (status >= 0 || errno == ESPIPE) { if (status >= 0 || errno == ESPIPE) {
while (length > 0) { while (length > 0) {
@ -609,7 +598,7 @@ static void uss_readdir(SftpServer *srv, SftpReplyBuilder *reply,
#if defined HAVE_FSTATAT && defined HAVE_DIRFD #if defined HAVE_FSTATAT && defined HAVE_DIRFD
struct stat st; struct stat st;
if (!fstatat(dirfd(udh->dp), de->d_name, &st, AT_SYMLINK_NOFOLLOW)) { if (!fstatat(dirfd(udh->dp), de->d_name, &st, AT_SYMLINK_NOFOLLOW)) {
char perms[11], sizebuf[32], *uidbuf = NULL, *gidbuf = NULL; char perms[11], *uidbuf = NULL, *gidbuf = NULL;
struct passwd *pwd; struct passwd *pwd;
struct group *grp; struct group *grp;
const char *user, *group; const char *user, *group;
@ -660,13 +649,11 @@ static void uss_readdir(SftpServer *srv, SftpReplyBuilder *reply,
else else
group = gidbuf = dupprintf("%u", (unsigned)st.st_gid); group = gidbuf = dupprintf("%u", (unsigned)st.st_gid);
uint64_decimal(uint64_from_off_t(st.st_size), sizebuf);
tm = *localtime(&st.st_mtime); tm = *localtime(&st.st_mtime);
longnamebuf = dupprintf( longnamebuf = dupprintf(
"%s %3u %-8s %-8s %8s %.3s %2d %02d:%02d %s", "%s %3u %-8s %-8s %8"PRIu64" %.3s %2d %02d:%02d %s",
perms, (unsigned)st.st_nlink, user, group, sizebuf, perms, (unsigned)st.st_nlink, user, group, st.st_size,
(&"JanFebMarAprMayJunJulAugSepOctNovDec"[3*tm.tm_mon]), (&"JanFebMarAprMayJunJulAugSepOctNovDec"[3*tm.tm_mon]),
tm.tm_mday, tm.tm_hour, tm.tm_min, de->d_name); tm.tm_mday, tm.tm_hour, tm.tm_min, de->d_name);
longname = ptrlen_from_asciz(longnamebuf); longname = ptrlen_from_asciz(longnamebuf);

View File

@ -10,7 +10,6 @@
#include "putty.h" #include "putty.h"
#include "psftp.h" #include "psftp.h"
#include "ssh.h" #include "ssh.h"
#include "int64.h"
#include "winsecur.h" #include "winsecur.h"
int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input) int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input)
@ -71,6 +70,11 @@ char *psftp_getcwd(void)
return ret; return ret;
} }
static inline uint64_t uint64_from_words(uint32_t hi, uint32_t lo)
{
return (((uint64_t)hi) << 32) | lo;
}
#define TIME_POSIX_TO_WIN(t, ft) do { \ #define TIME_POSIX_TO_WIN(t, ft) do { \
ULARGE_INTEGER uli; \ ULARGE_INTEGER uli; \
uli.QuadPart = ((ULONGLONG)(t) + 11644473600ull) * 10000000ull; \ uli.QuadPart = ((ULONGLONG)(t) + 11644473600ull) * 10000000ull; \
@ -89,7 +93,7 @@ struct RFile {
HANDLE h; HANDLE h;
}; };
RFile *open_existing_file(const char *name, uint64 *size, RFile *open_existing_file(const char *name, uint64_t *size,
unsigned long *mtime, unsigned long *atime, unsigned long *mtime, unsigned long *atime,
long *perms) long *perms)
{ {
@ -107,8 +111,7 @@ RFile *open_existing_file(const char *name, uint64 *size,
if (size) { if (size) {
DWORD lo, hi; DWORD lo, hi;
lo = GetFileSize(h, &hi); lo = GetFileSize(h, &hi);
size->lo = lo; *size = uint64_from_words(hi, lo);
size->hi = hi;
} }
if (mtime || atime) { if (mtime || atime) {
@ -163,7 +166,7 @@ WFile *open_new_file(const char *name, long perms)
return ret; return ret;
} }
WFile *open_existing_wfile(const char *name, uint64 *size) WFile *open_existing_wfile(const char *name, uint64_t *size)
{ {
HANDLE h; HANDLE h;
WFile *ret; WFile *ret;
@ -179,8 +182,7 @@ WFile *open_existing_wfile(const char *name, uint64 *size)
if (size) { if (size) {
DWORD lo, hi; DWORD lo, hi;
lo = GetFileSize(h, &hi); lo = GetFileSize(h, &hi);
size->lo = lo; *size = uint64_from_words(hi, lo);
size->hi = hi;
} }
return ret; return ret;
@ -213,7 +215,7 @@ void close_wfile(WFile *f)
/* Seek offset bytes through file, from whence, where whence is /* Seek offset bytes through file, from whence, where whence is
FROM_START, FROM_CURRENT, or FROM_END */ FROM_START, FROM_CURRENT, or FROM_END */
int seek_file(WFile *f, uint64 offset, int whence) int seek_file(WFile *f, uint64_t offset, int whence)
{ {
DWORD movemethod; DWORD movemethod;
@ -232,7 +234,7 @@ int seek_file(WFile *f, uint64 offset, int whence)
} }
{ {
LONG lo = offset.lo, hi = offset.hi; LONG lo = offset & 0xFFFFFFFFU, hi = offset >> 32;
SetFilePointer(f->h, lo, &hi, movemethod); SetFilePointer(f->h, lo, &hi, movemethod);
} }
@ -242,16 +244,12 @@ int seek_file(WFile *f, uint64 offset, int whence)
return 0; return 0;
} }
uint64 get_file_posn(WFile *f) uint64_t get_file_posn(WFile *f)
{ {
uint64 ret;
LONG lo, hi = 0; LONG lo, hi = 0;
lo = SetFilePointer(f->h, 0L, &hi, FILE_CURRENT); lo = SetFilePointer(f->h, 0L, &hi, FILE_CURRENT);
ret.lo = lo; return uint64_from_words(hi, lo);
ret.hi = hi;
return ret;
} }
int file_type(const char *name) int file_type(const char *name)