1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-26 01:32:25 +00:00

Run entire source base through GNU indent to tidy up the varying

coding styles of the various contributors! Woohoo!

[originally from svn r1098]
This commit is contained in:
Simon Tatham 2001-05-06 14:35:20 +00:00
parent 93101b5a71
commit 3730ada5ce
52 changed files with 15112 additions and 12426 deletions

View File

@ -17,14 +17,18 @@ struct backend_list backends[] = {
/* /*
* Stub implementations of functions not used in non-ssh versions. * Stub implementations of functions not used in non-ssh versions.
*/ */
void random_save_seed(void) { void random_save_seed(void)
{
} }
void random_destroy_seed(void) { void random_destroy_seed(void)
{
} }
void noise_ultralight(DWORD data) { void noise_ultralight(DWORD data)
{
} }
void noise_regular(void) { void noise_regular(void)
{
} }

15
int64.c
View File

@ -10,7 +10,8 @@ typedef struct {
unsigned long hi, lo; unsigned long hi, lo;
} uint64, int64; } uint64, int64;
uint64 uint64_div10(uint64 x, int *remainder) { uint64 uint64_div10(uint64 x, int *remainder)
{
uint64 y; uint64 y;
int rem, r2; int rem, r2;
y.hi = x.hi / 10; y.hi = x.hi / 10;
@ -30,7 +31,8 @@ uint64 uint64_div10(uint64 x, int *remainder) {
return y; return y;
} }
void uint64_decimal(uint64 x, char *buffer) { void uint64_decimal(uint64 x, char *buffer)
{
char buf[20]; char buf[20];
int start = 20; int start = 20;
int d; int d;
@ -45,20 +47,23 @@ void uint64_decimal(uint64 x, char *buffer) {
buffer[sizeof(buf) - start] = '\0'; buffer[sizeof(buf) - start] = '\0';
} }
uint64 uint64_make(unsigned long hi, unsigned long lo) { uint64 uint64_make(unsigned long hi, unsigned long lo)
{
uint64 y; uint64 y;
y.hi = hi; y.hi = hi;
y.lo = lo; y.lo = lo;
return y; return y;
} }
uint64 uint64_add(uint64 x, uint64 y) { uint64 uint64_add(uint64 x, uint64 y)
{
x.lo += y.lo; x.lo += y.lo;
x.hi += y.hi + (x.lo < y.lo ? 1 : 0); x.hi += y.hi + (x.lo < y.lo ? 1 : 0);
return x; return x;
} }
uint64 uint64_add32(uint64 x, unsigned long y) { uint64 uint64_add32(uint64 x, unsigned long y)
{
uint64 yy; uint64 yy;
yy.hi = 0; yy.hi = 0;
yy.lo = y; yy.lo = y;

36
ldisc.c
View File

@ -18,16 +18,17 @@
(cfg.localedit == LD_BACKEND && \ (cfg.localedit == LD_BACKEND && \
(back->ldisc(LD_EDIT) || term_ldisc(LD_EDIT)))) (back->ldisc(LD_EDIT) || term_ldisc(LD_EDIT))))
static void c_write (char *buf, int len) { static void c_write(char *buf, int len)
{
from_backend(0, buf, len); from_backend(0, buf, len);
} }
static char *term_buf = NULL; static char *term_buf = NULL;
static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0; static int term_buflen = 0, term_bufsiz = 0, term_quotenext = 0;
static int plen(unsigned char c) { static int plen(unsigned char c)
if ((c >= 32 && c <= 126) || {
(c >= 160)) if ((c >= 32 && c <= 126) || (c >= 160))
return 1; return 1;
else if (c < 128) else if (c < 128)
return 2; /* ^x for some x */ return 2; /* ^x for some x */
@ -35,9 +36,9 @@ static int plen(unsigned char c) {
return 4; /* <XY> for hex XY */ return 4; /* <XY> for hex XY */
} }
static void pwrite(unsigned char c) { static void pwrite(unsigned char c)
if ((c >= 32 && c <= 126) || {
(c >= 160)) { if ((c >= 32 && c <= 126) || (c >= 160)) {
c_write(&c, 1); c_write(&c, 1);
} else if (c < 128) { } else if (c < 128) {
char cc[2]; char cc[2];
@ -51,14 +52,16 @@ static void pwrite(unsigned char c) {
} }
} }
static void bsb(int n) { static void bsb(int n)
{
while (n--) while (n--)
c_write("\010 \010", 3); c_write("\010 \010", 3);
} }
#define CTRL(x) (x^'@') #define CTRL(x) (x^'@')
void ldisc_send(char *buf, int len) { void ldisc_send(char *buf, int len)
{
/* /*
* Called with len=0 when the options change. We must inform * Called with len=0 when the options change. We must inform
* the front end in case it needs to know. * the front end in case it needs to know.
@ -89,7 +92,8 @@ void ldisc_send(char *buf, int len) {
* else send line and reset to BOL * else send line and reset to BOL
* ^m: send line-plus-\r\n and reset to BOL * ^m: send line-plus-\r\n and reset to BOL
*/ */
case CTRL('H'): case CTRL('?'): /* backspace/delete */ case CTRL('H'):
case CTRL('?'): /* backspace/delete */
if (term_buflen > 0) { if (term_buflen > 0) {
if (ECHOING) if (ECHOING)
bsb(plen(term_buf[term_buflen - 1])); bsb(plen(term_buf[term_buflen - 1]));
@ -117,9 +121,12 @@ void ldisc_send(char *buf, int len) {
term_buflen--; term_buflen--;
} }
back->special(TS_EL); back->special(TS_EL);
if( c == CTRL('C') ) back->special (TS_IP); if (c == CTRL('C'))
if( c == CTRL('Z') ) back->special (TS_SUSP); back->special(TS_IP);
if( c == CTRL('\\') ) back->special (TS_ABORT); if (c == CTRL('Z'))
back->special(TS_SUSP);
if (c == CTRL('\\'))
back->special(TS_ABORT);
break; break;
case CTRL('R'): /* redraw line */ case CTRL('R'): /* redraw line */
if (ECHOING) { if (ECHOING) {
@ -164,8 +171,7 @@ void ldisc_send(char *buf, int len) {
} }
} }
} else { } else {
if( term_buflen != 0 ) if (term_buflen != 0) {
{
back->send(term_buf, term_buflen); back->send(term_buf, term_buflen);
while (term_buflen > 0) { while (term_buflen > 0) {
bsb(plen(term_buf[term_buflen - 1])); bsb(plen(term_buf[term_buflen - 1]));

71
misc.c
View File

@ -43,12 +43,14 @@ static long minefield_curpos = 0;
static unsigned short *minefield_admin = NULL; static unsigned short *minefield_admin = NULL;
static void *minefield_pages = NULL; static void *minefield_pages = NULL;
static void minefield_admin_hide(int hide) { static void minefield_admin_hide(int hide)
{
int access = hide ? PAGE_NOACCESS : PAGE_READWRITE; int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL); VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
} }
static void minefield_init(void) { static void minefield_init(void)
{
int size; int size;
int admin_size; int admin_size;
int i; int i;
@ -91,11 +93,13 @@ static void minefield_init(void) {
minefield_initialised = 1; minefield_initialised = 1;
} }
static void minefield_bomb(void) { static void minefield_bomb(void)
{
div(1, *(int *) minefield_pages); div(1, *(int *) minefield_pages);
} }
static void *minefield_alloc(int size) { static void *minefield_alloc(int size)
{
int npages; int npages;
int pos, lim, region_end, region_start; int pos, lim, region_end, region_start;
int start; int start;
@ -159,7 +163,8 @@ static void *minefield_alloc(int size) {
return (char *) minefield_pages + region_start; return (char *) minefield_pages + region_start;
} }
static void minefield_free(void *ptr) { static void minefield_free(void *ptr)
{
int region_start, i, j; int region_start, i, j;
minefield_admin_hide(0); minefield_admin_hide(0);
@ -178,7 +183,8 @@ static void minefield_free(void *ptr) {
minefield_admin_hide(1); minefield_admin_hide(1);
} }
static int minefield_get_size(void *ptr) { static int minefield_get_size(void *ptr)
{
int region_start, i, j; int region_start, i, j;
minefield_admin_hide(0); minefield_admin_hide(0);
@ -195,13 +201,17 @@ static int minefield_get_size(void *ptr) {
return j * PAGESIZE - region_start; return j * PAGESIZE - region_start;
} }
static void *minefield_c_malloc(size_t size) { static void *minefield_c_malloc(size_t size)
if (!minefield_initialised) minefield_init(); {
if (!minefield_initialised)
minefield_init();
return minefield_alloc(size); return minefield_alloc(size);
} }
static void minefield_c_free(void *p) { static void minefield_c_free(void *p)
if (!minefield_initialised) minefield_init(); {
if (!minefield_initialised)
minefield_init();
minefield_free(p); minefield_free(p);
} }
@ -209,10 +219,12 @@ static void minefield_c_free(void *p) {
* realloc _always_ moves the chunk, for rapid detection of code * realloc _always_ moves the chunk, for rapid detection of code
* that assumes it won't. * that assumes it won't.
*/ */
static void *minefield_c_realloc(void *p, size_t size) { static void *minefield_c_realloc(void *p, size_t size)
{
size_t oldsize; size_t oldsize;
void *q; void *q;
if (!minefield_initialised) minefield_init(); if (!minefield_initialised)
minefield_init();
q = minefield_alloc(size); q = minefield_alloc(size);
oldsize = minefield_get_size(p); oldsize = minefield_get_size(p);
memcpy(q, p, (oldsize < size ? oldsize : size)); memcpy(q, p, (oldsize < size ? oldsize : size));
@ -225,7 +237,8 @@ static void *minefield_c_realloc(void *p, size_t size) {
#ifdef MALLOC_LOG #ifdef MALLOC_LOG
static FILE *fp = NULL; static FILE *fp = NULL;
void mlog(char *file, int line) { void mlog(char *file, int line)
{
if (!fp) { if (!fp) {
fp = fopen("putty_mem.log", "w"); fp = fopen("putty_mem.log", "w");
setvbuf(fp, NULL, _IONBF, BUFSIZ); setvbuf(fp, NULL, _IONBF, BUFSIZ);
@ -235,7 +248,8 @@ void mlog(char *file, int line) {
} }
#endif #endif
void *safemalloc(size_t size) { void *safemalloc(size_t size)
{
void *p; void *p;
#ifdef MINEFIELD #ifdef MINEFIELD
p = minefield_c_malloc(size); p = minefield_c_malloc(size);
@ -254,7 +268,8 @@ void *safemalloc(size_t size) {
return p; return p;
} }
void *saferealloc(void *ptr, size_t size) { void *saferealloc(void *ptr, size_t size)
{
void *p; void *p;
if (!ptr) { if (!ptr) {
#ifdef MINEFIELD #ifdef MINEFIELD
@ -281,7 +296,8 @@ void *saferealloc(void *ptr, size_t size) {
return p; return p;
} }
void safefree(void *ptr) { void safefree(void *ptr)
{
if (ptr) { if (ptr) {
#ifdef MALLOC_LOG #ifdef MALLOC_LOG
if (fp) if (fp)
@ -303,7 +319,8 @@ void safefree(void *ptr) {
static FILE *debug_fp = NULL; static FILE *debug_fp = NULL;
static int debug_got_console = 0; static int debug_got_console = 0;
static void dputs (char *buf) { static void dputs(char *buf)
{
DWORD dw; DWORD dw;
if (!debug_got_console) { if (!debug_got_console) {
@ -314,13 +331,15 @@ static void dputs (char *buf) {
debug_fp = fopen("debug.log", "w"); debug_fp = fopen("debug.log", "w");
} }
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw,
NULL);
fputs(buf, debug_fp); fputs(buf, debug_fp);
fflush(debug_fp); fflush(debug_fp);
} }
void dprintf(char *fmt, ...) { void dprintf(char *fmt, ...)
{
char buf[2048]; char buf[2048];
va_list ap; va_list ap;
@ -331,7 +350,8 @@ void dprintf(char *fmt, ...) {
} }
void debug_memdump (void *buf, int len, int L) { void debug_memdump(void *buf, int len, int L)
{
int i; int i;
unsigned char *p = buf; unsigned char *p = buf;
char foo[17]; char foo[17];
@ -344,17 +364,17 @@ void debug_memdump (void *buf, int len, int L) {
} }
for (; 0 < len; p += 16, len -= 16) { for (; 0 < len; p += 16, len -= 16) {
dputs(" "); dputs(" ");
if (L) dprintf ("%p: ", p); if (L)
dprintf("%p: ", p);
strcpy(foo, "................"); /* sixteen dots */ strcpy(foo, "................"); /* sixteen dots */
for (i = 0; i < 16 && i < len; ++i) { for (i = 0; i < 16 && i < len; ++i) {
if (&p[i] < (unsigned char *) buf) { if (&p[i] < (unsigned char *) buf) {
dputs(" "); /* 3 spaces */ dputs(" "); /* 3 spaces */
foo[i] = ' '; foo[i] = ' ';
} else { } else {
dprintf ( dprintf("%c%02.2x",
"%c%02.2x", &p[i] != (unsigned char *) buf
&p[i] != (unsigned char *) buf && i % 4 ? '.' : ' ', && i % 4 ? '.' : ' ', p[i]
p[i]
); );
if (p[i] >= ' ' && p[i] <= '~') if (p[i] >= ' ' && p[i] <= '~')
foo[i] = (char) p[i]; foo[i] = (char) p[i];
@ -366,4 +386,3 @@ void debug_memdump (void *buf, int len, int L) {
} }
#endif /* def DEBUG */ #endif /* def DEBUG */

View File

@ -18,8 +18,7 @@ HCRYPTKEY hDESKey[2][3] = {{0,0,0},{0,0,0}}; /* global for now */
#define CSP MS_ENHANCED_PROV #define CSP MS_ENHANCED_PROV
static BYTE PrivateKeyWithExponentOfOne[] = static BYTE PrivateKeyWithExponentOfOne[] = {
{
0x07, 0x02, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x07, 0x02, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00,
0x52, 0x53, 0x41, 0x32, 0x00, 0x02, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32, 0x00, 0x02, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0xAB, 0xEF, 0xFA, 0xC6, 0x01, 0x00, 0x00, 0x00, 0xAB, 0xEF, 0xFA, 0xC6,
@ -73,7 +72,8 @@ static BYTE PrivateKeyWithExponentOfOne[] =
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
int crypto_startup() { int crypto_startup()
{
if (CryptAcquireContext(&hCryptProv, "Putty", CSP, PROV_RSA_FULL, if (CryptAcquireContext(&hCryptProv, "Putty", CSP, PROV_RSA_FULL,
CRYPT_NEWKEYSET) == 0) { CRYPT_NEWKEYSET) == 0) {
if (GetLastError() == NTE_EXISTS) { if (GetLastError() == NTE_EXISTS) {
@ -90,7 +90,8 @@ int crypto_startup() {
} }
void crypto_wrapup() { void crypto_wrapup()
{
int i, j; int i, j;
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
@ -109,32 +110,40 @@ void crypto_wrapup() {
* Random number functions * * Random number functions *
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
int random_byte(void) { int random_byte(void)
{
unsigned char b; unsigned char b;
if (!CryptGenRandom(hCryptProv, 1, &b)) if (!CryptGenRandom(hCryptProv, 1, &b))
fatalbox("random number generator failure!"); fatalbox("random number generator failure!");
return b; return b;
} }
void random_add_noise(void *noise, int length) { void random_add_noise(void *noise, int length)
{
/* do nothing */ /* do nothing */
} }
void random_init(void) { void random_init(void)
{
/* do nothing */ /* do nothing */
} }
void random_get_savedata(void **data, int *len) { void random_get_savedata(void **data, int *len)
{
/* do nothing */ /* do nothing */
} }
void noise_get_heavy(void (*func) (void *, int)) { void noise_get_heavy(void (*func) (void *, int))
{
/* do nothing */ /* do nothing */
} }
void noise_get_light(void (*func) (void *, int)) { void noise_get_light(void (*func) (void *, int))
{
/* do nothing */ /* do nothing */
} }
void noise_ultralight(DWORD data) { void noise_ultralight(DWORD data)
{
/* do nothing */ /* do nothing */
} }
void random_save_seed(void) { void random_save_seed(void)
{
/* do nothing */ /* do nothing */
} }
@ -144,20 +153,23 @@ void random_save_seed(void) {
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
void MD5Init(struct MD5Context *ctx) { void MD5Init(struct MD5Context *ctx)
{
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash)) if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash))
fatalbox("Error during CryptBeginHash!\n"); fatalbox("Error during CryptBeginHash!\n");
} }
void MD5Update(struct MD5Context *ctx, void MD5Update(struct MD5Context *ctx,
unsigned char const *buf, unsigned len) { unsigned char const *buf, unsigned len)
{
if (CryptHashData(ctx->hHash, buf, len, 0) == 0) if (CryptHashData(ctx->hHash, buf, len, 0) == 0)
fatalbox("Error during CryptHashSessionKey!\n"); fatalbox("Error during CryptHashSessionKey!\n");
} }
void MD5Final(unsigned char digest[16], struct MD5Context *ctx) { void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
{
DWORD cb = 16; DWORD cb = 16;
if (CryptGetHashParam(ctx->hHash, HP_HASHVAL, digest, &cb, 0) == 0) if (CryptGetHashParam(ctx->hHash, HP_HASHVAL, digest, &cb, 0) == 0)
fatalbox("Error during CryptGetHashParam!\n"); fatalbox("Error during CryptGetHashParam!\n");
@ -172,7 +184,8 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) {
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
int makekey(unsigned char *data, struct RSAKey *result, int makekey(unsigned char *data, struct RSAKey *result,
unsigned char **keystr) { unsigned char **keystr)
{
unsigned char *p = data; unsigned char *p = data;
int i; int i;
@ -205,13 +218,15 @@ int makekey(unsigned char *data, struct RSAKey *result,
memcpy(result->modulus, p, b); memcpy(result->modulus, p, b);
/* update callers pointer */ /* update callers pointer */
if (keystr) *keystr = p; /* point at key string, second time */ if (keystr)
*keystr = p; /* point at key string, second time */
return (p - data) + b; return (p - data) + b;
} }
void rsaencrypt(unsigned char *data, int length, struct RSAKey *rsakey) { void rsaencrypt(unsigned char *data, int length, struct RSAKey *rsakey)
{
int i; int i;
unsigned char *pKeybuf, *pKeyin; unsigned char *pKeybuf, *pKeyin;
@ -243,15 +258,16 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *rsakey) {
pBlob->bVersion = 0x02; pBlob->bVersion = 0x02;
pBlob->reserved = 0; pBlob->reserved = 0;
pBlob->aiKeyAlg = CALG_RSA_KEYX; pBlob->aiKeyAlg = CALG_RSA_KEYX;
pRPK = (RSAPUBKEY*)(((unsigned char*)pBlob) + sizeof(PUBLICKEYSTRUC)); pRPK =
(RSAPUBKEY *) (((unsigned char *) pBlob) + sizeof(PUBLICKEYSTRUC));
pRPK->magic = 0x31415352; /* "RSA1" */ pRPK->magic = 0x31415352; /* "RSA1" */
pRPK->bitlen = rsakey->bits; pRPK->bitlen = rsakey->bits;
pRPK->pubexp = rsakey->exponent; pRPK->pubexp = rsakey->exponent;
/* import public key blob into key container */ /* import public key blob into key container */
if (CryptImportKey(hCryptProv, (void *) pBlob, if (CryptImportKey(hCryptProv, (void *) pBlob,
sizeof(PUBLICKEYSTRUC)+sizeof(RSAPUBKEY)+rsakey->bytes, sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) +
0, 0, &hRsaKey) == 0) rsakey->bytes, 0, 0, &hRsaKey) == 0)
fatalbox("Error importing RSA key!"); fatalbox("Error importing RSA key!");
/* copy message into buffer */ /* copy message into buffer */
@ -277,12 +293,14 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *rsakey) {
} }
int rsastr_len(struct RSAKey *key) { int rsastr_len(struct RSAKey *key)
{
return 2 * (sizeof(DWORD) + key->bytes) + 10; return 2 * (sizeof(DWORD) + key->bytes) + 10;
} }
void rsastr_fmt(char *str, struct RSAKey *key) { void rsastr_fmt(char *str, struct RSAKey *key)
{
int len = 0, i; int len = 0, i;
@ -304,7 +322,8 @@ void rsastr_fmt(char *str, struct RSAKey *key) {
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
void des3_sesskey(unsigned char *key) { void des3_sesskey(unsigned char *key)
{
int i, j; int i, j;
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
@ -314,7 +333,8 @@ void des3_sesskey(unsigned char *key) {
} }
void des3_encrypt_blk(unsigned char *blk, int len) { void des3_encrypt_blk(unsigned char *blk, int len)
{
DWORD dlen; DWORD dlen;
dlen = len; dlen = len;
@ -328,7 +348,8 @@ void des3_encrypt_blk(unsigned char *blk, int len) {
} }
void des3_decrypt_blk(unsigned char *blk, int len) { void des3_decrypt_blk(unsigned char *blk, int len)
{
DWORD dlen; DWORD dlen;
dlen = len; dlen = len;
@ -348,7 +369,8 @@ struct ssh_cipher ssh_3des = {
}; };
void des_sesskey(unsigned char *key) { void des_sesskey(unsigned char *key)
{
int i; int i;
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
hDESKey[i][0] = create_des_key(key); hDESKey[i][0] = create_des_key(key);
@ -356,7 +378,8 @@ void des_sesskey(unsigned char *key) {
} }
void des_encrypt_blk(unsigned char *blk, int len) { void des_encrypt_blk(unsigned char *blk, int len)
{
DWORD dlen; DWORD dlen;
dlen = len; dlen = len;
if (CryptEncrypt(hDESKey[0][0], 0, FALSE, 0, blk, &dlen, len + 8) == 0) if (CryptEncrypt(hDESKey[0][0], 0, FALSE, 0, blk, &dlen, len + 8) == 0)
@ -364,7 +387,8 @@ void des_encrypt_blk(unsigned char *blk, int len) {
} }
void des_decrypt_blk(unsigned char *blk, int len) { void des_decrypt_blk(unsigned char *blk, int len)
{
DWORD dlen; DWORD dlen;
dlen = len; dlen = len;
if (CryptDecrypt(hDESKey[1][0], 0, FALSE, 0, blk, &dlen) == 0) if (CryptDecrypt(hDESKey[1][0], 0, FALSE, 0, blk, &dlen) == 0)
@ -378,7 +402,8 @@ struct ssh_cipher ssh_des = {
}; };
static HCRYPTKEY create_des_key(unsigned char *key) { static HCRYPTKEY create_des_key(unsigned char *key)
{
HCRYPTKEY hSessionKey, hPrivateKey; HCRYPTKEY hSessionKey, hPrivateKey;
DWORD dlen = 8; DWORD dlen = 8;

View File

@ -56,7 +56,8 @@ void sk_init(void); /* called once at program startup */
SockAddr sk_namelookup(char *host, char **canonicalname); SockAddr sk_namelookup(char *host, char **canonicalname);
void sk_addr_free(SockAddr addr); void sk_addr_free(SockAddr addr);
Socket sk_new(SockAddr addr, int port, int privport, int oobinline, Plug p); Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
Plug p);
#define sk_plug(s,p) (((*s)->plug) (s, p)) #define sk_plug(s,p) (((*s)->plug) (s, p))
#define sk_close(s) (((*s)->close) (s)) #define sk_close(s) (((*s)->close) (s))
@ -120,10 +121,8 @@ struct ssl_client_plug_function_table {
/* may return NULL if we want anonymity */ /* may return NULL if we want anonymity */
}; };
SSL_Client_Socket sk_ssl_client_over ( SSL_Client_Socket sk_ssl_client_over(Socket s, /* pre-existing (tcp) connection */
Socket s, /* pre-existing (tcp) connection */ SSL_Client_Plug p);
SSL_Client_Plug p
);
#define sk_renegotiate(s) (((*s)->renegotiate) (s)) #define sk_renegotiate(s) (((*s)->renegotiate) (s))

39
noise.c
View File

@ -22,7 +22,8 @@ static gsps_t gsps;
* free space and a process snapshot. * free space and a process snapshot.
*/ */
void noise_get_heavy(void (*func) (void *, int)) { void noise_get_heavy(void (*func) (void *, int))
{
HANDLE srch; HANDLE srch;
WIN32_FIND_DATA finddata; WIN32_FIND_DATA finddata;
char winpath[MAX_PATH + 3]; char winpath[MAX_PATH + 3];
@ -47,7 +48,8 @@ void noise_get_heavy(void (*func) (void *, int)) {
} }
} }
void random_save_seed(void) { void random_save_seed(void)
{
int len; int len;
void *data; void *data;
@ -60,7 +62,8 @@ void random_save_seed(void) {
* stirring, and will acquire the system time in all available * stirring, and will acquire the system time in all available
* forms and the battery status. * forms and the battery status.
*/ */
void noise_get_light(void (*func) (void *, int)) { void noise_get_light(void (*func) (void *, int))
{
SYSTEMTIME systime; SYSTEMTIME systime;
DWORD adjust[2]; DWORD adjust[2];
BOOL rubbish; BOOL rubbish;
@ -87,25 +90,34 @@ void noise_get_light(void (*func) (void *, int)) {
* virtual memory, the state of the process's message queue, which * virtual memory, the state of the process's message queue, which
* window is in the foreground, which owns the clipboard, etc. * window is in the foreground, which owns the clipboard, etc.
*/ */
void noise_regular(void) { void noise_regular(void)
{
HWND w; HWND w;
DWORD z; DWORD z;
POINT pt; POINT pt;
MEMORYSTATUS memstat; MEMORYSTATUS memstat;
FILETIME times[4]; FILETIME times[4];
w = GetForegroundWindow(); random_add_noise(&w, sizeof(w)); w = GetForegroundWindow();
w = GetCapture(); random_add_noise(&w, sizeof(w)); random_add_noise(&w, sizeof(w));
w = GetClipboardOwner(); random_add_noise(&w, sizeof(w)); w = GetCapture();
z = GetQueueStatus(QS_ALLEVENTS); random_add_noise(&z, sizeof(z)); random_add_noise(&w, sizeof(w));
w = GetClipboardOwner();
random_add_noise(&w, sizeof(w));
z = GetQueueStatus(QS_ALLEVENTS);
random_add_noise(&z, sizeof(z));
GetCursorPos(&pt); random_add_noise(&pt, sizeof(pt)); GetCursorPos(&pt);
random_add_noise(&pt, sizeof(pt));
GlobalMemoryStatus(&memstat); random_add_noise(&memstat, sizeof(memstat)); GlobalMemoryStatus(&memstat);
random_add_noise(&memstat, sizeof(memstat));
GetThreadTimes(GetCurrentThread(), times, times+1, times+2, times+3); GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
times + 3);
random_add_noise(&times, sizeof(times)); random_add_noise(&times, sizeof(times));
GetProcessTimes(GetCurrentProcess(), times, times+1, times+2, times+3); GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
times + 3);
random_add_noise(&times, sizeof(times)); random_add_noise(&times, sizeof(times));
} }
@ -115,7 +127,8 @@ void noise_regular(void) {
* counter to the noise pool. It gets the scan code or mouse * counter to the noise pool. It gets the scan code or mouse
* position passed in. * position passed in.
*/ */
void noise_ultralight(DWORD data) { void noise_ultralight(DWORD data)
{
DWORD wintime; DWORD wintime;
LARGE_INTEGER perftime; LARGE_INTEGER perftime;

210
pageant.c
View File

@ -50,8 +50,7 @@ static int has_security;
#ifndef NO_SECURITY #ifndef NO_SECURITY
typedef DWORD(WINAPI * gsi_fn_t) typedef DWORD(WINAPI * gsi_fn_t)
(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION, (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
PSID *, PSID *, PACL *, PACL *, PSID *, PSID *, PACL *, PACL *, PSECURITY_DESCRIPTOR *);
PSECURITY_DESCRIPTOR *);
static gsi_fn_t getsecurityinfo; static gsi_fn_t getsecurityinfo;
#endif #endif
@ -71,7 +70,8 @@ int agent_exists(void);
* won't generate true random numbers. So we must scream, panic, * won't generate true random numbers. So we must scream, panic,
* and exit immediately if that should happen. * and exit immediately if that should happen.
*/ */
int random_byte(void) { int random_byte(void)
{
MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR); MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
exit(0); exit(0);
} }
@ -90,7 +90,8 @@ static int cmpkeys_ssh2_asymm(void *av, void *bv);
* This function is needed to link with the DES code. We need not * This function is needed to link with the DES code. We need not
* have it do anything at all. * have it do anything at all.
*/ */
void logevent(char *msg) { void logevent(char *msg)
{
} }
#define GET_32BIT(cp) \ #define GET_32BIT(cp) \
@ -116,7 +117,8 @@ struct PassphraseProcStruct {
* Dialog-box function for the Licence box. * Dialog-box function for the Licence box.
*/ */
static int CALLBACK LicenceProc(HWND hwnd, UINT msg, static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
switch (msg) { switch (msg) {
case WM_INITDIALOG: case WM_INITDIALOG:
return 1; return 1;
@ -138,7 +140,8 @@ static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
* Dialog-box function for the About box. * Dialog-box function for the About box.
*/ */
static int CALLBACK AboutProc(HWND hwnd, UINT msg, static int CALLBACK AboutProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
switch (msg) { switch (msg) {
case WM_INITDIALOG: case WM_INITDIALOG:
SetDlgItemText(hwnd, 100, ver); SetDlgItemText(hwnd, 100, ver);
@ -169,7 +172,8 @@ static int CALLBACK AboutProc (HWND hwnd, UINT msg,
* Dialog-box function for the passphrase box. * Dialog-box function for the passphrase box.
*/ */
static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
static char *passphrase = NULL; static char *passphrase = NULL;
struct PassphraseProcStruct *p; struct PassphraseProcStruct *p;
@ -184,7 +188,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -212,7 +217,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
return 0; return 0;
case 102: /* edit box */ case 102: /* edit box */
if ((HIWORD(wParam) == EN_CHANGE) && passphrase) { if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1); GetDlgItemText(hwnd, 102, passphrase,
PASSPHRASE_MAXLEN - 1);
passphrase[PASSPHRASE_MAXLEN - 1] = '\0'; passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
} }
return 0; return 0;
@ -228,7 +234,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
/* /*
* Update the visible key list. * Update the visible key list.
*/ */
static void keylist_update(void) { static void keylist_update(void)
{
struct RSAKey *rkey; struct RSAKey *rkey;
struct ssh2_userkey *skey; struct ssh2_userkey *skey;
int i; int i;
@ -244,8 +251,12 @@ static void keylist_update(void) {
strcpy(listentry, "ssh1\t"); strcpy(listentry, "ssh1\t");
p = listentry + strlen(listentry); p = listentry + strlen(listentry);
rsa_fingerprint(p, sizeof(listentry) - (p - listentry), rkey); rsa_fingerprint(p, sizeof(listentry) - (p - listentry), rkey);
p = strchr(listentry, ' '); if (p) *p = '\t'; p = strchr(listentry, ' ');
p = strchr(listentry, ' '); if (p) *p = '\t'; if (p)
*p = '\t';
p = strchr(listentry, ' ');
if (p)
*p = '\t';
SendDlgItemMessage(keylist, 100, LB_ADDSTRING, SendDlgItemMessage(keylist, 100, LB_ADDSTRING,
0, (LPARAM) listentry); 0, (LPARAM) listentry);
} }
@ -258,15 +269,20 @@ static void keylist_update(void) {
*/ */
p = skey->alg->fingerprint(skey->data); p = skey->alg->fingerprint(skey->data);
strncpy(listentry, p, sizeof(listentry)); strncpy(listentry, p, sizeof(listentry));
p = strchr(listentry, ' '); if (p) *p = '\t'; p = strchr(listentry, ' ');
p = strchr(listentry, ' '); if (p) *p = '\t'; if (p)
*p = '\t';
p = strchr(listentry, ' ');
if (p)
*p = '\t';
len = strlen(listentry); len = strlen(listentry);
if (len < sizeof(listentry) - 2) { if (len < sizeof(listentry) - 2) {
listentry[len] = '\t'; listentry[len] = '\t';
strncpy(listentry+len+1, skey->comment, sizeof(listentry)-len-1); strncpy(listentry + len + 1, skey->comment,
sizeof(listentry) - len - 1);
} }
SendDlgItemMessage (keylist, 100, LB_ADDSTRING, SendDlgItemMessage(keylist, 100, LB_ADDSTRING, 0,
0, (LPARAM)listentry); (LPARAM) listentry);
} }
SendDlgItemMessage(keylist, 100, LB_SETCURSEL, (WPARAM) - 1, 0); SendDlgItemMessage(keylist, 100, LB_SETCURSEL, (WPARAM) - 1, 0);
} }
@ -275,7 +291,8 @@ static void keylist_update(void) {
/* /*
* This function loads a key from a file and adds it. * This function loads a key from a file and adds it.
*/ */
static void add_keyfile(char *filename) { static void add_keyfile(char *filename)
{
char passphrase[PASSPHRASE_MAXLEN]; char passphrase[PASSPHRASE_MAXLEN];
struct RSAKey *rkey; struct RSAKey *rkey;
struct ssh2_userkey *skey; struct ssh2_userkey *skey;
@ -306,10 +323,10 @@ static void add_keyfile(char *filename) {
if (needs_pass) { if (needs_pass) {
int dlgret; int dlgret;
dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210), dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
NULL, PassphraseProc, NULL, PassphraseProc, (LPARAM) & pps);
(LPARAM)&pps);
if (!dlgret) { if (!dlgret) {
if (comment) sfree(comment); if (comment)
sfree(comment);
if (ver == 1) if (ver == 1)
sfree(rkey); sfree(rkey);
return; /* operation cancelled */ return; /* operation cancelled */
@ -329,7 +346,8 @@ static void add_keyfile(char *filename) {
} }
attempts++; attempts++;
} while (ret == -1); } while (ret == -1);
if (comment) sfree(comment); if (comment)
sfree(comment);
if (ret == 0) { if (ret == 0) {
MessageBox(NULL, "Couldn't load private key.", APPNAME, MessageBox(NULL, "Couldn't load private key.", APPNAME,
MB_OK | MB_ICONERROR); MB_OK | MB_ICONERROR);
@ -351,8 +369,7 @@ static void add_keyfile(char *filename) {
ssh1_bignum_length(rkey->private_exponent) + ssh1_bignum_length(rkey->private_exponent) +
ssh1_bignum_length(rkey->iqmp) + ssh1_bignum_length(rkey->iqmp) +
ssh1_bignum_length(rkey->p) + ssh1_bignum_length(rkey->p) +
ssh1_bignum_length(rkey->q) + ssh1_bignum_length(rkey->q) + 4 + clen /* comment */
4 + clen /* comment */
; ;
request = smalloc(reqlen); request = smalloc(reqlen);
@ -363,7 +380,9 @@ static void add_keyfile(char *filename) {
reqlen += 4; reqlen += 4;
reqlen += ssh1_write_bignum(request + reqlen, rkey->modulus); reqlen += ssh1_write_bignum(request + reqlen, rkey->modulus);
reqlen += ssh1_write_bignum(request + reqlen, rkey->exponent); reqlen += ssh1_write_bignum(request + reqlen, rkey->exponent);
reqlen += ssh1_write_bignum(request+reqlen, rkey->private_exponent); reqlen +=
ssh1_write_bignum(request + reqlen,
rkey->private_exponent);
reqlen += ssh1_write_bignum(request + reqlen, rkey->iqmp); reqlen += ssh1_write_bignum(request + reqlen, rkey->iqmp);
reqlen += ssh1_write_bignum(request + reqlen, rkey->p); reqlen += ssh1_write_bignum(request + reqlen, rkey->p);
reqlen += ssh1_write_bignum(request + reqlen, rkey->q); reqlen += ssh1_write_bignum(request + reqlen, rkey->q);
@ -405,7 +424,8 @@ static void add_keyfile(char *filename) {
memcpy(request + reqlen, skey->alg->name, alglen); memcpy(request + reqlen, skey->alg->name, alglen);
reqlen += alglen; reqlen += alglen;
reqlen += skey->alg->openssh_fmtkey(skey->data, reqlen += skey->alg->openssh_fmtkey(skey->data,
request+reqlen, keybloblen); request + reqlen,
keybloblen);
PUT_32BIT(request + reqlen, clen); PUT_32BIT(request + reqlen, clen);
memcpy(request + reqlen + 4, skey->comment, clen); memcpy(request + reqlen + 4, skey->comment, clen);
PUT_32BIT(request, reqlen - 4); PUT_32BIT(request, reqlen - 4);
@ -428,7 +448,8 @@ static void add_keyfile(char *filename) {
/* /*
* This is the main agent function that answers messages. * This is the main agent function that answers messages.
*/ */
static void answer_msg(void *msg) { static void answer_msg(void *msg)
{
unsigned char *p = msg; unsigned char *p = msg;
unsigned char *ret = msg; unsigned char *ret = msg;
int type; int type;
@ -548,7 +569,8 @@ static void answer_msg(void *msg) {
p += ssh1_read_bignum(p, &reqkey.exponent); p += ssh1_read_bignum(p, &reqkey.exponent);
p += ssh1_read_bignum(p, &reqkey.modulus); p += ssh1_read_bignum(p, &reqkey.modulus);
p += ssh1_read_bignum(p, &challenge); p += ssh1_read_bignum(p, &challenge);
memcpy(response_source+32, p, 16); p += 16; memcpy(response_source + 32, p, 16);
p += 16;
if (GET_32BIT(p) != 1 || if (GET_32BIT(p) != 1 ||
(key = find234(rsakeys, &reqkey, NULL)) == NULL) { (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
freebn(reqkey.exponent); freebn(reqkey.exponent);
@ -654,8 +676,10 @@ static void answer_msg(void *msg) {
key = smalloc(sizeof(struct ssh2_userkey)); key = smalloc(sizeof(struct ssh2_userkey));
alglen = GET_32BIT(p); p += 4; alglen = GET_32BIT(p);
alg = p; p += alglen; p += 4;
alg = p;
p += alglen;
/* Add further algorithm names here. */ /* Add further algorithm names here. */
if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7)) if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
key->alg = &ssh_rsa; key->alg = &ssh_rsa;
@ -664,13 +688,17 @@ static void answer_msg(void *msg) {
goto failure; goto failure;
} }
bloblen = GET_32BIT((unsigned char *)msg) - (p-(unsigned char *)msg-4); bloblen =
GET_32BIT((unsigned char *) msg) - (p -
(unsigned char *) msg -
4);
key->data = key->alg->openssh_createkey(&p, &bloblen); key->data = key->alg->openssh_createkey(&p, &bloblen);
if (!key->data) { if (!key->data) {
sfree(key); sfree(key);
goto failure; goto failure;
} }
commlen = GET_32BIT(p); p += 4; commlen = GET_32BIT(p);
p += 4;
comment = smalloc(commlen + 1); comment = smalloc(commlen + 1);
if (comment) { if (comment) {
@ -794,7 +822,8 @@ static void answer_msg(void *msg) {
/* /*
* Key comparison function for the 2-3-4 tree of RSA keys. * Key comparison function for the 2-3-4 tree of RSA keys.
*/ */
static int cmpkeys_rsa(void *av, void *bv) { static int cmpkeys_rsa(void *av, void *bv)
{
struct RSAKey *a = (struct RSAKey *) av; struct RSAKey *a = (struct RSAKey *) av;
struct RSAKey *b = (struct RSAKey *) bv; struct RSAKey *b = (struct RSAKey *) bv;
Bignum am, bm; Bignum am, bm;
@ -807,7 +836,10 @@ static int cmpkeys_rsa(void *av, void *bv) {
*/ */
alen = bignum_bitcount(am); alen = bignum_bitcount(am);
blen = bignum_bitcount(bm); blen = bignum_bitcount(bm);
if (alen > blen) return +1; else if (alen < blen) return -1; if (alen > blen)
return +1;
else if (alen < blen)
return -1;
/* /*
* Now compare by moduli themselves. * Now compare by moduli themselves.
*/ */
@ -816,7 +848,10 @@ static int cmpkeys_rsa(void *av, void *bv) {
int abyte, bbyte; int abyte, bbyte;
abyte = bignum_byte(am, alen); abyte = bignum_byte(am, alen);
bbyte = bignum_byte(bm, alen); bbyte = bignum_byte(bm, alen);
if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1; if (abyte > bbyte)
return +1;
else if (abyte < bbyte)
return -1;
} }
/* /*
* Give up. * Give up.
@ -827,7 +862,8 @@ static int cmpkeys_rsa(void *av, void *bv) {
/* /*
* Key comparison function for the 2-3-4 tree of SSH2 keys. * Key comparison function for the 2-3-4 tree of SSH2 keys.
*/ */
static int cmpkeys_ssh2(void *av, void *bv) { static int cmpkeys_ssh2(void *av, void *bv)
{
struct ssh2_userkey *a = (struct ssh2_userkey *) av; struct ssh2_userkey *a = (struct ssh2_userkey *) av;
struct ssh2_userkey *b = (struct ssh2_userkey *) bv; struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
int i; int i;
@ -844,13 +880,17 @@ static int cmpkeys_ssh2(void *av, void *bv) {
c = 0; c = 0;
for (i = 0; i < alen && i < blen; i++) { for (i = 0; i < alen && i < blen; i++) {
if (ablob[i] < bblob[i]) { if (ablob[i] < bblob[i]) {
c = -1; break; c = -1;
break;
} else if (ablob[i] > bblob[i]) { } else if (ablob[i] > bblob[i]) {
c = +1; break; c = +1;
break;
} }
} }
if (c == 0 && i < alen) c = +1; /* a is longer */ if (c == 0 && i < alen)
if (c == 0 && i < blen) c = -1; /* a is longer */ c = +1; /* a is longer */
if (c == 0 && i < blen)
c = -1; /* a is longer */
sfree(ablob); sfree(ablob);
sfree(bblob); sfree(bblob);
@ -862,7 +902,8 @@ static int cmpkeys_ssh2(void *av, void *bv) {
* Key comparison function for looking up a blob in the 2-3-4 tree * Key comparison function for looking up a blob in the 2-3-4 tree
* of SSH2 keys. * of SSH2 keys.
*/ */
static int cmpkeys_ssh2_asymm(void *av, void *bv) { static int cmpkeys_ssh2_asymm(void *av, void *bv)
{
struct blob *a = (struct blob *) av; struct blob *a = (struct blob *) av;
struct ssh2_userkey *b = (struct ssh2_userkey *) bv; struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
int i; int i;
@ -880,27 +921,33 @@ static int cmpkeys_ssh2_asymm(void *av, void *bv) {
c = 0; c = 0;
for (i = 0; i < alen && i < blen; i++) { for (i = 0; i < alen && i < blen; i++) {
if (ablob[i] < bblob[i]) { if (ablob[i] < bblob[i]) {
c = -1; break; c = -1;
break;
} else if (ablob[i] > bblob[i]) { } else if (ablob[i] > bblob[i]) {
c = +1; break; c = +1;
break;
} }
} }
if (c == 0 && i < alen) c = +1; /* a is longer */ if (c == 0 && i < alen)
if (c == 0 && i < blen) c = -1; /* a is longer */ c = +1; /* a is longer */
if (c == 0 && i < blen)
c = -1; /* a is longer */
sfree(bblob); sfree(bblob);
return c; return c;
} }
static void error(char *s) { static void error(char *s)
{
MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR); MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
} }
/* /*
* Prompt for a key file to add, and add it. * Prompt for a key file to add, and add it.
*/ */
static void prompt_add_keyfile(void) { static void prompt_add_keyfile(void)
{
OPENFILENAME of; OPENFILENAME of;
char filename[FILENAME_MAX]; char filename[FILENAME_MAX];
memset(&of, 0, sizeof(of)); memset(&of, 0, sizeof(of));
@ -913,7 +960,8 @@ static void prompt_add_keyfile(void) {
of.lpstrFilter = "All Files\0*\0\0\0"; of.lpstrFilter = "All Files\0*\0\0\0";
of.lpstrCustomFilter = NULL; of.lpstrCustomFilter = NULL;
of.nFilterIndex = 1; of.nFilterIndex = 1;
of.lpstrFile = filename; *filename = '\0'; of.lpstrFile = filename;
*filename = '\0';
of.nMaxFile = sizeof(filename); of.nMaxFile = sizeof(filename);
of.lpstrFileTitle = NULL; of.lpstrFileTitle = NULL;
of.lpstrInitialDir = NULL; of.lpstrInitialDir = NULL;
@ -929,7 +977,8 @@ static void prompt_add_keyfile(void) {
* Dialog-box function for the key list box. * Dialog-box function for the key list box.
*/ */
static int CALLBACK KeyListProc(HWND hwnd, UINT msg, static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
struct RSAKey *rkey; struct RSAKey *rkey;
struct ssh2_userkey *skey; struct ssh2_userkey *skey;
@ -944,7 +993,8 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -953,7 +1003,8 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
{ {
static int tabs[] = { 35, 60, 210 }; static int tabs[] = { 35, 60, 210 };
SendDlgItemMessage(hwnd, 100, LB_SETTABSTOPS, SendDlgItemMessage(hwnd, 100, LB_SETTABSTOPS,
sizeof(tabs)/sizeof(*tabs), (LPARAM) tabs); sizeof(tabs) / sizeof(*tabs),
(LPARAM) tabs);
} }
keylist_update(); keylist_update();
return 0; return 0;
@ -987,8 +1038,8 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
freersakey(rkey); freersakey(rkey);
sfree(rkey); sfree(rkey);
} else { } else {
for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) for (i = 0; NULL != (skey = index234(ssh2keys, i));
if (n-- == 0) i++) if (n-- == 0)
break; break;
if (skey) { if (skey) {
del234(ssh2keys, skey); del234(ssh2keys, skey);
@ -1010,7 +1061,8 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
} }
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
int ret; int ret;
static int menuinprogress; static int menuinprogress;
@ -1106,7 +1158,8 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
#ifndef NO_SECURITY #ifndef NO_SECURITY
if (has_security) { if (has_security) {
if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE, if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
GetCurrentProcessId())) == NULL) { GetCurrentProcessId())) ==
NULL) {
#ifdef DEBUG_IPC #ifdef DEBUG_IPC
debug(("couldn't get handle for process\n")); debug(("couldn't get handle for process\n"));
#endif #endif
@ -1128,7 +1181,9 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
&mapowner, NULL, NULL, NULL, &mapowner, NULL, NULL, NULL,
&psd1) != ERROR_SUCCESS)) { &psd1) != ERROR_SUCCESS)) {
#ifdef DEBUG_IPC #ifdef DEBUG_IPC
debug(("couldn't get owner info for filemap: %d\n", rc)); debug(
("couldn't get owner info for filemap: %d\n",
rc));
#endif #endif
return 0; return 0;
} }
@ -1151,7 +1206,12 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
#ifdef DEBUG_IPC #ifdef DEBUG_IPC
debug(("p is %p\n", p)); debug(("p is %p\n", p));
{int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\n", i, ((unsigned char *)p)[i]));} {
int i;
for (i = 0; i < 5; i++)
debug(
("p[%d]=%02x\n", i,
((unsigned char *) p)[i]));}
#endif #endif
answer_msg(p); answer_msg(p);
ret = 1; ret = 1;
@ -1168,7 +1228,8 @@ static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
/* /*
* Fork and Exec the command in cmdline. [DBW] * Fork and Exec the command in cmdline. [DBW]
*/ */
void spawn_cmd(char *cmdline, int show) { void spawn_cmd(char *cmdline, int show)
{
if (ShellExecute(NULL, _T("open"), cmdline, if (ShellExecute(NULL, _T("open"), cmdline,
NULL, NULL, show) <= (HINSTANCE) 32) { NULL, NULL, show) <= (HINSTANCE) 32) {
TCHAR sMsg[140]; TCHAR sMsg[140];
@ -1178,7 +1239,8 @@ void spawn_cmd(char *cmdline, int show) {
} }
} }
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
WNDCLASS wndclass; WNDCLASS wndclass;
MSG msg; MSG msg;
OSVERSIONINFO osi; OSVERSIONINFO osi;
@ -1203,7 +1265,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
* Attempt to get the security API we need. * Attempt to get the security API we need.
*/ */
advapi = LoadLibrary("ADVAPI32.DLL"); advapi = LoadLibrary("ADVAPI32.DLL");
getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo"); getsecurityinfo =
(gsi_fn_t) GetProcAddress(advapi, "GetSecurityInfo");
if (!getsecurityinfo) { if (!getsecurityinfo) {
MessageBox(NULL, MessageBox(NULL,
"Unable to access security APIs. Pageant will\n" "Unable to access security APIs. Pageant will\n"
@ -1237,8 +1300,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
wndclass.cbClsExtra = 0; wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0; wndclass.cbWndExtra = 0;
wndclass.hInstance = inst; wndclass.hInstance = inst;
wndclass.hIcon = LoadIcon (inst, wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
MAKEINTRESOURCE(IDI_MAINICON));
wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM); wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
wndclass.hbrBackground = GetStockObject(BLACK_BRUSH); wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL; wndclass.lpszMenuName = NULL;
@ -1280,7 +1342,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
systray_menu = CreatePopupMenu(); systray_menu = CreatePopupMenu();
/* accelerators used: vkxa */ /* accelerators used: vkxa */
AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys"); AppendMenu(systray_menu, MF_ENABLED, IDM_VIEWKEYS,
"&View Keys");
AppendMenu(systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key"); AppendMenu(systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
AppendMenu(systray_menu, MF_ENABLED, IDM_ABOUT, "&About"); AppendMenu(systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
AppendMenu(systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit"); AppendMenu(systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
@ -1307,11 +1370,11 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
int ignorearg = 0; int ignorearg = 0;
p = cmdline; p = cmdline;
while (*p) { while (*p) {
while (*p && isspace(*p)) p++; while (*p && isspace(*p))
p++;
if (*p && !isspace(*p)) { if (*p && !isspace(*p)) {
char *q = p, *pp = p; char *q = p, *pp = p;
while (*p && (inquotes || !isspace(*p))) while (*p && (inquotes || !isspace(*p))) {
{
if (*p == '"') { if (*p == '"') {
inquotes = !inquotes; inquotes = !inquotes;
p++; p++;
@ -1320,7 +1383,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
*pp++ = *p++; *pp++ = *p++;
} }
if (*pp) { if (*pp) {
if (*p) p++; if (*p)
p++;
*pp++ = '\0'; *pp++ = '\0';
} }
if (!strcmp(q, "-c")) { if (!strcmp(q, "-c")) {
@ -1329,7 +1393,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
* command line should be treated as a * command line should be treated as a
* command to be spawned. * command to be spawned.
*/ */
while (*p && isspace(*p)) p++; while (*p && isspace(*p))
p++;
command = p; command = p;
break; break;
} else { } else {
@ -1340,7 +1405,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
} }
} }
if (command) spawn_cmd (command, show); if (command)
spawn_cmd(command, show);
/* /*
* If Pageant was already running, we leave now. If we haven't * If Pageant was already running, we leave now. If we haven't
@ -1352,7 +1418,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
MessageBox(NULL, "Pageant is already running", "Pageant Error", MessageBox(NULL, "Pageant is already running", "Pageant Error",
MB_ICONERROR | MB_OK); MB_ICONERROR | MB_OK);
} }
if (advapi) FreeLibrary(advapi); if (advapi)
FreeLibrary(advapi);
return 0; return 0;
} }
@ -1377,6 +1444,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
DestroyMenu(systray_menu); DestroyMenu(systray_menu);
} }
if (advapi) FreeLibrary(advapi); if (advapi)
FreeLibrary(advapi);
exit(msg.wParam); exit(msg.wParam);
} }

View File

@ -23,7 +23,8 @@
((unsigned long)(unsigned char)(cp)[2] << 8) | \ ((unsigned long)(unsigned char)(cp)[2] << 8) | \
((unsigned long)(unsigned char)(cp)[3])) ((unsigned long)(unsigned char)(cp)[3]))
int agent_exists(void) { int agent_exists(void)
{
HWND hwnd; HWND hwnd;
hwnd = FindWindow("Pageant", "Pageant"); hwnd = FindWindow("Pageant", "Pageant");
if (!hwnd) if (!hwnd)
@ -32,7 +33,8 @@ int agent_exists(void) {
return TRUE; return TRUE;
} }
void agent_query(void *in, int inlen, void **out, int *outlen) { void agent_query(void *in, int inlen, void **out, int *outlen)
{
HWND hwnd; HWND hwnd;
char mapname[64]; char mapname[64];
HANDLE filemap; HANDLE filemap;
@ -75,7 +77,8 @@ void agent_query(void *in, int inlen, void **out, int *outlen) {
#ifdef TESTMODE #ifdef TESTMODE
int main(void) { int main(void)
{
void *msg; void *msg;
int len; int len;
int i; int i;

91
plink.c
View File

@ -15,7 +15,8 @@
#include "storage.h" #include "storage.h"
#include "tree234.h" #include "tree234.h"
void fatalbox (char *p, ...) { void fatalbox(char *p, ...)
{
va_list ap; va_list ap;
fprintf(stderr, "FATAL ERROR: "); fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p); va_start(ap, p);
@ -25,7 +26,8 @@ void fatalbox (char *p, ...) {
WSACleanup(); WSACleanup();
exit(1); exit(1);
} }
void connection_fatal (char *p, ...) { void connection_fatal(char *p, ...)
{
va_list ap; va_list ap;
fprintf(stderr, "FATAL ERROR: "); fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p); va_start(ap, p);
@ -38,10 +40,13 @@ void connection_fatal (char *p, ...) {
static char *password = NULL; static char *password = NULL;
void logevent(char *string) { } void logevent(char *string)
{
}
void verify_ssh_host_key(char *host, int port, char *keytype, void verify_ssh_host_key(char *host, int port, char *keytype,
char *keystr, char *fingerprint) { char *keystr, char *fingerprint)
{
int ret; int ret;
HANDLE hin; HANDLE hin;
DWORD savemode, i; DWORD savemode, i;
@ -55,8 +60,7 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
"If you trust this host, enter \"y\" to add the key to\n" "If you trust this host, enter \"y\" to add the key to\n"
"PuTTY's cache and carry on connecting.\n" "PuTTY's cache and carry on connecting.\n"
"If you do not trust this host, enter \"n\" to abandon the\n" "If you do not trust this host, enter \"n\" to abandon the\n"
"connection.\n" "connection.\n" "Continue connecting? (y/n) ";
"Continue connecting? (y/n) ";
static const char wrongmsg[] = static const char wrongmsg[] =
"WARNING - POTENTIAL SECURITY BREACH!\n" "WARNING - POTENTIAL SECURITY BREACH!\n"
@ -128,7 +132,8 @@ DWORD orig_console_mode;
WSAEVENT netevent; WSAEVENT netevent;
void from_backend(int is_stderr, char *data, int len) { void from_backend(int is_stderr, char *data, int len)
{
int pos; int pos;
DWORD ret; DWORD ret;
HANDLE h = (is_stderr ? errhandle : outhandle); HANDLE h = (is_stderr ? errhandle : outhandle);
@ -141,8 +146,12 @@ void from_backend(int is_stderr, char *data, int len) {
} }
} }
int term_ldisc(int mode) { return FALSE; } int term_ldisc(int mode)
void ldisc_update(int echo, int edit) { {
return FALSE;
}
void ldisc_update(int echo, int edit)
{
/* Update stdin read mode to reflect changes in line discipline. */ /* Update stdin read mode to reflect changes in line discipline. */
DWORD mode; DWORD mode;
@ -202,7 +211,10 @@ static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
SetConsoleMode(hin, savemode); SetConsoleMode(hin, savemode);
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int) i > maxlen)
i = maxlen - 1;
else
i = i - 2;
str[i] = '\0'; str[i] = '\0';
if (is_pw) if (is_pw)
@ -211,7 +223,8 @@ static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
return 1; return 1;
} }
static DWORD WINAPI stdin_read_thread(void *param) { static DWORD WINAPI stdin_read_thread(void *param)
{
struct input_data *idata = (struct input_data *) param; struct input_data *idata = (struct input_data *) param;
HANDLE inhandle; HANDLE inhandle;
@ -246,7 +259,8 @@ static void usage(void)
exit(1); exit(1);
} }
char *do_select(SOCKET skt, int startup) { char *do_select(SOCKET skt, int startup)
{
int events; int events;
if (startup) { if (startup) {
events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE; events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE;
@ -255,14 +269,17 @@ char *do_select(SOCKET skt, int startup) {
} }
if (WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { if (WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
case WSAENETDOWN: return "Network is down"; case WSAENETDOWN:
default: return "WSAAsyncSelect(): unknown error"; return "Network is down";
default:
return "WSAAsyncSelect(): unknown error";
} }
} }
return NULL; return NULL;
} }
int main(int argc, char **argv) { int main(int argc, char **argv)
{
WSADATA wsadata; WSADATA wsadata;
WORD winsock_ver; WORD winsock_ver;
WSAEVENT stdinevent; WSAEVENT stdinevent;
@ -277,7 +294,8 @@ int main(int argc, char **argv) {
ssh_get_line = get_line; ssh_get_line = get_line;
sklist = NULL; skcount = sksize = 0; sklist = NULL;
skcount = sksize = 0;
flags = FLAG_STDERR; flags = FLAG_STDERR;
/* /*
@ -296,7 +314,8 @@ int main(int argc, char **argv) {
for (i = 0; backends[i].backend != NULL; i++) { for (i = 0; backends[i].backend != NULL; i++) {
if (!strcmp(backends[i].name, p)) { if (!strcmp(backends[i].name, p)) {
default_protocol = cfg.protocol = backends[i].protocol; default_protocol = cfg.protocol = backends[i].protocol;
default_port = cfg.port = backends[i].backend->default_port; default_port = cfg.port =
backends[i].backend->default_port;
break; break;
} }
} }
@ -372,7 +391,8 @@ int main(int argc, char **argv) {
q += 2; q += 2;
cfg.protocol = PROT_TELNET; cfg.protocol = PROT_TELNET;
p = q; p = q;
while (*p && *p != ':' && *p != '/') p++; while (*p && *p != ':' && *p != '/')
p++;
c = *p; c = *p;
if (*p) if (*p)
*p++ = '\0'; *p++ = '\0';
@ -396,8 +416,10 @@ int main(int argc, char **argv) {
j = strlen(backends[i].name); j = strlen(backends[i].name);
if (j == r - p && if (j == r - p &&
!memcmp(backends[i].name, p, j)) { !memcmp(backends[i].name, p, j)) {
default_protocol = cfg.protocol = backends[i].protocol; default_protocol = cfg.protocol =
portnumber = backends[i].backend->default_port; backends[i].protocol;
portnumber =
backends[i].backend->default_port;
p = r + 1; p = r + 1;
break; break;
} }
@ -415,7 +437,8 @@ int main(int argc, char **argv) {
* database. * database.
*/ */
r = strrchr(p, '@'); r = strrchr(p, '@');
if (r == p) p++, r = NULL; /* discount initial @ */ if (r == p)
p++, r = NULL; /* discount initial @ */
if (r == NULL) { if (r == NULL) {
/* /*
* One string. * One string.
@ -445,13 +468,19 @@ int main(int argc, char **argv) {
char *cp = cfg.remote_cmd; char *cp = cfg.remote_cmd;
int len2; int len2;
strncpy(cp, p, len); cp[len] = '\0'; strncpy(cp, p, len);
len2 = strlen(cp); len -= len2; cp += len2; cp[len] = '\0';
len2 = strlen(cp);
len -= len2;
cp += len2;
while (--argc) { while (--argc) {
if (len > 0) if (len > 0)
len--, *cp++ = ' '; len--, *cp++ = ' ';
strncpy(cp, *++argv, len); cp[len] = '\0'; strncpy(cp, *++argv, len);
len2 = strlen(cp); len -= len2; cp += len2; cp[len] = '\0';
len2 = strlen(cp);
len -= len2;
cp += len2;
} }
cfg.nopty = TRUE; /* command => no terminal */ cfg.nopty = TRUE; /* command => no terminal */
break; /* done with cmdline */ break; /* done with cmdline */
@ -479,7 +508,8 @@ int main(int argc, char **argv) {
break; break;
} }
if (back == NULL) { if (back == NULL) {
fprintf(stderr, "Internal fault: Unsupported protocol found\n"); fprintf(stderr,
"Internal fault: Unsupported protocol found\n");
return 1; return 1;
} }
} }
@ -585,9 +615,9 @@ int main(int argc, char **argv) {
*/ */
/* Count the active sockets. */ /* Count the active sockets. */
i = 0; i = 0;
for (socket = first_socket(&socketstate); socket != INVALID_SOCKET; for (socket = first_socket(&socketstate);
socket = next_socket(&socketstate)) socket != INVALID_SOCKET;
i++; socket = next_socket(&socketstate)) i++;
/* Expand the buffer if necessary. */ /* Expand the buffer if necessary. */
if (i > sksize) { if (i > sksize) {
@ -597,7 +627,8 @@ int main(int argc, char **argv) {
/* Retrieve the sockets into sklist. */ /* Retrieve the sockets into sklist. */
skcount = 0; skcount = 0;
for (socket = first_socket(&socketstate); socket != INVALID_SOCKET; for (socket = first_socket(&socketstate);
socket != INVALID_SOCKET;
socket = next_socket(&socketstate)) { socket = next_socket(&socketstate)) {
sklist[skcount++] = socket; sklist[skcount++] = socket;
} }

127
psftp.c
View File

@ -20,7 +20,8 @@
* String handling routines. * String handling routines.
*/ */
char *dupstr(char *s) { char *dupstr(char *s)
{
int len = strlen(s); int len = strlen(s);
char *p = smalloc(len + 1); char *p = smalloc(len + 1);
strcpy(p, s); strcpy(p, s);
@ -28,7 +29,8 @@ char *dupstr(char *s) {
} }
/* Allocate the concatenation of N strings. Terminate arg list with NULL. */ /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
char *dupcat(char *s1, ...) { char *dupcat(char *s1, ...)
{
int len; int len;
char *p, *q, *sn; char *p, *q, *sn;
va_list ap; va_list ap;
@ -75,7 +77,8 @@ char *pwd, *homedir;
* canonification fails, at least fall back to returning a _valid_ * canonification fails, at least fall back to returning a _valid_
* pathname (though it may be ugly, eg /home/simon/../foobar). * pathname (though it may be ugly, eg /home/simon/../foobar).
*/ */
char *canonify(char *name) { char *canonify(char *name)
{
char *fullname, *canonname; char *fullname, *canonname;
if (name[0] == '/') { if (name[0] == '/') {
@ -157,8 +160,8 @@ char *canonify(char *name) {
* component. Concatenate the last component and return. * component. Concatenate the last component and return.
*/ */
returnname = dupcat(canonname, returnname = dupcat(canonname,
canonname[strlen(canonname)-1] == '/' ? "" : "/", canonname[strlen(canonname) - 1] ==
fullname+i+1, NULL); '/' ? "" : "/", fullname + i + 1, NULL);
sfree(fullname); sfree(fullname);
sfree(canonname); sfree(canonname);
return returnname; return returnname;
@ -174,16 +177,19 @@ struct sftp_command {
int (*obey) (struct sftp_command *); /* returns <0 to quit */ int (*obey) (struct sftp_command *); /* returns <0 to quit */
}; };
int sftp_cmd_null(struct sftp_command *cmd) { int sftp_cmd_null(struct sftp_command *cmd)
{
return 0; return 0;
} }
int sftp_cmd_unknown(struct sftp_command *cmd) { int sftp_cmd_unknown(struct sftp_command *cmd)
{
printf("psftp: unknown command \"%s\"\n", cmd->words[0]); printf("psftp: unknown command \"%s\"\n", cmd->words[0]);
return 0; return 0;
} }
int sftp_cmd_quit(struct sftp_command *cmd) { int sftp_cmd_quit(struct sftp_command *cmd)
{
return -1; return -1;
} }
@ -191,12 +197,14 @@ int sftp_cmd_quit(struct sftp_command *cmd) {
* List a directory. If no arguments are given, list pwd; otherwise * List a directory. If no arguments are given, list pwd; otherwise
* list the directory given in words[1]. * list the directory given in words[1].
*/ */
static int sftp_ls_compare(const void *av, const void *bv) { static int sftp_ls_compare(const void *av, const void *bv)
{
const struct fxp_name *a = (const struct fxp_name *) av; const struct fxp_name *a = (const struct fxp_name *) av;
const struct fxp_name *b = (const struct fxp_name *) bv; const struct fxp_name *b = (const struct fxp_name *) bv;
return strcmp(a->filename, b->filename); return strcmp(a->filename, b->filename);
} }
int sftp_cmd_ls(struct sftp_command *cmd) { int sftp_cmd_ls(struct sftp_command *cmd)
{
struct fxp_handle *dirh; struct fxp_handle *dirh;
struct fxp_names *names; struct fxp_names *names;
struct fxp_name *ournames; struct fxp_name *ournames;
@ -240,7 +248,8 @@ int sftp_cmd_ls(struct sftp_command *cmd) {
if (nnames + names->nnames >= namesize) { if (nnames + names->nnames >= namesize) {
namesize += names->nnames + 128; namesize += names->nnames + 128;
ournames = srealloc(ournames, namesize * sizeof(*ournames)); ournames =
srealloc(ournames, namesize * sizeof(*ournames));
} }
for (i = 0; i < names->nnames; i++) for (i = 0; i < names->nnames; i++)
@ -273,7 +282,8 @@ int sftp_cmd_ls(struct sftp_command *cmd) {
* Change directories. We do this by canonifying the new name, then * Change directories. We do this by canonifying the new name, then
* trying to OPENDIR it. Only if that succeeds do we set the new pwd. * trying to OPENDIR it. Only if that succeeds do we set the new pwd.
*/ */
int sftp_cmd_cd(struct sftp_command *cmd) { int sftp_cmd_cd(struct sftp_command *cmd)
{
struct fxp_handle *dirh; struct fxp_handle *dirh;
char *dir; char *dir;
@ -306,7 +316,8 @@ int sftp_cmd_cd(struct sftp_command *cmd) {
/* /*
* Get a file and save it at the local end. * Get a file and save it at the local end.
*/ */
int sftp_cmd_get(struct sftp_command *cmd) { int sftp_cmd_get(struct sftp_command *cmd)
{
struct fxp_handle *fh; struct fxp_handle *fh;
char *fname, *outfname; char *fname, *outfname;
uint64 offset; uint64 offset;
@ -352,8 +363,7 @@ int sftp_cmd_get(struct sftp_command *cmd) {
int wpos, wlen; int wpos, wlen;
len = fxp_read(fh, buffer, offset, sizeof(buffer)); len = fxp_read(fh, buffer, offset, sizeof(buffer));
if ((len == -1 && fxp_error_type() == SSH_FX_EOF) || if ((len == -1 && fxp_error_type() == SSH_FX_EOF) || len == 0)
len == 0)
break; break;
if (len == -1) { if (len == -1) {
printf("error while reading: %s\n", fxp_error()); printf("error while reading: %s\n", fxp_error());
@ -384,7 +394,8 @@ int sftp_cmd_get(struct sftp_command *cmd) {
/* /*
* Send a file and store it at the remote end. * Send a file and store it at the remote end.
*/ */
int sftp_cmd_put(struct sftp_command *cmd) { int sftp_cmd_put(struct sftp_command *cmd)
{
struct fxp_handle *fh; struct fxp_handle *fh;
char *fname, *origoutfname, *outfname; char *fname, *origoutfname, *outfname;
uint64 offset; uint64 offset;
@ -457,20 +468,21 @@ static struct sftp_cmd_lookup {
* List of sftp commands. This is binary-searched so it MUST be * List of sftp commands. This is binary-searched so it MUST be
* in ASCII order. * in ASCII order.
*/ */
{"bye", sftp_cmd_quit}, {
{"cd", sftp_cmd_cd}, "bye", sftp_cmd_quit}, {
{"dir", sftp_cmd_ls}, "cd", sftp_cmd_cd}, {
{"exit", sftp_cmd_quit}, "dir", sftp_cmd_ls}, {
{"get", sftp_cmd_get}, "exit", sftp_cmd_quit}, {
{"ls", sftp_cmd_ls}, "get", sftp_cmd_get}, {
{"put", sftp_cmd_put}, "ls", sftp_cmd_ls}, {
{"quit", sftp_cmd_quit}, "put", sftp_cmd_put}, {
}; "quit", sftp_cmd_quit},};
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* Command line reading and parsing. * Command line reading and parsing.
*/ */
struct sftp_command *sftp_getcmd(void) { struct sftp_command *sftp_getcmd(void)
{
char *line; char *line;
int linelen, linesize; int linelen, linesize;
struct sftp_command *cmd; struct sftp_command *cmd;
@ -528,7 +540,8 @@ struct sftp_command *sftp_getcmd(void) {
p = line; p = line;
while (*p) { while (*p) {
/* skip whitespace */ /* skip whitespace */
while (*p && (*p == ' ' || *p == '\t')) p++; while (*p && (*p == ' ' || *p == '\t'))
p++;
/* mark start of word */ /* mark start of word */
q = r = p; /* q sits at start, r writes word */ q = r = p; /* q sits at start, r writes word */
quoting = 0; quoting = 0;
@ -542,11 +555,13 @@ struct sftp_command *sftp_getcmd(void) {
else else
*r++ = *p++; *r++ = *p++;
} }
if (*p) p++; /* skip over the whitespace */ if (*p)
p++; /* skip over the whitespace */
*r = '\0'; *r = '\0';
if (cmd->nwords >= cmd->wordssize) { if (cmd->nwords >= cmd->wordssize) {
cmd->wordssize = cmd->nwords + 16; cmd->wordssize = cmd->nwords + 16;
cmd->words = srealloc(cmd->words, cmd->wordssize*sizeof(char *)); cmd->words =
srealloc(cmd->words, cmd->wordssize * sizeof(char *));
} }
cmd->words[cmd->nwords++] = q; cmd->words[cmd->nwords++] = q;
} }
@ -581,14 +596,14 @@ struct sftp_command *sftp_getcmd(void) {
return cmd; return cmd;
} }
void do_sftp(void) { void do_sftp(void)
{
/* /*
* Do protocol initialisation. * Do protocol initialisation.
*/ */
if (!fxp_init()) { if (!fxp_init()) {
fprintf(stderr, fprintf(stderr,
"Fatal: unable to initialise SFTP: %s\n", "Fatal: unable to initialise SFTP: %s\n", fxp_error());
fxp_error());
return; return;
} }
@ -626,7 +641,8 @@ void do_sftp(void) {
static int verbose = 0; static int verbose = 0;
void verify_ssh_host_key(char *host, int port, char *keytype, void verify_ssh_host_key(char *host, int port, char *keytype,
char *keystr, char *fingerprint) { char *keystr, char *fingerprint)
{
int ret; int ret;
static const char absentmsg[] = static const char absentmsg[] =
@ -638,8 +654,7 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
"If you trust this host, enter \"y\" to add the key to\n" "If you trust this host, enter \"y\" to add the key to\n"
"PuTTY's cache and carry on connecting.\n" "PuTTY's cache and carry on connecting.\n"
"If you do not trust this host, enter \"n\" to abandon the\n" "If you do not trust this host, enter \"n\" to abandon the\n"
"connection.\n" "connection.\n" "Continue connecting? (y/n) ";
"Continue connecting? (y/n) ";
static const char wrongmsg[] = static const char wrongmsg[] =
"WARNING - POTENTIAL SECURITY BREACH!\n" "WARNING - POTENTIAL SECURITY BREACH!\n"
@ -723,9 +738,12 @@ void connection_fatal(char *fmt, ...)
exit(1); exit(1);
} }
void logevent(char *string) { } void logevent(char *string)
{
}
void ldisc_send(char *buf, int len) { void ldisc_send(char *buf, int len)
{
/* /*
* This is only here because of the calls to ldisc_send(NULL, * This is only here because of the calls to ldisc_send(NULL,
* 0) in ssh.c. Nothing in PSFTP actually needs to use the * 0) in ssh.c. Nothing in PSFTP actually needs to use the
@ -739,7 +757,8 @@ void ldisc_send(char *buf, int len) {
* Be told what socket we're supposed to be using. * Be told what socket we're supposed to be using.
*/ */
static SOCKET sftp_ssh_socket; static SOCKET sftp_ssh_socket;
char *do_select(SOCKET skt, int startup) { char *do_select(SOCKET skt, int startup)
{
if (startup) if (startup)
sftp_ssh_socket = skt; sftp_ssh_socket = skt;
else else
@ -761,7 +780,8 @@ static unsigned char *outptr; /* where to put the data */
static unsigned outlen; /* how much data required */ static unsigned outlen; /* how much data required */
static unsigned char *pending = NULL; /* any spare data */ static unsigned char *pending = NULL; /* any spare data */
static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
void from_backend(int is_stderr, char *data, int datalen) { void from_backend(int is_stderr, char *data, int datalen)
{
unsigned char *p = (unsigned char *) data; unsigned char *p = (unsigned char *) data;
unsigned len = (unsigned) datalen; unsigned len = (unsigned) datalen;
@ -782,10 +802,13 @@ void from_backend(int is_stderr, char *data, int datalen) {
if (outlen > 0) { if (outlen > 0) {
unsigned used = outlen; unsigned used = outlen;
if (used > len) used = len; if (used > len)
used = len;
memcpy(outptr, p, used); memcpy(outptr, p, used);
outptr += used; outlen -= used; outptr += used;
p += used; len -= used; outlen -= used;
p += used;
len -= used;
} }
if (len > 0) { if (len > 0) {
@ -800,7 +823,8 @@ void from_backend(int is_stderr, char *data, int datalen) {
pendlen += len; pendlen += len;
} }
} }
int sftp_recvdata(char *buf, int len) { int sftp_recvdata(char *buf, int len)
{
outptr = (unsigned char *) buf; outptr = (unsigned char *) buf;
outlen = len; outlen = len;
@ -838,7 +862,8 @@ int sftp_recvdata(char *buf, int len) {
return 1; return 1;
} }
int sftp_senddata(char *buf, int len) { int sftp_senddata(char *buf, int len)
{
back->send((unsigned char *) buf, len); back->send((unsigned char *) buf, len);
return 1; return 1;
} }
@ -846,7 +871,8 @@ int sftp_senddata(char *buf, int len) {
/* /*
* Loop through the ssh connection and authentication process. * Loop through the ssh connection and authentication process.
*/ */
static void ssh_sftp_init(void) { static void ssh_sftp_init(void)
{
if (sftp_ssh_socket == INVALID_SOCKET) if (sftp_ssh_socket == INVALID_SOCKET)
return; return;
while (!back->sendok()) { while (!back->sendok()) {
@ -898,7 +924,10 @@ static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
SetConsoleMode(hin, savemode); SetConsoleMode(hin, savemode);
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int) i > maxlen)
i = maxlen - 1;
else
i = i - 2;
str[i] = '\0'; str[i] = '\0';
if (is_pw) if (is_pw)
@ -920,8 +949,7 @@ static void init_winsock(void)
fprintf(stderr, "Unable to initialise WinSock"); fprintf(stderr, "Unable to initialise WinSock");
exit(1); exit(1);
} }
if (LOBYTE(wsadata.wVersion) != 1 || if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) {
HIBYTE(wsadata.wVersion) != 1) {
fprintf(stderr, "WinSock version is incompatible with 1.1"); fprintf(stderr, "WinSock version is incompatible with 1.1");
exit(1); exit(1);
} }
@ -998,7 +1026,8 @@ int main(int argc, char *argv[])
} else { } else {
*host++ = '\0'; *host++ = '\0';
if (user) { if (user) {
printf("psftp: multiple usernames specified; using \"%s\"\n", user); printf("psftp: multiple usernames specified; using \"%s\"\n",
user);
} else } else
user = userhost; user = userhost;
} }

View File

@ -38,7 +38,8 @@ struct progress {
HWND progbar; HWND progbar;
}; };
static void progress_update(void *param, int phase, int iprogress) { static void progress_update(void *param, int phase, int iprogress)
{
struct progress *p = (struct progress *) param; struct progress *p = (struct progress *) param;
unsigned progress = iprogress; unsigned progress = iprogress;
int position; int position;
@ -46,11 +47,13 @@ static void progress_update(void *param, int phase, int iprogress) {
switch (phase) { switch (phase) {
case -1: case -1:
p->phase1param = 0x10000 + progress; p->phase1param = 0x10000 + progress;
p->phase1current = 0x10000; p->phase1n = 0; p->phase1current = 0x10000;
p->phase1n = 0;
return; return;
case -2: case -2:
p->phase2param = 0x10000 + progress; p->phase2param = 0x10000 + progress;
p->phase2current = 0x10000; p->phase2n = 0; p->phase2current = 0x10000;
p->phase2n = 0;
return; return;
case -3: case -3:
p->phase3mult = PHASE3TOTAL / progress; p->phase3mult = PHASE3TOTAL / progress;
@ -92,7 +95,8 @@ struct PassphraseProcStruct {
* Dialog-box function for the passphrase box. * Dialog-box function for the passphrase box.
*/ */
static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
static char *passphrase = NULL; static char *passphrase = NULL;
struct PassphraseProcStruct *p; struct PassphraseProcStruct *p;
@ -111,7 +115,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -136,7 +141,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
return 0; return 0;
case 102: /* edit box */ case 102: /* edit box */
if ((HIWORD(wParam) == EN_CHANGE) && passphrase) { if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1); GetDlgItemText(hwnd, 102, passphrase,
PASSPHRASE_MAXLEN - 1);
passphrase[PASSPHRASE_MAXLEN - 1] = '\0'; passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
} }
return 0; return 0;
@ -154,7 +160,8 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
* FILENAME_MAX. * FILENAME_MAX.
*/ */
static int prompt_keyfile(HWND hwnd, char *dlgtitle, static int prompt_keyfile(HWND hwnd, char *dlgtitle,
char *filename, int save) { char *filename, int save)
{
OPENFILENAME of; OPENFILENAME of;
memset(&of, 0, sizeof(of)); memset(&of, 0, sizeof(of));
#ifdef OPENFILENAME_SIZE_VERSION_400 #ifdef OPENFILENAME_SIZE_VERSION_400
@ -166,7 +173,8 @@ static int prompt_keyfile(HWND hwnd, char *dlgtitle,
of.lpstrFilter = "All Files\0*\0\0\0"; of.lpstrFilter = "All Files\0*\0\0\0";
of.lpstrCustomFilter = NULL; of.lpstrCustomFilter = NULL;
of.nFilterIndex = 1; of.nFilterIndex = 1;
of.lpstrFile = filename; *filename = '\0'; of.lpstrFile = filename;
*filename = '\0';
of.nMaxFile = FILENAME_MAX; of.nMaxFile = FILENAME_MAX;
of.lpstrFileTitle = NULL; of.lpstrFileTitle = NULL;
of.lpstrInitialDir = NULL; of.lpstrInitialDir = NULL;
@ -182,14 +190,16 @@ static int prompt_keyfile(HWND hwnd, char *dlgtitle,
* This function is needed to link with the DES code. We need not * This function is needed to link with the DES code. We need not
* have it do anything at all. * have it do anything at all.
*/ */
void logevent(char *msg) { void logevent(char *msg)
{
} }
/* /*
* Dialog-box function for the Licence box. * Dialog-box function for the Licence box.
*/ */
static int CALLBACK LicenceProc(HWND hwnd, UINT msg, static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
switch (msg) { switch (msg) {
case WM_INITDIALOG: case WM_INITDIALOG:
/* /*
@ -201,7 +211,8 @@ static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -225,7 +236,8 @@ static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
* Dialog-box function for the About box. * Dialog-box function for the About box.
*/ */
static int CALLBACK AboutProc(HWND hwnd, UINT msg, static int CALLBACK AboutProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
switch (msg) { switch (msg) {
case WM_INITDIALOG: case WM_INITDIALOG:
/* /*
@ -237,7 +249,8 @@ static int CALLBACK AboutProc (HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -273,7 +286,8 @@ struct rsa_key_thread_params {
int keysize; /* bits in key */ int keysize; /* bits in key */
struct RSAKey *key; struct RSAKey *key;
}; };
static DWORD WINAPI generate_rsa_key_thread(void *param) { static DWORD WINAPI generate_rsa_key_thread(void *param)
{
struct rsa_key_thread_params *params = struct rsa_key_thread_params *params =
(struct rsa_key_thread_params *) param; (struct rsa_key_thread_params *) param;
struct progress prog; struct progress prog;
@ -300,13 +314,15 @@ struct MainDlgState {
struct RSAKey key; struct RSAKey key;
}; };
static void hidemany(HWND hwnd, const int *ids, int hideit) { static void hidemany(HWND hwnd, const int *ids, int hideit)
{
while (*ids) { while (*ids) {
ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW)); ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
} }
} }
static void setupbigedit1(HWND hwnd, int id, struct RSAKey *key) { static void setupbigedit1(HWND hwnd, int id, struct RSAKey *key)
{
char *buffer; char *buffer;
char *dec1, *dec2; char *dec1, *dec2;
@ -315,15 +331,15 @@ static void setupbigedit1(HWND hwnd, int id, struct RSAKey *key) {
buffer = smalloc(strlen(dec1) + strlen(dec2) + buffer = smalloc(strlen(dec1) + strlen(dec2) +
strlen(key->comment) + 30); strlen(key->comment) + 30);
sprintf(buffer, "%d %s %s %s", sprintf(buffer, "%d %s %s %s",
bignum_bitcount(key->modulus), bignum_bitcount(key->modulus), dec1, dec2, key->comment);
dec1, dec2, key->comment);
SetDlgItemText(hwnd, id, buffer); SetDlgItemText(hwnd, id, buffer);
sfree(dec1); sfree(dec1);
sfree(dec2); sfree(dec2);
sfree(buffer); sfree(buffer);
} }
static void setupbigedit2(HWND hwnd, int id, struct ssh2_userkey *key) { static void setupbigedit2(HWND hwnd, int id, struct ssh2_userkey *key)
{
unsigned char *pub_blob; unsigned char *pub_blob;
char *buffer, *p; char *buffer, *p;
int pub_len; int pub_len;
@ -353,7 +369,8 @@ static void setupbigedit2(HWND hwnd, int id, struct ssh2_userkey *key) {
* Dialog-box function for the main PuTTYgen dialog box. * Dialog-box function for the main PuTTYgen dialog box.
*/ */
static int CALLBACK MainDlgProc(HWND hwnd, UINT msg, static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) { WPARAM wParam, LPARAM lParam)
{
enum { enum {
controlidstart = 100, controlidstart = 100,
IDC_TITLE, IDC_TITLE,
@ -376,13 +393,15 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
IDC_ABOUT, IDC_ABOUT,
}; };
static const int nokey_ids[] = { IDC_NOKEY, 0 }; static const int nokey_ids[] = { IDC_NOKEY, 0 };
static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 }; static const int generating_ids[] =
{ IDC_GENERATING, IDC_PROGRESS, 0 };
static const int gotkey_ids[] = { static const int gotkey_ids[] = {
IDC_PKSTATIC, IDC_KEYDISPLAY, IDC_PKSTATIC, IDC_KEYDISPLAY,
IDC_FPSTATIC, IDC_FINGERPRINT, IDC_FPSTATIC, IDC_FINGERPRINT,
IDC_COMMENTSTATIC, IDC_COMMENTEDIT, IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 }; IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0
};
static const char generating_msg[] = static const char generating_msg[] =
"Please wait while a key is generated..."; "Please wait while a key is generated...";
static const char entropy_msg[] = static const char entropy_msg[] =
@ -400,7 +419,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
hw = GetDesktopWindow(); hw = GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, MoveWindow(hwnd,
(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 2, (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
rd.right - rd.left, rd.bottom - rd.top, TRUE); rd.right - rd.left, rd.bottom - rd.top, TRUE);
} }
@ -419,13 +439,11 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
ctlposinit(&cp, hwnd, 10, 10, 10); ctlposinit(&cp, hwnd, 10, 10, 10);
bartitle(&cp, "Public and private key generation for PuTTY", bartitle(&cp, "Public and private key generation for PuTTY",
IDC_TITLE); IDC_TITLE);
beginbox(&cp, "Key", beginbox(&cp, "Key", IDC_BOX_KEY);
IDC_BOX_KEY);
cp2 = cp; cp2 = cp;
statictext(&cp2, "No key.", IDC_NOKEY); statictext(&cp2, "No key.", IDC_NOKEY);
cp2 = cp; cp2 = cp;
statictext(&cp2, "", statictext(&cp2, "", IDC_GENERATING);
IDC_GENERATING);
progressbar(&cp2, IDC_PROGRESS); progressbar(&cp2, IDC_PROGRESS);
bigeditctrl(&cp, bigeditctrl(&cp,
"&Public key for pasting into authorized_keys file:", "&Public key for pasting into authorized_keys file:",
@ -433,16 +451,16 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0); SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC, staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
IDC_FINGERPRINT, 75); IDC_FINGERPRINT, 75);
SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0); SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1,
0);
staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC, staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
IDC_COMMENTEDIT, 75); IDC_COMMENTEDIT, 75);
staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC, staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
IDC_PASSPHRASE1EDIT, 75); IDC_PASSPHRASE1EDIT, 75);
staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC, staticpassedit(&cp, "C&onfirm passphrase:",
IDC_PASSPHRASE2EDIT, 75); IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 75);
endbox(&cp); endbox(&cp);
beginbox(&cp, "Actions", beginbox(&cp, "Actions", IDC_BOX_ACTIONS);
IDC_BOX_ACTIONS);
staticbtn(&cp, "Generate a public/private key pair", staticbtn(&cp, "Generate a public/private key pair",
IDC_GENSTATIC, "&Generate", IDC_GENERATE); IDC_GENSTATIC, "&Generate", IDC_GENERATE);
staticbtn(&cp, "Load an existing private key file", staticbtn(&cp, "Load an existing private key file",
@ -450,8 +468,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
staticbtn(&cp, "Save the generated key to a new file", staticbtn(&cp, "Save the generated key to a new file",
IDC_SAVESTATIC, "&Save", IDC_SAVE); IDC_SAVESTATIC, "&Save", IDC_SAVE);
endbox(&cp); endbox(&cp);
beginbox(&cp, "Parameters", beginbox(&cp, "Parameters", IDC_BOX_PARAMS);
IDC_BOX_PARAMS);
radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 2, radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 2,
"SSH&1 (RSA)", IDC_KEYSSH1, "SSH&1 (RSA)", IDC_KEYSSH1,
"SSH2 &RSA", IDC_KEYSSH2RSA, NULL); "SSH2 &RSA", IDC_KEYSSH2RSA, NULL);
@ -477,8 +494,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
if (state->collecting_entropy && if (state->collecting_entropy &&
state->entropy && state->entropy && state->entropy_got < state->entropy_required) {
state->entropy_got < state->entropy_required) {
state->entropy[state->entropy_got++] = lParam; state->entropy[state->entropy_got++] = lParam;
state->entropy[state->entropy_got++] = GetMessageTime(); state->entropy[state->entropy_got++] = GetMessageTime();
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
@ -532,7 +548,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
*state->commentptr = smalloc(len + 1); *state->commentptr = smalloc(len + 1);
GetWindowText(editctl, *state->commentptr, len + 1); GetWindowText(editctl, *state->commentptr, len + 1);
if (state->ssh2) { if (state->ssh2) {
setupbigedit2(hwnd, IDC_KEYDISPLAY, &state->ssh2key); setupbigedit2(hwnd, IDC_KEYDISPLAY,
&state->ssh2key);
} else { } else {
setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key);
} }
@ -546,12 +563,13 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
SetActiveWindow(hwnd); SetActiveWindow(hwnd);
return 0; return 0;
case IDC_GENERATE: case IDC_GENERATE:
state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); state =
(struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
if (!state->generation_thread_exists) { if (!state->generation_thread_exists) {
BOOL ok; BOOL ok;
state->keysize = GetDlgItemInt(hwnd, IDC_BITS, state->keysize = GetDlgItemInt(hwnd, IDC_BITS, &ok, FALSE);
&ok, FALSE); if (!ok)
if (!ok) state->keysize = DEFAULT_KEYSIZE; state->keysize = DEFAULT_KEYSIZE;
/* If we ever introduce a new key type, check it here! */ /* If we ever introduce a new key type, check it here! */
state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1); state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1);
if (state->keysize < 256) { if (state->keysize < 256) {
@ -601,7 +619,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
} }
break; break;
case IDC_SAVE: case IDC_SAVE:
state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); state =
(struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
if (state->key_exists) { if (state->key_exists) {
char filename[FILENAME_MAX]; char filename[FILENAME_MAX];
char passphrase[PASSPHRASE_MAXLEN]; char passphrase[PASSPHRASE_MAXLEN];
@ -613,8 +632,7 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
if (strcmp(passphrase, passphrase2)) { if (strcmp(passphrase, passphrase2)) {
MessageBox(hwnd, MessageBox(hwnd,
"The two passphrases given do not match.", "The two passphrases given do not match.",
"PuTTYgen Error", "PuTTYgen Error", MB_OK | MB_ICONERROR);
MB_OK | MB_ICONERROR);
break; break;
} }
if (!*passphrase) { if (!*passphrase) {
@ -643,25 +661,25 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
} }
if (state->ssh2) { if (state->ssh2) {
ret = ssh2_save_userkey(filename, &state->ssh2key, ret = ssh2_save_userkey(filename, &state->ssh2key,
*passphrase ? passphrase : NULL); *passphrase ? passphrase :
NULL);
} else { } else {
ret = saversakey(filename, &state->key, ret = saversakey(filename, &state->key,
*passphrase ? passphrase : NULL); *passphrase ? passphrase : NULL);
} }
if (ret <= 0) { if (ret <= 0) {
MessageBox(hwnd, "Unable to save key file", MessageBox(hwnd, "Unable to save key file",
"PuTTYgen Error", "PuTTYgen Error", MB_OK | MB_ICONERROR);
MB_OK | MB_ICONERROR);
} }
} }
} }
break; break;
case IDC_LOAD: case IDC_LOAD:
state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); state =
(struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
if (!state->generation_thread_exists) { if (!state->generation_thread_exists) {
char filename[FILENAME_MAX]; char filename[FILENAME_MAX];
if (prompt_keyfile(hwnd, "Load private key:", if (prompt_keyfile(hwnd, "Load private key:", filename, 0)) {
filename, 0)) {
char passphrase[PASSPHRASE_MAXLEN]; char passphrase[PASSPHRASE_MAXLEN];
int needs_pass; int needs_pass;
int ver; int ver;
@ -682,7 +700,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
if (ver == 1) if (ver == 1)
needs_pass = rsakey_encrypted(filename, &comment); needs_pass = rsakey_encrypted(filename, &comment);
else else
needs_pass = ssh2_userkey_encrypted(filename, &comment); needs_pass =
ssh2_userkey_encrypted(filename, &comment);
pps.passphrase = passphrase; pps.passphrase = passphrase;
pps.comment = comment; pps.comment = comment;
do { do {
@ -699,9 +718,11 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
} else } else
*passphrase = '\0'; *passphrase = '\0';
if (ver == 1) if (ver == 1)
ret = loadrsakey(filename, &newkey1, passphrase); ret =
loadrsakey(filename, &newkey1, passphrase);
else { else {
newkey2 = ssh2_load_userkey(filename, passphrase); newkey2 =
ssh2_load_userkey(filename, passphrase);
if (newkey2 == SSH2_WRONG_PASSPHRASE) if (newkey2 == SSH2_WRONG_PASSPHRASE)
ret = -1; ret = -1;
else if (!newkey2) else if (!newkey2)
@ -710,7 +731,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
ret = 1; ret = 1;
} }
} while (ret == -1); } while (ret == -1);
if (comment) sfree(comment); if (comment)
sfree(comment);
if (ret == 0) { if (ret == 0) {
MessageBox(NULL, "Couldn't load private key.", MessageBox(NULL, "Couldn't load private key.",
"PuTTYgen Error", MB_OK | MB_ICONERROR); "PuTTYgen Error", MB_OK | MB_ICONERROR);
@ -740,7 +762,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
*/ */
savecomment = state->key.comment; savecomment = state->key.comment;
state->key.comment = NULL; state->key.comment = NULL;
rsa_fingerprint(buf, sizeof(buf), &state->key); rsa_fingerprint(buf, sizeof(buf),
&state->key);
state->key.comment = savecomment; state->key.comment = savecomment;
SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
@ -749,26 +772,30 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
* of the key, for pasting into * of the key, for pasting into
* .ssh/authorized_keys on a Unix box. * .ssh/authorized_keys on a Unix box.
*/ */
setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); setupbigedit1(hwnd, IDC_KEYDISPLAY,
&state->key);
} else { } else {
char *fp; char *fp;
char *savecomment; char *savecomment;
state->ssh2 = TRUE; state->ssh2 = TRUE;
state->commentptr = &state->ssh2key.comment; state->commentptr =
&state->ssh2key.comment;
state->ssh2key = *newkey2; /* structure copy */ state->ssh2key = *newkey2; /* structure copy */
sfree(newkey2); sfree(newkey2);
savecomment = state->ssh2key.comment; savecomment = state->ssh2key.comment;
state->ssh2key.comment = NULL; state->ssh2key.comment = NULL;
fp = state-> fp =
ssh2key.alg->fingerprint(state->ssh2key.data); state->ssh2key.alg->
fingerprint(state->ssh2key.data);
state->ssh2key.comment = savecomment; state->ssh2key.comment = savecomment;
SetDlgItemText(hwnd, IDC_FINGERPRINT, fp); SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
sfree(fp); sfree(fp);
setupbigedit2(hwnd, IDC_KEYDISPLAY, &state->ssh2key); setupbigedit2(hwnd, IDC_KEYDISPLAY,
&state->ssh2key);
} }
SetDlgItemText(hwnd, IDC_COMMENTEDIT, SetDlgItemText(hwnd, IDC_COMMENTEDIT,
*state->commentptr); *state->commentptr);
@ -791,7 +818,8 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
state->generation_thread_exists = FALSE; state->generation_thread_exists = FALSE;
state->key_exists = TRUE; state->key_exists = TRUE;
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0); SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE,
0);
EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
@ -875,9 +903,11 @@ static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
return 0; return 0;
} }
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
InitCommonControls(); InitCommonControls();
hinst = inst; hinst = inst;
random_init(); random_init();
return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK; return DialogBox(hinst, MAKEINTRESOURCE(201), NULL,
MainDlgProc) != IDOK;
} }

35
raw.c
View File

@ -20,11 +20,14 @@ static char *sb_buf = NULL;
static int sb_size = 0; static int sb_size = 0;
#define SB_DELTA 1024 #define SB_DELTA 1024
static void c_write (char *buf, int len) { static void c_write(char *buf, int len)
{
from_backend(0, buf, len); from_backend(0, buf, len);
} }
static int raw_closing (Plug plug, char *error_msg, int error_code, int calling_back) { static int raw_closing(Plug plug, char *error_msg, int error_code,
int calling_back)
{
sk_close(s); sk_close(s);
s = NULL; s = NULL;
if (error_msg) { if (error_msg) {
@ -34,7 +37,8 @@ static int raw_closing (Plug plug, char *error_msg, int error_code, int calling_
return 0; return 0;
} }
static int raw_receive (Plug plug, int urgent, char *data, int len) { static int raw_receive(Plug plug, int urgent, char *data, int len)
{
c_write(data, len); c_write(data, len);
return 1; return 1;
} }
@ -46,7 +50,8 @@ static int raw_receive (Plug plug, int urgent, char *data, int len) {
* *
* Also places the canonical host name into `realhost'. * Also places the canonical host name into `realhost'.
*/ */
static char *raw_init (char *host, int port, char **realhost) { static char *raw_init(char *host, int port, char **realhost)
{
static struct plug_function_table fn_table = { static struct plug_function_table fn_table = {
raw_closing, raw_closing,
raw_receive raw_receive
@ -80,7 +85,8 @@ static char *raw_init (char *host, int port, char **realhost) {
/* /*
* Called to send data down the raw connection. * Called to send data down the raw connection.
*/ */
static void raw_send (char *buf, int len) { static void raw_send(char *buf, int len)
{
if (s == NULL) if (s == NULL)
return; return;
@ -91,7 +97,8 @@ static void raw_send (char *buf, int len) {
/* /*
* Called to set the size of the window * Called to set the size of the window
*/ */
static void raw_size(void) { static void raw_size(void)
{
/* Do nothing! */ /* Do nothing! */
return; return;
} }
@ -99,16 +106,24 @@ static void raw_size(void) {
/* /*
* Send raw special codes. * Send raw special codes.
*/ */
static void raw_special (Telnet_Special code) { static void raw_special(Telnet_Special code)
{
/* Do nothing! */ /* Do nothing! */
return; return;
} }
static Socket raw_socket(void) { return s; } static Socket raw_socket(void)
{
return s;
}
static int raw_sendok(void) { return 1; } static int raw_sendok(void)
{
return 1;
}
static int raw_ldisc(int option) { static int raw_ldisc(int option)
{
if (option == LD_EDIT || option == LD_ECHO) if (option == LD_EDIT || option == LD_ECHO)
return 1; return 1;
return 0; return 0;

View File

@ -21,11 +21,14 @@ static char *sb_buf = NULL;
static int sb_size = 0; static int sb_size = 0;
#define SB_DELTA 1024 #define SB_DELTA 1024
static void c_write (char *buf, int len) { static void c_write(char *buf, int len)
{
from_backend(0, buf, len); from_backend(0, buf, len);
} }
static int rlogin_closing (Plug plug, char *error_msg, int error_code, int calling_back) { static int rlogin_closing(Plug plug, char *error_msg, int error_code,
int calling_back)
{
sk_close(s); sk_close(s);
s = NULL; s = NULL;
if (error_msg) { if (error_msg) {
@ -35,11 +38,13 @@ static int rlogin_closing (Plug plug, char *error_msg, int error_code, int calli
return 0; return 0;
} }
static int rlogin_receive (Plug plug, int urgent, char *data, int len) { static int rlogin_receive(Plug plug, int urgent, char *data, int len)
{
if (urgent == 2) { if (urgent == 2) {
char c; char c;
c = *data++; len--; c = *data++;
len--;
if (c == '\x80') if (c == '\x80')
rlogin_size(); rlogin_size();
/* /*
@ -74,7 +79,8 @@ static int rlogin_receive (Plug plug, int urgent, char *data, int len) {
* *
* Also places the canonical host name into `realhost'. * Also places the canonical host name into `realhost'.
*/ */
static char *rlogin_init (char *host, int port, char **realhost) { static char *rlogin_init(char *host, int port, char **realhost)
{
static struct plug_function_table fn_table = { static struct plug_function_table fn_table = {
rlogin_closing, rlogin_closing,
rlogin_receive rlogin_receive
@ -127,7 +133,8 @@ static char *rlogin_init (char *host, int port, char **realhost) {
/* /*
* Called to send data down the rlogin connection. * Called to send data down the rlogin connection.
*/ */
static void rlogin_send (char *buf, int len) { static void rlogin_send(char *buf, int len)
{
if (s == NULL) if (s == NULL)
return; return;
@ -138,11 +145,14 @@ static void rlogin_send (char *buf, int len) {
/* /*
* Called to set the size of the window * Called to set the size of the window
*/ */
static void rlogin_size(void) { static void rlogin_size(void)
{
char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 }; char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
b[6] = cols >> 8; b[7] = cols & 0xFF; b[6] = cols >> 8;
b[4] = rows >> 8; b[5] = rows & 0xFF; b[7] = cols & 0xFF;
b[4] = rows >> 8;
b[5] = rows & 0xFF;
sk_write(s, b, 12); sk_write(s, b, 12);
return; return;
} }
@ -150,16 +160,24 @@ static void rlogin_size(void) {
/* /*
* Send rlogin special codes. * Send rlogin special codes.
*/ */
static void rlogin_special (Telnet_Special code) { static void rlogin_special(Telnet_Special code)
{
/* Do nothing! */ /* Do nothing! */
return; return;
} }
static Socket rlogin_socket(void) { return s; } static Socket rlogin_socket(void)
{
return s;
}
static int rlogin_sendok(void) { return 1; } static int rlogin_sendok(void)
{
return 1;
}
static int rlogin_ldisc(int option) { static int rlogin_ldisc(int option)
{
return 0; return 0;
} }

155
scp.c
View File

@ -75,9 +75,12 @@ static void send_str_msg(unsigned int msg_id, char *str);
static void gui_update_stats(char *name, unsigned long size, static void gui_update_stats(char *name, unsigned long size,
int percentage, unsigned long elapsed); int percentage, unsigned long elapsed);
void logevent(char *string) { } void logevent(char *string)
{
}
void ldisc_send(char *buf, int len) { void ldisc_send(char *buf, int len)
{
/* /*
* This is only here because of the calls to ldisc_send(NULL, * This is only here because of the calls to ldisc_send(NULL,
* 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
@ -88,7 +91,8 @@ void ldisc_send(char *buf, int len) {
} }
void verify_ssh_host_key(char *host, int port, char *keytype, void verify_ssh_host_key(char *host, int port, char *keytype,
char *keystr, char *fingerprint) { char *keystr, char *fingerprint)
{
int ret; int ret;
static const char absentmsg[] = static const char absentmsg[] =
@ -100,8 +104,7 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
"If you trust this host, enter \"y\" to add the key to\n" "If you trust this host, enter \"y\" to add the key to\n"
"PuTTY's cache and carry on connecting.\n" "PuTTY's cache and carry on connecting.\n"
"If you do not trust this host, enter \"n\" to abandon the\n" "If you do not trust this host, enter \"n\" to abandon the\n"
"connection.\n" "connection.\n" "Continue connecting? (y/n) ";
"Continue connecting? (y/n) ";
static const char wrongmsg[] = static const char wrongmsg[] =
"WARNING - POTENTIAL SECURITY BREACH!\n" "WARNING - POTENTIAL SECURITY BREACH!\n"
@ -168,10 +171,10 @@ static void tell_char(FILE *stream, char c)
{ {
if (!gui_mode) if (!gui_mode)
fputc(c, stream); fputc(c, stream);
else else {
{
unsigned int msg_id = WM_STD_OUT_CHAR; unsigned int msg_id = WM_STD_OUT_CHAR;
if (stream == stderr) msg_id = WM_STD_ERR_CHAR; if (stream == stderr)
msg_id = WM_STD_ERR_CHAR;
send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c); send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c);
} }
} }
@ -195,30 +198,30 @@ static void tell_user(FILE *stream, char *fmt, ...)
tell_str(stream, str); tell_str(stream, str);
} }
static void gui_update_stats(char *name, unsigned long size, int percentage, unsigned long elapsed) static void gui_update_stats(char *name, unsigned long size,
int percentage, unsigned long elapsed)
{ {
unsigned int i; unsigned int i;
if (strcmp(name,statname) != 0) if (strcmp(name, statname) != 0) {
{
for (i = 0; i < strlen(name); ++i) for (i = 0; i < strlen(name); ++i)
send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)name[i]); send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR,
(WPARAM) name[i]);
send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n'); send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n');
strcpy(statname, name); strcpy(statname, name);
} }
if (statsize != size) if (statsize != size) {
{
send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size); send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size);
statsize = size; statsize = size;
} }
if (statelapsed != elapsed) if (statelapsed != elapsed) {
{ send_msg((HWND) atoi(gui_hwnd), WM_STATS_ELAPSED,
send_msg( (HWND)atoi(gui_hwnd), WM_STATS_ELAPSED, (WPARAM)elapsed ); (WPARAM) elapsed);
statelapsed = elapsed; statelapsed = elapsed;
} }
if (statperct != percentage) if (statperct != percentage) {
{ send_msg((HWND) atoi(gui_hwnd), WM_STATS_PERCENT,
send_msg( (HWND)atoi(gui_hwnd), WM_STATS_PERCENT, (WPARAM)percentage ); (WPARAM) percentage);
statperct = percentage; statperct = percentage;
} }
} }
@ -257,7 +260,8 @@ void connection_fatal(char *fmt, ...)
* Be told what socket we're supposed to be using. * Be told what socket we're supposed to be using.
*/ */
static SOCKET scp_ssh_socket; static SOCKET scp_ssh_socket;
char *do_select(SOCKET skt, int startup) { char *do_select(SOCKET skt, int startup)
{
if (startup) if (startup)
scp_ssh_socket = skt; scp_ssh_socket = skt;
else else
@ -279,7 +283,8 @@ static unsigned char *outptr; /* where to put the data */
static unsigned outlen; /* how much data required */ static unsigned outlen; /* how much data required */
static unsigned char *pending = NULL; /* any spare data */ static unsigned char *pending = NULL; /* any spare data */
static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
void from_backend(int is_stderr, char *data, int datalen) { void from_backend(int is_stderr, char *data, int datalen)
{
unsigned char *p = (unsigned char *) data; unsigned char *p = (unsigned char *) data;
unsigned len = (unsigned) datalen; unsigned len = (unsigned) datalen;
@ -302,10 +307,13 @@ void from_backend(int is_stderr, char *data, int datalen) {
if (outlen > 0) { if (outlen > 0) {
unsigned used = outlen; unsigned used = outlen;
if (used > len) used = len; if (used > len)
used = len;
memcpy(outptr, p, used); memcpy(outptr, p, used);
outptr += used; outlen -= used; outptr += used;
p += used; len -= used; outlen -= used;
p += used;
len -= used;
} }
if (len > 0) { if (len > 0) {
@ -320,7 +328,8 @@ void from_backend(int is_stderr, char *data, int datalen) {
pendlen += len; pendlen += len;
} }
} }
static int ssh_scp_recv(unsigned char *buf, int len) { static int ssh_scp_recv(unsigned char *buf, int len)
{
outptr = buf; outptr = buf;
outlen = len; outlen = len;
@ -362,7 +371,8 @@ static int ssh_scp_recv(unsigned char *buf, int len) {
/* /*
* Loop through the ssh connection and authentication process. * Loop through the ssh connection and authentication process.
*/ */
static void ssh_scp_init(void) { static void ssh_scp_init(void)
{
if (scp_ssh_socket == INVALID_SOCKET) if (scp_ssh_socket == INVALID_SOCKET)
return; return;
while (!back->sendok()) { while (!back->sendok()) {
@ -417,7 +427,8 @@ static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
/* GUI Adaptation - Sept 2000 */ /* GUI Adaptation - Sept 2000 */
if (gui_mode) { if (gui_mode) {
if (maxlen>0) str[0] = '\0'; if (maxlen > 0)
str[0] = '\0';
} else { } else {
hin = GetStdHandle(STD_INPUT_HANDLE); hin = GetStdHandle(STD_INPUT_HANDLE);
hout = GetStdHandle(STD_OUTPUT_HANDLE); hout = GetStdHandle(STD_OUTPUT_HANDLE);
@ -437,7 +448,10 @@ static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
SetConsoleMode(hin, savemode); SetConsoleMode(hin, savemode);
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int) i > maxlen)
i = maxlen - 1;
else
i = i - 2;
str[i] = '\0'; str[i] = '\0';
if (is_pw) if (is_pw)
@ -478,7 +492,8 @@ static void do_cmd(char *host, char *user, char *cmd)
bump("Empty user name"); bump("Empty user name");
user = smalloc(namelen * sizeof(char)); user = smalloc(namelen * sizeof(char));
GetUserName(user, &namelen); GetUserName(user, &namelen);
if (verbose) tell_user(stderr, "Guessing user name: %s", user); if (verbose)
tell_user(stderr, "Guessing user name: %s", user);
strncpy(cfg.username, user, sizeof(cfg.username) - 1); strncpy(cfg.username, user, sizeof(cfg.username) - 1);
cfg.username[sizeof(cfg.username) - 1] = '\0'; cfg.username[sizeof(cfg.username) - 1] = '\0';
free(user); free(user);
@ -535,8 +550,7 @@ static void print_stats(char *name, unsigned long size, unsigned long done,
pct = (int) (100.0 * (float) done / size); pct = (int) (100.0 * (float) done / size);
printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
name, done / 1024, ratebs / 1024.0, name, done / 1024, ratebs / 1024.0, etastr, pct);
etastr, pct);
if (done == size) if (done == size)
printf("\n"); printf("\n");
@ -552,14 +566,9 @@ static char * colon(char *str)
/* We ignore a leading colon, since the hostname cannot be /* We ignore a leading colon, since the hostname cannot be
empty. We also ignore a colon as second character because empty. We also ignore a colon as second character because
of filenames like f:myfile.txt. */ of filenames like f:myfile.txt. */
if (str[0] == '\0' || if (str[0] == '\0' || str[0] == ':' || str[1] == ':')
str[0] == ':' ||
str[1] == ':')
return (NULL); return (NULL);
while (*str != '\0' && while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\')
*str != ':' &&
*str != '/' &&
*str != '\\')
str++; str++;
if (*str == ':') if (*str == ':')
return (str); return (str);
@ -710,16 +719,17 @@ static void source(char *src)
for (i = 0; i < size; i += 4096) { for (i = 0; i < size; i += 4096) {
char transbuf[4096]; char transbuf[4096];
DWORD j, k = 4096; DWORD j, k = 4096;
if (i + k > size) k = size - i; if (i + k > size)
k = size - i;
if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) { if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) {
if (statistics) printf("\n"); if (statistics)
printf("\n");
bump("%s: Read error", src); bump("%s: Read error", src);
} }
back->send(transbuf, k); back->send(transbuf, k);
if (statistics) { if (statistics) {
stat_bytes += k; stat_bytes += k;
if (time(NULL) != stat_lasttime || if (time(NULL) != stat_lasttime || i + k == size) {
i + k == size) {
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);
@ -767,8 +777,7 @@ static void rsource(char *src)
while (ok) { while (ok) {
if (strcmp(fdat.cFileName, ".") == 0 || if (strcmp(fdat.cFileName, ".") == 0 ||
strcmp(fdat.cFileName, "..") == 0) { strcmp(fdat.cFileName, "..") == 0) {
} else if (strlen(src) + 1 + strlen(fdat.cFileName) >= } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= sizeof(buf)) {
sizeof(buf)) {
run_err("%s/%s: Name too long", src, fdat.cFileName); run_err("%s/%s: Name too long", src, fdat.cFileName);
} else { } else {
sprintf(buf, "%s/%s", src, fdat.cFileName); sprintf(buf, "%s/%s", src, fdat.cFileName);
@ -838,8 +847,7 @@ static void sink(char *targ, char *src)
back->send("", 1); back->send("", 1);
return; return;
case 'T': case 'T':
if (sscanf(buf, "T%ld %*d %ld %*d", if (sscanf(buf, "T%ld %*d %ld %*d", &mtime, &atime) == 2) {
&mtime, &atime) == 2) {
settime = 1; settime = 1;
back->send("", 1); back->send("", 1);
goto gottime; goto gottime;
@ -879,8 +887,7 @@ static void sink(char *targ, char *src)
} }
if (!exists) { if (!exists) {
if (!CreateDirectory(namebuf, NULL)) { if (!CreateDirectory(namebuf, NULL)) {
run_err("%s: Cannot create directory", run_err("%s: Cannot create directory", namebuf);
namebuf);
continue; continue;
} }
} }
@ -913,10 +920,12 @@ static void sink(char *targ, char *src)
for (i = 0; i < size; i += 4096) { for (i = 0; i < size; i += 4096) {
char transbuf[4096]; char transbuf[4096];
DWORD j, k = 4096; DWORD j, k = 4096;
if (i + k > size) k = size - i; if (i + k > size)
k = size - i;
if (ssh_scp_recv(transbuf, k) == 0) if (ssh_scp_recv(transbuf, k) == 0)
bump("Lost connection"); bump("Lost connection");
if (wrerror) continue; if (wrerror)
continue;
if (!WriteFile(f, transbuf, k, &j, NULL) || j != k) { if (!WriteFile(f, transbuf, k, &j, NULL) || j != k) {
wrerror = 1; wrerror = 1;
if (statistics) if (statistics)
@ -927,8 +936,7 @@ static void sink(char *targ, char *src)
} }
if (statistics) { if (statistics) {
stat_bytes += k; stat_bytes += k;
if (time(NULL) > stat_lasttime || if (time(NULL) > stat_lasttime || i + k == size) {
i + k == size) {
stat_lasttime = time(NULL); stat_lasttime = time(NULL);
print_stats(stat_name, size, stat_bytes, print_stats(stat_name, size, stat_bytes,
stat_starttime, stat_lasttime); stat_starttime, stat_lasttime);
@ -1006,8 +1014,7 @@ static void toremote(int argc, char *argv[])
verbose ? " -v" : "", verbose ? " -v" : "",
recursive ? " -r" : "", recursive ? " -r" : "",
preserve ? " -p" : "", preserve ? " -p" : "",
targetshouldbedirectory ? " -d" : "", targetshouldbedirectory ? " -d" : "", targ);
targ);
do_cmd(host, user, cmd); do_cmd(host, user, cmd);
sfree(cmd); sfree(cmd);
@ -1051,8 +1058,7 @@ static void toremote(int argc, char *argv[])
} else } else
continue; /* ignore this one */ continue; /* ignore this one */
} }
if (strlen(src) + strlen(fdat.cFileName) >= if (strlen(src) + strlen(fdat.cFileName) >= sizeof(namebuf)) {
sizeof(namebuf)) {
tell_user(stderr, "%s: Name too long", src); tell_user(stderr, "%s: Name too long", src);
continue; continue;
} }
@ -1113,8 +1119,7 @@ static void tolocal(int argc, char *argv[])
verbose ? " -v" : "", verbose ? " -v" : "",
recursive ? " -r" : "", recursive ? " -r" : "",
preserve ? " -p" : "", preserve ? " -p" : "",
targetshouldbedirectory ? " -d" : "", targetshouldbedirectory ? " -d" : "", src);
src);
do_cmd(host, user, cmd); do_cmd(host, user, cmd);
sfree(cmd); sfree(cmd);
@ -1159,7 +1164,10 @@ static void get_dir_list(int argc, char *argv[])
p = cmd + strlen(cmd); p = cmd + strlen(cmd);
for (q = src; *q; q++) { for (q = src; *q; q++) {
if (*q == '\'') { if (*q == '\'') {
*p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\''; *p++ = '\'';
*p++ = '\\';
*p++ = '\'';
*p++ = '\'';
} else { } else {
*p++ = *q; *p++ = *q;
} }
@ -1185,8 +1193,7 @@ static void init_winsock(void)
winsock_ver = MAKEWORD(1, 1); winsock_ver = MAKEWORD(1, 1);
if (WSAStartup(winsock_ver, &wsadata)) if (WSAStartup(winsock_ver, &wsadata))
bump("Unable to initialise WinSock"); bump("Unable to initialise WinSock");
if (LOBYTE(wsadata.wVersion) != 1 || if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1)
HIBYTE(wsadata.wVersion) != 1)
bump("WinSock version is incompatible with 1.1"); bump("WinSock version is incompatible with 1.1");
} }
@ -1198,7 +1205,8 @@ static void usage(void)
printf("PuTTY Secure Copy client\n"); printf("PuTTY Secure Copy client\n");
printf("%s\n", ver); printf("%s\n", ver);
printf("Usage: pscp [options] [user@]host:source target\n"); printf("Usage: pscp [options] [user@]host:source target\n");
printf(" pscp [options] source [source...] [user@]host:target\n"); printf
(" pscp [options] source [source...] [user@]host:target\n");
printf(" pscp [options] -ls user@host:filespec\n"); printf(" pscp [options] -ls user@host:filespec\n");
printf("Options:\n"); printf("Options:\n");
printf(" -p preserve file attributes\n"); printf(" -p preserve file attributes\n");
@ -1215,7 +1223,8 @@ static void usage(void)
* the command-line help. The only people who need to know * the command-line help. The only people who need to know
* about it are programmers, and they can read the source. * about it are programmers, and they can read the source.
*/ */
printf(" -gui hWnd GUI mode with the windows handle for receiving messages\n"); printf
(" -gui hWnd GUI mode with the windows handle for receiving messages\n");
#endif #endif
exit(1); exit(1);
} }
@ -1246,8 +1255,7 @@ int main(int argc, char *argv[])
preserve = 1; preserve = 1;
else if (strcmp(argv[i], "-q") == 0) else if (strcmp(argv[i], "-q") == 0)
statistics = 0; statistics = 0;
else if (strcmp(argv[i], "-h") == 0 || else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0)
strcmp(argv[i], "-?") == 0)
usage(); usage();
else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc)
portnumber = atoi(argv[++i]); portnumber = atoi(argv[++i]);
@ -1258,9 +1266,10 @@ int main(int argc, char *argv[])
gui_mode = 1; gui_mode = 1;
} else if (strcmp(argv[i], "-ls") == 0) } else if (strcmp(argv[i], "-ls") == 0)
list = 1; list = 1;
else if (strcmp(argv[i], "--") == 0) else if (strcmp(argv[i], "--") == 0) {
{ i++; break; } i++;
else break;
} else
usage(); usage();
} }
argc -= i; argc -= i;
@ -1296,9 +1305,11 @@ int main(int argc, char *argv[])
/* GUI Adaptation - August 2000 */ /* GUI Adaptation - August 2000 */
if (gui_mode) { if (gui_mode) {
unsigned int msg_id = WM_RET_ERR_CNT; unsigned int msg_id = WM_RET_ERR_CNT;
if (list) msg_id = WM_LS_RET_ERR_CNT; if (list)
while (!PostMessage( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)errs, 0/*lParam*/ ) ) msg_id = WM_LS_RET_ERR_CNT;
SleepEx(1000,TRUE); while (!PostMessage
((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
0 /*lParam */ ))SleepEx(1000, TRUE);
} }
return (errs == 0 ? 0 : 1); return (errs == 0 ? 0 : 1);
} }

View File

@ -8,18 +8,21 @@
#include "putty.h" #include "putty.h"
#include "storage.h" #include "storage.h"
static void gpps(void *handle, char *name, char *def, char *val, int len) { static void gpps(void *handle, char *name, char *def, char *val, int len)
{
if (!read_setting_s(handle, name, val, len)) { if (!read_setting_s(handle, name, val, len)) {
strncpy(val, def, len); strncpy(val, def, len);
val[len - 1] = '\0'; val[len - 1] = '\0';
} }
} }
static void gppi(void *handle, char *name, int def, int *i) { static void gppi(void *handle, char *name, int def, int *i)
{
*i = read_setting_i(handle, name, def); *i = read_setting_i(handle, name, def);
} }
void save_settings (char *section, int do_host, Config *cfg) { void save_settings(char *section, int do_host, Config * cfg)
{
int i; int i;
char *p; char *p;
void *sesskey; void *sesskey;
@ -76,8 +79,7 @@ void save_settings (char *section, int do_host, Config *cfg) {
write_setting_s(sesskey, "Cipher", write_setting_s(sesskey, "Cipher",
cfg->cipher == CIPHER_BLOWFISH ? "blowfish" : cfg->cipher == CIPHER_BLOWFISH ? "blowfish" :
cfg->cipher == CIPHER_DES ? "des" : cfg->cipher == CIPHER_DES ? "des" :
cfg->cipher == CIPHER_AES ? "aes" : cfg->cipher == CIPHER_AES ? "aes" : "3des");
"3des");
write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth); write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
write_setting_i(sesskey, "SshProt", cfg->sshprot); write_setting_i(sesskey, "SshProt", cfg->sshprot);
write_setting_i(sesskey, "BuggyMAC", cfg->buggymac); write_setting_i(sesskey, "BuggyMAC", cfg->buggymac);
@ -162,7 +164,8 @@ void save_settings (char *section, int do_host, Config *cfg) {
close_settings_w(sesskey); close_settings_w(sesskey);
} }
void load_settings (char *section, int do_host, Config *cfg) { void load_settings(char *section, int do_host, Config * cfg)
{
int i; int i;
char prot[10]; char prot[10];
void *sesskey; void *sesskey;
@ -214,13 +217,15 @@ void load_settings (char *section, int do_host, Config *cfg) {
c = *p++; c = *p++;
*q++ = c; *q++ = c;
} }
if (*p == ',') p++; if (*p == ',')
p++;
*q++ = '\0'; *q++ = '\0';
} }
*q = '\0'; *q = '\0';
} }
gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username)); gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
gpps (sesskey, "LocalUserName", "", cfg->localusername, sizeof(cfg->localusername)); gpps(sesskey, "LocalUserName", "", cfg->localusername,
sizeof(cfg->localusername));
gppi(sesskey, "NoPTY", 0, &cfg->nopty); gppi(sesskey, "NoPTY", 0, &cfg->nopty);
gppi(sesskey, "Compression", 0, &cfg->compression); gppi(sesskey, "Compression", 0, &cfg->compression);
gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd); gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
@ -258,7 +263,8 @@ void load_settings (char *section, int do_host, Config *cfg) {
gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys); gppi(sesskey, "CtrlAltKeys", 1, &cfg->ctrlaltkeys);
gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho); gppi(sesskey, "LocalEcho", LD_BACKEND, &cfg->localecho);
gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit); gppi(sesskey, "LocalEdit", LD_BACKEND, &cfg->localedit);
gpps (sesskey, "Answerback", "PuTTY", cfg->answerback, sizeof(cfg->answerback)); gpps(sesskey, "Answerback", "PuTTY", cfg->answerback,
sizeof(cfg->answerback));
gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop); gppi(sesskey, "AlwaysOnTop", 0, &cfg->alwaysontop);
gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr); gppi(sesskey, "HideMousePtr", 0, &cfg->hide_mouseptr);
gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge); gppi(sesskey, "SunkenEdge", 0, &cfg->sunken_edge);
@ -336,8 +342,10 @@ void load_settings (char *section, int do_host, Config *cfg) {
p = buf2; p = buf2;
for (j = i; j < i + 32; j++) { for (j = i; j < i + 32; j++) {
char *q = p; char *q = p;
while (*p && *p != ',') p++; while (*p && *p != ',')
if (*p == ',') *p++ = '\0'; p++;
if (*p == ',')
*p++ = '\0';
cfg->wordness[j] = atoi(q); cfg->wordness[j] = atoi(q);
} }
} }
@ -358,14 +366,16 @@ void load_settings (char *section, int do_host, Config *cfg) {
close_settings_r(sesskey); close_settings_r(sesskey);
} }
void do_defaults (char *session, Config *cfg) { void do_defaults(char *session, Config * cfg)
{
if (session) if (session)
load_settings(session, TRUE, cfg); load_settings(session, TRUE, cfg);
else else
load_settings("Default Settings", FALSE, cfg); load_settings("Default Settings", FALSE, cfg);
} }
static int sessioncmp(const void *av, const void *bv) { static int sessioncmp(const void *av, const void *bv)
{
const char *a = *(const char *const *) av; const char *a = *(const char *const *) av;
const char *b = *(const char *const *) bv; const char *b = *(const char *const *) bv;
@ -380,7 +390,8 @@ static int sessioncmp(const void *av, const void *bv) {
return strcmp(a, b); /* otherwise, compare normally */ return strcmp(a, b); /* otherwise, compare normally */
} }
void get_sesslist(int allocate) { void get_sesslist(int allocate)
{
static char otherbuf[2048]; static char otherbuf[2048];
static char *buffer; static char *buffer;
int buflen, bufsize, i; int buflen, bufsize, i;
@ -421,7 +432,8 @@ void get_sesslist(int allocate) {
while (*p) { while (*p) {
if (strcmp(p, "Default Settings")) if (strcmp(p, "Default Settings"))
nsessions++; nsessions++;
while (*p) p++; while (*p)
p++;
p++; p++;
} }
@ -432,7 +444,8 @@ void get_sesslist(int allocate) {
while (*p) { while (*p) {
if (strcmp(p, "Default Settings")) if (strcmp(p, "Default Settings"))
sessions[i++] = p; sessions[i++] = p;
while (*p) p++; while (*p)
p++;
p++; p++;
} }

114
sftp.c
View File

@ -36,21 +36,25 @@ struct sftp_packet {
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* SFTP packet construction functions. * SFTP packet construction functions.
*/ */
static void sftp_pkt_ensure(struct sftp_packet *pkt, int length) { static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
{
if (pkt->maxlen < length) { if (pkt->maxlen < length) {
pkt->maxlen = length + 256; pkt->maxlen = length + 256;
pkt->data = srealloc(pkt->data, pkt->maxlen); pkt->data = srealloc(pkt->data, pkt->maxlen);
} }
} }
static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len) { static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
{
pkt->length += len; pkt->length += len;
sftp_pkt_ensure(pkt, pkt->length); sftp_pkt_ensure(pkt, pkt->length);
memcpy(pkt->data + pkt->length - len, data, len); memcpy(pkt->data + pkt->length - len, data, len);
} }
static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte) { static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
{
sftp_pkt_adddata(pkt, &byte, 1); sftp_pkt_adddata(pkt, &byte, 1);
} }
static struct sftp_packet *sftp_pkt_init(int pkt_type) { static struct sftp_packet *sftp_pkt_init(int pkt_type)
{
struct sftp_packet *pkt; struct sftp_packet *pkt;
pkt = smalloc(sizeof(struct sftp_packet)); pkt = smalloc(sizeof(struct sftp_packet));
pkt->data = NULL; pkt->data = NULL;
@ -60,36 +64,42 @@ static struct sftp_packet *sftp_pkt_init(int pkt_type) {
sftp_pkt_addbyte(pkt, (unsigned char) pkt_type); sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
return pkt; return pkt;
} }
static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value) { static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
{
sftp_pkt_adddata(pkt, &value, 1); sftp_pkt_adddata(pkt, &value, 1);
} }
static void sftp_pkt_adduint32(struct sftp_packet *pkt, unsigned long value) { static void sftp_pkt_adduint32(struct sftp_packet *pkt,
unsigned long value)
{
unsigned char x[4]; unsigned char x[4];
PUT_32BIT(x, value); PUT_32BIT(x, value);
sftp_pkt_adddata(pkt, x, 4); sftp_pkt_adddata(pkt, x, 4);
} }
static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value) { static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
{
unsigned char x[8]; unsigned char x[8];
PUT_32BIT(x, value.hi); PUT_32BIT(x, value.hi);
PUT_32BIT(x + 4, value.lo); PUT_32BIT(x + 4, value.lo);
sftp_pkt_adddata(pkt, x, 8); sftp_pkt_adddata(pkt, x, 8);
} }
static void sftp_pkt_addstring_start(struct sftp_packet *pkt) { static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
{
sftp_pkt_adduint32(pkt, 0); sftp_pkt_adduint32(pkt, 0);
pkt->savedpos = pkt->length; pkt->savedpos = pkt->length;
} }
static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data) { static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
{
sftp_pkt_adddata(pkt, data, strlen(data)); sftp_pkt_adddata(pkt, data, strlen(data));
PUT_32BIT(pkt->data + pkt->savedpos - 4, PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
pkt->length - pkt->savedpos);
} }
static void sftp_pkt_addstring_data(struct sftp_packet *pkt, static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
char *data, int len) { char *data, int len)
{
sftp_pkt_adddata(pkt, data, len); sftp_pkt_adddata(pkt, data, len);
PUT_32BIT(pkt->data + pkt->savedpos - 4, PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
pkt->length - pkt->savedpos);
} }
static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data) { static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
{
sftp_pkt_addstring_start(pkt); sftp_pkt_addstring_start(pkt);
sftp_pkt_addstring_str(pkt, data); sftp_pkt_addstring_str(pkt, data);
} }
@ -98,7 +108,8 @@ static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data) {
* SFTP packet decode functions. * SFTP packet decode functions.
*/ */
static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) { static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
{
unsigned char value; unsigned char value;
if (pkt->length - pkt->savedpos < 1) if (pkt->length - pkt->savedpos < 1)
return 0; /* arrgh, no way to decline (FIXME?) */ return 0; /* arrgh, no way to decline (FIXME?) */
@ -106,7 +117,8 @@ static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) {
pkt->savedpos++; pkt->savedpos++;
return value; return value;
} }
static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt) { static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
{
unsigned long value; unsigned long value;
if (pkt->length - pkt->savedpos < 4) if (pkt->length - pkt->savedpos < 4)
return 0; /* arrgh, no way to decline (FIXME?) */ return 0; /* arrgh, no way to decline (FIXME?) */
@ -115,7 +127,8 @@ static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt) {
return value; return value;
} }
static void sftp_pkt_getstring(struct sftp_packet *pkt, static void sftp_pkt_getstring(struct sftp_packet *pkt,
char **p, int *length) { char **p, int *length)
{
*p = NULL; *p = NULL;
if (pkt->length - pkt->savedpos < 4) if (pkt->length - pkt->savedpos < 4)
return; return;
@ -126,7 +139,8 @@ static void sftp_pkt_getstring(struct sftp_packet *pkt,
*p = pkt->data + pkt->savedpos; *p = pkt->data + pkt->savedpos;
pkt->savedpos += *length; pkt->savedpos += *length;
} }
static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) { static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
{
struct fxp_attrs ret; struct fxp_attrs ret;
ret.flags = sftp_pkt_getuint32(pkt); ret.flags = sftp_pkt_getuint32(pkt);
if (ret.flags & SSH_FILEXFER_ATTR_SIZE) { if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
@ -162,24 +176,27 @@ static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) {
} }
return ret; return ret;
} }
static void sftp_pkt_free(struct sftp_packet *pkt) { static void sftp_pkt_free(struct sftp_packet *pkt)
if (pkt->data) sfree(pkt->data); {
if (pkt->data)
sfree(pkt->data);
sfree(pkt); sfree(pkt);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* Send and receive packet functions. * Send and receive packet functions.
*/ */
int sftp_send(struct sftp_packet *pkt) { int sftp_send(struct sftp_packet *pkt)
{
int ret; int ret;
char x[4]; char x[4];
PUT_32BIT(x, pkt->length); PUT_32BIT(x, pkt->length);
ret = (sftp_senddata(x, 4) && ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
sftp_senddata(pkt->data, pkt->length));
sftp_pkt_free(pkt); sftp_pkt_free(pkt);
return ret; return ret;
} }
struct sftp_packet *sftp_recv(void) { struct sftp_packet *sftp_recv(void)
{
struct sftp_packet *pkt; struct sftp_packet *pkt;
char x[4]; char x[4];
@ -205,7 +222,8 @@ struct sftp_packet *sftp_recv(void) {
* String handling routines. * String handling routines.
*/ */
static char *mkstr(char *s, int len) { static char *mkstr(char *s, int len)
{
char *p = smalloc(len + 1); char *p = smalloc(len + 1);
memcpy(p, s, len); memcpy(p, s, len);
p[len] = '\0'; p[len] = '\0';
@ -224,7 +242,8 @@ static int fxp_errtype;
* SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error). * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
* Also place the status into fxp_errtype. * Also place the status into fxp_errtype.
*/ */
static int fxp_got_status(struct sftp_packet *pktin) { static int fxp_got_status(struct sftp_packet *pktin)
{
static const char *const messages[] = { static const char *const messages[] = {
/* SSH_FX_OK. The only time we will display a _message_ for this /* SSH_FX_OK. The only time we will display a _message_ for this
* is if we were expecting something other than FXP_STATUS on * is if we were expecting something other than FXP_STATUS on
@ -260,23 +279,27 @@ static int fxp_got_status(struct sftp_packet *pktin) {
return -1; return -1;
} }
static void fxp_internal_error(char *msg) { static void fxp_internal_error(char *msg)
{
fxp_error_message = msg; fxp_error_message = msg;
fxp_errtype = -1; fxp_errtype = -1;
} }
const char *fxp_error(void) { const char *fxp_error(void)
{
return fxp_error_message; return fxp_error_message;
} }
int fxp_error_type(void) { int fxp_error_type(void)
{
return fxp_errtype; return fxp_errtype;
} }
/* /*
* Perform exchange of init/version packets. Return 0 on failure. * Perform exchange of init/version packets. Return 0 on failure.
*/ */
int fxp_init(void) { int fxp_init(void)
{
struct sftp_packet *pktout, *pktin; struct sftp_packet *pktout, *pktin;
int remotever; int remotever;
@ -295,7 +318,8 @@ int fxp_init(void) {
} }
remotever = sftp_pkt_getuint32(pktin); remotever = sftp_pkt_getuint32(pktin);
if (remotever > SFTP_PROTO_VERSION) { if (remotever > SFTP_PROTO_VERSION) {
fxp_internal_error("remote protocol is more advanced than we support"); fxp_internal_error
("remote protocol is more advanced than we support");
return 0; return 0;
} }
/* /*
@ -312,7 +336,8 @@ int fxp_init(void) {
/* /*
* Canonify a pathname. * Canonify a pathname.
*/ */
char *fxp_realpath(char *path) { char *fxp_realpath(char *path)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -354,7 +379,8 @@ char *fxp_realpath(char *path) {
/* /*
* Open a file. * Open a file.
*/ */
struct fxp_handle *fxp_open(char *path, int type) { struct fxp_handle *fxp_open(char *path, int type)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -394,7 +420,8 @@ struct fxp_handle *fxp_open(char *path, int type) {
/* /*
* Open a directory. * Open a directory.
*/ */
struct fxp_handle *fxp_opendir(char *path) { struct fxp_handle *fxp_opendir(char *path)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -432,7 +459,8 @@ struct fxp_handle *fxp_opendir(char *path) {
/* /*
* Close a file/dir. * Close a file/dir.
*/ */
void fxp_close(struct fxp_handle *handle) { void fxp_close(struct fxp_handle *handle)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -458,7 +486,9 @@ void fxp_close(struct fxp_handle *handle) {
* will return 0 on EOF, or return -1 and store SSH_FX_EOF in the * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
* error indicator. It might even depend on the SFTP server.) * error indicator. It might even depend on the SFTP server.)
*/ */
int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) { int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
int len)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -498,7 +528,8 @@ int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
/* /*
* Read from a directory. * Read from a directory.
*/ */
struct fxp_names *fxp_readdir(struct fxp_handle *handle) { struct fxp_names *fxp_readdir(struct fxp_handle *handle)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -538,7 +569,9 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle) {
/* /*
* Write to a file. Returns 0 on error, 1 on OK. * Write to a file. Returns 0 on error, 1 on OK.
*/ */
int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len) { int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
int len)
{
struct sftp_packet *pktin, *pktout; struct sftp_packet *pktin, *pktout;
int id; int id;
@ -563,7 +596,8 @@ int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len) {
/* /*
* Free up an fxp_names structure. * Free up an fxp_names structure.
*/ */
void fxp_free_names(struct fxp_names *names) { void fxp_free_names(struct fxp_names *names)
{
int i; int i;
for (i = 0; i < names->nnames; i++) { for (i = 0; i < names->nnames; i++) {

6
sftp.h
View File

@ -124,12 +124,14 @@ void fxp_close(struct fxp_handle *handle);
/* /*
* Read from a file. * Read from a file.
*/ */
int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset, int len); int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
int len);
/* /*
* Write to a file. Returns 0 on error, 1 on OK. * Write to a file. Returns 0 on error, 1 on OK.
*/ */
int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset, int len); int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
int len);
/* /*
* Read from a directory. * Read from a directory.

View File

@ -77,7 +77,8 @@ static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
SelectObject(hdc, tip_font); SelectObject(hdc, tip_font);
GetTextExtentPoint32(hdc, str, _tcslen(str), &sz); GetTextExtentPoint32(hdc, str, _tcslen(str), &sz);
SetWindowPos(hWnd, NULL, 0, 0, sz.cx+6, sz.cy+6, SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE); SetWindowPos(hWnd, NULL, 0, 0, sz.cx + 6, sz.cy + 6,
SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
InvalidateRect(hWnd, NULL, FALSE); InvalidateRect(hWnd, NULL, FALSE);
DeleteDC(hdc); DeleteDC(hdc);
@ -95,7 +96,8 @@ void UpdateSizeTip(HWND src, int cx, int cy)
{ {
TCHAR str[32]; TCHAR str[32];
if (!tip_enabled) return; if (!tip_enabled)
return;
if (!tip_wnd) { if (!tip_wnd) {
NONCLIENTMETRICS nci; NONCLIENTMETRICS nci;
@ -117,7 +119,6 @@ void UpdateSizeTip(HWND src, int cx, int cy)
tip_class = RegisterClass(&wc); tip_class = RegisterClass(&wc);
} }
#if 0 #if 0
/* Default values based on Windows Standard color scheme */ /* Default values based on Windows Standard color scheme */
@ -133,7 +134,8 @@ void UpdateSizeTip(HWND src, int cx, int cy)
memset(&nci, 0, sizeof(NONCLIENTMETRICS)); memset(&nci, 0, sizeof(NONCLIENTMETRICS));
nci.cbSize = sizeof(NONCLIENTMETRICS); nci.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &nci, 0); SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &nci, 0);
tip_font = CreateFontIndirect(&nci.lfStatusFont); tip_font = CreateFontIndirect(&nci.lfStatusFont);
} }
@ -156,16 +158,19 @@ void UpdateSizeTip(HWND src, int cx, int cy)
GetWindowRect(src, &wr); GetWindowRect(src, &wr);
ix = wr.left; ix = wr.left;
if (ix<16) ix = 16; if (ix < 16)
ix = 16;
iy = wr.top - sz.cy; iy = wr.top - sz.cy;
if (iy<16) iy = 16; if (iy < 16)
iy = 16;
/* Create the tip window */ /* Create the tip window */
tip_wnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST, MAKEINTRESOURCE(tip_class), str, WS_POPUP, tip_wnd =
ix, iy, sz.cx, sz.cy, CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
NULL, NULL, hinst, NULL); MAKEINTRESOURCE(tip_class), str, WS_POPUP, ix,
iy, sz.cx, sz.cy, NULL, NULL, hinst, NULL);
ShowWindow(tip_wnd, SW_SHOWNOACTIVATE); ShowWindow(tip_wnd, SW_SHOWNOACTIVATE);

769
ssh.c

File diff suppressed because it is too large Load Diff

22
ssh.h
View File

@ -148,7 +148,8 @@ struct ssh_signkey {
char *(*fingerprint) (void *key); char *(*fingerprint) (void *key);
int (*verifysig) (void *key, char *sig, int siglen, int (*verifysig) (void *key, char *sig, int siglen,
char *data, int datalen); char *data, int datalen);
unsigned char *(*sign)(void *key, char *data, int datalen, int *siglen); unsigned char *(*sign) (void *key, char *data, int datalen,
int *siglen);
char *name; char *name;
char *keytype; /* for host key cache */ char *keytype; /* for host key cache */
}; };
@ -246,24 +247,29 @@ extern struct ssh2_userkey ssh2_wrong_passphrase;
int ssh2_userkey_encrypted(char *filename, char **comment); int ssh2_userkey_encrypted(char *filename, char **comment);
struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase); struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase);
char *ssh2_userkey_loadpub(char *filename, char **algorithm, int *pub_blob_len); char *ssh2_userkey_loadpub(char *filename, char **algorithm,
int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, char *passphrase); int *pub_blob_len);
int ssh2_save_userkey(char *filename, struct ssh2_userkey *key,
char *passphrase);
int keyfile_version(char *filename); int keyfile_version(char *filename);
void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len);
void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk,
void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); int len);
void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk,
int len);
/* /*
* For progress updates in the key generation utility. * For progress updates in the key generation utility.
*/ */
typedef void (*progfn_t) (void *param, int phase, int progress); typedef void (*progfn_t) (void *param, int phase, int progress);
int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, void *pfnparam); int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
Bignum primegen(int bits, int modulus, int residue, void *pfnparam);
int phase, progfn_t pfn, void *pfnparam); Bignum primegen(int bits, int modulus, int residue, int phase,
progfn_t pfn, void *pfnparam);
/* /*
* zlib compression. * zlib compression.

255
sshaes.c
View File

@ -96,59 +96,115 @@ static const word32 D0[256], D1[256], D2[256], D3[256];
* Core encrypt routines, expecting word32 inputs read big-endian * Core encrypt routines, expecting word32 inputs read big-endian
* from the byte-oriented input stream. * from the byte-oriented input stream.
*/ */
static void aes_encrypt_nb_4(AESContext *ctx, word32 *block) { static void aes_encrypt_nb_4(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 1, C2 = 2, C3 = 3, Nb = 4; static const int C1 = 1, C2 = 2, C3 = 3, Nb = 4;
word32 *keysched = ctx->keysched; word32 *keysched = ctx->keysched;
word32 newstate[4]; word32 newstate[4];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(3); MAKEWORD(0);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); MAKEWORD(1);
MAKEWORD(2);
MAKEWORD(3);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
} }
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(3); LASTWORD(0);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); LASTWORD(1);
LASTWORD(2);
LASTWORD(3);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
} }
static void aes_encrypt_nb_6(AESContext *ctx, word32 *block) { static void aes_encrypt_nb_6(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 1, C2 = 2, C3 = 3, Nb = 6; static const int C1 = 1, C2 = 2, C3 = 3, Nb = 6;
word32 *keysched = ctx->keysched; word32 *keysched = ctx->keysched;
word32 newstate[6]; word32 newstate[6];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(0);
MAKEWORD(3); MAKEWORD(4); MAKEWORD(5); MAKEWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MAKEWORD(2);
MOVEWORD(3); MOVEWORD(4); MOVEWORD(5); MAKEWORD(3);
MAKEWORD(4);
MAKEWORD(5);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
} }
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(0);
LASTWORD(3); LASTWORD(4); LASTWORD(5); LASTWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); LASTWORD(2);
MOVEWORD(3); MOVEWORD(4); MOVEWORD(5); LASTWORD(3);
LASTWORD(4);
LASTWORD(5);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
} }
static void aes_encrypt_nb_8(AESContext *ctx, word32 *block) { static void aes_encrypt_nb_8(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 1, C2 = 3, C3 = 4, Nb = 8; static const int C1 = 1, C2 = 3, C3 = 4, Nb = 8;
word32 *keysched = ctx->keysched; word32 *keysched = ctx->keysched;
word32 newstate[8]; word32 newstate[8];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(3); MAKEWORD(0);
MAKEWORD(4); MAKEWORD(5); MAKEWORD(6); MAKEWORD(7); MAKEWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); MAKEWORD(2);
MOVEWORD(4); MOVEWORD(5); MOVEWORD(6); MOVEWORD(7); MAKEWORD(3);
MAKEWORD(4);
MAKEWORD(5);
MAKEWORD(6);
MAKEWORD(7);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
MOVEWORD(6);
MOVEWORD(7);
} }
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(3); LASTWORD(0);
LASTWORD(4); LASTWORD(5); LASTWORD(6); LASTWORD(7); LASTWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); LASTWORD(2);
MOVEWORD(4); MOVEWORD(5); MOVEWORD(6); MOVEWORD(7); LASTWORD(3);
LASTWORD(4);
LASTWORD(5);
LASTWORD(6);
LASTWORD(7);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
MOVEWORD(6);
MOVEWORD(7);
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
} }
#undef MAKEWORD #undef MAKEWORD
#undef LASTWORD #undef LASTWORD
@ -169,59 +225,115 @@ static void aes_encrypt_nb_8(AESContext *ctx, word32 *block) {
* Core decrypt routines, expecting word32 inputs read big-endian * Core decrypt routines, expecting word32 inputs read big-endian
* from the byte-oriented input stream. * from the byte-oriented input stream.
*/ */
static void aes_decrypt_nb_4(AESContext *ctx, word32 *block) { static void aes_decrypt_nb_4(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 4 - 1, C2 = 4 - 2, C3 = 4 - 3, Nb = 4; static const int C1 = 4 - 1, C2 = 4 - 2, C3 = 4 - 3, Nb = 4;
word32 *keysched = ctx->invkeysched; word32 *keysched = ctx->invkeysched;
word32 newstate[4]; word32 newstate[4];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(3); MAKEWORD(0);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); MAKEWORD(1);
MAKEWORD(2);
MAKEWORD(3);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
} }
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(3); LASTWORD(0);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); LASTWORD(1);
LASTWORD(2);
LASTWORD(3);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
ADD_ROUND_KEY_4; ADD_ROUND_KEY_4;
} }
static void aes_decrypt_nb_6(AESContext *ctx, word32 *block) { static void aes_decrypt_nb_6(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 6 - 1, C2 = 6 - 2, C3 = 6 - 3, Nb = 6; static const int C1 = 6 - 1, C2 = 6 - 2, C3 = 6 - 3, Nb = 6;
word32 *keysched = ctx->invkeysched; word32 *keysched = ctx->invkeysched;
word32 newstate[6]; word32 newstate[6];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(0);
MAKEWORD(3); MAKEWORD(4); MAKEWORD(5); MAKEWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MAKEWORD(2);
MOVEWORD(3); MOVEWORD(4); MOVEWORD(5); MAKEWORD(3);
MAKEWORD(4);
MAKEWORD(5);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
} }
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(0);
LASTWORD(3); LASTWORD(4); LASTWORD(5); LASTWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); LASTWORD(2);
MOVEWORD(3); MOVEWORD(4); MOVEWORD(5); LASTWORD(3);
LASTWORD(4);
LASTWORD(5);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
ADD_ROUND_KEY_6; ADD_ROUND_KEY_6;
} }
static void aes_decrypt_nb_8(AESContext *ctx, word32 *block) { static void aes_decrypt_nb_8(AESContext * ctx, word32 * block)
{
int i; int i;
static const int C1 = 8 - 1, C2 = 8 - 3, C3 = 8 - 4, Nb = 8; static const int C1 = 8 - 1, C2 = 8 - 3, C3 = 8 - 4, Nb = 8;
word32 *keysched = ctx->invkeysched; word32 *keysched = ctx->invkeysched;
word32 newstate[8]; word32 newstate[8];
for (i = 0; i < ctx->Nr - 1; i++) { for (i = 0; i < ctx->Nr - 1; i++) {
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
MAKEWORD(0); MAKEWORD(1); MAKEWORD(2); MAKEWORD(3); MAKEWORD(0);
MAKEWORD(4); MAKEWORD(5); MAKEWORD(6); MAKEWORD(7); MAKEWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); MAKEWORD(2);
MOVEWORD(4); MOVEWORD(5); MOVEWORD(6); MOVEWORD(7); MAKEWORD(3);
MAKEWORD(4);
MAKEWORD(5);
MAKEWORD(6);
MAKEWORD(7);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
MOVEWORD(6);
MOVEWORD(7);
} }
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
LASTWORD(0); LASTWORD(1); LASTWORD(2); LASTWORD(3); LASTWORD(0);
LASTWORD(4); LASTWORD(5); LASTWORD(6); LASTWORD(7); LASTWORD(1);
MOVEWORD(0); MOVEWORD(1); MOVEWORD(2); MOVEWORD(3); LASTWORD(2);
MOVEWORD(4); MOVEWORD(5); MOVEWORD(6); MOVEWORD(7); LASTWORD(3);
LASTWORD(4);
LASTWORD(5);
LASTWORD(6);
LASTWORD(7);
MOVEWORD(0);
MOVEWORD(1);
MOVEWORD(2);
MOVEWORD(3);
MOVEWORD(4);
MOVEWORD(5);
MOVEWORD(6);
MOVEWORD(7);
ADD_ROUND_KEY_8; ADD_ROUND_KEY_8;
} }
#undef MAKEWORD #undef MAKEWORD
#undef LASTWORD #undef LASTWORD
@ -830,7 +942,8 @@ static const word32 D3[256] = {
* (256-bit). * (256-bit).
*/ */
void aes_setup(AESContext * ctx, int blocklen, void aes_setup(AESContext * ctx, int blocklen,
unsigned char *key, int keylen) { unsigned char *key, int keylen)
{
int i, j, Nk, rconst; int i, j, Nk, rconst;
assert(blocklen == 16 || blocklen == 24 || blocklen == 32); assert(blocklen == 16 || blocklen == 24 || blocklen == 32);
@ -917,15 +1030,18 @@ void aes_setup(AESContext *ctx, int blocklen,
} }
} }
static void aes_encrypt(AESContext *ctx, word32 *block) { static void aes_encrypt(AESContext * ctx, word32 * block)
{
ctx->encrypt(ctx, block); ctx->encrypt(ctx, block);
} }
static void aes_decrypt(AESContext *ctx, word32 *block) { static void aes_decrypt(AESContext * ctx, word32 * block)
{
ctx->decrypt(ctx, block); ctx->decrypt(ctx, block);
} }
static void aes_encrypt_cbc(unsigned char *blk, int len, AESContext *ctx) { static void aes_encrypt_cbc(unsigned char *blk, int len, AESContext * ctx)
{
word32 iv[4]; word32 iv[4];
int i; int i;
@ -946,7 +1062,8 @@ static void aes_encrypt_cbc(unsigned char *blk, int len, AESContext *ctx) {
memcpy(ctx->iv, iv, sizeof(iv)); memcpy(ctx->iv, iv, sizeof(iv));
} }
static void aes_decrypt_cbc(unsigned char *blk, int len, AESContext *ctx) { static void aes_decrypt_cbc(unsigned char *blk, int len, AESContext * ctx)
{
word32 iv[4], x[4], ct[4]; word32 iv[4], x[4], ct[4];
int i; int i;
@ -971,64 +1088,76 @@ static void aes_decrypt_cbc(unsigned char *blk, int len, AESContext *ctx) {
static AESContext csctx, scctx; static AESContext csctx, scctx;
static void aes128_cskey(unsigned char *key) { static void aes128_cskey(unsigned char *key)
{
aes_setup(&csctx, 16, key, 16); aes_setup(&csctx, 16, key, 16);
logevent("Initialised AES-128 client->server encryption"); logevent("Initialised AES-128 client->server encryption");
} }
static void aes128_sckey(unsigned char *key) { static void aes128_sckey(unsigned char *key)
{
aes_setup(&scctx, 16, key, 16); aes_setup(&scctx, 16, key, 16);
logevent("Initialised AES-128 server->client encryption"); logevent("Initialised AES-128 server->client encryption");
} }
static void aes192_cskey(unsigned char *key) { static void aes192_cskey(unsigned char *key)
{
aes_setup(&csctx, 16, key, 24); aes_setup(&csctx, 16, key, 24);
logevent("Initialised AES-192 client->server encryption"); logevent("Initialised AES-192 client->server encryption");
} }
static void aes192_sckey(unsigned char *key) { static void aes192_sckey(unsigned char *key)
{
aes_setup(&scctx, 16, key, 24); aes_setup(&scctx, 16, key, 24);
logevent("Initialised AES-192 server->client encryption"); logevent("Initialised AES-192 server->client encryption");
} }
static void aes256_cskey(unsigned char *key) { static void aes256_cskey(unsigned char *key)
{
aes_setup(&csctx, 16, key, 32); aes_setup(&csctx, 16, key, 32);
logevent("Initialised AES-256 client->server encryption"); logevent("Initialised AES-256 client->server encryption");
} }
static void aes256_sckey(unsigned char *key) { static void aes256_sckey(unsigned char *key)
{
aes_setup(&scctx, 16, key, 32); aes_setup(&scctx, 16, key, 32);
logevent("Initialised AES-256 server->client encryption"); logevent("Initialised AES-256 server->client encryption");
} }
static void aes_csiv(unsigned char *iv) { static void aes_csiv(unsigned char *iv)
{
int i; int i;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
csctx.iv[i] = GET_32BIT_MSB_FIRST(iv + 4 * i); csctx.iv[i] = GET_32BIT_MSB_FIRST(iv + 4 * i);
} }
static void aes_sciv(unsigned char *iv) { static void aes_sciv(unsigned char *iv)
{
int i; int i;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
scctx.iv[i] = GET_32BIT_MSB_FIRST(iv + 4 * i); scctx.iv[i] = GET_32BIT_MSB_FIRST(iv + 4 * i);
} }
static void aes_ssh2_encrypt_blk(unsigned char *blk, int len) { static void aes_ssh2_encrypt_blk(unsigned char *blk, int len)
{
aes_encrypt_cbc(blk, len, &csctx); aes_encrypt_cbc(blk, len, &csctx);
} }
static void aes_ssh2_decrypt_blk(unsigned char *blk, int len) { static void aes_ssh2_decrypt_blk(unsigned char *blk, int len)
{
aes_decrypt_cbc(blk, len, &scctx); aes_decrypt_cbc(blk, len, &scctx);
} }
void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len) { void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
{
AESContext ctx; AESContext ctx;
aes_setup(&ctx, 16, key, 32); aes_setup(&ctx, 16, key, 32);
memset(ctx.iv, 0, sizeof(ctx.iv)); memset(ctx.iv, 0, sizeof(ctx.iv));
aes_encrypt_cbc(blk, len, &ctx); aes_encrypt_cbc(blk, len, &ctx);
} }
void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len) { void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
{
AESContext ctx; AESContext ctx;
aes_setup(&ctx, 16, key, 32); aes_setup(&ctx, 16, key, 32);
memset(ctx.iv, 0, sizeof(ctx.iv)); memset(ctx.iv, 0, sizeof(ctx.iv));

View File

@ -236,7 +236,8 @@ static const word32 sbox3[] = {
#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(word32 xL, word32 xR, word32 * output,
BlowfishContext *ctx) { BlowfishContext * ctx)
{
word32 *S0 = ctx->S0; word32 *S0 = ctx->S0;
word32 *S1 = ctx->S1; word32 *S1 = ctx->S1;
word32 *S2 = ctx->S2; word32 *S2 = ctx->S2;
@ -268,7 +269,8 @@ static void blowfish_encrypt(word32 xL, word32 xR, word32 *output,
} }
static void blowfish_decrypt(word32 xL, word32 xR, word32 * output, static void blowfish_decrypt(word32 xL, word32 xR, word32 * output,
BlowfishContext *ctx) { BlowfishContext * ctx)
{
word32 *S0 = ctx->S0; word32 *S0 = ctx->S0;
word32 *S1 = ctx->S1; word32 *S1 = ctx->S1;
word32 *S2 = ctx->S2; word32 *S2 = ctx->S2;
@ -300,12 +302,14 @@ 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; word32 xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
iv0 = ctx->iv0; iv1 = ctx->iv1; iv0 = ctx->iv0;
iv1 = ctx->iv1;
while (len > 0) { while (len > 0) {
xL = GET_32BIT_LSB_FIRST(blk); xL = GET_32BIT_LSB_FIRST(blk);
@ -321,16 +325,19 @@ static void blowfish_lsb_encrypt_cbc(unsigned char *blk, int len,
len -= 8; len -= 8;
} }
ctx->iv0 = iv0; ctx->iv1 = iv1; ctx->iv0 = iv0;
ctx->iv1 = iv1;
} }
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; word32 xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
iv0 = ctx->iv0; iv1 = ctx->iv1; iv0 = ctx->iv0;
iv1 = ctx->iv1;
while (len > 0) { while (len > 0) {
xL = GET_32BIT_LSB_FIRST(blk); xL = GET_32BIT_LSB_FIRST(blk);
@ -346,16 +353,19 @@ static void blowfish_lsb_decrypt_cbc(unsigned char *blk, int len,
len -= 8; len -= 8;
} }
ctx->iv0 = iv0; ctx->iv1 = iv1; ctx->iv0 = iv0;
ctx->iv1 = iv1;
} }
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; word32 xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
iv0 = ctx->iv0; iv1 = ctx->iv1; iv0 = ctx->iv0;
iv1 = ctx->iv1;
while (len > 0) { while (len > 0) {
xL = GET_32BIT_MSB_FIRST(blk); xL = GET_32BIT_MSB_FIRST(blk);
@ -371,16 +381,19 @@ static void blowfish_msb_encrypt_cbc(unsigned char *blk, int len,
len -= 8; len -= 8;
} }
ctx->iv0 = iv0; ctx->iv1 = iv1; ctx->iv0 = iv0;
ctx->iv1 = iv1;
} }
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; word32 xL, xR, out[2], iv0, iv1;
assert((len & 7) == 0); assert((len & 7) == 0);
iv0 = ctx->iv0; iv1 = ctx->iv1; iv0 = ctx->iv0;
iv1 = ctx->iv1;
while (len > 0) { while (len > 0) {
xL = GET_32BIT_MSB_FIRST(blk); xL = GET_32BIT_MSB_FIRST(blk);
@ -396,11 +409,13 @@ static void blowfish_msb_decrypt_cbc(unsigned char *blk, int len,
len -= 8; len -= 8;
} }
ctx->iv0 = iv0; ctx->iv1 = iv1; ctx->iv0 = iv0;
ctx->iv1 = iv1;
} }
static void blowfish_setkey(BlowfishContext * ctx, static void blowfish_setkey(BlowfishContext * ctx,
const unsigned char *key, short keybytes) { const unsigned char *key, short keybytes)
{
word32 *S0 = ctx->S0; word32 *S0 = ctx->S0;
word32 *S1 = ctx->S1; word32 *S1 = ctx->S1;
word32 *S2 = ctx->S2; word32 *S2 = ctx->S2;
@ -411,9 +426,12 @@ static void blowfish_setkey(BlowfishContext *ctx,
for (i = 0; i < 18; i++) { for (i = 0; i < 18; i++) {
P[i] = parray[i]; P[i] = parray[i];
P[i] ^= ((word32)(unsigned char)(key[ (i*4+0) % keybytes ])) << 24; P[i] ^=
P[i] ^= ((word32)(unsigned char)(key[ (i*4+1) % keybytes ])) << 16; ((word32) (unsigned char) (key[(i * 4 + 0) % keybytes])) << 24;
P[i] ^= ((word32)(unsigned char)(key[ (i*4+2) % keybytes ])) << 8; P[i] ^=
((word32) (unsigned char) (key[(i * 4 + 1) % keybytes])) << 16;
P[i] ^=
((word32) (unsigned char) (key[(i * 4 + 2) % keybytes])) << 8;
P[i] ^= ((word32) (unsigned char) (key[(i * 4 + 3) % keybytes])); P[i] ^= ((word32) (unsigned char) (key[(i * 4 + 3) % keybytes]));
} }
@ -428,24 +446,29 @@ static void blowfish_setkey(BlowfishContext *ctx,
for (i = 0; i < 18; i += 2) { for (i = 0; i < 18; i += 2) {
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
P[i] = str[0]; P[i+1] = str[1]; P[i] = str[0];
P[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S0[i] = str[0]; S0[i+1] = str[1]; S0[i] = str[0];
S0[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S1[i] = str[0]; S1[i+1] = str[1]; S1[i] = str[0];
S1[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S2[i] = str[0]; S2[i+1] = str[1]; S2[i] = str[0];
S2[i + 1] = str[1];
} }
for (i = 0; i < 256; i += 2) { for (i = 0; i < 256; i += 2) {
blowfish_encrypt(str[0], str[1], str, ctx); blowfish_encrypt(str[0], str[1], str, ctx);
S3[i] = str[0]; S3[i+1] = str[1]; S3[i] = str[0];
S3[i + 1] = str[1];
} }
} }

238
sshbn.c
View File

@ -38,7 +38,8 @@ unsigned short bnOne[2] = { 1, 1 };
Bignum Zero = bnZero, One = bnOne; Bignum Zero = bnZero, One = bnOne;
static Bignum newbn(int length) { static Bignum newbn(int length)
{
Bignum b = smalloc((length + 1) * sizeof(unsigned short)); Bignum b = smalloc((length + 1) * sizeof(unsigned short));
if (!b) if (!b)
abort(); /* FIXME */ abort(); /* FIXME */
@ -47,11 +48,14 @@ static Bignum newbn(int length) {
return b; return b;
} }
void bn_restore_invariant(Bignum b) { void bn_restore_invariant(Bignum b)
while (b[0] > 1 && b[b[0]] == 0) b[0]--; {
while (b[0] > 1 && b[b[0]] == 0)
b[0]--;
} }
Bignum copybn(Bignum orig) { Bignum copybn(Bignum orig)
{
Bignum b = smalloc((orig[0] + 1) * sizeof(unsigned short)); Bignum b = smalloc((orig[0] + 1) * sizeof(unsigned short));
if (!b) if (!b)
abort(); /* FIXME */ abort(); /* FIXME */
@ -59,7 +63,8 @@ Bignum copybn(Bignum orig) {
return b; return b;
} }
void freebn(Bignum b) { void freebn(Bignum b)
{
/* /*
* Burn the evidence, just in case. * Burn the evidence, just in case.
*/ */
@ -67,7 +72,8 @@ void freebn(Bignum b) {
sfree(b); sfree(b);
} }
Bignum bn_power_2(int n) { Bignum bn_power_2(int n)
{
Bignum ret = newbn(n / 16 + 1); Bignum ret = newbn(n / 16 + 1);
bignum_set_bit(ret, n, 1); bignum_set_bit(ret, n, 1);
return ret; return ret;
@ -101,7 +107,8 @@ static void internal_mul(unsigned short *a, unsigned short *b,
} }
static void internal_add_shifted(unsigned short *number, static void internal_add_shifted(unsigned short *number,
unsigned n, int shift) { unsigned n, int shift)
{
int word = 1 + (shift / 16); int word = 1 + (shift / 16);
int bshift = shift % 16; int bshift = shift % 16;
unsigned long addend; unsigned long addend;
@ -169,8 +176,7 @@ static void internal_mod(unsigned short *a, int alen,
t -= m1; t -= m1;
r = (r + m0) & 0xffff; /* overflow? */ r = (r + m0) & 0xffff; /* overflow? */
if (r >= (unsigned long) m0 && if (r >= (unsigned long) m0 &&
t > ((unsigned long) r << 16) + ai1) t > ((unsigned long) r << 16) + ai1) q--;
q--;
} }
/* Subtract q * m from a[i...] */ /* Subtract q * m from a[i...] */
@ -179,7 +185,8 @@ static void internal_mod(unsigned short *a, int alen,
t = (long) q *(long) m[k]; t = (long) q *(long) m[k];
t += c; t += c;
c = t >> 16; c = t >> 16;
if ((unsigned short) t > a[i+k]) c++; if ((unsigned short) t > a[i + k])
c++;
a[i + k] -= (unsigned short) t; a[i + k] -= (unsigned short) t;
} }
@ -216,11 +223,13 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
/* We use big endian internally */ /* We use big endian internally */
mlen = mod[0]; mlen = mod[0];
m = smalloc(mlen * sizeof(unsigned short)); m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j]; for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */ /* Shift m left to make msb bit set */
for (mshift = 0; mshift < 15; mshift++) for (mshift = 0; mshift < 15; mshift++)
if ((m[0] << mshift) & 0x8000) break; if ((m[0] << mshift) & 0x8000)
break;
if (mshift) { if (mshift) {
for (i = 0; i < mlen - 1; i++) for (i = 0; i < mlen - 1; i++)
m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift)); m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift));
@ -230,20 +239,27 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
/* Allocate n of size mlen, copy base to n */ /* Allocate n of size mlen, copy base to n */
n = smalloc(mlen * sizeof(unsigned short)); n = smalloc(mlen * sizeof(unsigned short));
i = mlen - base[0]; i = mlen - base[0];
for (j = 0; j < i; j++) n[j] = 0; for (j = 0; j < i; j++)
for (j = 0; j < base[0]; j++) n[i+j] = base[base[0] - j]; n[j] = 0;
for (j = 0; j < base[0]; j++)
n[i + j] = base[base[0] - j];
/* Allocate a and b of size 2*mlen. Set a = 1 */ /* Allocate a and b of size 2*mlen. Set a = 1 */
a = smalloc(2 * mlen * sizeof(unsigned short)); a = smalloc(2 * mlen * sizeof(unsigned short));
b = smalloc(2 * mlen * sizeof(unsigned short)); b = smalloc(2 * mlen * sizeof(unsigned short));
for (i = 0; i < 2*mlen; i++) a[i] = 0; for (i = 0; i < 2 * mlen; i++)
a[i] = 0;
a[2 * mlen - 1] = 1; a[2 * mlen - 1] = 1;
/* Skip leading zero bits of exp. */ /* Skip leading zero bits of exp. */
i = 0; j = 15; i = 0;
j = 15;
while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
j--; j--;
if (j < 0) { i++; j = 15; } if (j < 0) {
i++;
j = 15;
}
} }
/* Main computation */ /* Main computation */
@ -256,11 +272,14 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
internal_mod(a, mlen * 2, m, mlen, NULL, 0); internal_mod(a, mlen * 2, m, mlen, NULL, 0);
} else { } else {
unsigned short *t; unsigned short *t;
t = a; a = b; b = t; t = a;
a = b;
b = t;
} }
j--; j--;
} }
i++; j = 15; i++;
j = 15;
} }
/* Fixup result in case the modulus was shifted */ /* Fixup result in case the modulus was shifted */
@ -277,13 +296,22 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod)
result = newbn(mod[0]); result = newbn(mod[0]);
for (i = 0; i < mlen; i++) for (i = 0; i < mlen; i++)
result[result[0] - i] = a[i + mlen]; result[result[0] - i] = a[i + mlen];
while (result[0] > 1 && result[result[0]] == 0) result[0]--; while (result[0] > 1 && result[result[0]] == 0)
result[0]--;
/* Free temporary arrays */ /* Free temporary arrays */
for (i = 0; i < 2*mlen; i++) a[i] = 0; sfree(a); for (i = 0; i < 2 * mlen; i++)
for (i = 0; i < 2*mlen; i++) b[i] = 0; sfree(b); a[i] = 0;
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m); sfree(a);
for (i = 0; i < mlen; i++) n[i] = 0; sfree(n); for (i = 0; i < 2 * mlen; i++)
b[i] = 0;
sfree(b);
for (i = 0; i < mlen; i++)
m[i] = 0;
sfree(m);
for (i = 0; i < mlen; i++)
n[i] = 0;
sfree(n);
return result; return result;
} }
@ -304,11 +332,13 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
/* We use big endian internally */ /* We use big endian internally */
mlen = mod[0]; mlen = mod[0];
m = smalloc(mlen * sizeof(unsigned short)); m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j]; for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */ /* Shift m left to make msb bit set */
for (mshift = 0; mshift < 15; mshift++) for (mshift = 0; mshift < 15; mshift++)
if ((m[0] << mshift) & 0x8000) break; if ((m[0] << mshift) & 0x8000)
break;
if (mshift) { if (mshift) {
for (i = 0; i < mlen - 1; i++) for (i = 0; i < mlen - 1; i++)
m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift)); m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift));
@ -320,14 +350,18 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
/* Allocate n of size pqlen, copy p to n */ /* Allocate n of size pqlen, copy p to n */
n = smalloc(pqlen * sizeof(unsigned short)); n = smalloc(pqlen * sizeof(unsigned short));
i = pqlen - p[0]; i = pqlen - p[0];
for (j = 0; j < i; j++) n[j] = 0; for (j = 0; j < i; j++)
for (j = 0; j < p[0]; j++) n[i+j] = p[p[0] - j]; n[j] = 0;
for (j = 0; j < p[0]; j++)
n[i + j] = p[p[0] - j];
/* Allocate o of size pqlen, copy q to o */ /* Allocate o of size pqlen, copy q to o */
o = smalloc(pqlen * sizeof(unsigned short)); o = smalloc(pqlen * sizeof(unsigned short));
i = pqlen - q[0]; i = pqlen - q[0];
for (j = 0; j < i; j++) o[j] = 0; for (j = 0; j < i; j++)
for (j = 0; j < q[0]; j++) o[i+j] = q[q[0] - j]; o[j] = 0;
for (j = 0; j < q[0]; j++)
o[i + j] = q[q[0] - j];
/* Allocate a of size 2*pqlen for result */ /* Allocate a of size 2*pqlen for result */
a = smalloc(2 * pqlen * sizeof(unsigned short)); a = smalloc(2 * pqlen * sizeof(unsigned short));
@ -351,13 +385,22 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
result = newbn(rlen); result = newbn(rlen);
for (i = 0; i < rlen; i++) for (i = 0; i < rlen; i++)
result[result[0] - i] = a[i + 2 * pqlen - rlen]; result[result[0] - i] = a[i + 2 * pqlen - rlen];
while (result[0] > 1 && result[result[0]] == 0) result[0]--; while (result[0] > 1 && result[result[0]] == 0)
result[0]--;
/* Free temporary arrays */ /* Free temporary arrays */
for (i = 0; i < 2*pqlen; i++) a[i] = 0; sfree(a); for (i = 0; i < 2 * pqlen; i++)
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m); a[i] = 0;
for (i = 0; i < pqlen; i++) n[i] = 0; sfree(n); sfree(a);
for (i = 0; i < pqlen; i++) o[i] = 0; sfree(o); for (i = 0; i < mlen; i++)
m[i] = 0;
sfree(m);
for (i = 0; i < pqlen; i++)
n[i] = 0;
sfree(n);
for (i = 0; i < pqlen; i++)
o[i] = 0;
sfree(o);
return result; return result;
} }
@ -378,11 +421,13 @@ void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
/* We use big endian internally */ /* We use big endian internally */
mlen = mod[0]; mlen = mod[0];
m = smalloc(mlen * sizeof(unsigned short)); m = smalloc(mlen * sizeof(unsigned short));
for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j]; for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
/* Shift m left to make msb bit set */ /* Shift m left to make msb bit set */
for (mshift = 0; mshift < 15; mshift++) for (mshift = 0; mshift < 15; mshift++)
if ((m[0] << mshift) & 0x8000) break; if ((m[0] << mshift) & 0x8000)
break;
if (mshift) { if (mshift) {
for (i = 0; i < mlen - 1; i++) for (i = 0; i < mlen - 1; i++)
m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift)); m[i] = (m[i] << mshift) | (m[i + 1] >> (16 - mshift));
@ -391,12 +436,15 @@ void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
plen = p[0]; plen = p[0];
/* Ensure plen > mlen */ /* Ensure plen > mlen */
if (plen <= mlen) plen = mlen+1; if (plen <= mlen)
plen = mlen + 1;
/* Allocate n of size plen, copy p to n */ /* Allocate n of size plen, copy p to n */
n = smalloc(plen * sizeof(unsigned short)); n = smalloc(plen * sizeof(unsigned short));
for (j = 0; j < plen; j++) n[j] = 0; for (j = 0; j < plen; j++)
for (j = 1; j <= p[0]; j++) n[plen-j] = p[j]; n[j] = 0;
for (j = 1; j <= p[0]; j++)
n[plen - j] = p[j];
/* Main computation */ /* Main computation */
internal_mod(n, plen, m, mlen, quotient, mshift); internal_mod(n, plen, m, mlen, quotient, mshift);
@ -418,21 +466,27 @@ void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
} }
/* Free temporary arrays */ /* Free temporary arrays */
for (i = 0; i < mlen; i++) m[i] = 0; sfree(m); for (i = 0; i < mlen; i++)
for (i = 0; i < plen; i++) n[i] = 0; sfree(n); m[i] = 0;
sfree(m);
for (i = 0; i < plen; i++)
n[i] = 0;
sfree(n);
} }
/* /*
* Decrement a number. * Decrement a number.
*/ */
void decbn(Bignum bn) { void decbn(Bignum bn)
{
int i = 1; int i = 1;
while (i < bn[0] && bn[i] == 0) while (i < bn[0] && bn[i] == 0)
bn[i++] = 0xFFFF; bn[i++] = 0xFFFF;
bn[i]--; bn[i]--;
} }
Bignum bignum_from_bytes(unsigned char *data, int nbytes) { Bignum bignum_from_bytes(unsigned char *data, int nbytes)
{
Bignum result; Bignum result;
int w, i; int w, i;
@ -449,7 +503,8 @@ Bignum bignum_from_bytes(unsigned char *data, int nbytes) {
result[1 + i / 2] |= byte; result[1 + i / 2] |= byte;
} }
while (result[0] > 1 && result[result[0]] == 0) result[0]--; while (result[0] > 1 && result[result[0]] == 0)
result[0]--;
return result; return result;
} }
@ -457,7 +512,8 @@ Bignum bignum_from_bytes(unsigned char *data, int nbytes) {
* Read an ssh1-format bignum from a data buffer. Return the number * Read an ssh1-format bignum from a data buffer. Return the number
* of bytes consumed. * of bytes consumed.
*/ */
int ssh1_read_bignum(unsigned char *data, Bignum *result) { int ssh1_read_bignum(unsigned char *data, Bignum * result)
{
unsigned char *p = data; unsigned char *p = data;
int i; int i;
int w, b; int w, b;
@ -478,31 +534,35 @@ int ssh1_read_bignum(unsigned char *data, Bignum *result) {
/* /*
* Return the bit count of a bignum, for ssh1 encoding. * Return the bit count of a bignum, for ssh1 encoding.
*/ */
int bignum_bitcount(Bignum bn) { int bignum_bitcount(Bignum bn)
{
int bitcount = bn[0] * 16 - 1; int bitcount = bn[0] * 16 - 1;
while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0) while (bitcount >= 0
bitcount--; && (bn[bitcount / 16 + 1] >> (bitcount % 16)) == 0) bitcount--;
return bitcount + 1; return bitcount + 1;
} }
/* /*
* Return the byte length of a bignum when ssh1 encoded. * Return the byte length of a bignum when ssh1 encoded.
*/ */
int ssh1_bignum_length(Bignum bn) { int ssh1_bignum_length(Bignum bn)
{
return 2 + (bignum_bitcount(bn) + 7) / 8; return 2 + (bignum_bitcount(bn) + 7) / 8;
} }
/* /*
* Return the byte length of a bignum when ssh2 encoded. * Return the byte length of a bignum when ssh2 encoded.
*/ */
int ssh2_bignum_length(Bignum bn) { int ssh2_bignum_length(Bignum bn)
{
return 4 + (bignum_bitcount(bn) + 8) / 8; return 4 + (bignum_bitcount(bn) + 8) / 8;
} }
/* /*
* Return a byte from a bignum; 0 is least significant, etc. * Return a byte from a bignum; 0 is least significant, etc.
*/ */
int bignum_byte(Bignum bn, int i) { int bignum_byte(Bignum bn, int i)
{
if (i >= 2 * bn[0]) if (i >= 2 * bn[0])
return 0; /* beyond the end */ return 0; /* beyond the end */
else if (i & 1) else if (i & 1)
@ -514,7 +574,8 @@ int bignum_byte(Bignum bn, int i) {
/* /*
* Return a bit from a bignum; 0 is least significant, etc. * Return a bit from a bignum; 0 is least significant, etc.
*/ */
int bignum_bit(Bignum bn, int i) { int bignum_bit(Bignum bn, int i)
{
if (i >= 16 * bn[0]) if (i >= 16 * bn[0])
return 0; /* beyond the end */ return 0; /* beyond the end */
else else
@ -524,7 +585,8 @@ int bignum_bit(Bignum bn, int i) {
/* /*
* Set a bit in a bignum; 0 is least significant, etc. * Set a bit in a bignum; 0 is least significant, etc.
*/ */
void bignum_set_bit(Bignum bn, int bitnum, int value) { void bignum_set_bit(Bignum bn, int bitnum, int value)
{
if (bitnum >= 16 * bn[0]) if (bitnum >= 16 * bn[0])
abort(); /* beyond the end */ abort(); /* beyond the end */
else { else {
@ -541,7 +603,8 @@ void bignum_set_bit(Bignum bn, int bitnum, int value) {
* Write a ssh1-format bignum into a buffer. It is assumed the * Write a ssh1-format bignum into a buffer. It is assumed the
* buffer is big enough. Returns the number of bytes used. * buffer is big enough. Returns the number of bytes used.
*/ */
int ssh1_write_bignum(void *data, Bignum bn) { int ssh1_write_bignum(void *data, Bignum bn)
{
unsigned char *p = data; unsigned char *p = data;
int len = ssh1_bignum_length(bn); int len = ssh1_bignum_length(bn);
int i; int i;
@ -557,14 +620,17 @@ int ssh1_write_bignum(void *data, Bignum bn) {
/* /*
* Compare two bignums. Returns like strcmp. * Compare two bignums. Returns like strcmp.
*/ */
int bignum_cmp(Bignum a, Bignum b) { int bignum_cmp(Bignum a, Bignum b)
{
int amax = a[0], bmax = b[0]; int amax = a[0], bmax = b[0];
int i = (amax > bmax ? amax : bmax); int i = (amax > bmax ? amax : bmax);
while (i) { while (i) {
unsigned short aval = (i > amax ? 0 : a[i]); unsigned short aval = (i > amax ? 0 : a[i]);
unsigned short bval = (i > bmax ? 0 : b[i]); unsigned short bval = (i > bmax ? 0 : b[i]);
if (aval < bval) return -1; if (aval < bval)
if (aval > bval) return +1; return -1;
if (aval > bval)
return +1;
i--; i--;
} }
return 0; return 0;
@ -573,7 +639,8 @@ int bignum_cmp(Bignum a, Bignum b) {
/* /*
* Right-shift one bignum to form another. * Right-shift one bignum to form another.
*/ */
Bignum bignum_rshift(Bignum a, int shift) { Bignum bignum_rshift(Bignum a, int shift)
{
Bignum ret; Bignum ret;
int i, shiftw, shiftb, shiftbb, bits; int i, shiftw, shiftb, shiftbb, bits;
unsigned short ai, ai1; unsigned short ai, ai1;
@ -600,7 +667,8 @@ Bignum bignum_rshift(Bignum a, int shift) {
/* /*
* Non-modular multiplication and addition. * Non-modular multiplication and addition.
*/ */
Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) { Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
{
int alen = a[0], blen = b[0]; int alen = a[0], blen = b[0];
int mlen = (alen > blen ? alen : blen); int mlen = (alen > blen ? alen : blen);
int rlen, i, maxspot; int rlen, i, maxspot;
@ -614,7 +682,8 @@ Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0); workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0);
} }
internal_mul(workspace+0*mlen, workspace+1*mlen, workspace+2*mlen, mlen); internal_mul(workspace + 0 * mlen, workspace + 1 * mlen,
workspace + 2 * mlen, mlen);
/* now just copy the result back */ /* now just copy the result back */
rlen = alen + blen + 1; rlen = alen + blen + 1;
@ -649,7 +718,8 @@ Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
/* /*
* Non-modular multiplication. * Non-modular multiplication.
*/ */
Bignum bigmul(Bignum a, Bignum b) { Bignum bigmul(Bignum a, Bignum b)
{
return bigmuladd(a, b, NULL); return bigmuladd(a, b, NULL);
} }
@ -658,7 +728,8 @@ Bignum bigmul(Bignum a, Bignum b) {
* is, the smallest integer which is >= N and is also one less than * is, the smallest integer which is >= N and is also one less than
* a power of two. * a power of two.
*/ */
Bignum bignum_bitmask(Bignum n) { Bignum bignum_bitmask(Bignum n)
{
Bignum ret = copybn(n); Bignum ret = copybn(n);
int i; int i;
unsigned short j; unsigned short j;
@ -680,7 +751,8 @@ Bignum bignum_bitmask(Bignum n) {
/* /*
* Convert a (max 16-bit) short into a bignum. * Convert a (max 16-bit) short into a bignum.
*/ */
Bignum bignum_from_short(unsigned short n) { Bignum bignum_from_short(unsigned short n)
{
Bignum ret; Bignum ret;
ret = newbn(2); ret = newbn(2);
@ -693,7 +765,8 @@ Bignum bignum_from_short(unsigned short n) {
/* /*
* Add a long to a bignum. * Add a long to a bignum.
*/ */
Bignum bignum_add_long(Bignum number, unsigned long addend) { Bignum bignum_add_long(Bignum number, unsigned long addend)
{
Bignum ret = newbn(number[0] + 1); Bignum ret = newbn(number[0] + 1);
int i, maxspot = 0; int i, maxspot = 0;
unsigned long carry = 0; unsigned long carry = 0;
@ -714,7 +787,8 @@ Bignum bignum_add_long(Bignum number, unsigned long addend) {
/* /*
* Compute the residue of a bignum, modulo a (max 16-bit) short. * Compute the residue of a bignum, modulo a (max 16-bit) short.
*/ */
unsigned short bignum_mod_short(Bignum number, unsigned short modulus) { unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
{
unsigned long mod, r; unsigned long mod, r;
int i; int i;
@ -725,25 +799,33 @@ unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
return (unsigned short) r; return (unsigned short) r;
} }
void diagbn(char *prefix, Bignum md) { void diagbn(char *prefix, Bignum md)
{
int i, nibbles, morenibbles; int i, nibbles, morenibbles;
static const char hex[] = "0123456789ABCDEF"; static const char hex[] = "0123456789ABCDEF";
debugprint(("%s0x", prefix ? prefix : "")); debugprint(("%s0x", prefix ? prefix : ""));
nibbles = (3 + bignum_bitcount(md))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(md)) / 4;
if (nibbles < 1)
nibbles = 1;
morenibbles = 4 * md[0] - nibbles; morenibbles = 4 * md[0] - nibbles;
for (i=0; i<morenibbles; i++) debugprint(("-")); for (i = 0; i < morenibbles; i++)
debugprint(("-"));
for (i = nibbles; i--;) for (i = nibbles; i--;)
debugprint(("%c",hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF])); debugprint(
("%c",
hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]));
if (prefix) debugprint(("\n")); if (prefix)
debugprint(("\n"));
} }
/* /*
* Greatest common divisor. * Greatest common divisor.
*/ */
Bignum biggcd(Bignum av, Bignum bv) { Bignum biggcd(Bignum av, Bignum bv)
{
Bignum a = copybn(av); Bignum a = copybn(av);
Bignum b = copybn(bv); Bignum b = copybn(bv);
@ -753,7 +835,8 @@ Bignum biggcd(Bignum av, Bignum bv) {
Bignum t = newbn(b[0]); Bignum t = newbn(b[0]);
bigmod(a, b, t, NULL); bigmod(a, b, t, NULL);
diagbn("t = ", t); diagbn("t = ", t);
while (t[0] > 1 && t[t[0]] == 0) t[0]--; while (t[0] > 1 && t[t[0]] == 0)
t[0]--;
freebn(a); freebn(a);
a = b; a = b;
b = t; b = t;
@ -766,7 +849,8 @@ Bignum biggcd(Bignum av, Bignum bv) {
/* /*
* Modular inverse, using Euclid's extended algorithm. * Modular inverse, using Euclid's extended algorithm.
*/ */
Bignum modinv(Bignum number, Bignum modulus) { Bignum modinv(Bignum number, Bignum modulus)
{
Bignum a = copybn(modulus); Bignum a = copybn(modulus);
Bignum b = copybn(number); Bignum b = copybn(number);
Bignum xp = copybn(Zero); Bignum xp = copybn(Zero);
@ -777,7 +861,8 @@ Bignum modinv(Bignum number, Bignum modulus) {
Bignum t = newbn(b[0]); Bignum t = newbn(b[0]);
Bignum q = newbn(a[0]); Bignum q = newbn(a[0]);
bigmod(a, b, t, q); bigmod(a, b, t, q);
while (t[0] > 1 && t[t[0]] == 0) t[0]--; while (t[0] > 1 && t[t[0]] == 0)
t[0]--;
freebn(a); freebn(a);
a = b; a = b;
b = t; b = t;
@ -822,7 +907,8 @@ Bignum modinv(Bignum number, Bignum modulus) {
* Render a bignum into decimal. Return a malloced string holding * Render a bignum into decimal. Return a malloced string holding
* the decimal representation. * the decimal representation.
*/ */
char *bignum_decimal(Bignum x) { char *bignum_decimal(Bignum x)
{
int ndigits, ndigit; int ndigits, ndigit;
int i, iszero; int i, iszero;
unsigned long carry; unsigned long carry;

View File

@ -100,7 +100,8 @@
*/ */
static unsigned long crc32_table[256]; static unsigned long crc32_table[256];
void crc32_init(void) { void crc32_init(void)
{
unsigned long crcword; unsigned long crcword;
int i; int i;
@ -193,7 +194,8 @@ static const unsigned long crc32_table[256] = {
#endif #endif
#ifdef GENPROGRAM #ifdef GENPROGRAM
int main(void) { int main(void)
{
unsigned long crcword; unsigned long crcword;
int i; int i;
@ -209,7 +211,8 @@ int main(void) {
} }
#endif #endif
unsigned long crc32(const void *buf, size_t len) { unsigned long crc32(const void *buf, size_t len)
{
unsigned long crcword = 0L; unsigned long crcword = 0L;
const unsigned char *p = (const unsigned char *) buf; const unsigned char *p = (const unsigned char *) buf;
while (len--) { while (len--) {

133
sshdes.c
View File

@ -285,7 +285,8 @@ typedef struct {
#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 word32 bitsel(word32 * input, const int *bitnums, int size)
{
word32 ret = 0; word32 ret = 0;
while (size--) { while (size--) {
int bitpos = *bitnums++; int bitpos = *bitnums++;
@ -296,7 +297,8 @@ static word32 bitsel(word32 *input, const int *bitnums, int size) {
return ret; return ret;
} }
void des_key_setup(word32 key_msw, word32 key_lsw, DESContext *sched) { void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
{
static const int PC1_Cbits[] = { static const int PC1_Cbits[] = {
7, 15, 23, 31, 39, 47, 55, 63, 6, 14, 22, 30, 38, 46, 7, 15, 23, 31, 39, 47, 55, 63, 6, 14, 22, 30, 38, 46,
@ -322,7 +324,8 @@ void des_key_setup(word32 key_msw, word32 key_lsw, DESContext *sched) {
-1, -1, 57, 32, 45, 54, 39, 50, -1, -1, 44, 53, 33, 40, 47, 58, -1, -1, 57, 32, 45, 54, 39, 50, -1, -1, 44, 53, 33, 40, 47, 58,
-1, -1, 26, 16, 5, 11, 23, 8, -1, -1, 10, 14, 6, 20, 27, 24 -1, -1, 26, 16, 5, 11, 23, 8, -1, -1, 10, 14, 6, 20, 27, 24
}; };
static const int leftshifts[] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; static const int leftshifts[] =
{ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
word32 C, D; word32 C, D;
word32 buf[2]; word32 buf[2];
@ -519,7 +522,8 @@ 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))
void des_encipher(word32 *output, word32 L, word32 R, DESContext *sched) { void des_encipher(word32 * output, word32 L, word32 R, DESContext * sched)
{
word32 swap, s0246, s1357; word32 swap, s0246, s1357;
IP(L, R); IP(L, R);
@ -547,7 +551,9 @@ void des_encipher(word32 *output, word32 L, word32 R, DESContext *sched) {
L = rotl(L, 31); L = rotl(L, 31);
R = rotl(R, 31); R = rotl(R, 31);
swap = L; L = R; R = swap; swap = L;
L = R;
R = swap;
FP(L, R); FP(L, R);
@ -555,7 +561,8 @@ void des_encipher(word32 *output, word32 L, word32 R, DESContext *sched) {
output[1] = R; output[1] = R;
} }
void des_decipher(word32 *output, word32 L, word32 R, DESContext *sched) { void des_decipher(word32 * output, word32 L, word32 R, DESContext * sched)
{
word32 swap, s0246, s1357; word32 swap, s0246, s1357;
IP(L, R); IP(L, R);
@ -583,7 +590,9 @@ void des_decipher(word32 *output, word32 L, word32 R, DESContext *sched) {
L = rotl(L, 31); L = rotl(L, 31);
R = rotl(R, 31); R = rotl(R, 31);
swap = L; L = R; R = swap; swap = L;
L = R;
R = swap;
FP(L, R); FP(L, R);
@ -604,7 +613,8 @@ void des_decipher(word32 *output, word32 L, word32 R, DESContext *sched) {
(cp)[0] = (value) >> 24; } while (0) (cp)[0] = (value) >> 24; } while (0)
static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src, static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *sched) { unsigned int len, DESContext * sched)
{
word32 out[2], iv0, iv1; word32 out[2], iv0, iv1;
unsigned int i; unsigned int i;
@ -613,20 +623,25 @@ static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
iv0 = sched->eiv0; iv0 = sched->eiv0;
iv1 = sched->eiv1; iv1 = sched->eiv1;
for (i = 0; i < len; i += 8) { for (i = 0; i < len; i += 8) {
iv0 ^= GET_32BIT_MSB_FIRST(src); src += 4; iv0 ^= GET_32BIT_MSB_FIRST(src);
iv1 ^= GET_32BIT_MSB_FIRST(src); src += 4; src += 4;
iv1 ^= GET_32BIT_MSB_FIRST(src);
src += 4;
des_encipher(out, iv0, iv1, sched); des_encipher(out, iv0, iv1, sched);
iv0 = out[0]; iv0 = out[0];
iv1 = out[1]; iv1 = out[1];
PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4; PUT_32BIT_MSB_FIRST(dest, iv0);
PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4; dest += 4;
PUT_32BIT_MSB_FIRST(dest, iv1);
dest += 4;
} }
sched->eiv0 = iv0; sched->eiv0 = iv0;
sched->eiv1 = iv1; sched->eiv1 = iv1;
} }
static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src, static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *sched) { unsigned int len, DESContext * sched)
{
word32 out[2], iv0, iv1, xL, xR; word32 out[2], iv0, iv1, xL, xR;
unsigned int i; unsigned int i;
@ -635,13 +650,17 @@ static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
iv0 = sched->div0; iv0 = sched->div0;
iv1 = sched->div1; iv1 = sched->div1;
for (i = 0; i < len; i += 8) { for (i = 0; i < len; i += 8) {
xL = GET_32BIT_MSB_FIRST(src); src += 4; xL = GET_32BIT_MSB_FIRST(src);
xR = GET_32BIT_MSB_FIRST(src); src += 4; src += 4;
xR = GET_32BIT_MSB_FIRST(src);
src += 4;
des_decipher(out, xL, xR, sched); des_decipher(out, xL, xR, sched);
iv0 ^= out[0]; iv0 ^= out[0];
iv1 ^= out[1]; iv1 ^= out[1];
PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4; PUT_32BIT_MSB_FIRST(dest, iv0);
PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4; dest += 4;
PUT_32BIT_MSB_FIRST(dest, iv1);
dest += 4;
iv0 = xL; iv0 = xL;
iv1 = xR; iv1 = xR;
} }
@ -650,14 +669,16 @@ static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
} }
static void des_3cbc_encrypt(unsigned char *dest, const unsigned char *src, static void des_3cbc_encrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *scheds) { unsigned int len, DESContext * scheds)
{
des_cbc_encrypt(dest, src, len, &scheds[0]); des_cbc_encrypt(dest, src, len, &scheds[0]);
des_cbc_decrypt(dest, src, len, &scheds[1]); des_cbc_decrypt(dest, src, len, &scheds[1]);
des_cbc_encrypt(dest, src, len, &scheds[2]); des_cbc_encrypt(dest, src, len, &scheds[2]);
} }
static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src, static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *scheds) { unsigned int len, DESContext * scheds)
{
word32 out[2], iv0, iv1; word32 out[2], iv0, iv1;
unsigned int i; unsigned int i;
@ -666,29 +687,35 @@ static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src,
iv0 = scheds->eiv0; iv0 = scheds->eiv0;
iv1 = scheds->eiv1; iv1 = scheds->eiv1;
for (i = 0; i < len; i += 8) { for (i = 0; i < len; i += 8) {
iv0 ^= GET_32BIT_MSB_FIRST(src); src += 4; iv0 ^= GET_32BIT_MSB_FIRST(src);
iv1 ^= GET_32BIT_MSB_FIRST(src); src += 4; src += 4;
iv1 ^= GET_32BIT_MSB_FIRST(src);
src += 4;
des_encipher(out, iv0, iv1, &scheds[0]); des_encipher(out, iv0, iv1, &scheds[0]);
des_decipher(out, out[0], out[1], &scheds[1]); des_decipher(out, out[0], out[1], &scheds[1]);
des_encipher(out, out[0], out[1], &scheds[2]); des_encipher(out, out[0], out[1], &scheds[2]);
iv0 = out[0]; iv0 = out[0];
iv1 = out[1]; iv1 = out[1];
PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4; PUT_32BIT_MSB_FIRST(dest, iv0);
PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4; dest += 4;
PUT_32BIT_MSB_FIRST(dest, iv1);
dest += 4;
} }
scheds->eiv0 = iv0; scheds->eiv0 = iv0;
scheds->eiv1 = iv1; scheds->eiv1 = iv1;
} }
static void des_3cbc_decrypt(unsigned char *dest, const unsigned char *src, static void des_3cbc_decrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *scheds) { unsigned int len, DESContext * scheds)
{
des_cbc_decrypt(dest, src, len, &scheds[2]); des_cbc_decrypt(dest, src, len, &scheds[2]);
des_cbc_encrypt(dest, src, len, &scheds[1]); des_cbc_encrypt(dest, src, len, &scheds[1]);
des_cbc_decrypt(dest, src, len, &scheds[0]); des_cbc_decrypt(dest, src, len, &scheds[0]);
} }
static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src, static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
unsigned int len, DESContext *scheds) { unsigned int len, DESContext * scheds)
{
word32 out[2], iv0, iv1, xL, xR; word32 out[2], iv0, iv1, xL, xR;
unsigned int i; unsigned int i;
@ -697,15 +724,19 @@ static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
iv0 = scheds->div0; iv0 = scheds->div0;
iv1 = scheds->div1; iv1 = scheds->div1;
for (i = 0; i < len; i += 8) { for (i = 0; i < len; i += 8) {
xL = GET_32BIT_MSB_FIRST(src); src += 4; xL = GET_32BIT_MSB_FIRST(src);
xR = GET_32BIT_MSB_FIRST(src); src += 4; src += 4;
xR = GET_32BIT_MSB_FIRST(src);
src += 4;
des_decipher(out, xL, xR, &scheds[2]); des_decipher(out, xL, xR, &scheds[2]);
des_encipher(out, out[0], out[1], &scheds[1]); des_encipher(out, out[0], out[1], &scheds[1]);
des_decipher(out, out[0], out[1], &scheds[0]); des_decipher(out, out[0], out[1], &scheds[0]);
iv0 ^= out[0]; iv0 ^= out[0];
iv1 ^= out[1]; iv1 ^= out[1];
PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4; PUT_32BIT_MSB_FIRST(dest, iv0);
PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4; dest += 4;
PUT_32BIT_MSB_FIRST(dest, iv1);
dest += 4;
iv0 = xL; iv0 = xL;
iv1 = xR; iv1 = xR;
} }
@ -715,7 +746,8 @@ static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
static DESContext cskeys[3], sckeys[3]; static DESContext cskeys[3], sckeys[3];
static void des3_cskey(unsigned char *key) { static void des3_cskey(unsigned char *key)
{
des_key_setup(GET_32BIT_MSB_FIRST(key), des_key_setup(GET_32BIT_MSB_FIRST(key),
GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]); GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]);
des_key_setup(GET_32BIT_MSB_FIRST(key + 8), des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
@ -725,17 +757,20 @@ static void des3_cskey(unsigned char *key) {
logevent("Initialised triple-DES client->server encryption"); logevent("Initialised triple-DES client->server encryption");
} }
static void des3_csiv(unsigned char *key) { static void des3_csiv(unsigned char *key)
{
cskeys[0].eiv0 = GET_32BIT_MSB_FIRST(key); cskeys[0].eiv0 = GET_32BIT_MSB_FIRST(key);
cskeys[0].eiv1 = GET_32BIT_MSB_FIRST(key + 4); cskeys[0].eiv1 = GET_32BIT_MSB_FIRST(key + 4);
} }
static void des3_sciv(unsigned char *key) { static void des3_sciv(unsigned char *key)
{
sckeys[0].div0 = GET_32BIT_MSB_FIRST(key); sckeys[0].div0 = GET_32BIT_MSB_FIRST(key);
sckeys[0].div1 = GET_32BIT_MSB_FIRST(key + 4); sckeys[0].div1 = GET_32BIT_MSB_FIRST(key + 4);
} }
static void des3_sckey(unsigned char *key) { static void des3_sckey(unsigned char *key)
{
des_key_setup(GET_32BIT_MSB_FIRST(key), des_key_setup(GET_32BIT_MSB_FIRST(key),
GET_32BIT_MSB_FIRST(key + 4), &sckeys[0]); GET_32BIT_MSB_FIRST(key + 4), &sckeys[0]);
des_key_setup(GET_32BIT_MSB_FIRST(key + 8), des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
@ -745,29 +780,34 @@ static void des3_sckey(unsigned char *key) {
logevent("Initialised triple-DES server->client encryption"); logevent("Initialised triple-DES server->client encryption");
} }
static void des3_sesskey(unsigned char *key) { static void des3_sesskey(unsigned char *key)
{
des3_cskey(key); des3_cskey(key);
des3_sckey(key); des3_sckey(key);
} }
static void des3_encrypt_blk(unsigned char *blk, int len) { static void des3_encrypt_blk(unsigned char *blk, int len)
{
des_3cbc_encrypt(blk, blk, len, cskeys); des_3cbc_encrypt(blk, blk, len, cskeys);
} }
static void des3_decrypt_blk(unsigned char *blk, int len) { static void des3_decrypt_blk(unsigned char *blk, int len)
{
des_3cbc_decrypt(blk, blk, len, sckeys); des_3cbc_decrypt(blk, blk, len, sckeys);
} }
static void des3_ssh2_encrypt_blk(unsigned char *blk, int len) { static void des3_ssh2_encrypt_blk(unsigned char *blk, int len)
{
des_cbc3_encrypt(blk, blk, len, cskeys); des_cbc3_encrypt(blk, blk, len, cskeys);
} }
static void des3_ssh2_decrypt_blk(unsigned char *blk, int len) { static void des3_ssh2_decrypt_blk(unsigned char *blk, int len)
{
des_cbc3_decrypt(blk, blk, len, sckeys); des_cbc3_decrypt(blk, blk, len, sckeys);
} }
void des3_decrypt_pubkey(unsigned char *key, void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
unsigned char *blk, int len) { {
DESContext ourkeys[3]; DESContext ourkeys[3];
des_key_setup(GET_32BIT_MSB_FIRST(key), des_key_setup(GET_32BIT_MSB_FIRST(key),
GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]); GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]);
@ -778,8 +818,8 @@ void des3_decrypt_pubkey(unsigned char *key,
des_3cbc_decrypt(blk, blk, len, ourkeys); des_3cbc_decrypt(blk, blk, len, ourkeys);
} }
void des3_encrypt_pubkey(unsigned char *key, void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
unsigned char *blk, int len) { {
DESContext ourkeys[3]; DESContext ourkeys[3];
des_key_setup(GET_32BIT_MSB_FIRST(key), des_key_setup(GET_32BIT_MSB_FIRST(key),
GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]); GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]);
@ -815,17 +855,20 @@ const struct ssh_cipher ssh_3des = {
8 8
}; };
static void des_sesskey(unsigned char *key) { static void des_sesskey(unsigned char *key)
{
des_key_setup(GET_32BIT_MSB_FIRST(key), des_key_setup(GET_32BIT_MSB_FIRST(key),
GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]); GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]);
logevent("Initialised single-DES encryption"); logevent("Initialised single-DES encryption");
} }
static void des_encrypt_blk(unsigned char *blk, int len) { static void des_encrypt_blk(unsigned char *blk, int len)
{
des_cbc_encrypt(blk, blk, len, cskeys); des_cbc_encrypt(blk, blk, len, cskeys);
} }
static void des_decrypt_blk(unsigned char *blk, int len) { static void des_decrypt_blk(unsigned char *blk, int len)
{
des_cbc_decrypt(blk, blk, len, cskeys); des_cbc_decrypt(blk, blk, len, cskeys);
} }

21
sshdh.c
View File

@ -39,7 +39,8 @@ static int need_to_free_pg;
/* /*
* Common DH initialisation. * Common DH initialisation.
*/ */
static void dh_init(void) { static void dh_init(void)
{
q = bignum_rshift(p, 1); q = bignum_rshift(p, 1);
qmask = bignum_bitmask(q); qmask = bignum_bitmask(q);
} }
@ -47,7 +48,8 @@ static void dh_init(void) {
/* /*
* Initialise DH for the standard group1. * Initialise DH for the standard group1.
*/ */
void dh_setup_group1(void) { void dh_setup_group1(void)
{
p = bignum_from_bytes(P, sizeof(P)); p = bignum_from_bytes(P, sizeof(P));
g = bignum_from_bytes(G, sizeof(G)); g = bignum_from_bytes(G, sizeof(G));
dh_init(); dh_init();
@ -56,7 +58,8 @@ void dh_setup_group1(void) {
/* /*
* Initialise DH for an alternative group. * Initialise DH for an alternative group.
*/ */
void dh_setup_group(Bignum pval, Bignum gval) { void dh_setup_group(Bignum pval, Bignum gval)
{
p = copybn(pval); p = copybn(pval);
g = copybn(gval); g = copybn(gval);
dh_init(); dh_init();
@ -65,7 +68,8 @@ void dh_setup_group(Bignum pval, Bignum gval) {
/* /*
* Clean up. * Clean up.
*/ */
void dh_cleanup(void) { void dh_cleanup(void)
{
freebn(p); freebn(p);
freebn(g); freebn(g);
freebn(q); freebn(q);
@ -87,7 +91,8 @@ void dh_cleanup(void) {
* Advances in Cryptology: Proceedings of Eurocrypt '96 * Advances in Cryptology: Proceedings of Eurocrypt '96
* Springer-Verlag, May 1996. * Springer-Verlag, May 1996.
*/ */
Bignum dh_create_e(int nbits) { Bignum dh_create_e(int nbits)
{
int i; int i;
int nbytes; int nbytes;
@ -101,7 +106,8 @@ Bignum dh_create_e(int nbits) {
* Create a potential x, by ANDing a string of random bytes * Create a potential x, by ANDing a string of random bytes
* with qmask. * with qmask.
*/ */
if (x) freebn(x); if (x)
freebn(x);
if (nbits == 0 || nbits > bignum_bitcount(qmask)) { if (nbits == 0 || nbits > bignum_bitcount(qmask)) {
ssh1_write_bignum(buf, qmask); ssh1_write_bignum(buf, qmask);
for (i = 2; i < nbytes; i++) for (i = 2; i < nbytes; i++)
@ -134,7 +140,8 @@ Bignum dh_create_e(int nbits) {
/* /*
* DH stage 2: given a number f, compute K = f^x mod p. * DH stage 2: given a number f, compute K = f^x mod p.
*/ */
Bignum dh_find_K(Bignum f) { Bignum dh_find_K(Bignum f)
{
Bignum ret; Bignum ret;
ret = modpow(f, x, p); ret = modpow(f, x, p);
return ret; return ret;

122
sshdss.c
View File

@ -22,18 +22,22 @@
#define diagbn(x,y) #define diagbn(x,y)
#endif #endif
static void getstring(char **data, int *datalen, char **p, int *length) { static void getstring(char **data, int *datalen, char **p, int *length)
{
*p = NULL; *p = NULL;
if (*datalen < 4) if (*datalen < 4)
return; return;
*length = GET_32BIT(*data); *length = GET_32BIT(*data);
*datalen -= 4; *data += 4; *datalen -= 4;
*data += 4;
if (*datalen < *length) if (*datalen < *length)
return; return;
*p = *data; *p = *data;
*data += *length; *datalen -= *length; *data += *length;
*datalen -= *length;
} }
static Bignum getmp(char **data, int *datalen) { static Bignum getmp(char **data, int *datalen)
{
char *p; char *p;
int length; int length;
Bignum b; Bignum b;
@ -47,11 +51,13 @@ static Bignum getmp(char **data, int *datalen) {
return b; return b;
} }
static Bignum get160(char **data, int *datalen) { static Bignum get160(char **data, int *datalen)
{
Bignum b; Bignum b;
b = bignum_from_bytes(*data, 20); b = bignum_from_bytes(*data, 20);
*data += 20; *datalen -= 20; *data += 20;
*datalen -= 20;
return b; return b;
} }
@ -60,13 +66,15 @@ struct dss_key {
Bignum p, q, g, y; Bignum p, q, g, y;
}; };
static void *dss_newkey(char *data, int len) { static void *dss_newkey(char *data, int len)
{
char *p; char *p;
int slen; int slen;
struct dss_key *dss; struct dss_key *dss;
dss = smalloc(sizeof(struct dss_key)); dss = smalloc(sizeof(struct dss_key));
if (!dss) return NULL; if (!dss)
return NULL;
getstring(&data, &len, &p, &slen); getstring(&data, &len, &p, &slen);
#ifdef DEBUG_DSS #ifdef DEBUG_DSS
@ -91,7 +99,8 @@ static void *dss_newkey(char *data, int len) {
return dss; return dss;
} }
static void dss_freekey(void *key) { static void dss_freekey(void *key)
{
struct dss_key *dss = (struct dss_key *) key; struct dss_key *dss = (struct dss_key *) key;
freebn(dss->p); freebn(dss->p);
freebn(dss->q); freebn(dss->q);
@ -100,7 +109,8 @@ static void dss_freekey(void *key) {
sfree(dss); sfree(dss);
} }
static char *dss_fmtkey(void *key) { static char *dss_fmtkey(void *key)
{
struct dss_key *dss = (struct dss_key *) key; struct dss_key *dss = (struct dss_key *) key;
char *p; char *p;
int len, i, pos, nibbles; int len, i, pos, nibbles;
@ -113,30 +123,44 @@ static char *dss_fmtkey(void *key) {
len += 4 * (bignum_bitcount(dss->g) + 15) / 16; len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
len += 4 * (bignum_bitcount(dss->y) + 15) / 16; len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
p = smalloc(len); p = smalloc(len);
if (!p) return NULL; if (!p)
return NULL;
pos = 0; pos = 0;
pos += sprintf(p + pos, "0x"); pos += sprintf(p + pos, "0x");
nibbles = (3 + bignum_bitcount(dss->p))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(dss->p)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
p[pos++] = hex[(bignum_byte(dss->p, i/2) >> (4*(i%2))) & 0xF]; p[pos++] =
hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF];
pos += sprintf(p + pos, ",0x"); pos += sprintf(p + pos, ",0x");
nibbles = (3 + bignum_bitcount(dss->q))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(dss->q)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
p[pos++] = hex[(bignum_byte(dss->q, i/2) >> (4*(i%2))) & 0xF]; p[pos++] =
hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF];
pos += sprintf(p + pos, ",0x"); pos += sprintf(p + pos, ",0x");
nibbles = (3 + bignum_bitcount(dss->g))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(dss->g)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
p[pos++] = hex[(bignum_byte(dss->g, i/2) >> (4*(i%2))) & 0xF]; p[pos++] =
hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF];
pos += sprintf(p + pos, ",0x"); pos += sprintf(p + pos, ",0x");
nibbles = (3 + bignum_bitcount(dss->y))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(dss->y)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
p[pos++] = hex[(bignum_byte(dss->y, i/2) >> (4*(i%2))) & 0xF]; p[pos++] =
hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF];
p[pos] = '\0'; p[pos] = '\0';
return p; return p;
} }
static char *dss_fingerprint(void *key) { static char *dss_fingerprint(void *key)
{
struct dss_key *dss = (struct dss_key *) key; struct dss_key *dss = (struct dss_key *) key;
struct MD5Context md5c; struct MD5Context md5c;
unsigned char digest[16], lenbuf[4]; unsigned char digest[16], lenbuf[4];
@ -164,7 +188,8 @@ static char *dss_fingerprint(void *key) {
sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p)); sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p));
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]); sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
digest[i]);
ret = smalloc(strlen(buffer) + 1); ret = smalloc(strlen(buffer) + 1);
if (ret) if (ret)
strcpy(ret, buffer); strcpy(ret, buffer);
@ -172,7 +197,8 @@ static char *dss_fingerprint(void *key) {
} }
static int dss_verifysig(void *key, char *sig, int siglen, static int dss_verifysig(void *key, char *sig, int siglen,
char *data, int datalen) { char *data, int datalen)
{
struct dss_key *dss = (struct dss_key *) key; struct dss_key *dss = (struct dss_key *) key;
char *p; char *p;
int slen; int slen;
@ -231,7 +257,9 @@ static int dss_verifysig(void *key, char *sig, int siglen,
* Step 2. u1 <- SHA(message) * w mod q. * Step 2. u1 <- SHA(message) * w mod q.
*/ */
SHA_Simple(data, datalen, hash); SHA_Simple(data, datalen, hash);
p = hash; slen = 20; sha = get160(&p, &slen); p = hash;
slen = 20;
sha = get160(&p, &slen);
diagbn("sha=", sha); diagbn("sha=", sha);
u1 = modmul(sha, w, dss->q); u1 = modmul(sha, w, dss->q);
diagbn("u1=", u1); diagbn("u1=", u1);
@ -273,7 +301,8 @@ static int dss_verifysig(void *key, char *sig, int siglen,
return ret; return ret;
} }
static unsigned char *dss_public_blob(void *key, int *len) { static unsigned char *dss_public_blob(void *key, int *len)
{
struct dss_key *dss = (struct dss_key *) key; struct dss_key *dss = (struct dss_key *) key;
int plen, qlen, glen, ylen, bloblen; int plen, qlen, glen, ylen, bloblen;
int i; int i;
@ -291,39 +320,54 @@ static unsigned char *dss_public_blob(void *key, int *len) {
bloblen = 27 + plen + qlen + glen + ylen; bloblen = 27 + plen + qlen + glen + ylen;
blob = smalloc(bloblen); blob = smalloc(bloblen);
p = blob; p = blob;
PUT_32BIT(p, 7); p += 4; PUT_32BIT(p, 7);
memcpy(p, "ssh-dss", 7); p += 7; p += 4;
PUT_32BIT(p, plen); p += 4; memcpy(p, "ssh-dss", 7);
for (i = plen; i-- ;) *p++ = bignum_byte(dss->p, i); p += 7;
PUT_32BIT(p, qlen); p += 4; PUT_32BIT(p, plen);
for (i = qlen; i-- ;) *p++ = bignum_byte(dss->q, i); p += 4;
PUT_32BIT(p, glen); p += 4; for (i = plen; i--;)
for (i = glen; i-- ;) *p++ = bignum_byte(dss->g, i); *p++ = bignum_byte(dss->p, i);
PUT_32BIT(p, ylen); p += 4; PUT_32BIT(p, qlen);
for (i = ylen; i-- ;) *p++ = bignum_byte(dss->y, i); p += 4;
for (i = qlen; i--;)
*p++ = bignum_byte(dss->q, i);
PUT_32BIT(p, glen);
p += 4;
for (i = glen; i--;)
*p++ = bignum_byte(dss->g, i);
PUT_32BIT(p, ylen);
p += 4;
for (i = ylen; i--;)
*p++ = bignum_byte(dss->y, i);
assert(p == blob + bloblen); assert(p == blob + bloblen);
*len = bloblen; *len = bloblen;
return blob; return blob;
} }
static unsigned char *dss_private_blob(void *key, int *len) { static unsigned char *dss_private_blob(void *key, int *len)
{
return NULL; /* can't handle DSS private keys */ return NULL; /* can't handle DSS private keys */
} }
static void *dss_createkey(unsigned char *pub_blob, int pub_len, static void *dss_createkey(unsigned char *pub_blob, int pub_len,
unsigned char *priv_blob, int priv_len) { unsigned char *priv_blob, int priv_len)
{
return NULL; /* can't handle DSS private keys */ return NULL; /* can't handle DSS private keys */
} }
static void *dss_openssh_createkey(unsigned char **blob, int *len) { static void *dss_openssh_createkey(unsigned char **blob, int *len)
{
return NULL; /* can't handle DSS private keys */ return NULL; /* can't handle DSS private keys */
} }
static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len) { static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
{
return -1; /* can't handle DSS private keys */ return -1; /* can't handle DSS private keys */
} }
unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen) { unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
{
return NULL; /* can't handle DSS private keys */ return NULL; /* can't handle DSS private keys */
} }

View File

@ -19,17 +19,22 @@
#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)
void MD5_Core_Init(MD5_Core_State *s) { void MD5_Core_Init(MD5_Core_State * s)
{
s->h[0] = 0x67452301; s->h[0] = 0x67452301;
s->h[1] = 0xefcdab89; s->h[1] = 0xefcdab89;
s->h[2] = 0x98badcfe; s->h[2] = 0x98badcfe;
s->h[3] = 0x10325476; s->h[3] = 0x10325476;
} }
void MD5_Block(MD5_Core_State *s, uint32 *block) { void MD5_Block(MD5_Core_State * s, uint32 * block)
{
uint32 a, b, c, d; uint32 a, b, c, d;
a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; a = s->h[0];
b = s->h[1];
c = s->h[2];
d = s->h[3];
subround(F, a, b, c, d, 0, 7, 0xd76aa478); subround(F, a, b, c, d, 0, 7, 0xd76aa478);
subround(F, d, a, b, c, 1, 12, 0xe8c7b756); subround(F, d, a, b, c, 1, 12, 0xe8c7b756);
@ -96,7 +101,10 @@ void MD5_Block(MD5_Core_State *s, uint32 *block) {
subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb); subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb);
subround(I, b, c, d, a, 9, 21, 0xeb86d391); subround(I, b, c, d, a, 9, 21, 0xeb86d391);
s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; s->h[0] += a;
s->h[1] += b;
s->h[2] += c;
s->h[3] += d;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -107,14 +115,15 @@ void MD5_Block(MD5_Core_State *s, uint32 *block) {
#define BLKSIZE 64 #define BLKSIZE 64
void MD5Init(struct MD5Context *s) { 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->lenhi = s->lenlo = 0;
} }
void MD5Update(struct MD5Context *s, unsigned char const *p, void MD5Update(struct MD5Context *s, unsigned char const *p, unsigned len)
unsigned len) { {
unsigned char *q = (unsigned char *) p; unsigned char *q = (unsigned char *) p;
uint32 wordblock[16]; uint32 wordblock[16];
uint32 lenw = len; uint32 lenw = len;
@ -156,7 +165,8 @@ void MD5Update(struct MD5Context *s, unsigned char const *p,
} }
} }
void MD5Final(unsigned char output[16], struct MD5Context *s) { 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];
@ -202,7 +212,8 @@ static struct MD5Context md5_cs_mac_s1, md5_cs_mac_s2;
static struct MD5Context md5_sc_mac_s1, md5_sc_mac_s2; static struct MD5Context md5_sc_mac_s1, md5_sc_mac_s2;
static void md5_key(struct MD5Context *s1, struct MD5Context *s2, static void md5_key(struct MD5Context *s1, struct MD5Context *s2,
unsigned char *key, int len) { unsigned char *key, int len)
{
unsigned char foo[64]; unsigned char foo[64];
int i; int i;
@ -221,17 +232,20 @@ static void md5_key(struct MD5Context *s1, struct MD5Context *s2,
memset(foo, 0, 64); /* burn the evidence */ memset(foo, 0, 64); /* burn the evidence */
} }
static void md5_cskey(unsigned char *key) { static void md5_cskey(unsigned char *key)
{
md5_key(&md5_cs_mac_s1, &md5_cs_mac_s2, key, 16); md5_key(&md5_cs_mac_s1, &md5_cs_mac_s2, key, 16);
} }
static void md5_sckey(unsigned char *key) { static void md5_sckey(unsigned char *key)
{
md5_key(&md5_sc_mac_s1, &md5_sc_mac_s2, key, 16); md5_key(&md5_sc_mac_s1, &md5_sc_mac_s2, key, 16);
} }
static void md5_do_hmac(struct MD5Context *s1, struct MD5Context *s2, static void md5_do_hmac(struct MD5Context *s1, struct MD5Context *s2,
unsigned char *blk, int len, unsigned long seq, unsigned char *blk, int len, unsigned long seq,
unsigned char *hmac) { unsigned char *hmac)
{
struct MD5Context s; struct MD5Context s;
unsigned char intermediate[16]; unsigned char intermediate[16];
@ -249,11 +263,13 @@ static void md5_do_hmac(struct MD5Context *s1, struct MD5Context *s2,
MD5Final(hmac, &s); MD5Final(hmac, &s);
} }
static void md5_generate(unsigned char *blk, int len, unsigned long seq) { static void md5_generate(unsigned char *blk, int len, unsigned long seq)
{
md5_do_hmac(&md5_cs_mac_s1, &md5_cs_mac_s2, blk, len, seq, blk + len); md5_do_hmac(&md5_cs_mac_s1, &md5_cs_mac_s2, blk, len, seq, blk + len);
} }
static int md5_verify(unsigned char *blk, int len, unsigned long seq) { static int md5_verify(unsigned char *blk, int len, unsigned long seq)
{
unsigned char correct[16]; unsigned char correct[16];
md5_do_hmac(&md5_sc_mac_s1, &md5_sc_mac_s2, blk, len, seq, correct); md5_do_hmac(&md5_sc_mac_s1, &md5_sc_mac_s2, blk, len, seq, correct);
return !memcmp(correct, blk + len, 16); return !memcmp(correct, blk + len, 16);

1620
sshprime.c

File diff suppressed because it is too large Load Diff

168
sshpubk.c
View File

@ -32,7 +32,8 @@
(x)=='/' ? 63 : 0 ) (x)=='/' ? 63 : 0 )
static int loadrsakey_main(FILE * fp, struct RSAKey *key, static int loadrsakey_main(FILE * fp, struct RSAKey *key,
char **commentptr, char *passphrase) { char **commentptr, char *passphrase)
{
unsigned char buf[16384]; unsigned char buf[16384];
unsigned char keybuf[16]; unsigned char keybuf[16];
int len; int len;
@ -65,8 +66,8 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key,
i++; i++;
if (len - i < 4) if (len - i < 4)
goto end; /* reserved field not present */ goto end; /* reserved field not present */
if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0) if (buf[i] != 0 || buf[i + 1] != 0 || buf[i + 2] != 0
goto end; /* reserved field nonzero, panic! */ || buf[i + 3] != 0) goto end; /* reserved field nonzero, panic! */
i += 4; i += 4;
/* Now the serious stuff. An ordinary SSH 1 public key. */ /* Now the serious stuff. An ordinary SSH 1 public key. */
@ -77,7 +78,8 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key,
/* Next, the comment field. */ /* Next, the comment field. */
j = GET_32BIT(buf + i); j = GET_32BIT(buf + i);
i += 4; i += 4;
if (len-i < j) goto end; if (len - i < j)
goto end;
comment = smalloc(j + 1); comment = smalloc(j + 1);
if (comment) { if (comment) {
memcpy(comment, buf + i, j); memcpy(comment, buf + i, j);
@ -107,8 +109,12 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key,
* We are now in the secret part of the key. The first four * We are now in the secret part of the key. The first four
* bytes should be of the form a, b, a, b. * bytes should be of the form a, b, a, b.
*/ */
if (len-i < 4) goto end; if (len - i < 4)
if (buf[i] != buf[i+2] || buf[i+1] != buf[i+3]) { ret = -1; goto end; } goto end;
if (buf[i] != buf[i + 2] || buf[i + 1] != buf[i + 3]) {
ret = -1;
goto end;
}
i += 4; i += 4;
/* /*
@ -117,13 +123,17 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key,
* (iqmp, q, p). * (iqmp, q, p).
*/ */
i += makeprivate(buf + i, key); i += makeprivate(buf + i, key);
if (len-i < 0) goto end; if (len - i < 0)
goto end;
i += ssh1_read_bignum(buf + i, &key->iqmp); i += ssh1_read_bignum(buf + i, &key->iqmp);
if (len-i < 0) goto end; if (len - i < 0)
goto end;
i += ssh1_read_bignum(buf + i, &key->q); i += ssh1_read_bignum(buf + i, &key->q);
if (len-i < 0) goto end; if (len - i < 0)
goto end;
i += ssh1_read_bignum(buf + i, &key->p); i += ssh1_read_bignum(buf + i, &key->p);
if (len-i < 0) goto end; if (len - i < 0)
goto end;
if (!rsa_verify(key)) { if (!rsa_verify(key)) {
freersakey(key); freersakey(key);
@ -136,7 +146,8 @@ static int loadrsakey_main(FILE *fp, struct RSAKey *key,
return ret; return ret;
} }
int loadrsakey(char *filename, struct RSAKey *key, char *passphrase) { int loadrsakey(char *filename, struct RSAKey *key, char *passphrase)
{
FILE *fp; FILE *fp;
unsigned char buf[64]; unsigned char buf[64];
@ -148,8 +159,7 @@ int loadrsakey(char *filename, struct RSAKey *key, char *passphrase) {
* Read the first line of the file and see if it's a v1 private * Read the first line of the file and see if it's a v1 private
* key file. * key file.
*/ */
if (fgets(buf, sizeof(buf), fp) && if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
!strcmp(buf, rsa_signature)) {
return loadrsakey_main(fp, key, NULL, passphrase); return loadrsakey_main(fp, key, NULL, passphrase);
} }
@ -164,7 +174,8 @@ int loadrsakey(char *filename, struct RSAKey *key, char *passphrase) {
* See whether an RSA key is encrypted. Return its comment field as * See whether an RSA key is encrypted. Return its comment field as
* well. * well.
*/ */
int rsakey_encrypted(char *filename, char **comment) { int rsakey_encrypted(char *filename, char **comment)
{
FILE *fp; FILE *fp;
unsigned char buf[64]; unsigned char buf[64];
@ -176,8 +187,7 @@ int rsakey_encrypted(char *filename, char **comment) {
* Read the first line of the file and see if it's a v1 private * Read the first line of the file and see if it's a v1 private
* key file. * key file.
*/ */
if (fgets(buf, sizeof(buf), fp) && if (fgets(buf, sizeof(buf), fp) && !strcmp(buf, rsa_signature)) {
!strcmp(buf, rsa_signature)) {
return loadrsakey_main(fp, NULL, comment, NULL); return loadrsakey_main(fp, NULL, comment, NULL);
} }
fclose(fp); fclose(fp);
@ -187,7 +197,8 @@ int rsakey_encrypted(char *filename, char **comment) {
/* /*
* Save an RSA key file. Return nonzero on success. * Save an RSA key file. Return nonzero on success.
*/ */
int saversakey(char *filename, struct RSAKey *key, char *passphrase) { int saversakey(char *filename, struct RSAKey *key, char *passphrase)
{
unsigned char buf[16384]; unsigned char buf[16384];
unsigned char keybuf[16]; unsigned char keybuf[16];
struct MD5Context md5c; struct MD5Context md5c;
@ -206,14 +217,16 @@ int saversakey(char *filename, struct RSAKey *key, char *passphrase) {
* uint32. * uint32.
*/ */
*p++ = (passphrase ? SSH_CIPHER_3DES : 0); *p++ = (passphrase ? SSH_CIPHER_3DES : 0);
PUT_32BIT(p, 0); p += 4; PUT_32BIT(p, 0);
p += 4;
/* /*
* An ordinary SSH 1 public key consists of: a uint32 * An ordinary SSH 1 public key consists of: a uint32
* containing the bit count, then two bignums containing the * containing the bit count, then two bignums containing the
* modulus and exponent respectively. * modulus and exponent respectively.
*/ */
PUT_32BIT(p, bignum_bitcount(key->modulus)); p += 4; PUT_32BIT(p, bignum_bitcount(key->modulus));
p += 4;
p += ssh1_write_bignum(p, key->modulus); p += ssh1_write_bignum(p, key->modulus);
p += ssh1_write_bignum(p, key->exponent); p += ssh1_write_bignum(p, key->exponent);
@ -221,11 +234,13 @@ int saversakey(char *filename, struct RSAKey *key, char *passphrase) {
* A string containing the comment field. * A string containing the comment field.
*/ */
if (key->comment) { if (key->comment) {
PUT_32BIT(p, strlen(key->comment)); p += 4; PUT_32BIT(p, strlen(key->comment));
p += 4;
memcpy(p, key->comment, strlen(key->comment)); memcpy(p, key->comment, strlen(key->comment));
p += strlen(key->comment); p += strlen(key->comment);
} else { } else {
PUT_32BIT(p, 0); p += 4; PUT_32BIT(p, 0);
p += 4;
} }
/* /*
@ -238,7 +253,9 @@ int saversakey(char *filename, struct RSAKey *key, char *passphrase) {
*/ */
*p++ = random_byte(); *p++ = random_byte();
*p++ = random_byte(); *p++ = random_byte();
p[0] = p[-2]; p[1] = p[-1]; p += 2; p[0] = p[-2];
p[1] = p[-1];
p += 2;
/* /*
* Four more bignums: the decryption exponent, then iqmp, then * Four more bignums: the decryption exponent, then iqmp, then
@ -345,7 +362,8 @@ int saversakey(char *filename, struct RSAKey *key, char *passphrase) {
* section other than just x. * section other than just x.
*/ */
static int read_header(FILE *fp, char *header) { static int read_header(FILE * fp, char *header)
{
int len = 39; int len = 39;
int c; int c;
@ -368,7 +386,8 @@ static int read_header(FILE *fp, char *header) {
return 0; /* failure */ return 0; /* failure */
} }
static char *read_body(FILE *fp) { static char *read_body(FILE * fp)
{
char *text; char *text;
int len; int len;
int size; int size;
@ -400,7 +419,8 @@ static char *read_body(FILE *fp) {
} }
} }
int base64_decode_atom(char *atom, unsigned char *out) { int base64_decode_atom(char *atom, unsigned char *out)
{
int vals[4]; int vals[4];
int i, v, len; int i, v, len;
unsigned word; unsigned word;
@ -438,9 +458,7 @@ int base64_decode_atom(char *atom, unsigned char *out) {
len = 1; len = 1;
word = ((vals[0] << 18) | word = ((vals[0] << 18) |
(vals[1] << 12) | (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
((vals[2] & 0x3F) << 6) |
(vals[3] & 0x3F));
out[0] = (word >> 16) & 0xFF; out[0] = (word >> 16) & 0xFF;
if (len > 1) if (len > 1)
out[1] = (word >> 8) & 0xFF; out[1] = (word >> 8) & 0xFF;
@ -449,7 +467,8 @@ int base64_decode_atom(char *atom, unsigned char *out) {
return len; return len;
} }
static char *read_blob(FILE *fp, int nlines, int *bloblen) { static char *read_blob(FILE * fp, int nlines, int *bloblen)
{
unsigned char *blob; unsigned char *blob;
char *line; char *line;
int linelen, len; int linelen, len;
@ -492,7 +511,8 @@ struct ssh2_userkey ssh2_wrong_passphrase = {
NULL, NULL, NULL NULL, NULL, NULL
}; };
struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase) { struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase)
{
FILE *fp; FILE *fp;
char header[40], *b, *comment, *hash; char header[40], *b, *comment, *hash;
const struct ssh_signkey *alg; const struct ssh_signkey *alg;
@ -511,7 +531,8 @@ struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase) {
goto error; goto error;
/* Read the first header line which contains the key type. */ /* Read the first header line which contains the key type. */
if (!read_header(fp, header) || 0!=strcmp(header, "PuTTY-User-Key-File-1")) if (!read_header(fp, header)
|| 0 != strcmp(header, "PuTTY-User-Key-File-1"))
goto error; goto error;
if ((b = read_body(fp)) == NULL) if ((b = read_body(fp)) == NULL)
goto error; goto error;
@ -530,9 +551,11 @@ struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase) {
if ((b = read_body(fp)) == NULL) if ((b = read_body(fp)) == NULL)
goto error; goto error;
if (!strcmp(b, "aes256-cbc")) { if (!strcmp(b, "aes256-cbc")) {
cipher = 1; cipherblk = 16; cipher = 1;
cipherblk = 16;
} else if (!strcmp(b, "none")) { } else if (!strcmp(b, "none")) {
cipher = 0; cipherblk = 1; cipher = 0;
cipherblk = 1;
} else { } else {
sfree(b); sfree(b);
goto error; goto error;
@ -641,15 +664,22 @@ struct ssh2_userkey *ssh2_load_userkey(char *filename, char *passphrase) {
* Error processing. * Error processing.
*/ */
error: error:
if (fp) fclose(fp); if (fp)
if (comment) sfree(comment); fclose(fp);
if (hash) sfree(hash); if (comment)
if (public_blob) sfree(public_blob); sfree(comment);
if (private_blob) sfree(private_blob); if (hash)
sfree(hash);
if (public_blob)
sfree(public_blob);
if (private_blob)
sfree(private_blob);
return ret; return ret;
} }
char *ssh2_userkey_loadpub(char *filename, char **algorithm, int *pub_blob_len) { char *ssh2_userkey_loadpub(char *filename, char **algorithm,
int *pub_blob_len)
{
FILE *fp; FILE *fp;
char header[40], *b; char header[40], *b;
const struct ssh_signkey *alg; const struct ssh_signkey *alg;
@ -664,7 +694,8 @@ char *ssh2_userkey_loadpub(char *filename, char **algorithm, int *pub_blob_len)
goto error; goto error;
/* Read the first header line which contains the key type. */ /* Read the first header line which contains the key type. */
if (!read_header(fp, header) || 0!=strcmp(header, "PuTTY-User-Key-File-1")) if (!read_header(fp, header)
|| 0 != strcmp(header, "PuTTY-User-Key-File-1"))
goto error; goto error;
if ((b = read_body(fp)) == NULL) if ((b = read_body(fp)) == NULL)
goto error; goto error;
@ -710,45 +741,59 @@ char *ssh2_userkey_loadpub(char *filename, char **algorithm, int *pub_blob_len)
* Error processing. * Error processing.
*/ */
error: error:
if (fp) fclose(fp); if (fp)
if (public_blob) sfree(public_blob); fclose(fp);
if (public_blob)
sfree(public_blob);
return NULL; return NULL;
} }
int ssh2_userkey_encrypted(char *filename, char **commentptr) { int ssh2_userkey_encrypted(char *filename, char **commentptr)
{
FILE *fp; FILE *fp;
char header[40], *b, *comment; char header[40], *b, *comment;
int ret; int ret;
if (commentptr) *commentptr = NULL; if (commentptr)
*commentptr = NULL;
fp = fopen(filename, "rb"); fp = fopen(filename, "rb");
if (!fp) if (!fp)
return 0; return 0;
if (!read_header(fp, header) || 0!=strcmp(header, "PuTTY-User-Key-File-1")) { if (!read_header(fp, header)
fclose(fp); return 0; || 0 != strcmp(header, "PuTTY-User-Key-File-1")) {
fclose(fp);
return 0;
} }
if ((b = read_body(fp)) == NULL) { if ((b = read_body(fp)) == NULL) {
fclose(fp); return 0; fclose(fp);
return 0;
} }
sfree(b); /* we don't care about key type here */ sfree(b); /* we don't care about key type here */
/* Read the Encryption header line. */ /* Read the Encryption header line. */
if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) { if (!read_header(fp, header) || 0 != strcmp(header, "Encryption")) {
fclose(fp); return 0; fclose(fp);
return 0;
} }
if ((b = read_body(fp)) == NULL) { if ((b = read_body(fp)) == NULL) {
fclose(fp); return 0; fclose(fp);
return 0;
} }
/* Read the Comment header line. */ /* Read the Comment header line. */
if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) { if (!read_header(fp, header) || 0 != strcmp(header, "Comment")) {
fclose(fp); sfree(b); return 1; fclose(fp);
sfree(b);
return 1;
} }
if ((comment = read_body(fp)) == NULL) { if ((comment = read_body(fp)) == NULL) {
fclose(fp); sfree(b); return 1; fclose(fp);
sfree(b);
return 1;
} }
if (commentptr) *commentptr = comment; if (commentptr)
*commentptr = comment;
fclose(fp); fclose(fp);
if (!strcmp(b, "aes256-cbc")) if (!strcmp(b, "aes256-cbc"))
@ -759,12 +804,14 @@ int ssh2_userkey_encrypted(char *filename, char **commentptr) {
return ret; return ret;
} }
int base64_lines(int datalen) { int base64_lines(int datalen)
{
/* When encoding, we use 64 chars/line, which equals 48 real chars. */ /* When encoding, we use 64 chars/line, which equals 48 real chars. */
return (datalen + 47) / 48; return (datalen + 47) / 48;
} }
void base64_encode_atom(unsigned char *data, int n, char *out) { void base64_encode_atom(unsigned char *data, int n, char *out)
{
static const char base64_chars[] = static const char base64_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -787,7 +834,8 @@ void base64_encode_atom(unsigned char *data, int n, char *out) {
out[3] = '='; out[3] = '=';
} }
void base64_encode(FILE *fp, unsigned char *data, int datalen) { void base64_encode(FILE * fp, unsigned char *data, int datalen)
{
int linelen = 0; int linelen = 0;
char out[4]; char out[4];
int n; int n;
@ -807,7 +855,9 @@ void base64_encode(FILE *fp, unsigned char *data, int datalen) {
fputc('\n', fp); fputc('\n', fp);
} }
int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, char *passphrase) { int ssh2_save_userkey(char *filename, struct ssh2_userkey *key,
char *passphrase)
{
FILE *fp; FILE *fp;
unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted; unsigned char *pub_blob, *priv_blob, *priv_blob_encrypted;
int pub_blob_len, priv_blob_len, priv_encrypted_len; int pub_blob_len, priv_blob_len, priv_encrypted_len;
@ -867,7 +917,8 @@ int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, char *passphrase
SHA_Bytes(&s, "\0\0\0\1", 4); SHA_Bytes(&s, "\0\0\0\1", 4);
SHA_Bytes(&s, passphrase, passlen); SHA_Bytes(&s, passphrase, passlen);
SHA_Final(&s, key + 20); SHA_Final(&s, key + 20);
aes256_encrypt_pubkey(key, priv_blob_encrypted, priv_encrypted_len); aes256_encrypt_pubkey(key, priv_blob_encrypted,
priv_encrypted_len);
} }
fp = fopen(filename, "w"); fp = fopen(filename, "w");
@ -892,7 +943,8 @@ int ssh2_save_userkey(char *filename, struct ssh2_userkey *key, char *passphrase
* A function to determine which version of SSH to try on a private * A function to determine which version of SSH to try on a private
* key file. Returns 0 on failure, 1 or 2 on success. * key file. Returns 0 on failure, 1 or 2 on success.
*/ */
int keyfile_version(char *filename) { int keyfile_version(char *filename)
{
FILE *fp; FILE *fp;
int i; int i;

View File

@ -41,7 +41,8 @@ struct RandPool {
static struct RandPool pool; static struct RandPool pool;
static int random_active = 0; static int random_active = 0;
void random_stir(void) { void random_stir(void)
{
word32 block[HASHINPUT / sizeof(word32)]; word32 block[HASHINPUT / sizeof(word32)];
word32 digest[HASHSIZE / sizeof(word32)]; word32 digest[HASHSIZE / sizeof(word32)];
int i, j, k; int i, j, k;
@ -111,7 +112,8 @@ void random_stir(void) {
pool.poolpos = sizeof(pool.incoming); pool.poolpos = sizeof(pool.incoming);
} }
void random_add_noise(void *noise, int length) { void random_add_noise(void *noise, int length)
{
unsigned char *p = noise; unsigned char *p = noise;
int i; int i;
@ -144,7 +146,8 @@ void random_add_noise(void *noise, int length) {
pool.incomingpos += length; pool.incomingpos += length;
} }
void random_add_heavynoise(void *noise, int length) { void random_add_heavynoise(void *noise, int length)
{
unsigned char *p = noise; unsigned char *p = noise;
int i; int i;
@ -160,7 +163,8 @@ void random_add_heavynoise(void *noise, int length) {
random_stir(); random_stir();
} }
static void random_add_heavynoise_bitbybit(void *noise, int length) { static void random_add_heavynoise_bitbybit(void *noise, int length)
{
unsigned char *p = noise; unsigned char *p = noise;
int i; int i;
@ -177,7 +181,8 @@ static void random_add_heavynoise_bitbybit(void *noise, int length) {
pool.poolpos = i; pool.poolpos = i;
} }
void random_init(void) { void random_init(void)
{
memset(&pool, 0, sizeof(pool)); /* just to start with */ memset(&pool, 0, sizeof(pool)); /* just to start with */
random_active = 1; random_active = 1;
@ -186,14 +191,16 @@ void random_init(void) {
random_stir(); random_stir();
} }
int random_byte(void) { int random_byte(void)
{
if (pool.poolpos >= POOLSIZE) if (pool.poolpos >= POOLSIZE)
random_stir(); random_stir();
return pool.pool[pool.poolpos++]; return pool.pool[pool.poolpos++];
} }
void random_get_savedata(void **data, int *len) { void random_get_savedata(void **data, int *len)
{
random_stir(); random_stir();
*data = pool.pool + pool.poolpos; *data = pool.pool + pool.poolpos;
*len = POOLSIZE / 2; *len = POOLSIZE / 2;

155
sshrsa.c
View File

@ -14,7 +14,8 @@
int makekey(unsigned char *data, struct RSAKey *result, int makekey(unsigned char *data, struct RSAKey *result,
unsigned char **keystr, int order) { unsigned char **keystr, int order)
{
unsigned char *p = data; unsigned char *p = data;
int i; int i;
@ -35,7 +36,8 @@ int makekey(unsigned char *data, struct RSAKey *result,
p += ssh1_read_bignum(p, result ? &result->exponent : NULL); p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
if (result) if (result)
result->bytes = (((p[0] << 8) + p[1]) + 7) / 8; result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
if (keystr) *keystr = p+2; if (keystr)
*keystr = p + 2;
p += ssh1_read_bignum(p, result ? &result->modulus : NULL); p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
if (order == 1) if (order == 1)
p += ssh1_read_bignum(p, result ? &result->exponent : NULL); p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
@ -43,11 +45,13 @@ int makekey(unsigned char *data, struct RSAKey *result,
return p - data; return p - data;
} }
int makeprivate(unsigned char *data, struct RSAKey *result) { int makeprivate(unsigned char *data, struct RSAKey *result)
{
return ssh1_read_bignum(data, &result->private_exponent); return ssh1_read_bignum(data, &result->private_exponent);
} }
void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) { void rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
{
Bignum b1, b2; Bignum b1, b2;
int i; int i;
unsigned char *p; unsigned char *p;
@ -76,13 +80,15 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
freebn(b2); freebn(b2);
} }
Bignum rsadecrypt(Bignum input, struct RSAKey *key) { Bignum rsadecrypt(Bignum input, struct RSAKey *key)
{
Bignum ret; Bignum ret;
ret = modpow(input, key->private_exponent, key->modulus); ret = modpow(input, key->private_exponent, key->modulus);
return ret; return ret;
} }
int rsastr_len(struct RSAKey *key) { int rsastr_len(struct RSAKey *key)
{
Bignum md, ex; Bignum md, ex;
int mdlen, exlen; int mdlen, exlen;
@ -93,7 +99,8 @@ int rsastr_len(struct RSAKey *key) {
return 4 * (mdlen + exlen) + 20; return 4 * (mdlen + exlen) + 20;
} }
void rsastr_fmt(char *str, struct RSAKey *key) { void rsastr_fmt(char *str, struct RSAKey *key)
{
Bignum md, ex; Bignum md, ex;
int len = 0, i, nibbles; int len = 0, i, nibbles;
static const char hex[] = "0123456789abcdef"; static const char hex[] = "0123456789abcdef";
@ -103,13 +110,17 @@ void rsastr_fmt(char *str, struct RSAKey *key) {
len += sprintf(str + len, "0x"); len += sprintf(str + len, "0x");
nibbles = (3 + bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(ex)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF]; str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
len += sprintf(str + len, ",0x"); len += sprintf(str + len, ",0x");
nibbles = (3 + bignum_bitcount(md))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(md)) / 4;
if (nibbles < 1)
nibbles = 1;
for (i = nibbles; i--;) for (i = nibbles; i--;)
str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]; str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
@ -120,7 +131,8 @@ void rsastr_fmt(char *str, struct RSAKey *key) {
* Generate a fingerprint string for the key. Compatible with the * Generate a fingerprint string for the key. Compatible with the
* OpenSSH fingerprint code. * OpenSSH fingerprint code.
*/ */
void rsa_fingerprint(char *str, int len, struct RSAKey *key) { void rsa_fingerprint(char *str, int len, struct RSAKey *key)
{
struct MD5Context md5c; struct MD5Context md5c;
unsigned char digest[16]; unsigned char digest[16];
char buffer[16 * 3 + 40]; char buffer[16 * 3 + 40];
@ -141,8 +153,10 @@ void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
sprintf(buffer, "%d ", bignum_bitcount(key->modulus)); sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]); sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
strncpy(str, buffer, len); str[len-1] = '\0'; digest[i]);
strncpy(str, buffer, len);
str[len - 1] = '\0';
slen = strlen(str); slen = strlen(str);
if (key->comment && slen < len - 1) { if (key->comment && slen < len - 1) {
str[slen] = ' '; str[slen] = ' ';
@ -156,7 +170,8 @@ void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
* data. We also check the private data itself: we ensure that p > * data. We also check the private data itself: we ensure that p >
* q and that iqmp really is the inverse of q mod p. * q and that iqmp really is the inverse of q mod p.
*/ */
int rsa_verify(struct RSAKey *key) { int rsa_verify(struct RSAKey *key)
{
Bignum n, ed, pm1, qm1; Bignum n, ed, pm1, qm1;
int cmp; int cmp;
@ -202,11 +217,16 @@ int rsa_verify(struct RSAKey *key) {
return 1; return 1;
} }
void freersakey(struct RSAKey *key) { void freersakey(struct RSAKey *key)
if (key->modulus) freebn(key->modulus); {
if (key->exponent) freebn(key->exponent); if (key->modulus)
if (key->private_exponent) freebn(key->private_exponent); freebn(key->modulus);
if (key->comment) sfree(key->comment); if (key->exponent)
freebn(key->exponent);
if (key->private_exponent)
freebn(key->private_exponent);
if (key->comment)
sfree(key->comment);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -225,18 +245,22 @@ void freersakey(struct RSAKey *key) {
(cp)[2] = (unsigned char)((value) >> 8); \ (cp)[2] = (unsigned char)((value) >> 8); \
(cp)[3] = (unsigned char)(value); } (cp)[3] = (unsigned char)(value); }
static void getstring(char **data, int *datalen, char **p, int *length) { static void getstring(char **data, int *datalen, char **p, int *length)
{
*p = NULL; *p = NULL;
if (*datalen < 4) if (*datalen < 4)
return; return;
*length = GET_32BIT(*data); *length = GET_32BIT(*data);
*datalen -= 4; *data += 4; *datalen -= 4;
*data += 4;
if (*datalen < *length) if (*datalen < *length)
return; return;
*p = *data; *p = *data;
*data += *length; *datalen -= *length; *data += *length;
*datalen -= *length;
} }
static Bignum getmp(char **data, int *datalen) { static Bignum getmp(char **data, int *datalen)
{
char *p; char *p;
int length; int length;
Bignum b; Bignum b;
@ -248,13 +272,15 @@ static Bignum getmp(char **data, int *datalen) {
return b; return b;
} }
static void *rsa2_newkey(char *data, int len) { static void *rsa2_newkey(char *data, int len)
{
char *p; char *p;
int slen; int slen;
struct RSAKey *rsa; struct RSAKey *rsa;
rsa = smalloc(sizeof(struct RSAKey)); rsa = smalloc(sizeof(struct RSAKey));
if (!rsa) return NULL; if (!rsa)
return NULL;
getstring(&data, &len, &p, &slen); getstring(&data, &len, &p, &slen);
if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
@ -269,13 +295,15 @@ static void *rsa2_newkey(char *data, int len) {
return rsa; return rsa;
} }
static void rsa2_freekey(void *key) { static void rsa2_freekey(void *key)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
freersakey(rsa); freersakey(rsa);
sfree(rsa); sfree(rsa);
} }
static char *rsa2_fmtkey(void *key) { static char *rsa2_fmtkey(void *key)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
char *p; char *p;
int len; int len;
@ -286,7 +314,8 @@ static char *rsa2_fmtkey(void *key) {
return p; return p;
} }
static unsigned char *rsa2_public_blob(void *key, int *len) { static unsigned char *rsa2_public_blob(void *key, int *len)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
int elen, mlen, bloblen; int elen, mlen, bloblen;
int i; int i;
@ -302,18 +331,25 @@ static unsigned char *rsa2_public_blob(void *key, int *len) {
bloblen = 19 + elen + mlen; bloblen = 19 + elen + mlen;
blob = smalloc(bloblen); blob = smalloc(bloblen);
p = blob; p = blob;
PUT_32BIT(p, 7); p += 4; PUT_32BIT(p, 7);
memcpy(p, "ssh-rsa", 7); p += 7; p += 4;
PUT_32BIT(p, elen); p += 4; memcpy(p, "ssh-rsa", 7);
for (i = elen; i-- ;) *p++ = bignum_byte(rsa->exponent, i); p += 7;
PUT_32BIT(p, mlen); p += 4; PUT_32BIT(p, elen);
for (i = mlen; i-- ;) *p++ = bignum_byte(rsa->modulus, i); p += 4;
for (i = elen; i--;)
*p++ = bignum_byte(rsa->exponent, i);
PUT_32BIT(p, mlen);
p += 4;
for (i = mlen; i--;)
*p++ = bignum_byte(rsa->modulus, i);
assert(p == blob + bloblen); assert(p == blob + bloblen);
*len = bloblen; *len = bloblen;
return blob; return blob;
} }
static unsigned char *rsa2_private_blob(void *key, int *len) { static unsigned char *rsa2_private_blob(void *key, int *len)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
int dlen, plen, qlen, ulen, bloblen; int dlen, plen, qlen, ulen, bloblen;
int i; int i;
@ -331,21 +367,30 @@ static unsigned char *rsa2_private_blob(void *key, int *len) {
bloblen = 16 + dlen + plen + qlen + ulen; bloblen = 16 + dlen + plen + qlen + ulen;
blob = smalloc(bloblen); blob = smalloc(bloblen);
p = blob; p = blob;
PUT_32BIT(p, dlen); p += 4; PUT_32BIT(p, dlen);
for (i = dlen; i-- ;) *p++ = bignum_byte(rsa->private_exponent, i); p += 4;
PUT_32BIT(p, plen); p += 4; for (i = dlen; i--;)
for (i = plen; i-- ;) *p++ = bignum_byte(rsa->p, i); *p++ = bignum_byte(rsa->private_exponent, i);
PUT_32BIT(p, qlen); p += 4; PUT_32BIT(p, plen);
for (i = qlen; i-- ;) *p++ = bignum_byte(rsa->q, i); p += 4;
PUT_32BIT(p, ulen); p += 4; for (i = plen; i--;)
for (i = ulen; i-- ;) *p++ = bignum_byte(rsa->iqmp, i); *p++ = bignum_byte(rsa->p, i);
PUT_32BIT(p, qlen);
p += 4;
for (i = qlen; i--;)
*p++ = bignum_byte(rsa->q, i);
PUT_32BIT(p, ulen);
p += 4;
for (i = ulen; i--;)
*p++ = bignum_byte(rsa->iqmp, i);
assert(p == blob + bloblen); assert(p == blob + bloblen);
*len = bloblen; *len = bloblen;
return blob; return blob;
} }
static void *rsa2_createkey(unsigned char *pub_blob, int pub_len, static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
unsigned char *priv_blob, int priv_len) { unsigned char *priv_blob, int priv_len)
{
struct RSAKey *rsa; struct RSAKey *rsa;
char *pb = (char *) priv_blob; char *pb = (char *) priv_blob;
@ -363,12 +408,14 @@ static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
return rsa; return rsa;
} }
static void *rsa2_openssh_createkey(unsigned char **blob, int *len) { static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
{
char **b = (char **) blob; char **b = (char **) blob;
struct RSAKey *rsa; struct RSAKey *rsa;
rsa = smalloc(sizeof(struct RSAKey)); rsa = smalloc(sizeof(struct RSAKey));
if (!rsa) return NULL; if (!rsa)
return NULL;
rsa->comment = NULL; rsa->comment = NULL;
rsa->modulus = getmp(b, len); rsa->modulus = getmp(b, len);
@ -393,7 +440,8 @@ static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
return rsa; return rsa;
} }
static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) { static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
int bloblen, i; int bloblen, i;
@ -402,8 +450,7 @@ static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) {
ssh2_bignum_length(rsa->exponent) + ssh2_bignum_length(rsa->exponent) +
ssh2_bignum_length(rsa->private_exponent) + ssh2_bignum_length(rsa->private_exponent) +
ssh2_bignum_length(rsa->iqmp) + ssh2_bignum_length(rsa->iqmp) +
ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
ssh2_bignum_length(rsa->q);
if (bloblen > len) if (bloblen > len)
return bloblen; return bloblen;
@ -422,7 +469,8 @@ static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len) {
return bloblen; return bloblen;
} }
static char *rsa2_fingerprint(void *key) { static char *rsa2_fingerprint(void *key)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
struct MD5Context md5c; struct MD5Context md5c;
unsigned char digest[16], lenbuf[4]; unsigned char digest[16], lenbuf[4];
@ -448,7 +496,8 @@ static char *rsa2_fingerprint(void *key) {
sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus)); sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]); sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
digest[i]);
ret = smalloc(strlen(buffer) + 1); ret = smalloc(strlen(buffer) + 1);
if (ret) if (ret)
strcpy(ret, buffer); strcpy(ret, buffer);
@ -487,7 +536,8 @@ static unsigned char asn1_weird_stuff[] = {
#define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) ) #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
static int rsa2_verifysig(void *key, char *sig, int siglen, static int rsa2_verifysig(void *key, char *sig, int siglen,
char *data, int datalen) { char *data, int datalen)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
Bignum in, out; Bignum in, out;
char *p; char *p;
@ -532,7 +582,8 @@ static int rsa2_verifysig(void *key, char *sig, int siglen,
return ret; return ret;
} }
unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) { unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen)
{
struct RSAKey *rsa = (struct RSAKey *) key; struct RSAKey *rsa = (struct RSAKey *) key;
unsigned char *bytes; unsigned char *bytes;
int nbytes; int nbytes;

View File

@ -7,23 +7,30 @@
#define RSA_EXPONENT 37 /* we like this prime */ #define RSA_EXPONENT 37 /* we like this prime */
#if 0 /* bignum diagnostic function */ #if 0 /* bignum diagnostic function */
static void diagbn(char *prefix, Bignum md) { static void diagbn(char *prefix, Bignum md)
{
int i, nibbles, morenibbles; int i, nibbles, morenibbles;
static const char hex[] = "0123456789ABCDEF"; static const char hex[] = "0123456789ABCDEF";
printf("%s0x", prefix ? prefix : ""); printf("%s0x", prefix ? prefix : "");
nibbles = (3 + bignum_bitcount(md))/4; if (nibbles<1) nibbles=1; nibbles = (3 + bignum_bitcount(md)) / 4;
if (nibbles < 1)
nibbles = 1;
morenibbles = 4 * md[0] - nibbles; morenibbles = 4 * md[0] - nibbles;
for (i=0; i<morenibbles; i++) putchar('-'); for (i = 0; i < morenibbles; i++)
putchar('-');
for (i = nibbles; i--;) for (i = nibbles; i--;)
putchar(hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]); putchar(hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]);
if (prefix) putchar('\n'); if (prefix)
putchar('\n');
} }
#endif #endif
int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, void *pfnparam) { int rsa_generate(struct RSAKey *key, int bits, progfn_t pfn,
void *pfnparam)
{
Bignum pm1, qm1, phi_n; Bignum pm1, qm1, phi_n;
/* /*

View File

@ -13,7 +13,8 @@
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) ) #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
void SHA_Core_Init(uint32 h[5]) { void SHA_Core_Init(uint32 h[5])
{
h[0] = 0x67452301; h[0] = 0x67452301;
h[1] = 0xefcdab89; h[1] = 0xefcdab89;
h[2] = 0x98badcfe; h[2] = 0x98badcfe;
@ -21,7 +22,8 @@ void SHA_Core_Init(uint32 h[5]) {
h[4] = 0xc3d2e1f0; h[4] = 0xc3d2e1f0;
} }
void SHATransform(word32 *digest, word32 *block) { void SHATransform(word32 * digest, word32 * block)
{
word32 w[80]; word32 w[80];
word32 a, b, c, d, e; word32 a, b, c, d, e;
int t; int t;
@ -41,20 +43,39 @@ 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 = rol(a, 5) + ( (b&c) | (d&~b) ) + e + w[t] + 0x5a827999; word32 tmp =
e = d; d = c; c = rol(b, 30); b = a; a = tmp; rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
e = d;
d = c;
c = rol(b, 30);
b = a;
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; word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
e = d; d = c; c = rol(b, 30); b = a; a = tmp; e = d;
d = c;
c = rol(b, 30);
b = a;
a = tmp;
} }
for (t = 40; t < 60; t++) { for (t = 40; t < 60; t++) {
word32 tmp = rol(a, 5) + ( (b&c) | (b&d) | (c&d) ) + e + w[t] + 0x8f1bbcdc; word32 tmp = rol(a,
e = d; d = c; c = rol(b, 30); b = a; a = tmp; 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
0x8f1bbcdc;
e = d;
d = c;
c = rol(b, 30);
b = a;
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; word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
e = d; d = c; c = rol(b, 30); b = a; a = tmp; e = d;
d = c;
c = rol(b, 30);
b = a;
a = tmp;
} }
digest[0] += a; digest[0] += a;
@ -70,13 +91,15 @@ void SHATransform(word32 *digest, word32 *block) {
* the end, and pass those blocks to the core SHA algorithm. * the end, and pass those blocks to the core SHA algorithm.
*/ */
void SHA_Init(SHA_State *s) { 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->lenhi = s->lenlo = 0;
} }
void SHA_Bytes(SHA_State *s, void *p, int len) { void SHA_Bytes(SHA_State * s, void *p, int len)
{
unsigned char *q = (unsigned char *) p; unsigned char *q = (unsigned char *) p;
uint32 wordblock[16]; uint32 wordblock[16];
uint32 lenw = len; uint32 lenw = len;
@ -118,7 +141,8 @@ void SHA_Bytes(SHA_State *s, void *p, int len) {
} }
} }
void SHA_Final(SHA_State *s, unsigned char *output) { 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];
@ -155,7 +179,8 @@ void SHA_Final(SHA_State *s, unsigned char *output) {
} }
} }
void SHA_Simple(void *p, int len, unsigned char *output) { void SHA_Simple(void *p, int len, unsigned char *output)
{
SHA_State s; SHA_State s;
SHA_Init(&s); SHA_Init(&s);
@ -172,7 +197,8 @@ static SHA_State sha1_cs_mac_s1, sha1_cs_mac_s2;
static SHA_State sha1_sc_mac_s1, sha1_sc_mac_s2; static SHA_State sha1_sc_mac_s1, sha1_sc_mac_s2;
static void sha1_key(SHA_State * s1, SHA_State * s2, static void sha1_key(SHA_State * s1, SHA_State * s2,
unsigned char *key, int len) { unsigned char *key, int len)
{
unsigned char foo[64]; unsigned char foo[64];
int i; int i;
@ -191,25 +217,30 @@ static void sha1_key(SHA_State *s1, SHA_State *s2,
memset(foo, 0, 64); /* burn the evidence */ memset(foo, 0, 64); /* burn the evidence */
} }
static void sha1_cskey(unsigned char *key) { static void sha1_cskey(unsigned char *key)
{
sha1_key(&sha1_cs_mac_s1, &sha1_cs_mac_s2, key, 20); sha1_key(&sha1_cs_mac_s1, &sha1_cs_mac_s2, key, 20);
} }
static void sha1_sckey(unsigned char *key) { static void sha1_sckey(unsigned char *key)
{
sha1_key(&sha1_sc_mac_s1, &sha1_sc_mac_s2, key, 20); sha1_key(&sha1_sc_mac_s1, &sha1_sc_mac_s2, key, 20);
} }
static void sha1_cskey_buggy(unsigned char *key) { static void sha1_cskey_buggy(unsigned char *key)
{
sha1_key(&sha1_cs_mac_s1, &sha1_cs_mac_s2, key, 16); sha1_key(&sha1_cs_mac_s1, &sha1_cs_mac_s2, key, 16);
} }
static void sha1_sckey_buggy(unsigned char *key) { static void sha1_sckey_buggy(unsigned char *key)
{
sha1_key(&sha1_sc_mac_s1, &sha1_sc_mac_s2, key, 16); sha1_key(&sha1_sc_mac_s1, &sha1_sc_mac_s2, key, 16);
} }
static void sha1_do_hmac(SHA_State * s1, SHA_State * s2, static void sha1_do_hmac(SHA_State * s1, SHA_State * s2,
unsigned char *blk, int len, unsigned long seq, unsigned char *blk, int len, unsigned long seq,
unsigned char *hmac) { unsigned char *hmac)
{
SHA_State s; SHA_State s;
unsigned char intermediate[20]; unsigned char intermediate[20];
@ -227,11 +258,14 @@ static void sha1_do_hmac(SHA_State *s1, SHA_State *s2,
SHA_Final(&s, hmac); SHA_Final(&s, hmac);
} }
static void sha1_generate(unsigned char *blk, int len, unsigned long seq) { static void sha1_generate(unsigned char *blk, int len, unsigned long seq)
sha1_do_hmac(&sha1_cs_mac_s1, &sha1_cs_mac_s2, blk, len, seq, blk+len); {
sha1_do_hmac(&sha1_cs_mac_s1, &sha1_cs_mac_s2, blk, len, seq,
blk + len);
} }
static int sha1_verify(unsigned char *blk, int len, unsigned long seq) { static int sha1_verify(unsigned char *blk, int len, unsigned long seq)
{
unsigned char correct[20]; unsigned char correct[20];
sha1_do_hmac(&sha1_sc_mac_s1, &sha1_sc_mac_s2, blk, len, seq, correct); sha1_do_hmac(&sha1_sc_mac_s1, &sha1_sc_mac_s2, blk, len, seq, correct);
return !memcmp(correct, blk + len, 20); return !memcmp(correct, blk + len, 20);

View File

@ -116,11 +116,13 @@ struct LZ77InternalContext {
int npending; int npending;
}; };
static int lz77_hash(unsigned char *data) { static int lz77_hash(unsigned char *data)
{
return (257 * data[0] + 263 * data[1] + 269 * data[2]) % HASHMAX; return (257 * data[0] + 263 * data[1] + 269 * data[2]) % HASHMAX;
} }
static int lz77_init(struct LZ77Context *ctx) { static int lz77_init(struct LZ77Context *ctx)
{
struct LZ77InternalContext *st; struct LZ77InternalContext *st;
int i; int i;
@ -142,7 +144,8 @@ static int lz77_init(struct LZ77Context *ctx) {
} }
static void lz77_advance(struct LZ77InternalContext *st, static void lz77_advance(struct LZ77InternalContext *st,
unsigned char c, int hash) { unsigned char c, int hash)
{
int off; int off;
/* /*
@ -176,7 +179,8 @@ static void lz77_advance(struct LZ77InternalContext *st,
#define CHARAT(k) ( (k)<0 ? st->data[(st->winpos+k)&(WINSIZE-1)] : data[k] ) #define CHARAT(k) ( (k)<0 ? st->data[(st->winpos+k)&(WINSIZE-1)] : data[k] )
static void lz77_compress(struct LZ77Context *ctx, static void lz77_compress(struct LZ77Context *ctx,
unsigned char *data, int len, int compress) { unsigned char *data, int len, int compress)
{
struct LZ77InternalContext *st = ctx->ictx; struct LZ77InternalContext *st = ctx->ictx;
int i, hash, distance, off, nmatch, matchlen, advance; int i, hash, distance, off, nmatch, matchlen, advance;
struct Match defermatch, matches[MAXMATCH]; struct Match defermatch, matches[MAXMATCH];
@ -221,7 +225,8 @@ static void lz77_compress(struct LZ77Context *ctx,
off != INVALID; off = st->win[off].next) { off != INVALID; off = st->win[off].next) {
/* distance = 1 if off == st->winpos-1 */ /* distance = 1 if off == st->winpos-1 */
/* distance = WINSIZE if off == st->winpos */ /* distance = WINSIZE if off == st->winpos */
distance = WINSIZE - (off + WINSIZE - st->winpos) % WINSIZE; distance =
WINSIZE - (off + WINSIZE - st->winpos) % WINSIZE;
for (i = 0; i < HASHCHARS; i++) for (i = 0; i < HASHCHARS; i++)
if (CHARAT(i) != CHARAT(i - distance)) if (CHARAT(i) != CHARAT(i - distance))
break; break;
@ -342,7 +347,8 @@ struct Outbuf {
int comp_disabled; int comp_disabled;
}; };
static void outbits(struct Outbuf *out, unsigned long bits, int nbits) { static void outbits(struct Outbuf *out, unsigned long bits, int nbits)
{
assert(out->noutbits + nbits <= 32); assert(out->noutbits + nbits <= 32);
out->outbits |= bits << out->noutbits; out->outbits |= bits << out->noutbits;
out->noutbits += nbits; out->noutbits += nbits;
@ -462,7 +468,8 @@ static const coderecord distcodes[] = {
{29, 13, 24577, 32768}, {29, 13, 24577, 32768},
}; };
static void zlib_literal(struct LZ77Context *ectx, unsigned char c) { static void zlib_literal(struct LZ77Context *ectx, unsigned char c)
{
struct Outbuf *out = (struct Outbuf *) ectx->userdata; struct Outbuf *out = (struct Outbuf *) ectx->userdata;
if (out->comp_disabled) { if (out->comp_disabled) {
@ -482,7 +489,8 @@ static void zlib_literal(struct LZ77Context *ectx, unsigned char c) {
} }
} }
static void zlib_match(struct LZ77Context *ectx, int distance, int len) { static void zlib_match(struct LZ77Context *ectx, int distance, int len)
{
const coderecord *d, *l; const coderecord *d, *l;
int i, j, k; int i, j, k;
struct Outbuf *out = (struct Outbuf *) ectx->userdata; struct Outbuf *out = (struct Outbuf *) ectx->userdata;
@ -509,7 +517,8 @@ static void zlib_match(struct LZ77Context *ectx, int distance, int len) {
* Binary-search to find which length code we're * Binary-search to find which length code we're
* transmitting. * transmitting.
*/ */
i = -1; j = sizeof(lencodes)/sizeof(*lencodes); i = -1;
j = sizeof(lencodes) / sizeof(*lencodes);
while (j - i >= 2) { while (j - i >= 2) {
k = (j + i) / 2; k = (j + i) / 2;
if (thislen < lencodes[k].min) if (thislen < lencodes[k].min)
@ -543,7 +552,8 @@ static void zlib_match(struct LZ77Context *ectx, int distance, int len) {
* Binary-search to find which distance code we're * Binary-search to find which distance code we're
* transmitting. * transmitting.
*/ */
i = -1; j = sizeof(distcodes)/sizeof(*distcodes); i = -1;
j = sizeof(distcodes) / sizeof(*distcodes);
while (j - i >= 2) { while (j - i >= 2) {
k = (j + i) / 2; k = (j + i) / 2;
if (distance < distcodes[k].min) if (distance < distcodes[k].min)
@ -569,7 +579,8 @@ static void zlib_match(struct LZ77Context *ectx, int distance, int len) {
} }
} }
void zlib_compress_init(void) { void zlib_compress_init(void)
{
struct Outbuf *out; struct Outbuf *out;
lz77_init(&ectx); lz77_init(&ectx);
@ -591,7 +602,8 @@ void zlib_compress_init(void) {
* length adjustment (which is only valid for packets < 65536 * length adjustment (which is only valid for packets < 65536
* bytes, but that seems reasonable enough). * bytes, but that seems reasonable enough).
*/ */
int zlib_disable_compression(void) { int zlib_disable_compression(void)
{
struct Outbuf *out = (struct Outbuf *) ectx.userdata; struct Outbuf *out = (struct Outbuf *) ectx.userdata;
int n; int n;
@ -626,7 +638,8 @@ int zlib_disable_compression(void) {
} }
int zlib_compress_block(unsigned char *block, int len, int zlib_compress_block(unsigned char *block, int len,
unsigned char **outblock, int *outlen) { unsigned char **outblock, int *outlen)
{
struct Outbuf *out = (struct Outbuf *) ectx.userdata; struct Outbuf *out = (struct Outbuf *) ectx.userdata;
int in_block; int in_block;
@ -780,7 +793,8 @@ struct zlib_table {
*/ */
static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths, static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
int nsyms, int nsyms,
int pfx, int pfxbits, int bits) { int pfx, int pfxbits, int bits)
{
struct zlib_table *tab = smalloc(sizeof(struct zlib_table)); struct zlib_table *tab = smalloc(sizeof(struct zlib_table));
int pfxmask = (1 << pfxbits) - 1; int pfxmask = (1 << pfxbits) - 1;
int nbits, i, j, code; int nbits, i, j, code;
@ -825,14 +839,17 @@ static struct zlib_table *zlib_mkonetab(int *codes, unsigned char *lengths,
/* /*
* Build a decode table, given a set of Huffman tree lengths. * Build a decode table, given a set of Huffman tree lengths.
*/ */
static struct zlib_table *zlib_mktable(unsigned char *lengths, int nlengths) { static struct zlib_table *zlib_mktable(unsigned char *lengths,
int nlengths)
{
int count[MAXCODELEN], startcode[MAXCODELEN], codes[MAXSYMS]; int count[MAXCODELEN], startcode[MAXCODELEN], codes[MAXSYMS];
int code, maxlen; int code, maxlen;
int i, j; int i, j;
/* Count the codes of each length. */ /* Count the codes of each length. */
maxlen = 0; maxlen = 0;
for (i = 1; i < MAXCODELEN; i++) count[i] = 0; for (i = 1; i < MAXCODELEN; i++)
count[i] = 0;
for (i = 0; i < nlengths; i++) { for (i = 0; i < nlengths; i++) {
count[lengths[i]]++; count[lengths[i]]++;
if (maxlen < lengths[i]) if (maxlen < lengths[i])
@ -863,7 +880,8 @@ static struct zlib_table *zlib_mktable(unsigned char *lengths, int nlengths) {
maxlen < 9 ? maxlen : 9); maxlen < 9 ? maxlen : 9);
} }
static int zlib_freetable(struct zlib_table ** ztab) { static int zlib_freetable(struct zlib_table **ztab)
{
struct zlib_table *tab; struct zlib_table *tab;
int code; int code;
@ -897,7 +915,8 @@ static struct zlib_decompress_ctx {
INBLK, GOTLENSYM, GOTLEN, GOTDISTSYM, INBLK, GOTLENSYM, GOTLEN, GOTDISTSYM,
UNCOMP_LEN, UNCOMP_NLEN, UNCOMP_DATA UNCOMP_LEN, UNCOMP_NLEN, UNCOMP_DATA
} state; } state;
int sym, hlit, hdist, hclen, lenptr, lenextrabits, lenaddon, len, lenrep; int sym, hlit, hdist, hclen, lenptr, lenextrabits, lenaddon, len,
lenrep;
int uncomplen; int uncomplen;
unsigned char lenlen[19]; unsigned char lenlen[19];
unsigned char lengths[286 + 32]; unsigned char lengths[286 + 32];
@ -909,7 +928,8 @@ static struct zlib_decompress_ctx {
int outlen, outsize; int outlen, outsize;
} dctx; } dctx;
void zlib_decompress_init(void) { void zlib_decompress_init(void)
{
unsigned char lengths[288]; unsigned char lengths[288];
memset(lengths, 8, 144); memset(lengths, 8, 144);
memset(lengths + 144, 9, 256 - 144); memset(lengths + 144, 9, 256 - 144);
@ -925,7 +945,9 @@ void zlib_decompress_init(void) {
logevent("Initialised zlib (RFC1950) decompression"); logevent("Initialised zlib (RFC1950) decompression");
} }
int zlib_huflookup(unsigned long *bitsp, int *nbitsp, struct zlib_table *tab) { int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
struct zlib_table *tab)
{
unsigned long bits = *bitsp; unsigned long bits = *bitsp;
int nbits = *nbitsp; int nbits = *nbitsp;
while (1) { while (1) {
@ -945,7 +967,8 @@ int zlib_huflookup(unsigned long *bitsp, int *nbitsp, struct zlib_table *tab) {
} }
} }
static void zlib_emit_char(int c) { static void zlib_emit_char(int c)
{
dctx.window[dctx.winpos] = c; dctx.window[dctx.winpos] = c;
dctx.winpos = (dctx.winpos + 1) & (WINSIZE - 1); dctx.winpos = (dctx.winpos + 1) & (WINSIZE - 1);
if (dctx.outlen >= dctx.outsize) { if (dctx.outlen >= dctx.outsize) {
@ -958,7 +981,8 @@ static void zlib_emit_char(int c) {
#define EATBITS(n) ( dctx.nbits -= (n), dctx.bits >>= (n) ) #define EATBITS(n) ( dctx.nbits -= (n), dctx.bits >>= (n) )
int zlib_decompress_block(unsigned char *block, int len, int zlib_decompress_block(unsigned char *block, int len,
unsigned char **outblock, int *outlen) { unsigned char **outblock, int *outlen)
{
const coderecord *rec; const coderecord *rec;
int code, blktype, rep, dist, nlen; int code, blktype, rep, dist, nlen;
static const unsigned char lenlenmap[] = { static const unsigned char lenlenmap[] = {
@ -1008,9 +1032,12 @@ int zlib_decompress_block(unsigned char *block, int len,
*/ */
if (dctx.nbits < 5 + 5 + 4) if (dctx.nbits < 5 + 5 + 4)
goto finished; /* done all we can */ goto finished; /* done all we can */
dctx.hlit = 257 + (dctx.bits & 31); EATBITS(5); dctx.hlit = 257 + (dctx.bits & 31);
dctx.hdist = 1 + (dctx.bits & 31); EATBITS(5); EATBITS(5);
dctx.hclen = 4 + (dctx.bits & 15); EATBITS(4); dctx.hdist = 1 + (dctx.bits & 31);
EATBITS(5);
dctx.hclen = 4 + (dctx.bits & 15);
EATBITS(4);
dctx.lenptr = 0; dctx.lenptr = 0;
dctx.state = TREES_LENLEN; dctx.state = TREES_LENLEN;
memset(dctx.lenlen, 0, sizeof(dctx.lenlen)); memset(dctx.lenlen, 0, sizeof(dctx.lenlen));
@ -1038,7 +1065,8 @@ int zlib_decompress_block(unsigned char *block, int len,
dctx.state = INBLK; dctx.state = INBLK;
break; break;
} }
code = zlib_huflookup(&dctx.bits, &dctx.nbits, dctx.lenlentable); code =
zlib_huflookup(&dctx.bits, &dctx.nbits, dctx.lenlentable);
if (code == -1) if (code == -1)
goto finished; goto finished;
if (code < 16) if (code < 16)
@ -1054,7 +1082,9 @@ int zlib_decompress_block(unsigned char *block, int len,
case TREES_LENREP: case TREES_LENREP:
if (dctx.nbits < dctx.lenextrabits) if (dctx.nbits < dctx.lenextrabits)
goto finished; goto finished;
rep = dctx.lenaddon + (dctx.bits & ((1<<dctx.lenextrabits)-1)); rep =
dctx.lenaddon +
(dctx.bits & ((1 << dctx.lenextrabits) - 1));
EATBITS(dctx.lenextrabits); EATBITS(dctx.lenextrabits);
while (rep > 0 && dctx.lenptr < dctx.hlit + dctx.hdist) { while (rep > 0 && dctx.lenptr < dctx.hlit + dctx.hdist) {
dctx.lengths[dctx.lenptr] = dctx.lenrep; dctx.lengths[dctx.lenptr] = dctx.lenrep;
@ -1064,7 +1094,8 @@ int zlib_decompress_block(unsigned char *block, int len,
dctx.state = TREES_LEN; dctx.state = TREES_LEN;
break; break;
case INBLK: case INBLK:
code = zlib_huflookup(&dctx.bits, &dctx.nbits, dctx.currlentable); code =
zlib_huflookup(&dctx.bits, &dctx.nbits, dctx.currlentable);
if (code == -1) if (code == -1)
goto finished; goto finished;
if (code < 256) if (code < 256)
@ -1084,12 +1115,15 @@ int zlib_decompress_block(unsigned char *block, int len,
rec = &lencodes[dctx.sym - 257]; rec = &lencodes[dctx.sym - 257];
if (dctx.nbits < rec->extrabits) if (dctx.nbits < rec->extrabits)
goto finished; goto finished;
dctx.len = rec->min + (dctx.bits & ((1<<rec->extrabits)-1)); dctx.len =
rec->min + (dctx.bits & ((1 << rec->extrabits) - 1));
EATBITS(rec->extrabits); EATBITS(rec->extrabits);
dctx.state = GOTLEN; dctx.state = GOTLEN;
break; break;
case GOTLEN: case GOTLEN:
code = zlib_huflookup(&dctx.bits, &dctx.nbits, dctx.currdisttable); code =
zlib_huflookup(&dctx.bits, &dctx.nbits,
dctx.currdisttable);
if (code == -1) if (code == -1)
goto finished; goto finished;
dctx.state = GOTDISTSYM; dctx.state = GOTDISTSYM;
@ -1103,7 +1137,8 @@ int zlib_decompress_block(unsigned char *block, int len,
EATBITS(rec->extrabits); EATBITS(rec->extrabits);
dctx.state = INBLK; dctx.state = INBLK;
while (dctx.len--) while (dctx.len--)
zlib_emit_char(dctx.window[(dctx.winpos-dist) & (WINSIZE-1)]); zlib_emit_char(dctx.window[(dctx.winpos - dist) &
(WINSIZE - 1)]);
break; break;
case UNCOMP_LEN: case UNCOMP_LEN:
/* /*

296
telnet.c
View File

@ -91,15 +91,49 @@ static Socket s = NULL;
#define iswritable(x) ( (x) != IAC && (x) != CR ) #define iswritable(x) ( (x) != IAC && (x) != CR )
static char *telopt(int opt) { static char *telopt(int opt)
{
#define i(x) if (opt == TELOPT_ ## x) return #x; #define i(x) if (opt == TELOPT_ ## x) return #x;
i(BINARY); i(ECHO); i(RCP); i(SGA); i(NAMS); i(STATUS); i(TM); i(RCTE); i(BINARY);
i(NAOL); i(NAOP); i(NAOCRD); i(NAOHTS); i(NAOHTD); i(NAOFFD); i(NAOVTS); i(ECHO);
i(NAOVTD); i(NAOLFD); i(XASCII); i(LOGOUT); i(BM); i(DET); i(SUPDUP); i(RCP);
i(SUPDUPOUTPUT); i(SNDLOC); i(TTYPE); i(EOR); i(TUID); i(OUTMRK); i(SGA);
i(TTYLOC); i(X3PAD); i(NAWS); i(TSPEED); i(LFLOW); i(LINEMODE); i(NAMS);
i(XDISPLOC); i(OLD_ENVIRON); i(AUTHENTICATION); i(ENCRYPT); i(STATUS);
i(NEW_ENVIRON); i(EXOPL); i(TM);
i(RCTE);
i(NAOL);
i(NAOP);
i(NAOCRD);
i(NAOHTS);
i(NAOHTD);
i(NAOFFD);
i(NAOVTS);
i(NAOVTD);
i(NAOLFD);
i(XASCII);
i(LOGOUT);
i(BM);
i(DET);
i(SUPDUP);
i(SUPDUPOUTPUT);
i(SNDLOC);
i(TTYPE);
i(EOR);
i(TUID);
i(OUTMRK);
i(TTYLOC);
i(X3PAD);
i(NAWS);
i(TSPEED);
i(LFLOW);
i(LINEMODE);
i(XDISPLOC);
i(OLD_ENVIRON);
i(AUTHENTICATION);
i(ENCRYPT);
i(NEW_ENVIRON);
i(EXOPL);
#undef i #undef i
return "<unknown>"; return "<unknown>";
} }
@ -116,16 +150,24 @@ struct Opt {
} state; } state;
}; };
static struct Opt o_naws = {WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED}; static struct Opt o_naws =
static struct Opt o_tspeed = {WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED}; { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED };
static struct Opt o_ttype = {WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED}; static struct Opt o_tspeed =
{ WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED };
static struct Opt o_ttype =
{ WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED };
static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
INACTIVE}; INACTIVE
};
static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
REQUESTED}; REQUESTED
static struct Opt o_echo = {DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED}; };
static struct Opt o_we_sga = {WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED}; static struct Opt o_echo =
static struct Opt o_they_sga = {DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED}; { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED };
static struct Opt o_we_sga =
{ WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED };
static struct Opt o_they_sga =
{ DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED };
static struct Opt *opts[] = { static struct Opt *opts[] = {
&o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
@ -140,12 +182,14 @@ static char *sb_buf = NULL;
static int sb_size = 0; static int sb_size = 0;
#define SB_DELTA 1024 #define SB_DELTA 1024
static void c_write1(int c) { static void c_write1(int c)
{
char cc = (char) c; char cc = (char) c;
from_backend(0, &cc, 1); from_backend(0, &cc, 1);
} }
static void log_option (char *sender, int cmd, int option) { static void log_option(char *sender, int cmd, int option)
{
char buf[50]; char buf[50];
sprintf(buf, "%s:\t%s %s", sender, sprintf(buf, "%s:\t%s %s", sender,
(cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
@ -154,15 +198,19 @@ static void log_option (char *sender, int cmd, int option) {
logevent(buf); logevent(buf);
} }
static void send_opt (int cmd, int option) { static void send_opt(int cmd, int option)
{
unsigned char b[3]; unsigned char b[3];
b[0] = IAC; b[1] = cmd; b[2] = option; b[0] = IAC;
b[1] = cmd;
b[2] = option;
sk_write(s, b, 3); sk_write(s, b, 3);
log_option("client", cmd, option); log_option("client", cmd, option);
} }
static void deactivate_option (struct Opt *o) { static void deactivate_option(struct Opt *o)
{
if (o->state == REQUESTED || o->state == ACTIVE) if (o->state == REQUESTED || o->state == ACTIVE)
send_opt(o->nsend, o->option); send_opt(o->nsend, o->option);
o->state = REALLY_INACTIVE; o->state = REALLY_INACTIVE;
@ -171,7 +219,8 @@ static void deactivate_option (struct Opt *o) {
/* /*
* Generate side effects of enabling or disabling an option. * Generate side effects of enabling or disabling an option.
*/ */
static void option_side_effects(struct Opt *o, int enabled) { static void option_side_effects(struct Opt *o, int enabled)
{
if (o->option == TELOPT_ECHO && o->send == DO) if (o->option == TELOPT_ECHO && o->send == DO)
echoing = !enabled; echoing = !enabled;
else if (o->option == TELOPT_SGA && o->send == DO) else if (o->option == TELOPT_SGA && o->send == DO)
@ -179,7 +228,8 @@ static void option_side_effects(struct Opt *o, int enabled) {
ldisc_send(NULL, 0); /* cause ldisc to notice the change */ ldisc_send(NULL, 0); /* cause ldisc to notice the change */
} }
static void activate_option (struct Opt *o) { static void activate_option(struct Opt *o)
{
if (o->send == WILL && o->option == TELOPT_NAWS) if (o->send == WILL && o->option == TELOPT_NAWS)
telnet_size(); telnet_size();
if (o->send == WILL && if (o->send == WILL &&
@ -189,12 +239,14 @@ static void activate_option (struct Opt *o) {
* We may only have one kind of ENVIRON going at a time. * We may only have one kind of ENVIRON going at a time.
* This is a hack, but who cares. * This is a hack, but who cares.
*/ */
deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); deactivate_option(o->option ==
TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
} }
option_side_effects(o, 1); option_side_effects(o, 1);
} }
static void refused_option (struct Opt *o) { static void refused_option(struct Opt *o)
{
if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
o_oenv.state == INACTIVE) { o_oenv.state == INACTIVE) {
send_opt(WILL, TELOPT_OLD_ENVIRON); send_opt(WILL, TELOPT_OLD_ENVIRON);
@ -203,7 +255,8 @@ static void refused_option (struct Opt *o) {
option_side_effects(o, 0); option_side_effects(o, 0);
} }
static void proc_rec_opt (int cmd, int option) { static void proc_rec_opt(int cmd, int option)
{
struct Opt **o; struct Opt **o;
log_option("server", cmd, option); log_option("server", cmd, option);
@ -251,7 +304,8 @@ static void proc_rec_opt (int cmd, int option) {
send_opt((cmd == WILL ? DONT : WONT), option); send_opt((cmd == WILL ? DONT : WONT), option);
} }
static void process_subneg (void) { static void process_subneg(void)
{
unsigned char b[2048], *p, *q; unsigned char b[2048], *p, *q;
int var, value, n; int var, value, n;
char *e; char *e;
@ -260,11 +314,14 @@ static void process_subneg (void) {
case TELOPT_TSPEED: case TELOPT_TSPEED:
if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
char logbuf[sizeof(cfg.termspeed) + 80]; char logbuf[sizeof(cfg.termspeed) + 80];
b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED; b[0] = IAC;
b[1] = SB;
b[2] = TELOPT_TSPEED;
b[3] = TELQUAL_IS; b[3] = TELQUAL_IS;
strcpy(b + 4, cfg.termspeed); strcpy(b + 4, cfg.termspeed);
n = 4 + strlen(cfg.termspeed); n = 4 + strlen(cfg.termspeed);
b[n] = IAC; b[n+1] = SE; b[n] = IAC;
b[n + 1] = SE;
sk_write(s, b, n + 2); sk_write(s, b, n + 2);
logevent("server:\tSB TSPEED SEND"); logevent("server:\tSB TSPEED SEND");
sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
@ -275,12 +332,17 @@ static void process_subneg (void) {
case TELOPT_TTYPE: case TELOPT_TTYPE:
if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
char logbuf[sizeof(cfg.termtype) + 80]; char logbuf[sizeof(cfg.termtype) + 80];
b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; b[0] = IAC;
b[1] = SB;
b[2] = TELOPT_TTYPE;
b[3] = TELQUAL_IS; b[3] = TELQUAL_IS;
for (n = 0; cfg.termtype[n]; n++) for (n = 0; cfg.termtype[n]; n++)
b[n+4] = (cfg.termtype[n] >= 'a' && cfg.termtype[n] <= 'z' ? b[n + 4] = (cfg.termtype[n] >= 'a'
cfg.termtype[n] + 'A'-'a' : cfg.termtype[n]); && cfg.termtype[n] <=
b[n+4] = IAC; b[n+5] = SE; 'z' ? cfg.termtype[n] + 'A' -
'a' : cfg.termtype[n]);
b[n + 4] = IAC;
b[n + 5] = SE;
sk_write(s, b, n + 6); sk_write(s, b, n + 6);
b[n + 4] = 0; b[n + 4] = 0;
logevent("server:\tSB TTYPE SEND"); logevent("server:\tSB TTYPE SEND");
@ -327,25 +389,36 @@ static void process_subneg (void) {
value = RFC_VALUE; value = RFC_VALUE;
var = RFC_VAR; var = RFC_VAR;
} }
b[0] = IAC; b[1] = SB; b[2] = sb_opt; b[0] = IAC;
b[1] = SB;
b[2] = sb_opt;
b[3] = TELQUAL_IS; b[3] = TELQUAL_IS;
n = 4; n = 4;
e = cfg.environmt; e = cfg.environmt;
while (*e) { while (*e) {
b[n++] = var; b[n++] = var;
while (*e && *e != '\t') b[n++] = *e++; while (*e && *e != '\t')
if (*e == '\t') e++; b[n++] = *e++;
if (*e == '\t')
e++;
b[n++] = value; b[n++] = value;
while (*e) b[n++] = *e++; while (*e)
b[n++] = *e++;
e++; e++;
} }
if (*cfg.username) { if (*cfg.username) {
b[n++] = var; b[n++] = 'U'; b[n++] = 'S'; b[n++] = var;
b[n++] = 'E'; b[n++] = 'R'; b[n++] = value; b[n++] = 'U';
b[n++] = 'S';
b[n++] = 'E';
b[n++] = 'R';
b[n++] = value;
e = cfg.username; e = cfg.username;
while (*e) b[n++] = *e++; while (*e)
b[n++] = *e++;
} }
b[n++] = IAC; b[n++] = SE; b[n++] = IAC;
b[n++] = SE;
sk_write(s, b, n); sk_write(s, b, n);
sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
n == 6 ? "<nothing>" : "<stuff>"); n == 6 ? "<nothing>" : "<stuff>");
@ -360,7 +433,8 @@ static enum {
SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
} telnet_state = TOPLEVEL; } telnet_state = TOPLEVEL;
static void do_telnet_read (char *buf, int len) { static void do_telnet_read(char *buf, int len)
{
while (len--) { while (len--) {
int c = (unsigned char) *buf++; int c = (unsigned char) *buf++;
@ -385,7 +459,8 @@ static void do_telnet_read (char *buf, int len) {
* Oh well, we do get the DM in the right place so I'll * Oh well, we do get the DM in the right place so I'll
* just stop hiding on the next 0xf2 and hope for the best. * just stop hiding on the next 0xf2 and hope for the best.
*/ */
else if (c == DM) in_synch = 0; else if (c == DM)
in_synch = 0;
#endif #endif
if (c == CR) if (c == CR)
telnet_state = SEENCR; telnet_state = SEENCR;
@ -394,16 +469,20 @@ static void do_telnet_read (char *buf, int len) {
} }
break; break;
case SEENIAC: case SEENIAC:
if (c == DO) telnet_state = SEENDO; if (c == DO)
else if (c == DONT) telnet_state = SEENDONT; telnet_state = SEENDO;
else if (c == WILL) telnet_state = SEENWILL; else if (c == DONT)
else if (c == WONT) telnet_state = SEENWONT; telnet_state = SEENDONT;
else if (c == SB) telnet_state = SEENSB; else if (c == WILL)
telnet_state = SEENWILL;
else if (c == WONT)
telnet_state = SEENWONT;
else if (c == SB)
telnet_state = SEENSB;
else if (c == DM) { else if (c == DM) {
in_synch = 0; in_synch = 0;
telnet_state = TOPLEVEL; telnet_state = TOPLEVEL;
} } else {
else {
/* ignore everything else; print it if it's IAC */ /* ignore everything else; print it if it's IAC */
if (c == IAC) { if (c == IAC) {
c_write1(c); c_write1(c);
@ -465,7 +544,9 @@ static void do_telnet_read (char *buf, int len) {
} }
} }
static int telnet_closing (Plug plug, char *error_msg, int error_code, int calling_back) { static int telnet_closing(Plug plug, char *error_msg, int error_code,
int calling_back)
{
sk_close(s); sk_close(s);
s = NULL; s = NULL;
if (error_msg) { if (error_msg) {
@ -475,8 +556,10 @@ static int telnet_closing (Plug plug, char *error_msg, int error_code, int calli
return 0; return 0;
} }
static int telnet_receive(Plug plug, int urgent, char *data, int len) { static int telnet_receive(Plug plug, int urgent, char *data, int len)
if(urgent) in_synch = TRUE; {
if (urgent)
in_synch = TRUE;
do_telnet_read(data, len); do_telnet_read(data, len);
return 1; return 1;
} }
@ -488,7 +571,8 @@ static int telnet_receive(Plug plug, int urgent, char *data, int len) {
* *
* Also places the canonical host name into `realhost'. * Also places the canonical host name into `realhost'.
*/ */
static char *telnet_init (char *host, int port, char **realhost) { static char *telnet_init(char *host, int port, char **realhost)
{
static struct plug_function_table fn_table = { static struct plug_function_table fn_table = {
telnet_closing, telnet_closing,
telnet_receive telnet_receive
@ -538,7 +622,8 @@ static char *telnet_init (char *host, int port, char **realhost) {
/* /*
* Called to send data down the Telnet connection. * Called to send data down the Telnet connection.
*/ */
static void telnet_send (char *buf, int len) { static void telnet_send(char *buf, int len)
{
char *p; char *p;
static unsigned char iac[2] = { IAC, IAC }; static unsigned char iac[2] = { IAC, IAC };
static unsigned char cr[2] = { CR, NUL }; static unsigned char cr[2] = { CR, NUL };
@ -551,7 +636,8 @@ static void telnet_send (char *buf, int len) {
while (p < buf + len) { while (p < buf + len) {
char *q = p; char *q = p;
while (iswritable((unsigned char)*p) && p < buf+len) p++; while (iswritable((unsigned char) *p) && p < buf + len)
p++;
sk_write(s, q, p - q); sk_write(s, q, p - q);
while (p < buf + len && !iswritable((unsigned char) *p)) { while (p < buf + len && !iswritable((unsigned char) *p)) {
@ -564,16 +650,22 @@ static void telnet_send (char *buf, int len) {
/* /*
* Called to set the size of the window from Telnet's POV. * Called to set the size of the window from Telnet's POV.
*/ */
static void telnet_size(void) { static void telnet_size(void)
{
unsigned char b[16]; unsigned char b[16];
char logbuf[50]; char logbuf[50];
if (s == NULL || o_naws.state != ACTIVE) if (s == NULL || o_naws.state != ACTIVE)
return; return;
b[0] = IAC; b[1] = SB; b[2] = TELOPT_NAWS; b[0] = IAC;
b[3] = cols >> 8; b[4] = cols & 0xFF; b[1] = SB;
b[5] = rows >> 8; b[6] = rows & 0xFF; b[2] = TELOPT_NAWS;
b[7] = IAC; b[8] = SE; b[3] = cols >> 8;
b[4] = cols & 0xFF;
b[5] = rows >> 8;
b[6] = rows & 0xFF;
b[7] = IAC;
b[8] = SE;
sk_write(s, b, 9); sk_write(s, b, 9);
sprintf(logbuf, "client:\tSB NAWS %d,%d", sprintf(logbuf, "client:\tSB NAWS %d,%d",
((unsigned char) b[3] << 8) + (unsigned char) b[4], ((unsigned char) b[3] << 8) + (unsigned char) b[4],
@ -584,7 +676,8 @@ static void telnet_size(void) {
/* /*
* Send Telnet special codes. * Send Telnet special codes.
*/ */
static void telnet_special (Telnet_Special code) { static void telnet_special(Telnet_Special code)
{
unsigned char b[2]; unsigned char b[2];
if (s == NULL) if (s == NULL)
@ -592,18 +685,54 @@ static void telnet_special (Telnet_Special code) {
b[0] = IAC; b[0] = IAC;
switch (code) { switch (code) {
case TS_AYT: b[1] = AYT; sk_write(s, b, 2); break; case TS_AYT:
case TS_BRK: b[1] = BREAK; sk_write(s, b, 2); break; b[1] = AYT;
case TS_EC: b[1] = EC; sk_write(s, b, 2); break; sk_write(s, b, 2);
case TS_EL: b[1] = EL; sk_write(s, b, 2); break; break;
case TS_GA: b[1] = GA; sk_write(s, b, 2); break; case TS_BRK:
case TS_NOP: b[1] = NOP; sk_write(s, b, 2); break; b[1] = BREAK;
case TS_ABORT: b[1] = ABORT; sk_write(s, b, 2); break; sk_write(s, b, 2);
case TS_AO: b[1] = AO; sk_write(s, b, 2); break; break;
case TS_IP: b[1] = IP; sk_write(s, b, 2); break; case TS_EC:
case TS_SUSP: b[1] = SUSP; sk_write(s, b, 2); break; b[1] = EC;
case TS_EOR: b[1] = EOR; sk_write(s, b, 2); break; sk_write(s, b, 2);
case TS_EOF: b[1] = xEOF; sk_write(s, b, 2); break; break;
case TS_EL:
b[1] = EL;
sk_write(s, b, 2);
break;
case TS_GA:
b[1] = GA;
sk_write(s, b, 2);
break;
case TS_NOP:
b[1] = NOP;
sk_write(s, b, 2);
break;
case TS_ABORT:
b[1] = ABORT;
sk_write(s, b, 2);
break;
case TS_AO:
b[1] = AO;
sk_write(s, b, 2);
break;
case TS_IP:
b[1] = IP;
sk_write(s, b, 2);
break;
case TS_SUSP:
b[1] = SUSP;
sk_write(s, b, 2);
break;
case TS_EOR:
b[1] = EOR;
sk_write(s, b, 2);
break;
case TS_EOF:
b[1] = xEOF;
sk_write(s, b, 2);
break;
case TS_SYNCH: case TS_SYNCH:
b[1] = DM; b[1] = DM;
sk_write(s, b, 1); sk_write(s, b, 1);
@ -630,13 +759,22 @@ static void telnet_special (Telnet_Special code) {
} }
} }
static Socket telnet_socket(void) { return s; } static Socket telnet_socket(void)
{
return s;
}
static int telnet_sendok(void) { return 1; } static int telnet_sendok(void)
{
return 1;
}
static int telnet_ldisc(int option) { static int telnet_ldisc(int option)
if (option == LD_ECHO) return echoing; {
if (option == LD_EDIT) return editing; if (option == LD_ECHO)
return echoing;
if (option == LD_EDIT)
return editing;
return FALSE; return FALSE;
} }

File diff suppressed because it is too large Load Diff

350
tree234.c
View File

@ -59,7 +59,8 @@ struct node234_Tag {
/* /*
* Create a 2-3-4 tree. * Create a 2-3-4 tree.
*/ */
tree234 *newtree234(cmpfn234 cmp) { tree234 *newtree234(cmpfn234 cmp)
{
tree234 *ret = mknew(tree234); tree234 *ret = mknew(tree234);
LOG(("created tree %p\n", ret)); LOG(("created tree %p\n", ret));
ret->root = NULL; ret->root = NULL;
@ -70,7 +71,8 @@ tree234 *newtree234(cmpfn234 cmp) {
/* /*
* Free a 2-3-4 tree (not including freeing the elements). * Free a 2-3-4 tree (not including freeing the elements).
*/ */
static void freenode234(node234 *n) { static void freenode234(node234 * n)
{
if (!n) if (!n)
return; return;
freenode234(n->kids[0]); freenode234(n->kids[0]);
@ -79,7 +81,9 @@ static void freenode234(node234 *n) {
freenode234(n->kids[3]); freenode234(n->kids[3]);
sfree(n); sfree(n);
} }
void freetree234(tree234 *t) {
void freetree234(tree234 * t)
{
freenode234(t->root); freenode234(t->root);
sfree(t); sfree(t);
} }
@ -87,7 +91,8 @@ void freetree234(tree234 *t) {
/* /*
* Internal function to count a node. * Internal function to count a node.
*/ */
static int countnode234(node234 *n) { static int countnode234(node234 * n)
{
int count = 0; int count = 0;
int i; int i;
if (!n) if (!n)
@ -103,7 +108,8 @@ static int countnode234(node234 *n) {
/* /*
* Count the elements in a tree. * Count the elements in a tree.
*/ */
int count234(tree234 *t) { int count234(tree234 * t)
{
if (t->root) if (t->root)
return countnode234(t->root); return countnode234(t->root);
else else
@ -114,7 +120,8 @@ int count234(tree234 *t) {
* Add an element e to a 2-3-4 tree t. Returns e on success, or if * Add an element e to a 2-3-4 tree t. Returns e on success, or if
* an existing element compares equal, returns that. * an existing element compares equal, returns that.
*/ */
static void *add234_internal(tree234 *t, void *e, int index) { static void *add234_internal(tree234 * t, void *e, int index)
{
node234 *n, **np, *left, *right; node234 *n, **np, *left, *right;
void *orig_e = e; void *orig_e = e;
int c, lcount, rcount; int c, lcount, rcount;
@ -186,12 +193,12 @@ static void *add234_internal(tree234 *t, void *e, int index) {
childnum = 0; childnum = 0;
else if (c == 0) else if (c == 0)
return n->elems[0]; /* already exists */ return n->elems[0]; /* already exists */
else if (n->elems[1] == NULL || (c = t->cmp(e, n->elems[1])) < 0) else if (n->elems[1] == NULL
childnum = 1; || (c = t->cmp(e, n->elems[1])) < 0) childnum = 1;
else if (c == 0) else if (c == 0)
return n->elems[1]; /* already exists */ return n->elems[1]; /* already exists */
else if (n->elems[2] == NULL || (c = t->cmp(e, n->elems[2])) < 0) else if (n->elems[2] == NULL
childnum = 2; || (c = t->cmp(e, n->elems[2])) < 0) childnum = 2;
else if (c == 0) else if (c == 0)
return n->elems[2]; /* already exists */ return n->elems[2]; /* already exists */
else else
@ -204,8 +211,10 @@ static void *add234_internal(tree234 *t, void *e, int index) {
/* /*
* We need to insert the new element in n at position np. * We need to insert the new element in n at position np.
*/ */
left = NULL; lcount = 0; left = NULL;
right = NULL; rcount = 0; lcount = 0;
right = NULL;
rcount = 0;
while (n) { while (n) {
LOG((" at %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n", LOG((" at %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n",
n, n,
@ -221,20 +230,28 @@ static void *add234_internal(tree234 *t, void *e, int index) {
*/ */
if (np == &n->kids[0]) { if (np == &n->kids[0]) {
LOG((" inserting on left of 2-node\n")); LOG((" inserting on left of 2-node\n"));
n->kids[2] = n->kids[1]; n->counts[2] = n->counts[1]; n->kids[2] = n->kids[1];
n->counts[2] = n->counts[1];
n->elems[1] = n->elems[0]; n->elems[1] = n->elems[0];
n->kids[1] = right; n->counts[1] = rcount; n->kids[1] = right;
n->counts[1] = rcount;
n->elems[0] = e; n->elems[0] = e;
n->kids[0] = left; n->counts[0] = lcount; n->kids[0] = left;
n->counts[0] = lcount;
} else { /* np == &n->kids[1] */ } else { /* np == &n->kids[1] */
LOG((" inserting on right of 2-node\n")); LOG((" inserting on right of 2-node\n"));
n->kids[2] = right; n->counts[2] = rcount; n->kids[2] = right;
n->counts[2] = rcount;
n->elems[1] = e; n->elems[1] = e;
n->kids[1] = left; n->counts[1] = lcount; n->kids[1] = left;
n->counts[1] = lcount;
} }
if (n->kids[0]) n->kids[0]->parent = n; if (n->kids[0])
if (n->kids[1]) n->kids[1]->parent = n; n->kids[0]->parent = n;
if (n->kids[2]) n->kids[2]->parent = n; if (n->kids[1])
n->kids[1]->parent = n;
if (n->kids[2])
n->kids[2]->parent = n;
LOG((" done\n")); LOG((" done\n"));
break; break;
} else if (n->elems[2] == NULL) { } else if (n->elems[2] == NULL) {
@ -243,30 +260,43 @@ static void *add234_internal(tree234 *t, void *e, int index) {
*/ */
if (np == &n->kids[0]) { if (np == &n->kids[0]) {
LOG((" inserting on left of 3-node\n")); LOG((" inserting on left of 3-node\n"));
n->kids[3] = n->kids[2]; n->counts[3] = n->counts[2]; n->kids[3] = n->kids[2];
n->counts[3] = n->counts[2];
n->elems[2] = n->elems[1]; n->elems[2] = n->elems[1];
n->kids[2] = n->kids[1]; n->counts[2] = n->counts[1]; n->kids[2] = n->kids[1];
n->counts[2] = n->counts[1];
n->elems[1] = n->elems[0]; n->elems[1] = n->elems[0];
n->kids[1] = right; n->counts[1] = rcount; n->kids[1] = right;
n->counts[1] = rcount;
n->elems[0] = e; n->elems[0] = e;
n->kids[0] = left; n->counts[0] = lcount; n->kids[0] = left;
n->counts[0] = lcount;
} else if (np == &n->kids[1]) { } else if (np == &n->kids[1]) {
LOG((" inserting in middle of 3-node\n")); LOG((" inserting in middle of 3-node\n"));
n->kids[3] = n->kids[2]; n->counts[3] = n->counts[2]; n->kids[3] = n->kids[2];
n->counts[3] = n->counts[2];
n->elems[2] = n->elems[1]; n->elems[2] = n->elems[1];
n->kids[2] = right; n->counts[2] = rcount; n->kids[2] = right;
n->counts[2] = rcount;
n->elems[1] = e; n->elems[1] = e;
n->kids[1] = left; n->counts[1] = lcount; n->kids[1] = left;
n->counts[1] = lcount;
} else { /* np == &n->kids[2] */ } else { /* np == &n->kids[2] */
LOG((" inserting on right of 3-node\n")); LOG((" inserting on right of 3-node\n"));
n->kids[3] = right; n->counts[3] = rcount; n->kids[3] = right;
n->counts[3] = rcount;
n->elems[2] = e; n->elems[2] = e;
n->kids[2] = left; n->counts[2] = lcount; n->kids[2] = left;
n->counts[2] = lcount;
} }
if (n->kids[0]) n->kids[0]->parent = n; if (n->kids[0])
if (n->kids[1]) n->kids[1]->parent = n; n->kids[0]->parent = n;
if (n->kids[2]) n->kids[2]->parent = n; if (n->kids[1])
if (n->kids[3]) n->kids[3]->parent = n; n->kids[1]->parent = n;
if (n->kids[2])
n->kids[2]->parent = n;
if (n->kids[3])
n->kids[3]->parent = n;
LOG((" done\n")); LOG((" done\n"));
break; break;
} else { } else {
@ -282,54 +312,79 @@ static void *add234_internal(tree234 *t, void *e, int index) {
* always. * always.
*/ */
if (np == &n->kids[0]) { if (np == &n->kids[0]) {
m->kids[0] = left; m->counts[0] = lcount; m->kids[0] = left;
m->counts[0] = lcount;
m->elems[0] = e; m->elems[0] = e;
m->kids[1] = right; m->counts[1] = rcount; m->kids[1] = right;
m->counts[1] = rcount;
m->elems[1] = n->elems[0]; m->elems[1] = n->elems[0];
m->kids[2] = n->kids[1]; m->counts[2] = n->counts[1]; m->kids[2] = n->kids[1];
m->counts[2] = n->counts[1];
e = n->elems[1]; e = n->elems[1];
n->kids[0] = n->kids[2]; n->counts[0] = n->counts[2]; n->kids[0] = n->kids[2];
n->counts[0] = n->counts[2];
n->elems[0] = n->elems[2]; n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3]; n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else if (np == &n->kids[1]) { } else if (np == &n->kids[1]) {
m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0]; m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0]; m->elems[0] = n->elems[0];
m->kids[1] = left; m->counts[1] = lcount; m->kids[1] = left;
m->counts[1] = lcount;
m->elems[1] = e; m->elems[1] = e;
m->kids[2] = right; m->counts[2] = rcount; m->kids[2] = right;
m->counts[2] = rcount;
e = n->elems[1]; e = n->elems[1];
n->kids[0] = n->kids[2]; n->counts[0] = n->counts[2]; n->kids[0] = n->kids[2];
n->counts[0] = n->counts[2];
n->elems[0] = n->elems[2]; n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3]; n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else if (np == &n->kids[2]) { } else if (np == &n->kids[2]) {
m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0]; m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0]; m->elems[0] = n->elems[0];
m->kids[1] = n->kids[1]; m->counts[1] = n->counts[1]; m->kids[1] = n->kids[1];
m->counts[1] = n->counts[1];
m->elems[1] = n->elems[1]; m->elems[1] = n->elems[1];
m->kids[2] = left; m->counts[2] = lcount; m->kids[2] = left;
m->counts[2] = lcount;
/* e = e; */ /* e = e; */
n->kids[0] = right; n->counts[0] = rcount; n->kids[0] = right;
n->counts[0] = rcount;
n->elems[0] = n->elems[2]; n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3]; n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else { /* np == &n->kids[3] */ } else { /* np == &n->kids[3] */
m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0]; m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0]; m->elems[0] = n->elems[0];
m->kids[1] = n->kids[1]; m->counts[1] = n->counts[1]; m->kids[1] = n->kids[1];
m->counts[1] = n->counts[1];
m->elems[1] = n->elems[1]; m->elems[1] = n->elems[1];
m->kids[2] = n->kids[2]; m->counts[2] = n->counts[2]; m->kids[2] = n->kids[2];
n->kids[0] = left; n->counts[0] = lcount; m->counts[2] = n->counts[2];
n->kids[0] = left;
n->counts[0] = lcount;
n->elems[0] = e; n->elems[0] = e;
n->kids[1] = right; n->counts[1] = rcount; n->kids[1] = right;
n->counts[1] = rcount;
e = n->elems[2]; e = n->elems[2];
} }
m->kids[3] = n->kids[3] = n->kids[2] = NULL; m->kids[3] = n->kids[3] = n->kids[2] = NULL;
m->counts[3] = n->counts[3] = n->counts[2] = 0; m->counts[3] = n->counts[3] = n->counts[2] = 0;
m->elems[2] = n->elems[2] = n->elems[1] = NULL; m->elems[2] = n->elems[2] = n->elems[1] = NULL;
if (m->kids[0]) m->kids[0]->parent = m; if (m->kids[0])
if (m->kids[1]) m->kids[1]->parent = m; m->kids[0]->parent = m;
if (m->kids[2]) m->kids[2]->parent = m; if (m->kids[1])
if (n->kids[0]) n->kids[0]->parent = n; m->kids[1]->parent = m;
if (n->kids[1]) n->kids[1]->parent = n; if (m->kids[2])
m->kids[2]->parent = m;
if (n->kids[0])
n->kids[0]->parent = n;
if (n->kids[1])
n->kids[1]->parent = n;
LOG((" left (%p): %p/%d [%p] %p/%d [%p] %p/%d\n", m, LOG((" left (%p): %p/%d [%p] %p/%d [%p] %p/%d\n", m,
m->kids[0], m->counts[0], m->elems[0], m->kids[0], m->counts[0], m->elems[0],
m->kids[1], m->counts[1], m->elems[1], m->kids[1], m->counts[1], m->elems[1],
@ -337,8 +392,10 @@ static void *add234_internal(tree234 *t, void *e, int index) {
LOG((" right (%p): %p/%d [%p] %p/%d\n", n, LOG((" right (%p): %p/%d [%p] %p/%d\n", n,
n->kids[0], n->counts[0], n->elems[0], n->kids[0], n->counts[0], n->elems[0],
n->kids[1], n->counts[1])); n->kids[1], n->counts[1]));
left = m; lcount = countnode234(left); left = m;
right = n; rcount = countnode234(right); lcount = countnode234(left);
right = n;
rcount = countnode234(right);
} }
if (n->parent) if (n->parent)
np = (n->parent->kids[0] == n ? &n->parent->kids[0] : np = (n->parent->kids[0] == n ? &n->parent->kids[0] :
@ -367,32 +424,39 @@ static void *add234_internal(tree234 *t, void *e, int index) {
} else { } else {
LOG((" root is overloaded, split into two\n")); LOG((" root is overloaded, split into two\n"));
t->root = mknew(node234); t->root = mknew(node234);
t->root->kids[0] = left; t->root->counts[0] = lcount; t->root->kids[0] = left;
t->root->counts[0] = lcount;
t->root->elems[0] = e; t->root->elems[0] = e;
t->root->kids[1] = right; t->root->counts[1] = rcount; t->root->kids[1] = right;
t->root->counts[1] = rcount;
t->root->elems[1] = NULL; t->root->elems[1] = NULL;
t->root->kids[2] = NULL; t->root->counts[2] = 0; t->root->kids[2] = NULL;
t->root->counts[2] = 0;
t->root->elems[2] = NULL; t->root->elems[2] = NULL;
t->root->kids[3] = NULL; t->root->counts[3] = 0; t->root->kids[3] = NULL;
t->root->counts[3] = 0;
t->root->parent = NULL; t->root->parent = NULL;
if (t->root->kids[0]) t->root->kids[0]->parent = t->root; if (t->root->kids[0])
if (t->root->kids[1]) t->root->kids[1]->parent = t->root; t->root->kids[0]->parent = t->root;
if (t->root->kids[1])
t->root->kids[1]->parent = t->root;
LOG((" new root is %p/%d [%p] %p/%d\n", LOG((" new root is %p/%d [%p] %p/%d\n",
t->root->kids[0], t->root->counts[0], t->root->kids[0], t->root->counts[0],
t->root->elems[0], t->root->elems[0], t->root->kids[1], t->root->counts[1]));
t->root->kids[1], t->root->counts[1]));
} }
return orig_e; return orig_e;
} }
void *add234(tree234 *t, void *e) { void *add234(tree234 * t, void *e)
{
if (!t->cmp) /* tree is unsorted */ if (!t->cmp) /* tree is unsorted */
return NULL; return NULL;
return add234_internal(t, e, -1); return add234_internal(t, e, -1);
} }
void *addpos234(tree234 *t, void *e, int index) { void *addpos234(tree234 * t, void *e, int index)
{
if (index < 0 || /* index out of range */ if (index < 0 || /* index out of range */
t->cmp) /* tree is sorted */ t->cmp) /* tree is sorted */
return NULL; /* return failure */ return NULL; /* return failure */
@ -404,7 +468,8 @@ void *addpos234(tree234 *t, void *e, int index) {
* Look up the element at a given numeric index in a 2-3-4 tree. * Look up the element at a given numeric index in a 2-3-4 tree.
* Returns NULL if the index is out of range. * Returns NULL if the index is out of range.
*/ */
void *index234(tree234 *t, int index) { void *index234(tree234 * t, int index)
{
node234 *n; node234 *n;
if (!t->root) if (!t->root)
@ -444,7 +509,8 @@ void *index234(tree234 *t, int index) {
* will be used. * will be used.
*/ */
void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp, void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp,
int relation, int *index) { int relation, int *index)
{
node234 *n; node234 *n;
void *ret; void *ret;
int c; int c;
@ -479,7 +545,8 @@ void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp,
(c = cmpret ? cmpret : cmp(e, n->elems[kcount])) < 0) { (c = cmpret ? cmpret : cmp(e, n->elems[kcount])) < 0) {
break; break;
} }
if (n->kids[kcount]) idx += n->counts[kcount]; if (n->kids[kcount])
idx += n->counts[kcount];
if (c == 0) { if (c == 0) {
ecount = kcount; ecount = kcount;
break; break;
@ -501,7 +568,8 @@ void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp,
* relation is EQ, LE or GE we can now go home. * relation is EQ, LE or GE we can now go home.
*/ */
if (relation != REL234_LT && relation != REL234_GT) { if (relation != REL234_LT && relation != REL234_GT) {
if (index) *index = idx; if (index)
*index = idx;
return n->elems[ecount]; return n->elems[ecount];
} }
@ -544,16 +612,20 @@ void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp,
* bounds, which is exactly what we want. * bounds, which is exactly what we want.
*/ */
ret = index234(t, idx); ret = index234(t, idx);
if (ret && index) *index = idx; if (ret && index)
*index = idx;
return ret; return ret;
} }
void *find234(tree234 *t, void *e, cmpfn234 cmp) { void *find234(tree234 * t, void *e, cmpfn234 cmp)
{
return findrelpos234(t, e, cmp, REL234_EQ, NULL); return findrelpos234(t, e, cmp, REL234_EQ, NULL);
} }
void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation) { void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation)
{
return findrelpos234(t, e, cmp, relation, NULL); return findrelpos234(t, e, cmp, relation, NULL);
} }
void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index) { void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index)
{
return findrelpos234(t, e, cmp, REL234_EQ, index); return findrelpos234(t, e, cmp, REL234_EQ, index);
} }
@ -561,7 +633,8 @@ void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index) {
* Delete an element e in a 2-3-4 tree. Does not free the element, * Delete an element e in a 2-3-4 tree. Does not free the element,
* merely removes all links to it from the tree nodes. * merely removes all links to it from the tree nodes.
*/ */
static void *delpos234_internal(tree234 *t, int index) { static void *delpos234_internal(tree234 * t, int index)
{
node234 *n; node234 *n;
void *retval; void *retval;
int ei = -1; int ei = -1;
@ -575,25 +648,26 @@ static void *delpos234_internal(tree234 *t, int index) {
int ki; int ki;
node234 *sub; node234 *sub;
LOG((" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d index=%d\n", LOG(
n, (" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d index=%d\n",
n->kids[0], n->counts[0], n->elems[0], n, n->kids[0], n->counts[0], n->elems[0], n->kids[1],
n->kids[1], n->counts[1], n->elems[1], n->counts[1], n->elems[1], n->kids[2], n->counts[2],
n->kids[2], n->counts[2], n->elems[2], n->elems[2], n->kids[3], n->counts[3], index));
n->kids[3], n->counts[3],
index));
if (index < n->counts[0]) { if (index < n->counts[0]) {
ki = 0; ki = 0;
} else if (index -= n->counts[0] + 1, index < 0) { } else if (index -= n->counts[0] + 1, index < 0) {
ei = 0; break; ei = 0;
break;
} else if (index < n->counts[1]) { } else if (index < n->counts[1]) {
ki = 1; ki = 1;
} else if (index -= n->counts[1] + 1, index < 0) { } else if (index -= n->counts[1] + 1, index < 0) {
ei = 1; break; ei = 1;
break;
} else if (index < n->counts[2]) { } else if (index < n->counts[2]) {
ki = 2; ki = 2;
} else if (index -= n->counts[2] + 1, index < 0) { } else if (index -= n->counts[2] + 1, index < 0) {
ei = 2; break; ei = 2;
break;
} else { } else {
ki = 3; ki = 3;
} }
@ -627,22 +701,25 @@ static void *delpos234_internal(tree234 *t, int index) {
sub->elems[0] = n->elems[ki - 1]; sub->elems[0] = n->elems[ki - 1];
sub->kids[0] = sib->kids[lastelem + 1]; sub->kids[0] = sib->kids[lastelem + 1];
sub->counts[0] = sib->counts[lastelem + 1]; sub->counts[0] = sib->counts[lastelem + 1];
if (sub->kids[0]) sub->kids[0]->parent = sub; if (sub->kids[0])
sub->kids[0]->parent = sub;
n->elems[ki - 1] = sib->elems[lastelem]; n->elems[ki - 1] = sib->elems[lastelem];
sib->kids[lastelem + 1] = NULL; sib->kids[lastelem + 1] = NULL;
sib->counts[lastelem + 1] = 0; sib->counts[lastelem + 1] = 0;
sib->elems[lastelem] = NULL; sib->elems[lastelem] = NULL;
n->counts[ki] = countnode234(sub); n->counts[ki] = countnode234(sub);
LOG((" case 3a left\n")); LOG((" case 3a left\n"));
LOG((" index and left subtree count before adjustment: %d, %d\n", LOG(
(" index and left subtree count before adjustment: %d, %d\n",
index, n->counts[ki - 1])); index, n->counts[ki - 1]));
index += n->counts[ki - 1]; index += n->counts[ki - 1];
n->counts[ki - 1] = countnode234(sib); n->counts[ki - 1] = countnode234(sib);
index -= n->counts[ki - 1]; index -= n->counts[ki - 1];
LOG((" index and left subtree count after adjustment: %d, %d\n", LOG(
(" index and left subtree count after adjustment: %d, %d\n",
index, n->counts[ki - 1])); index, n->counts[ki - 1]));
} else if (ki < 3 && n->kids[ki+1] && } else if (ki < 3 && n->kids[ki + 1]
n->kids[ki+1]->elems[1]) { && n->kids[ki + 1]->elems[1]) {
/* /*
* Case 3a, right-handed variant. ki has only * Case 3a, right-handed variant. ki has only
* one element but ki+1 has two or more. Move a * one element but ki+1 has two or more. Move a
@ -657,7 +734,8 @@ static void *delpos234_internal(tree234 *t, int index) {
sub->elems[1] = n->elems[ki]; sub->elems[1] = n->elems[ki];
sub->kids[2] = sib->kids[0]; sub->kids[2] = sib->kids[0];
sub->counts[2] = sib->counts[0]; sub->counts[2] = sib->counts[0];
if (sub->kids[2]) sub->kids[2]->parent = sub; if (sub->kids[2])
sub->kids[2]->parent = sub;
n->elems[ki] = sib->elems[0]; n->elems[ki] = sib->elems[0];
sib->kids[0] = sib->kids[1]; sib->kids[0] = sib->kids[1];
sib->counts[0] = sib->counts[1]; sib->counts[0] = sib->counts[1];
@ -710,11 +788,13 @@ static void *delpos234_internal(tree234 *t, int index) {
sub->elems[1] = n->elems[ki]; sub->elems[1] = n->elems[ki];
sub->kids[1] = sib->kids[1]; sub->kids[1] = sib->kids[1];
sub->counts[1] = sib->counts[1]; sub->counts[1] = sib->counts[1];
if (sub->kids[1]) sub->kids[1]->parent = sub; if (sub->kids[1])
sub->kids[1]->parent = sub;
sub->elems[0] = sib->elems[0]; sub->elems[0] = sib->elems[0];
sub->kids[0] = sib->kids[0]; sub->kids[0] = sib->kids[0];
sub->counts[0] = sib->counts[0]; sub->counts[0] = sib->counts[0];
if (sub->kids[0]) sub->kids[0]->parent = sub; if (sub->kids[0])
sub->kids[0]->parent = sub;
n->counts[ki + 1] = countnode234(sub); n->counts[ki + 1] = countnode234(sub);
@ -731,7 +811,8 @@ static void *delpos234_internal(tree234 *t, int index) {
} }
n->kids[j] = NULL; n->kids[j] = NULL;
n->counts[j] = 0; n->counts[j] = 0;
if (j < 3) n->elems[j] = NULL; if (j < 3)
n->elems[j] = NULL;
LOG((" case 3b ki=%d\n", ki)); LOG((" case 3b ki=%d\n", ki));
if (!n->elems[0]) { if (!n->elems[0]) {
@ -849,11 +930,13 @@ static void *delpos234_internal(tree234 *t, int index) {
a->elems[1] = n->elems[ei]; a->elems[1] = n->elems[ei];
a->kids[2] = b->kids[0]; a->kids[2] = b->kids[0];
a->counts[2] = b->counts[0]; a->counts[2] = b->counts[0];
if (a->kids[2]) a->kids[2]->parent = a; if (a->kids[2])
a->kids[2]->parent = a;
a->elems[2] = b->elems[0]; a->elems[2] = b->elems[0];
a->kids[3] = b->kids[1]; a->kids[3] = b->kids[1];
a->counts[3] = b->counts[1]; a->counts[3] = b->counts[1];
if (a->kids[3]) a->kids[3]->parent = a; if (a->kids[3])
a->kids[3]->parent = a;
sfree(b); sfree(b);
n->counts[ei] = countnode234(a); n->counts[ei] = countnode234(a);
/* /*
@ -888,12 +971,14 @@ static void *delpos234_internal(tree234 *t, int index) {
} }
} }
} }
void *delpos234(tree234 *t, int index) { void *delpos234(tree234 * t, int index)
{
if (index < 0 || index >= countnode234(t->root)) if (index < 0 || index >= countnode234(t->root))
return NULL; return NULL;
return delpos234_internal(t, index); return delpos234_internal(t, index);
} }
void *del234(tree234 *t, void *e) { void *del234(tree234 * t, void *e)
{
int index; int index;
if (!findrelpos234(t, e, NULL, REL234_EQ, &index)) if (!findrelpos234(t, e, NULL, REL234_EQ, &index))
return NULL; /* it wasn't in there anyway */ return NULL; /* it wasn't in there anyway */
@ -932,7 +1017,8 @@ void *del234(tree234 *t, void *e) {
/* /*
* Error reporting function. * Error reporting function.
*/ */
void error(char *fmt, ...) { void error(char *fmt, ...)
{
va_list ap; va_list ap;
printf("ERROR: "); printf("ERROR: ");
va_start(ap, fmt); va_start(ap, fmt);
@ -955,7 +1041,8 @@ typedef struct {
} chkctx; } chkctx;
int chknode(chkctx * ctx, int level, node234 * node, int chknode(chkctx * ctx, int level, node234 * node,
void *lowbound, void *highbound) { void *lowbound, void *highbound)
{
int nkids, nelems; int nkids, nelems;
int i; int i;
int count; int count;
@ -1026,7 +1113,8 @@ int chknode(chkctx *ctx, int level, node234 *node,
if (cmp) { if (cmp) {
for (i = -1; i < nelems; i++) { for (i = -1; i < nelems; i++) {
void *lower = (i == -1 ? lowbound : node->elems[i]); void *lower = (i == -1 ? lowbound : node->elems[i]);
void *higher = (i+1 == nelems ? highbound : node->elems[i+1]); void *higher =
(i + 1 == nelems ? highbound : node->elems[i + 1]);
if (lower && higher && cmp(lower, higher) >= 0) { if (lower && higher && cmp(lower, higher) >= 0) {
error("node %p: kid comparison [%d=%s,%d=%s] failed", error("node %p: kid comparison [%d=%s,%d=%s] failed",
node, i, lower, i + 1, higher); node, i, lower, i + 1, higher);
@ -1053,7 +1141,8 @@ int chknode(chkctx *ctx, int level, node234 *node,
for (i = 0; i < nkids; i++) { for (i = 0; i < nkids; i++) {
void *lower = (i == 0 ? lowbound : node->elems[i - 1]); void *lower = (i == 0 ? lowbound : node->elems[i - 1]);
void *higher = (i >= nelems ? highbound : node->elems[i]); void *higher = (i >= nelems ? highbound : node->elems[i]);
int subcount = chknode(ctx, level+1, node->kids[i], lower, higher); int subcount =
chknode(ctx, level + 1, node->kids[i], lower, higher);
if (node->counts[i] != subcount) { if (node->counts[i] != subcount) {
error("node %p kid %d: count says %d, subtree really has %d", error("node %p kid %d: count says %d, subtree really has %d",
node, i, node->counts[i], subcount); node, i, node->counts[i], subcount);
@ -1064,7 +1153,8 @@ int chknode(chkctx *ctx, int level, node234 *node,
return count; return count;
} }
void verify(void) { void verify(void)
{
chkctx ctx; chkctx ctx;
int i; int i;
void *p; void *p;
@ -1104,7 +1194,8 @@ void verify(void) {
} }
} }
void internal_addtest(void *elem, int index, void *realret) { void internal_addtest(void *elem, int index, void *realret)
{
int i, j; int i, j;
void *retval; void *retval;
@ -1129,7 +1220,8 @@ void internal_addtest(void *elem, int index, void *realret) {
verify(); verify();
} }
void addtest(void *elem) { void addtest(void *elem)
{
int i; int i;
void *realret; void *realret;
@ -1147,7 +1239,8 @@ void addtest(void *elem) {
internal_addtest(elem, i, realret); internal_addtest(elem, i, realret);
} }
void addpostest(void *elem, int i) { void addpostest(void *elem, int i)
{
void *realret; void *realret;
realret = addpos234(tree, elem, i); realret = addpos234(tree, elem, i);
@ -1155,7 +1248,8 @@ void addpostest(void *elem, int i) {
internal_addtest(elem, i, realret); internal_addtest(elem, i, realret);
} }
void delpostest(int i) { void delpostest(int i)
{
int index = i; int index = i;
void *elem = array[i], *ret; void *elem = array[i], *ret;
@ -1178,7 +1272,8 @@ void delpostest(int i) {
verify(); verify();
} }
void deltest(void *elem) { void deltest(void *elem)
{
int i; int i;
i = 0; i = 0;
@ -1197,13 +1292,15 @@ void deltest(void *elem) {
* given in ANSI C99 draft N869. It assumes `unsigned' is 32 bits; * given in ANSI C99 draft N869. It assumes `unsigned' is 32 bits;
* change it if not. * change it if not.
*/ */
int randomnumber(unsigned *seed) { int randomnumber(unsigned *seed)
{
*seed *= 1103515245; *seed *= 1103515245;
*seed += 12345; *seed += 12345;
return ((*seed) / 65536) % 32768; return ((*seed) / 65536) % 32768;
} }
int mycmp(void *av, void *bv) { int mycmp(void *av, void *bv)
{
char const *a = (char const *) av; char const *a = (char const *) av;
char const *b = (char const *) bv; char const *b = (char const *) bv;
return strcmp(a, b); return strcmp(a, b);
@ -1227,7 +1324,8 @@ char *strings[] = {
#define NSTR lenof(strings) #define NSTR lenof(strings)
int findtest(void) { int findtest(void)
{
const static int rels[] = { const static int rels[] = {
REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT
}; };
@ -1243,7 +1341,8 @@ int findtest(void) {
for (j = 0; j < sizeof(rels) / sizeof(*rels); j++) { for (j = 0; j < sizeof(rels) / sizeof(*rels); j++) {
rel = rels[j]; rel = rels[j];
lo = 0; hi = arraylen-1; lo = 0;
hi = arraylen - 1;
while (lo <= hi) { while (lo <= hi) {
mid = (lo + hi) / 2; mid = (lo + hi) / 2;
c = strcmp(p, array[mid]); c = strcmp(p, array[mid]);
@ -1302,26 +1401,27 @@ int findtest(void) {
error("find(NULL,GT) gave %s(%d) should be %s(0)", error("find(NULL,GT) gave %s(%d) should be %s(0)",
realret, index, array[0]); realret, index, array[0]);
} else if (!arraylen && (realret != NULL)) { } else if (!arraylen && (realret != NULL)) {
error("find(NULL,GT) gave %s(%d) should be NULL", error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);
realret, index);
} }
realret = findrelpos234(tree, NULL, NULL, REL234_LT, &index); realret = findrelpos234(tree, NULL, NULL, REL234_LT, &index);
if (arraylen && (realret != array[arraylen-1] || index != arraylen-1)) { if (arraylen
error("find(NULL,LT) gave %s(%d) should be %s(0)", && (realret != array[arraylen - 1] || index != arraylen - 1)) {
realret, index, array[arraylen-1]); error("find(NULL,LT) gave %s(%d) should be %s(0)", realret, index,
array[arraylen - 1]);
} else if (!arraylen && (realret != NULL)) { } else if (!arraylen && (realret != NULL)) {
error("find(NULL,LT) gave %s(%d) should be NULL", error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);
realret, index);
} }
} }
int main(void) { int main(void)
{
int in[NSTR]; int in[NSTR];
int i, j, k; int i, j, k;
unsigned seed = 0; unsigned seed = 0;
for (i = 0; i < NSTR; i++) in[i] = 0; for (i = 0; i < NSTR; i++)
in[i] = 0;
array = NULL; array = NULL;
arraylen = arraysize = 0; arraylen = arraysize = 0;
tree = newtree234(mycmp); tree = newtree234(mycmp);

View File

@ -22,7 +22,8 @@
#define PROGBARHEIGHT 14 #define PROGBARHEIGHT 14
void ctlposinit(struct ctlpos *cp, HWND hwnd, void ctlposinit(struct ctlpos *cp, HWND hwnd,
int leftborder, int rightborder, int topborder) { int leftborder, int rightborder, int topborder)
{
RECT r, r2; RECT r, r2;
cp->hwnd = hwnd; cp->hwnd = hwnd;
cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0); cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
@ -39,8 +40,8 @@ void ctlposinit(struct ctlpos *cp, HWND hwnd,
} }
void doctl(struct ctlpos *cp, RECT r, void doctl(struct ctlpos *cp, RECT r,
char *wclass, int wstyle, int exstyle, char *wclass, int wstyle, int exstyle, char *wtext, int wid)
char *wtext, int wid) { {
HWND ctl; HWND ctl;
/* /*
* Note nonstandard use of RECT. This is deliberate: by * Note nonstandard use of RECT. This is deliberate: by
@ -60,11 +61,14 @@ void doctl(struct ctlpos *cp, RECT r,
/* /*
* A title bar across the top of a sub-dialog. * A title bar across the top of a sub-dialog.
*/ */
void bartitle(struct ctlpos *cp, char *name, int id) { void bartitle(struct ctlpos *cp, char *name, int id)
{
RECT r; RECT r;
r.left = GAPBETWEEN; r.right = cp->width; r.left = GAPBETWEEN;
r.top = cp->ypos; r.bottom = STATICHEIGHT; r.right = cp->width;
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
} }
@ -72,7 +76,8 @@ void bartitle(struct ctlpos *cp, char *name, int id) {
/* /*
* Begin a grouping box, with or without a group title. * Begin a grouping box, with or without a group title.
*/ */
void beginbox(struct ctlpos *cp, char *name, int idbox) { void beginbox(struct ctlpos *cp, char *name, int idbox)
{
cp->boxystart = cp->ypos; cp->boxystart = cp->ypos;
if (!name) if (!name)
cp->boxystart -= STATICHEIGHT / 2; cp->boxystart -= STATICHEIGHT / 2;
@ -88,13 +93,16 @@ void beginbox(struct ctlpos *cp, char *name, int idbox) {
/* /*
* End a grouping box. * End a grouping box.
*/ */
void endbox(struct ctlpos *cp) { void endbox(struct ctlpos *cp)
{
RECT r; RECT r;
cp->xoff -= GAPXBOX; cp->xoff -= GAPXBOX;
cp->width += 2 * GAPXBOX; cp->width += 2 * GAPXBOX;
cp->ypos += GAPYBOX - GAPBETWEEN; cp->ypos += GAPYBOX - GAPBETWEEN;
r.left = GAPBETWEEN; r.right = cp->width; r.left = GAPBETWEEN;
r.top = cp->boxystart; r.bottom = cp->ypos - cp->boxystart; r.right = cp->width;
r.top = cp->boxystart;
r.bottom = cp->ypos - cp->boxystart;
doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0, doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
cp->boxtext ? cp->boxtext : "", cp->boxid); cp->boxtext ? cp->boxtext : "", cp->boxid);
cp->ypos += GAPYBOX; cp->ypos += GAPYBOX;
@ -104,7 +112,8 @@ void endbox(struct ctlpos *cp) {
* Some edit boxes. Each one has a static above it. The percentages * Some edit boxes. Each one has a static above it. The percentages
* of the horizontal space are provided. * of the horizontal space are provided.
*/ */
void multiedit(struct ctlpos *cp, ...) { void multiedit(struct ctlpos *cp, ...)
{
RECT r; RECT r;
va_list ap; va_list ap;
int percent, xpos; int percent, xpos;
@ -126,14 +135,14 @@ void multiedit(struct ctlpos *cp, ...) {
xpos = (cp->width + GAPBETWEEN) * percent / 100; xpos = (cp->width + GAPBETWEEN) * percent / 100;
r.right = xpos - r.left; r.right = xpos - r.left;
r.top = cp->ypos; r.bottom = STATICHEIGHT; r.top = cp->ypos;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, r.bottom = STATICHEIGHT;
text, staticid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
r.top = cp->ypos + 8 + GAPWITHIN; r.bottom = EDITHEIGHT; r.top = cp->ypos + 8 + GAPWITHIN;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", editid);
"", editid);
} }
va_end(ap); va_end(ap);
cp->ypos += 8 + GAPWITHIN + 12 + GAPBETWEEN; cp->ypos += 8 + GAPWITHIN + 12 + GAPBETWEEN;
@ -153,16 +162,18 @@ void multiedit(struct ctlpos *cp, ...) {
* *
* (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
*/ */
void radioline(struct ctlpos *cp, void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
char *text, int id, int nacross, ...) { {
RECT r; RECT r;
va_list ap; va_list ap;
int group; int group;
int i; int i;
char *btext; char *btext;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
va_start(ap, nacross); va_start(ap, nacross);
@ -182,14 +193,15 @@ void radioline(struct ctlpos *cp,
nextbtext = va_arg(ap, char *); nextbtext = va_arg(ap, char *);
r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross; r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
if (nextbtext) if (nextbtext)
r.right = (i+1) * (cp->width+GAPBETWEEN)/nacross - r.left; r.right =
(i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
else else
r.right = cp->width - r.left; r.right = cp->width - r.left;
r.top = cp->ypos; r.bottom = RADIOHEIGHT; r.top = cp->ypos;
r.bottom = RADIOHEIGHT;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP | group, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
0, group, 0, btext, bid);
btext, bid);
group = 0; group = 0;
i++; i++;
btext = nextbtext; btext = nextbtext;
@ -202,13 +214,16 @@ void radioline(struct ctlpos *cp,
* A set of radio buttons on multiple lines, with a static above * A set of radio buttons on multiple lines, with a static above
* them. * them.
*/ */
void radiobig(struct ctlpos *cp, char *text, int id, ...) { void radiobig(struct ctlpos *cp, char *text, int id, ...)
{
RECT r; RECT r;
va_list ap; va_list ap;
int group; int group;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
va_start(ap, id); va_start(ap, id);
@ -220,13 +235,14 @@ void radiobig(struct ctlpos *cp, char *text, int id, ...) {
if (!btext) if (!btext)
break; break;
bid = va_arg(ap, int); bid = va_arg(ap, int);
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP | group, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
0, group, 0, btext, bid);
btext, bid);
group = 0; group = 0;
} }
va_end(ap); va_end(ap);
@ -236,11 +252,14 @@ void radiobig(struct ctlpos *cp, char *text, int id, ...) {
/* /*
* A single standalone checkbox. * A single standalone checkbox.
*/ */
void checkbox(struct ctlpos *cp, char *text, int id) { void checkbox(struct ctlpos *cp, char *text, int id)
{
RECT r; RECT r;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = CHECKBOXHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = CHECKBOXHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
@ -250,11 +269,14 @@ void checkbox(struct ctlpos *cp, char *text, int id) {
/* /*
* A single standalone static text control. * A single standalone static text control.
*/ */
void statictext(struct ctlpos *cp, char *text, int id) { void statictext(struct ctlpos *cp, char *text, int id)
{
RECT r; RECT r;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
} }
@ -263,7 +285,8 @@ void statictext(struct ctlpos *cp, char *text, int id) {
* A button on the right hand side, with a static to its left. * A button on the right hand side, with a static to its left.
*/ */
void staticbtn(struct ctlpos *cp, char *stext, int sid, void staticbtn(struct ctlpos *cp, char *stext, int sid,
char *btext, int bid) { char *btext, int bid)
{
const int height = (PUSHBTNHEIGHT > STATICHEIGHT ? const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
PUSHBTNHEIGHT : STATICHEIGHT); PUSHBTNHEIGHT : STATICHEIGHT);
RECT r; RECT r;
@ -273,16 +296,19 @@ void staticbtn(struct ctlpos *cp, char *stext, int sid,
lwid = rpos - 2 * GAPBETWEEN; lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos; rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN; r.top = cp->ypos + (height-STATICHEIGHT)/2; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = STATICHEIGHT; r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos; r.top = cp->ypos + (height-PUSHBTNHEIGHT)/2; r.left = rpos;
r.right = rwid; r.bottom = PUSHBTNHEIGHT; r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, 0, btext, bid);
btext, bid);
cp->ypos += height + GAPBETWEEN; cp->ypos += height + GAPBETWEEN;
} }
@ -292,37 +318,44 @@ void staticbtn(struct ctlpos *cp, char *stext, int sid,
*/ */
static void staticedit_internal(struct ctlpos *cp, char *stext, static void staticedit_internal(struct ctlpos *cp, char *stext,
int sid, int eid, int percentedit, int sid, int eid, int percentedit,
int style) { int style)
{
const int height = (EDITHEIGHT > STATICHEIGHT ? const int height = (EDITHEIGHT > STATICHEIGHT ?
EDITHEIGHT : STATICHEIGHT); EDITHEIGHT : STATICHEIGHT);
RECT r; RECT r;
int lwid, rwid, rpos; int lwid, rwid, rpos;
rpos = GAPBETWEEN + (100-percentedit) * (cp->width + GAPBETWEEN) / 100; rpos =
GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN; lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos; rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN; r.top = cp->ypos + (height-STATICHEIGHT)/2; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = STATICHEIGHT; r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos; r.top = cp->ypos + (height-EDITHEIGHT)/2; r.left = rpos;
r.right = rwid; r.bottom = EDITHEIGHT; r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", eid);
"", eid);
cp->ypos += height + GAPBETWEEN; cp->ypos += height + GAPBETWEEN;
} }
void staticedit(struct ctlpos *cp, char *stext, void staticedit(struct ctlpos *cp, char *stext,
int sid, int eid, int percentedit) { int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, 0); staticedit_internal(cp, stext, sid, eid, percentedit, 0);
} }
void staticpassedit(struct ctlpos *cp, char *stext, void staticpassedit(struct ctlpos *cp, char *stext,
int sid, int eid, int percentedit) { int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD); staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
} }
@ -330,28 +363,32 @@ void staticpassedit(struct ctlpos *cp, char *stext,
* A big multiline edit control with a static labelling it. * A big multiline edit control with a static labelling it.
*/ */
void bigeditctrl(struct ctlpos *cp, char *stext, void bigeditctrl(struct ctlpos *cp, char *stext,
int sid, int eid, int lines) { int sid, int eid, int lines)
{
RECT r; RECT r;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = EDITHEIGHT + (lines-1) * STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", eid);
"", eid);
} }
/* /*
* A tab-control substitute when a real tab control is unavailable. * A tab-control substitute when a real tab control is unavailable.
*/ */
void ersatztab(struct ctlpos *cp, char *stext, int sid, void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
int lid, int s2id) { {
const int height = (COMBOHEIGHT > STATICHEIGHT ? const int height = (COMBOHEIGHT > STATICHEIGHT ?
COMBOHEIGHT : STATICHEIGHT); COMBOHEIGHT : STATICHEIGHT);
RECT r; RECT r;
@ -365,22 +402,26 @@ void ersatztab(struct ctlpos *cp, char *stext, int sid,
lwid = rpos - 2 * BIGGAP; lwid = rpos - 2 * BIGGAP;
rwid = bigwid + BIGGAP - rpos; rwid = bigwid + BIGGAP - rpos;
r.left = BIGGAP; r.top = cp->ypos + (height-STATICHEIGHT)/2; r.left = BIGGAP;
r.right = lwid; r.bottom = STATICHEIGHT; r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos; r.top = cp->ypos + (height-COMBOHEIGHT)/2; r.left = rpos;
r.right = rwid; r.bottom = COMBOHEIGHT*10; r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT * 10;
doctl(cp, r, "COMBOBOX", doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
CBS_DROPDOWNLIST | CBS_HASSTRINGS, CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
WS_EX_CLIENTEDGE,
"", lid);
cp->ypos += height + MEDGAP + GAPBETWEEN; cp->ypos += height + MEDGAP + GAPBETWEEN;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = 2; r.top = cp->ypos;
r.right = cp->width;
r.bottom = 2;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ, doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
0, "", s2id); 0, "", s2id);
} }
@ -390,14 +431,17 @@ void ersatztab(struct ctlpos *cp, char *stext, int sid,
* and a button on the right. * and a button on the right.
*/ */
void editbutton(struct ctlpos *cp, char *stext, int sid, void editbutton(struct ctlpos *cp, char *stext, int sid,
int eid, char *btext, int bid) { int eid, char *btext, int bid)
{
const int height = (EDITHEIGHT > PUSHBTNHEIGHT ? const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
EDITHEIGHT : PUSHBTNHEIGHT); EDITHEIGHT : PUSHBTNHEIGHT);
RECT r; RECT r;
int lwid, rwid, rpos; int lwid, rwid, rpos;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
@ -405,19 +449,21 @@ void editbutton(struct ctlpos *cp, char *stext, int sid,
lwid = rpos - 2 * GAPBETWEEN; lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos; rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN; r.top = cp->ypos + (height-EDITHEIGHT)/2; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = EDITHEIGHT; r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = lwid;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", eid);
"", eid);
r.left = rpos; r.top = cp->ypos + (height-PUSHBTNHEIGHT)/2; r.left = rpos;
r.right = rwid; r.bottom = PUSHBTNHEIGHT; r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, 0, btext, bid);
btext, bid);
cp->ypos += height + GAPBETWEEN; cp->ypos += height + GAPBETWEEN;
} }
@ -429,7 +475,8 @@ void editbutton(struct ctlpos *cp, char *stext, int sid,
* buttons. * buttons.
*/ */
void sesssaver(struct ctlpos *cp, char *text, void sesssaver(struct ctlpos *cp, char *text,
int staticid, int editid, int listid, ...) { int staticid, int editid, int listid, ...)
{
RECT r; RECT r;
va_list ap; va_list ap;
int lwid, rwid, rpos; int lwid, rwid, rpos;
@ -441,19 +488,22 @@ void sesssaver(struct ctlpos *cp, char *text,
rwid = cp->width + GAPBETWEEN - rpos; rwid = cp->width + GAPBETWEEN - rpos;
/* The static control. */ /* The static control. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = lwid;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
/* The edit control. */ /* The edit control. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = EDITHEIGHT; r.top = cp->ypos;
r.right = lwid;
r.bottom = EDITHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", editid);
"", editid);
/* /*
* The buttons (we should hold off on the list box until we * The buttons (we should hold off on the list box until we
@ -464,29 +514,32 @@ void sesssaver(struct ctlpos *cp, char *text,
while (1) { while (1) {
char *btext = va_arg(ap, char *); char *btext = va_arg(ap, char *);
int bid; int bid;
if (!btext) break; if (!btext)
break;
bid = va_arg(ap, int); bid = va_arg(ap, int);
r.left = rpos; r.top = y; r.left = rpos;
r.right = rwid; r.bottom = PUSHBTNHEIGHT; r.top = y;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
y += r.bottom + GAPWITHIN; y += r.bottom + GAPWITHIN;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, 0, btext, bid);
btext, bid);
} }
/* Compute list box height. LISTDEFHEIGHT, or height of buttons. */ /* Compute list box height. LISTDEFHEIGHT, or height of buttons. */
y -= cp->ypos; y -= cp->ypos;
y -= GAPWITHIN; y -= GAPWITHIN;
if (y < LISTDEFHEIGHT) y = LISTDEFHEIGHT; if (y < LISTDEFHEIGHT)
r.left = GAPBETWEEN; r.top = cp->ypos; y = LISTDEFHEIGHT;
r.right = lwid; r.bottom = y; r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = lwid;
r.bottom = y;
cp->ypos += y + GAPBETWEEN; cp->ypos += y + GAPBETWEEN;
doctl(cp, r, "LISTBOX", doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
LBS_NOTIFY | LBS_HASSTRINGS, LBS_NOTIFY | LBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
WS_EX_CLIENTEDGE,
"", listid);
} }
/* /*
@ -497,20 +550,22 @@ void sesssaver(struct ctlpos *cp, char *text,
void envsetter(struct ctlpos *cp, char *stext, int sid, void envsetter(struct ctlpos *cp, char *stext, int sid,
char *e1stext, int e1sid, int e1id, char *e1stext, int e1sid, int e1id,
char *e2stext, int e2sid, int e2id, char *e2stext, int e2sid, int e2id,
int listid, int listid, char *b1text, int b1id, char *b2text, int b2id)
char *b1text, int b1id, char *b2text, int b2id) { {
RECT r; RECT r;
const int height = (STATICHEIGHT > EDITHEIGHT && STATICHEIGHT > PUSHBTNHEIGHT ? const int height = (STATICHEIGHT > EDITHEIGHT
STATICHEIGHT : && STATICHEIGHT >
EDITHEIGHT > PUSHBTNHEIGHT ? PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
EDITHEIGHT : PUSHBTNHEIGHT); PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
const static int percents[] = { 20, 35, 10, 25 }; const static int percents[] = { 20, 35, 10, 25 };
int i, j, xpos, percent; int i, j, xpos, percent;
const int LISTHEIGHT = 42; const int LISTHEIGHT = 42;
/* The static control. */ /* The static control. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
@ -525,8 +580,7 @@ void envsetter(struct ctlpos *cp, char *stext, int sid,
r.right = xpos - r.left; r.right = xpos - r.left;
r.top = cp->ypos; r.top = cp->ypos;
r.bottom = (i == 0 ? STATICHEIGHT : r.bottom = (i == 0 ? STATICHEIGHT :
i==1 ? EDITHEIGHT : i == 1 ? EDITHEIGHT : PUSHBTNHEIGHT);
PUSHBTNHEIGHT);
r.top += (height - r.bottom) / 2; r.top += (height - r.bottom) / 2;
if (i == 0) { if (i == 0) {
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
@ -534,27 +588,25 @@ void envsetter(struct ctlpos *cp, char *stext, int sid,
} else if (i == 1) { } else if (i == 1) {
doctl(cp, r, "EDIT", doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE, "", j == 0 ? e1id : e2id);
"", j==0 ? e1id : e2id);
} else if (i == 3) { } else if (i == 3) {
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, 0, j == 0 ? b1text : b2text, j == 0 ? b1id : b2id);
j==0 ? b1text : b2text, j==0 ? b1id : b2id);
} }
} }
cp->ypos += height + GAPWITHIN; cp->ypos += height + GAPWITHIN;
} }
/* The list box. */ /* The list box. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = LISTHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = LISTHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "LISTBOX", doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
LBS_USETABSTOPS, | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
WS_EX_CLIENTEDGE,
"", listid);
} }
/* /*
@ -563,31 +615,34 @@ void envsetter(struct ctlpos *cp, char *stext, int sid,
* button-and-static-and-edit. * button-and-static-and-edit.
*/ */
void charclass(struct ctlpos *cp, char *stext, int sid, int listid, void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
char *btext, int bid, int eid, char *s2text, int s2id) { char *btext, int bid, int eid, char *s2text, int s2id)
{
RECT r; RECT r;
const int height = (STATICHEIGHT > EDITHEIGHT && STATICHEIGHT > PUSHBTNHEIGHT ? const int height = (STATICHEIGHT > EDITHEIGHT
STATICHEIGHT : && STATICHEIGHT >
EDITHEIGHT > PUSHBTNHEIGHT ? PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
EDITHEIGHT : PUSHBTNHEIGHT); PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
const static int percents[] = { 30, 40, 30 }; const static int percents[] = { 30, 40, 30 };
int i, xpos, percent; int i, xpos, percent;
const int LISTHEIGHT = 66; const int LISTHEIGHT = 66;
/* The static control. */ /* The static control. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
/* The list box. */ /* The list box. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = LISTHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = LISTHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "LISTBOX", doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
LBS_USETABSTOPS, | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
WS_EX_CLIENTEDGE,
"", listid);
/* The button+static+edit. */ /* The button+static+edit. */
percent = xpos = 0; percent = xpos = 0;
@ -598,8 +653,7 @@ void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
r.right = xpos - r.left; r.right = xpos - r.left;
r.top = cp->ypos; r.top = cp->ypos;
r.bottom = (i == 0 ? PUSHBTNHEIGHT : r.bottom = (i == 0 ? PUSHBTNHEIGHT :
i==1 ? STATICHEIGHT : i == 1 ? STATICHEIGHT : EDITHEIGHT);
EDITHEIGHT);
r.top += (height - r.bottom) / 2; r.top += (height - r.bottom) / 2;
if (i == 0) { if (i == 0) {
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
@ -623,7 +677,8 @@ void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
* two-part statics followed by a button. * two-part statics followed by a button.
*/ */
void colouredit(struct ctlpos *cp, char *stext, int sid, int listid, void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
char *btext, int bid, ...) { char *btext, int bid, ...)
{
RECT r; RECT r;
int y; int y;
va_list ap; va_list ap;
@ -631,8 +686,10 @@ void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
const int LISTHEIGHT = 66; const int LISTHEIGHT = 66;
/* The static control. */ /* The static control. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = STATICHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN; cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
@ -641,13 +698,13 @@ void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
rwid = cp->width + GAPBETWEEN - rpos; rwid = cp->width + GAPBETWEEN - rpos;
/* The list box. */ /* The list box. */
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = lwid; r.bottom = LISTHEIGHT; r.top = cp->ypos;
r.right = lwid;
r.bottom = LISTHEIGHT;
doctl(cp, r, "LISTBOX", doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
LBS_USETABSTOPS | LBS_NOTIFY, | LBS_USETABSTOPS | LBS_NOTIFY, WS_EX_CLIENTEDGE, "", listid);
WS_EX_CLIENTEDGE,
"", listid);
/* The statics. */ /* The statics. */
y = cp->ypos; y = cp->ypos;
@ -656,21 +713,28 @@ void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
char *ltext; char *ltext;
int lid, rid; int lid, rid;
ltext = va_arg(ap, char *); ltext = va_arg(ap, char *);
if (!ltext) break; if (!ltext)
break;
lid = va_arg(ap, int); lid = va_arg(ap, int);
rid = va_arg(ap, int); rid = va_arg(ap, int);
r.top = y; r.bottom = STATICHEIGHT; r.top = y;
r.bottom = STATICHEIGHT;
y += r.bottom + GAPWITHIN; y += r.bottom + GAPWITHIN;
r.left = rpos; r.right = rwid/2; r.left = rpos;
r.right = rwid / 2;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, ltext, lid); doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, ltext, lid);
r.left = rpos + r.right; r.right = rwid - r.right; r.left = rpos + r.right;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_RIGHT, 0, "", rid); r.right = rwid - r.right;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_RIGHT, 0, "",
rid);
} }
va_end(ap); va_end(ap);
/* The button. */ /* The button. */
r.top = y + 2*GAPWITHIN; r.bottom = PUSHBTNHEIGHT; r.top = y + 2 * GAPWITHIN;
r.left = rpos; r.right = rwid; r.bottom = PUSHBTNHEIGHT;
r.left = rpos;
r.right = rwid;
doctl(cp, r, "BUTTON", doctl(cp, r, "BUTTON",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
0, btext, bid); 0, btext, bid);
@ -683,15 +747,17 @@ void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
* to be smooth and unbroken, without those ugly divisions; some * to be smooth and unbroken, without those ugly divisions; some
* older compilers may not support that, but that's life. * older compilers may not support that, but that's life.
*/ */
void progressbar(struct ctlpos *cp, int id) { void progressbar(struct ctlpos *cp, int id)
{
RECT r; RECT r;
r.left = GAPBETWEEN; r.top = cp->ypos; r.left = GAPBETWEEN;
r.right = cp->width; r.bottom = PROGBARHEIGHT; r.top = cp->ypos;
r.right = cp->width;
r.bottom = PROGBARHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN; cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, PROGRESS_CLASS, doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
WS_CHILD | WS_VISIBLE
#ifdef PBS_SMOOTH #ifdef PBS_SMOOTH
| PBS_SMOOTH | PBS_SMOOTH
#endif #endif

615
windlg.c

File diff suppressed because it is too large Load Diff

986
window.c

File diff suppressed because it is too large Load Diff

306
winnet.c
View File

@ -102,64 +102,109 @@ struct buffer {
static tree234 *sktree; static tree234 *sktree;
static int cmpfortree(void *av, void *bv) { static int cmpfortree(void *av, void *bv)
{
Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s; unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
if (as < bs) return -1; if (as < bs)
if (as > bs) return +1; return -1;
if (as > bs)
return +1;
return 0; return 0;
} }
static int cmpforsearch(void *av, void *bv) { static int cmpforsearch(void *av, void *bv)
{
Actual_Socket b = (Actual_Socket) bv; Actual_Socket b = (Actual_Socket) bv;
unsigned long as = (unsigned long) av, bs = (unsigned long) b->s; unsigned long as = (unsigned long) av, bs = (unsigned long) b->s;
if (as < bs) return -1; if (as < bs)
if (as > bs) return +1; return -1;
if (as > bs)
return +1;
return 0; return 0;
} }
void sk_init(void) { void sk_init(void)
{
sktree = newtree234(cmpfortree); sktree = newtree234(cmpfortree);
} }
char *winsock_error_string(int error) { char *winsock_error_string(int error)
{
switch (error) { switch (error) {
case WSAEACCES: return "Network error: Permission denied"; case WSAEACCES:
case WSAEADDRINUSE: return "Network error: Address already in use"; return "Network error: Permission denied";
case WSAEADDRNOTAVAIL: return "Network error: Cannot assign requested address"; case WSAEADDRINUSE:
case WSAEAFNOSUPPORT: return "Network error: Address family not supported by protocol family"; return "Network error: Address already in use";
case WSAEALREADY: return "Network error: Operation already in progress"; case WSAEADDRNOTAVAIL:
case WSAECONNABORTED: return "Network error: Software caused connection abort"; return "Network error: Cannot assign requested address";
case WSAECONNREFUSED: return "Network error: Connection refused"; case WSAEAFNOSUPPORT:
case WSAECONNRESET: return "Network error: Connection reset by peer"; return
case WSAEDESTADDRREQ: return "Network error: Destination address required"; "Network error: Address family not supported by protocol family";
case WSAEFAULT: return "Network error: Bad address"; case WSAEALREADY:
case WSAEHOSTDOWN: return "Network error: Host is down"; return "Network error: Operation already in progress";
case WSAEHOSTUNREACH: return "Network error: No route to host"; case WSAECONNABORTED:
case WSAEINPROGRESS: return "Network error: Operation now in progress"; return "Network error: Software caused connection abort";
case WSAEINTR: return "Network error: Interrupted function call"; case WSAECONNREFUSED:
case WSAEINVAL: return "Network error: Invalid argument"; return "Network error: Connection refused";
case WSAEISCONN: return "Network error: Socket is already connected"; case WSAECONNRESET:
case WSAEMFILE: return "Network error: Too many open files"; return "Network error: Connection reset by peer";
case WSAEMSGSIZE: return "Network error: Message too long"; case WSAEDESTADDRREQ:
case WSAENETDOWN: return "Network error: Network is down"; return "Network error: Destination address required";
case WSAENETRESET: return "Network error: Network dropped connection on reset"; case WSAEFAULT:
case WSAENETUNREACH: return "Network error: Network is unreachable"; return "Network error: Bad address";
case WSAENOBUFS: return "Network error: No buffer space available"; case WSAEHOSTDOWN:
case WSAENOPROTOOPT: return "Network error: Bad protocol option"; return "Network error: Host is down";
case WSAENOTCONN: return "Network error: Socket is not connected"; case WSAEHOSTUNREACH:
case WSAENOTSOCK: return "Network error: Socket operation on non-socket"; return "Network error: No route to host";
case WSAEOPNOTSUPP: return "Network error: Operation not supported"; case WSAEINPROGRESS:
case WSAEPFNOSUPPORT: return "Network error: Protocol family not supported"; return "Network error: Operation now in progress";
case WSAEPROCLIM: return "Network error: Too many processes"; case WSAEINTR:
case WSAEPROTONOSUPPORT: return "Network error: Protocol not supported"; return "Network error: Interrupted function call";
case WSAEPROTOTYPE: return "Network error: Protocol wrong type for socket"; case WSAEINVAL:
case WSAESHUTDOWN: return "Network error: Cannot send after socket shutdown"; return "Network error: Invalid argument";
case WSAESOCKTNOSUPPORT: return "Network error: Socket type not supported"; case WSAEISCONN:
case WSAETIMEDOUT: return "Network error: Connection timed out"; return "Network error: Socket is already connected";
case WSAEWOULDBLOCK: return "Network error: Resource temporarily unavailable"; case WSAEMFILE:
case WSAEDISCON: return "Network error: Graceful shutdown in progress"; return "Network error: Too many open files";
default: return "Unknown network error"; case WSAEMSGSIZE:
return "Network error: Message too long";
case WSAENETDOWN:
return "Network error: Network is down";
case WSAENETRESET:
return "Network error: Network dropped connection on reset";
case WSAENETUNREACH:
return "Network error: Network is unreachable";
case WSAENOBUFS:
return "Network error: No buffer space available";
case WSAENOPROTOOPT:
return "Network error: Bad protocol option";
case WSAENOTCONN:
return "Network error: Socket is not connected";
case WSAENOTSOCK:
return "Network error: Socket operation on non-socket";
case WSAEOPNOTSUPP:
return "Network error: Operation not supported";
case WSAEPFNOSUPPORT:
return "Network error: Protocol family not supported";
case WSAEPROCLIM:
return "Network error: Too many processes";
case WSAEPROTONOSUPPORT:
return "Network error: Protocol not supported";
case WSAEPROTOTYPE:
return "Network error: Protocol wrong type for socket";
case WSAESHUTDOWN:
return "Network error: Cannot send after socket shutdown";
case WSAESOCKTNOSUPPORT:
return "Network error: Socket type not supported";
case WSAETIMEDOUT:
return "Network error: Connection timed out";
case WSAEWOULDBLOCK:
return "Network error: Resource temporarily unavailable";
case WSAEDISCON:
return "Network error: Graceful shutdown in progress";
default:
return "Unknown network error";
} }
} }
@ -174,8 +219,7 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
ret->family = 0; /* We set this one when we have resolved the host. */ ret->family = 0; /* We set this one when we have resolved the host. */
*canonicalname = ret->realhost; /* This makes sure we always have a hostname to return. */ *canonicalname = ret->realhost; /* This makes sure we always have a hostname to return. */
if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
{
#ifdef IPV6 #ifdef IPV6
/* Try to get the getaddrinfo() function from wship6.dll */ /* Try to get the getaddrinfo() function from wship6.dll */
@ -183,7 +227,8 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
* it will fallback to IPv4. */ * it will fallback to IPv4. */
typedef int (CALLBACK * FGETADDRINFO) (const char *nodename, typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
const char *servname, const char *servname,
const struct addrinfo *hints, const struct addrinfo *
hints,
struct addrinfo ** res); struct addrinfo ** res);
FGETADDRINFO fGetAddrInfo = NULL; FGETADDRINFO fGetAddrInfo = NULL;
@ -196,32 +241,32 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
* Use fGetAddrInfo when it's available (which usually also * Use fGetAddrInfo when it's available (which usually also
* means IPv6 is installed...) * means IPv6 is installed...)
*/ */
if (fGetAddrInfo) if (fGetAddrInfo) {
{
/*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */ /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0) if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0)
ret->family = ret->ai->ai_family; ret->family = ret->ai->ai_family;
} } else
else
#endif #endif
{
/* /*
* Otherwise use the IPv4-only gethostbyname... * Otherwise use the IPv4-only gethostbyname...
* (NOTE: we don't use gethostbyname as a * (NOTE: we don't use gethostbyname as a
* fallback!) * fallback!)
*/ */
if (ret->family == 0) if (ret->family == 0) {
{
/*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
if (h = gethostbyname(host)) ret->family = AF_INET; if (h = gethostbyname(host))
ret->family = AF_INET;
}
} }
/*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */ /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
if (ret->family == 0) if (ret->family == 0) {
{
DWORD err = WSAGetLastError(); DWORD err = WSAGetLastError();
ret->error = (err == WSAENETDOWN ? "Network is down" : ret->error = (err == WSAENETDOWN ? "Network is down" :
err == WSAHOST_NOT_FOUND ? "Host does not exist" : err ==
err == WSATRY_AGAIN ? "Host not found" : WSAHOST_NOT_FOUND ? "Host does not exist" : err
== WSATRY_AGAIN ? "Host not found" :
#ifdef IPV6 #ifdef IPV6
fGetAddrInfo ? "getaddrinfo: unknown error" : fGetAddrInfo ? "getaddrinfo: unknown error" :
#endif #endif
@ -229,21 +274,22 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
#ifdef DEBUG #ifdef DEBUG
{ {
LPVOID lpMsgBuf; LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) & lpMsgBuf, 0, NULL);
/*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */ /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
/* Free the buffer. */ /* Free the buffer. */
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
} }
#endif #endif
} } else {
else
{
ret->error = NULL; ret->error = NULL;
#ifdef IPV6 #ifdef IPV6
/* If we got an address info use that... */ /* If we got an address info use that... */
if (ret->ai) if (ret->ai) {
{
typedef int (CALLBACK * FGETNAMEINFO) typedef int (CALLBACK * FGETNAMEINFO)
(const struct sockaddr FAR * sa, socklen_t salen, (const struct sockaddr FAR * sa, socklen_t salen,
char FAR * host, size_t hostlen, char FAR * serv, char FAR * host, size_t hostlen, char FAR * serv,
@ -253,18 +299,21 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
/* Are we in IPv4 fallback mode? */ /* Are we in IPv4 fallback mode? */
/* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
if (ret->family == AF_INET) if (ret->family == AF_INET)
memcpy(&a, (char *)&((SOCKADDR_IN *)ret->ai->ai_addr)->sin_addr, sizeof(a)); memcpy(&a,
(char *) &((SOCKADDR_IN *) ret->ai->
ai_addr)->sin_addr, sizeof(a));
/* Now let's find that canonicalname... */ /* Now let's find that canonicalname... */
if ((dllWSHIP6) && (fGetNameInfo = (FGETNAMEINFO)GetProcAddress(dllWSHIP6, "getnameinfo"))) if ((dllWSHIP6)
{ && (fGetNameInfo =
if (fGetNameInfo((struct sockaddr *)ret->ai->ai_addr, (FGETNAMEINFO) GetProcAddress(dllWSHIP6,
ret->family == AF_INET ? "getnameinfo"))) {
sizeof(SOCKADDR_IN) : if (fGetNameInfo
((struct sockaddr *) ret->ai->ai_addr,
ret->family ==
AF_INET ? sizeof(SOCKADDR_IN) :
sizeof(SOCKADDR_IN6), ret->realhost, sizeof(SOCKADDR_IN6), ret->realhost,
sizeof(ret->realhost), NULL, sizeof(ret->realhost), NULL, 0, 0) != 0) {
0, 0) != 0)
{
strncpy(ret->realhost, host, strncpy(ret->realhost, host,
sizeof(ret->realhost)); sizeof(ret->realhost));
} }
@ -272,21 +321,17 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
} }
/* We used the IPv4-only gethostbyname()... */ /* We used the IPv4-only gethostbyname()... */
else else
{
#endif #endif
{
memcpy(&a, h->h_addr, sizeof(a)); memcpy(&a, h->h_addr, sizeof(a));
/* This way we are always sure the h->h_name is valid :) */ /* This way we are always sure the h->h_name is valid :) */
strncpy(ret->realhost, h->h_name, sizeof(ret->realhost)); strncpy(ret->realhost, h->h_name, sizeof(ret->realhost));
#ifdef IPV6
} }
#endif
} }
#ifdef IPV6 #ifdef IPV6
FreeLibrary(dllWSHIP6); FreeLibrary(dllWSHIP6);
#endif #endif
} } else {
else
{
/* /*
* This must be a numeric IPv4 address because it caused a * This must be a numeric IPv4 address because it caused a
* success return from inet_addr. * success return from inet_addr.
@ -298,18 +343,22 @@ SockAddr sk_namelookup(char *host, char **canonicalname)
return ret; return ret;
} }
void sk_addr_free(SockAddr addr) { void sk_addr_free(SockAddr addr)
{
sfree(addr); sfree(addr);
} }
static Plug sk_tcp_plug (Socket sock, Plug p) { static Plug sk_tcp_plug(Socket sock, Plug p)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
Plug ret = s->plug; Plug ret = s->plug;
if (p) s->plug = p; if (p)
s->plug = p;
return ret; return ret;
} }
static void sk_tcp_flush (Socket s) { static void sk_tcp_flush(Socket s)
{
/* /*
* We send data to the socket as soon as we can anyway, * We send data to the socket as soon as we can anyway,
* so we don't need to do anything here. :-) * so we don't need to do anything here. :-)
@ -386,25 +435,24 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
int retcode; int retcode;
#ifdef IPV6 #ifdef IPV6
if (addr->family == AF_INET6) if (addr->family == AF_INET6) {
{
memset(&a6, 0, sizeof(a6)); memset(&a6, 0, sizeof(a6));
a6.sin6_family = AF_INET6; a6.sin6_family = AF_INET6;
/*a6.sin6_addr = in6addr_any; *//* == 0 */ /*a6.sin6_addr = in6addr_any; *//* == 0 */
a6.sin6_port = htons(localport); a6.sin6_port = htons(localport);
} } else
else
{
#endif #endif
{
a.sin_family = AF_INET; a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_ANY); a.sin_addr.s_addr = htonl(INADDR_ANY);
a.sin_port = htons(localport); a.sin_port = htons(localport);
#ifdef IPV6
} }
#ifdef IPV6
retcode = bind(s, (addr->family == AF_INET6 ? retcode = bind(s, (addr->family == AF_INET6 ?
(struct sockaddr *) &a6 : (struct sockaddr *) &a6 :
(struct sockaddr *) &a), (struct sockaddr *) &a),
(addr->family == AF_INET6 ? sizeof(a6) : sizeof(a))); (addr->family ==
AF_INET6 ? sizeof(a6) : sizeof(a)));
#else #else
retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
#endif #endif
@ -424,8 +472,7 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
break; /* we might have got to the end */ break; /* we might have got to the end */
} }
if (err) if (err) {
{
ret->error = winsock_error_string(err); ret->error = winsock_error_string(err);
return (Socket) ret; return (Socket) ret;
} }
@ -434,26 +481,28 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
* Connect to remote address. * Connect to remote address.
*/ */
#ifdef IPV6 #ifdef IPV6
if (addr->family == AF_INET6) if (addr->family == AF_INET6) {
{
memset(&a, 0, sizeof(a)); memset(&a, 0, sizeof(a));
a6.sin6_family = AF_INET6; a6.sin6_family = AF_INET6;
a6.sin6_port = htons((short) port); a6.sin6_port = htons((short) port);
a6.sin6_addr = ((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr; a6.sin6_addr =
} ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
else } else
{
#endif #endif
{
a.sin_family = AF_INET; a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(addr->address); a.sin_addr.s_addr = htonl(addr->address);
a.sin_port = htons((short) port); a.sin_port = htons((short) port);
#ifdef IPV6
} }
if (connect (s, (addr->family == AF_INET6) ? (struct sockaddr *)&a6 : (struct sockaddr *)&a, (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) == SOCKET_ERROR) if ((
#ifdef IPV6
connect(s, ((addr->family == AF_INET6) ?
(struct sockaddr *) &a6 : (struct sockaddr *) &a),
(addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
#else #else
if (connect (s, (struct sockaddr *)&a, sizeof(a)) == SOCKET_ERROR) connect(s, (struct sockaddr *) &a, sizeof(a))
#endif #endif
{ ) == SOCKET_ERROR) {
err = WSAGetLastError(); err = WSAGetLastError();
ret->error = winsock_error_string(err); ret->error = winsock_error_string(err);
return (Socket) ret; return (Socket) ret;
@ -472,7 +521,8 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
return (Socket) ret; return (Socket) ret;
} }
static void sk_tcp_close(Socket sock) { static void sk_tcp_close(Socket sock)
{
extern char *do_select(SOCKET skt, int startup); extern char *do_select(SOCKET skt, int startup);
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
@ -486,7 +536,8 @@ static void sk_tcp_close(Socket sock) {
* The function which tries to send on a socket once it's deemed * The function which tries to send on a socket once it's deemed
* writable. * writable.
*/ */
void try_send(Actual_Socket s) { void try_send(Actual_Socket s)
{
while (s->head) { while (s->head) {
int nsent; int nsent;
DWORD err; DWORD err;
@ -500,7 +551,8 @@ void try_send(Actual_Socket s) {
len = s->head->buflen - s->head->bufpos; len = s->head->buflen - s->head->bufpos;
} }
nsent = send(s->s, s->head->buf + s->head->bufpos, len, urgentflag); nsent =
send(s->s, s->head->buf + s->head->bufpos, len, urgentflag);
noise_ultralight(nsent); noise_ultralight(nsent);
if (nsent <= 0) { if (nsent <= 0) {
err = (nsent < 0 ? WSAGetLastError() : 0); err = (nsent < 0 ? WSAGetLastError() : 0);
@ -516,8 +568,7 @@ void try_send(Actual_Socket s) {
s->writable = FALSE; s->writable = FALSE;
return; return;
} else if (nsent == 0 || } else if (nsent == 0 ||
err == WSAECONNABORTED || err == WSAECONNABORTED || err == WSAECONNRESET) {
err == WSAECONNRESET) {
/* /*
* FIXME. This will have to be done better when we * FIXME. This will have to be done better when we
* start managing multiple sockets (e.g. SSH port * start managing multiple sockets (e.g. SSH port
@ -548,7 +599,8 @@ void try_send(Actual_Socket s) {
} }
} }
static void sk_tcp_write(Socket sock, char *buf, int len) { static void sk_tcp_write(Socket sock, char *buf, int len)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
/* /*
@ -585,7 +637,8 @@ static void sk_tcp_write(Socket sock, char *buf, int len) {
try_send(s); try_send(s);
} }
static void sk_tcp_write_oob(Socket sock, char *buf, int len) { static void sk_tcp_write_oob(Socket sock, char *buf, int len)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
/* /*
@ -618,7 +671,8 @@ static void sk_tcp_write_oob(Socket sock, char *buf, int len) {
try_send(s); try_send(s);
} }
int select_result(WPARAM wParam, LPARAM lParam) { int select_result(WPARAM wParam, LPARAM lParam)
{
int ret, open; int ret, open;
DWORD err; DWORD err;
char buf[20480]; /* nice big buffer for plenty of speed */ char buf[20480]; /* nice big buffer for plenty of speed */
@ -670,7 +724,8 @@ int select_result(WPARAM wParam, LPARAM lParam) {
} }
} }
if (ret < 0) { if (ret < 0) {
return plug_closing (s->plug, winsock_error_string(err), err, 0); return plug_closing(s->plug, winsock_error_string(err), err,
0);
} else if (0 == ret) { } else if (0 == ret) {
return plug_closing(s->plug, NULL, 0, 0); return plug_closing(s->plug, NULL, 0, 0);
} else { } else {
@ -706,10 +761,13 @@ int select_result(WPARAM wParam, LPARAM lParam) {
err = WSAGetLastError(); err = WSAGetLastError();
if (err == WSAEWOULDBLOCK) if (err == WSAEWOULDBLOCK)
break; break;
return plug_closing (s->plug, winsock_error_string(err), err, 0); return plug_closing(s->plug, winsock_error_string(err),
err, 0);
} else { } else {
if (ret) open &= plug_receive (s->plug, 0, buf, ret); if (ret)
else open &= plug_closing (s->plug, NULL, 0, 0); open &= plug_receive(s->plug, 0, buf, ret);
else
open &= plug_closing(s->plug, NULL, 0, 0);
} }
} while (ret > 0); } while (ret > 0);
return open; return open;
@ -722,11 +780,14 @@ int select_result(WPARAM wParam, LPARAM lParam) {
* Each socket abstraction contains a `void *' private field in * Each socket abstraction contains a `void *' private field in
* which the client can keep state. * which the client can keep state.
*/ */
void sk_set_private_ptr(Socket sock, void *ptr) { void sk_set_private_ptr(Socket sock, void *ptr)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
s->private_ptr = ptr; s->private_ptr = ptr;
} }
void *sk_get_private_ptr(Socket sock) {
void *sk_get_private_ptr(Socket sock)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
return s->private_ptr; return s->private_ptr;
} }
@ -736,10 +797,12 @@ void *sk_get_private_ptr(Socket sock) {
* if there's a problem. These functions extract an error message, * if there's a problem. These functions extract an error message,
* or return NULL if there's no problem. * or return NULL if there's no problem.
*/ */
char *sk_addr_error(SockAddr addr) { char *sk_addr_error(SockAddr addr)
{
return addr->error; return addr->error;
} }
static char *sk_tcp_socket_error(Socket sock) { static char *sk_tcp_socket_error(Socket sock)
{
Actual_Socket s = (Actual_Socket) sock; Actual_Socket s = (Actual_Socket) sock;
return s->error; return s->error;
} }
@ -747,13 +810,16 @@ static char *sk_tcp_socket_error(Socket sock) {
/* /*
* For Plink: enumerate all sockets currently active. * For Plink: enumerate all sockets currently active.
*/ */
SOCKET first_socket(int *state) { SOCKET first_socket(int *state)
{
Actual_Socket s; Actual_Socket s;
*state = 0; *state = 0;
s = index234(sktree, (*state)++); s = index234(sktree, (*state)++);
return s ? s->s : INVALID_SOCKET; return s ? s->s : INVALID_SOCKET;
} }
SOCKET next_socket(int *state) {
SOCKET next_socket(int *state)
{
Actual_Socket s = index234(sktree, (*state)++); Actual_Socket s = index234(sktree, (*state)++);
return s ? s->s : INVALID_SOCKET; return s ? s->s : INVALID_SOCKET;
} }

View File

@ -15,12 +15,14 @@ static char seedpath[2*MAX_PATH+10] = "\0";
static char hex[16] = "0123456789ABCDEF"; static char hex[16] = "0123456789ABCDEF";
static void mungestr(char *in, char *out) { static void mungestr(char *in, char *out)
{
int candot = 0; int candot = 0;
while (*in) { while (*in) {
if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' || if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
*in == '%' || *in < ' ' || *in > '~' || (*in == '.' && !candot)) { *in == '%' || *in < ' ' || *in > '~' || (*in == '.'
&& !candot)) {
*out++ = '%'; *out++ = '%';
*out++ = hex[((unsigned char) *in) >> 4]; *out++ = hex[((unsigned char) *in) >> 4];
*out++ = hex[((unsigned char) *in) & 15]; *out++ = hex[((unsigned char) *in) & 15];
@ -33,27 +35,33 @@ static void mungestr(char *in, char *out) {
return; return;
} }
static void unmungestr(char *in, char *out, int outlen) { static void unmungestr(char *in, char *out, int outlen)
{
while (*in) { while (*in) {
if (*in == '%' && in[1] && in[2]) { if (*in == '%' && in[1] && in[2]) {
int i, j; int i, j;
i = in[1] - '0'; i -= (i > 9 ? 7 : 0); i = in[1] - '0';
j = in[2] - '0'; j -= (j > 9 ? 7 : 0); i -= (i > 9 ? 7 : 0);
j = in[2] - '0';
j -= (j > 9 ? 7 : 0);
*out++ = (i << 4) + j; *out++ = (i << 4) + j;
if (!--outlen) return; if (!--outlen)
return;
in += 3; in += 3;
} else { } else {
*out++ = *in++; *out++ = *in++;
if (!--outlen) return; if (!--outlen)
return;
} }
} }
*out = '\0'; *out = '\0';
return; return;
} }
void *open_settings_w(char *sessionname) { void *open_settings_w(char *sessionname)
{
HKEY subkey1, sesskey; HKEY subkey1, sesskey;
int ret; int ret;
char *p; char *p;
@ -74,22 +82,27 @@ void *open_settings_w(char *sessionname) {
return (void *) sesskey; return (void *) sesskey;
} }
void write_setting_s(void *handle, char *key, char *value) { void write_setting_s(void *handle, char *key, char *value)
{
if (handle) if (handle)
RegSetValueEx((HKEY)handle, key, 0, REG_SZ, value, 1+strlen(value)); RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
1 + strlen(value));
} }
void write_setting_i(void *handle, char *key, int value) { void write_setting_i(void *handle, char *key, int value)
{
if (handle) if (handle)
RegSetValueEx((HKEY) handle, key, 0, REG_DWORD, RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
(CONST BYTE *) & value, sizeof(value)); (CONST BYTE *) & value, sizeof(value));
} }
void close_settings_w(void *handle) { void close_settings_w(void *handle)
{
RegCloseKey((HKEY) handle); RegCloseKey((HKEY) handle);
} }
void *open_settings_r(char *sessionname) { void *open_settings_r(char *sessionname)
{
HKEY subkey1, sesskey; HKEY subkey1, sesskey;
char *p; char *p;
@ -110,20 +123,21 @@ void *open_settings_r(char *sessionname) {
return (void *) sesskey; return (void *) sesskey;
} }
char *read_setting_s(void *handle, char *key, char *buffer, int buflen) { char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
{
DWORD type, size; DWORD type, size;
size = buflen; size = buflen;
if (!handle || if (!handle ||
RegQueryValueEx((HKEY) handle, key, 0, RegQueryValueEx((HKEY) handle, key, 0,
&type, buffer, &size) != ERROR_SUCCESS || &type, buffer, &size) != ERROR_SUCCESS ||
type != REG_SZ) type != REG_SZ) return NULL;
return NULL;
else else
return buffer; return buffer;
} }
int read_setting_i(void *handle, char *key, int defvalue) { int read_setting_i(void *handle, char *key, int defvalue)
{
DWORD type, val, size; DWORD type, val, size;
size = sizeof(val); size = sizeof(val);
@ -136,11 +150,13 @@ int read_setting_i(void *handle, char *key, int defvalue) {
return val; return val;
} }
void close_settings_r(void *handle) { void close_settings_r(void *handle)
{
RegCloseKey((HKEY) handle); RegCloseKey((HKEY) handle);
} }
void del_settings (char *sessionname) { void del_settings(char *sessionname)
{
HKEY subkey1; HKEY subkey1;
char *p; char *p;
@ -160,7 +176,8 @@ struct enumsettings {
int i; int i;
}; };
void *enum_settings_start(void) { void *enum_settings_start(void)
{
struct enumsettings *ret; struct enumsettings *ret;
HKEY key; HKEY key;
@ -176,7 +193,8 @@ void *enum_settings_start(void) {
return ret; return ret;
} }
char *enum_settings_next(void *handle, char *buffer, int buflen) { char *enum_settings_next(void *handle, char *buffer, int buflen)
{
struct enumsettings *e = (struct enumsettings *) handle; struct enumsettings *e = (struct enumsettings *) handle;
char *otherbuf; char *otherbuf;
otherbuf = smalloc(3 * buflen); otherbuf = smalloc(3 * buflen);
@ -190,14 +208,16 @@ char *enum_settings_next(void *handle, char *buffer, int buflen) {
} }
void enum_settings_finish(void *handle) { void enum_settings_finish(void *handle)
{
struct enumsettings *e = (struct enumsettings *) handle; struct enumsettings *e = (struct enumsettings *) handle;
RegCloseKey(e->key); RegCloseKey(e->key);
sfree(e); sfree(e);
} }
static void hostkey_regname(char *buffer, char *hostname, static void hostkey_regname(char *buffer, char *hostname,
int port, char *keytype) { int port, char *keytype)
{
int len; int len;
strcpy(buffer, keytype); strcpy(buffer, keytype);
strcat(buffer, "@"); strcat(buffer, "@");
@ -206,7 +226,8 @@ static void hostkey_regname(char *buffer, char *hostname,
mungestr(hostname, buffer + strlen(buffer)); mungestr(hostname, buffer + strlen(buffer));
} }
int verify_host_key(char *hostname, int port, char *keytype, char *key) { int verify_host_key(char *hostname, int port, char *keytype, char *key)
{
char *otherstr, *regname; char *otherstr, *regname;
int len; int len;
HKEY rkey; HKEY rkey;
@ -264,7 +285,8 @@ int verify_host_key(char *hostname, int port, char *keytype, char *key) {
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
int ndigits, nwords; int ndigits, nwords;
*p++ = '0'; *p++ = 'x'; *p++ = '0';
*p++ = 'x';
ndigits = strcspn(q, "/"); /* find / or end of string */ ndigits = strcspn(q, "/"); /* find / or end of string */
nwords = ndigits / 4; nwords = ndigits / 4;
/* now trim ndigits to remove leading zeros */ /* now trim ndigits to remove leading zeros */
@ -309,7 +331,8 @@ int verify_host_key(char *hostname, int port, char *keytype, char *key) {
return 0; /* key matched OK in registry */ return 0; /* key matched OK in registry */
} }
void store_host_key(char *hostname, int port, char *keytype, char *key) { void store_host_key(char *hostname, int port, char *keytype, char *key)
{
char *regname; char *regname;
HKEY rkey; HKEY rkey;
@ -320,21 +343,22 @@ void store_host_key(char *hostname, int port, char *keytype, char *key) {
if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys", if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
&rkey) != ERROR_SUCCESS) &rkey) != ERROR_SUCCESS)
return; /* key does not exist in registry */ return; /* key does not exist in registry */
RegSetValueEx(rkey, regname, 0, REG_SZ, key, RegSetValueEx(rkey, regname, 0, REG_SZ, key, strlen(key) + 1);
strlen(key)+1);
RegCloseKey(rkey); RegCloseKey(rkey);
} }
/* /*
* Find the random seed file path and store it in `seedpath'. * Find the random seed file path and store it in `seedpath'.
*/ */
static void get_seedpath(void) { static void get_seedpath(void)
{
HKEY rkey; HKEY rkey;
DWORD type, size; DWORD type, size;
size = sizeof(seedpath); size = sizeof(seedpath);
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey)==ERROR_SUCCESS) { if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey) ==
ERROR_SUCCESS) {
int ret = RegQueryValueEx(rkey, "RandSeedFile", int ret = RegQueryValueEx(rkey, "RandSeedFile",
0, &type, seedpath, &size); 0, &type, seedpath, &size);
if (ret != ERROR_SUCCESS || type != REG_SZ) if (ret != ERROR_SUCCESS || type != REG_SZ)
@ -346,8 +370,11 @@ static void get_seedpath(void) {
if (!seedpath[0]) { if (!seedpath[0]) {
int len, ret; int len, ret;
len = GetEnvironmentVariable("HOMEDRIVE", seedpath, sizeof(seedpath)); len =
ret = GetEnvironmentVariable("HOMEPATH", seedpath+len, GetEnvironmentVariable("HOMEDRIVE", seedpath,
sizeof(seedpath));
ret =
GetEnvironmentVariable("HOMEPATH", seedpath + len,
sizeof(seedpath) - len); sizeof(seedpath) - len);
if (ret == 0) { /* probably win95; store in \WINDOWS */ if (ret == 0) { /* probably win95; store in \WINDOWS */
GetWindowsDirectory(seedpath, sizeof(seedpath)); GetWindowsDirectory(seedpath, sizeof(seedpath));
@ -358,7 +385,8 @@ static void get_seedpath(void) {
} }
} }
void read_random_seed(noise_consumer_t consumer) { void read_random_seed(noise_consumer_t consumer)
{
HANDLE seedf; HANDLE seedf;
if (!seedpath[0]) if (!seedpath[0])
@ -382,7 +410,8 @@ void read_random_seed(noise_consumer_t consumer) {
} }
} }
void write_random_seed(void *data, int len) { void write_random_seed(void *data, int len)
{
HANDLE seedf; HANDLE seedf;
if (!seedpath[0]) if (!seedpath[0])
@ -402,7 +431,8 @@ void write_random_seed(void *data, int len) {
/* /*
* Recursively delete a registry key and everything under it. * Recursively delete a registry key and everything under it.
*/ */
static void registry_recursive_remove(HKEY key) { static void registry_recursive_remove(HKEY key)
{
DWORD i; DWORD i;
char name[MAX_PATH + 1]; char name[MAX_PATH + 1];
HKEY subkey; HKEY subkey;
@ -417,7 +447,8 @@ static void registry_recursive_remove(HKEY key) {
} }
} }
void cleanup_all(void) { void cleanup_all(void)
{
HKEY key; HKEY key;
int ret; int ret;
char name[MAX_PATH + 1]; char name[MAX_PATH + 1];
@ -436,7 +467,8 @@ void cleanup_all(void) {
/* /*
* Open the main PuTTY registry key and remove everything in it. * Open the main PuTTY registry key and remove everything in it.
*/ */
if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == ERROR_SUCCESS) { if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) ==
ERROR_SUCCESS) {
registry_recursive_remove(key); registry_recursive_remove(key);
RegCloseKey(key); RegCloseKey(key);
} }

View File

@ -32,14 +32,12 @@ struct ctlpos {
void ctlposinit(struct ctlpos *cp, HWND hwnd, void ctlposinit(struct ctlpos *cp, HWND hwnd,
int leftborder, int rightborder, int topborder); int leftborder, int rightborder, int topborder);
void doctl(struct ctlpos *cp, RECT r, void doctl(struct ctlpos *cp, RECT r,
char *wclass, int wstyle, int exstyle, char *wclass, int wstyle, int exstyle, char *wtext, int wid);
char *wtext, int wid);
void bartitle(struct ctlpos *cp, char *name, int id); void bartitle(struct ctlpos *cp, char *name, int id);
void beginbox(struct ctlpos *cp, char *name, int idbox); void beginbox(struct ctlpos *cp, char *name, int idbox);
void endbox(struct ctlpos *cp); void endbox(struct ctlpos *cp);
void multiedit(struct ctlpos *cp, ...); void multiedit(struct ctlpos *cp, ...);
void radioline(struct ctlpos *cp, void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...);
char *text, int id, int nacross, ...);
void radiobig(struct ctlpos *cp, char *text, int id, ...); void radiobig(struct ctlpos *cp, char *text, int id, ...);
void checkbox(struct ctlpos *cp, char *text, int id); void checkbox(struct ctlpos *cp, char *text, int id);
void statictext(struct ctlpos *cp, char *text, int id); void statictext(struct ctlpos *cp, char *text, int id);
@ -51,8 +49,7 @@ void staticpassedit(struct ctlpos *cp, char *stext,
int sid, int eid, int percentedit); int sid, int eid, int percentedit);
void bigeditctrl(struct ctlpos *cp, char *stext, void bigeditctrl(struct ctlpos *cp, char *stext,
int sid, int eid, int lines); int sid, int eid, int lines);
void ersatztab(struct ctlpos *cp, char *stext, int sid, void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id);
int lid, int s2id);
void editbutton(struct ctlpos *cp, char *stext, int sid, void editbutton(struct ctlpos *cp, char *stext, int sid,
int eid, char *btext, int bid); int eid, char *btext, int bid);
void sesssaver(struct ctlpos *cp, char *text, void sesssaver(struct ctlpos *cp, char *text,
@ -60,8 +57,7 @@ void sesssaver(struct ctlpos *cp, char *text,
void envsetter(struct ctlpos *cp, char *stext, int sid, void envsetter(struct ctlpos *cp, char *stext, int sid,
char *e1stext, int e1sid, int e1id, char *e1stext, int e1sid, int e1id,
char *e2stext, int e2sid, int e2id, char *e2stext, int e2sid, int e2id,
int listid, int listid, char *b1text, int b1id, char *b2text, int b2id);
char *b1text, int b1id, char *b2text, int b2id);
void charclass(struct ctlpos *cp, char *stext, int sid, int listid, void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
char *btext, int bid, int eid, char *s2text, int s2id); char *btext, int bid, int eid, char *s2text, int s2id);
void colouredit(struct ctlpos *cp, char *stext, int sid, int listid, void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,

View File

@ -79,7 +79,8 @@ static unsigned char x11_authdata[64];
static int x11_authdatalen; static int x11_authdatalen;
void x11_invent_auth(char *proto, int protomaxlen, void x11_invent_auth(char *proto, int protomaxlen,
char *data, int datamaxlen) { char *data, int datamaxlen)
{
char ourdata[64]; char ourdata[64];
int i; int i;
@ -96,7 +97,8 @@ void x11_invent_auth(char *proto, int protomaxlen,
strncpy(data, ourdata, datamaxlen); strncpy(data, ourdata, datamaxlen);
} }
static int x11_verify(char *proto, unsigned char *data, int dlen) { static int x11_verify(char *proto, unsigned char *data, int dlen)
{
if (strcmp(proto, "MIT-MAGIC-COOKIE-1") != 0) if (strcmp(proto, "MIT-MAGIC-COOKIE-1") != 0)
return 0; /* wrong protocol attempted */ return 0; /* wrong protocol attempted */
if (dlen != x11_authdatalen) if (dlen != x11_authdatalen)
@ -106,7 +108,9 @@ static int x11_verify(char *proto, unsigned char *data, int dlen) {
return 1; return 1;
} }
static int x11_closing (Plug plug, char *error_msg, int error_code, int calling_back) { static int x11_closing(Plug plug, char *error_msg, int error_code,
int calling_back)
{
struct X11Private *pr = (struct X11Private *) plug; struct X11Private *pr = (struct X11Private *) plug;
/* /*
@ -119,7 +123,8 @@ static int x11_closing (Plug plug, char *error_msg, int error_code, int calling_
return 1; return 1;
} }
static int x11_receive (Plug plug, int urgent, char *data, int len) { static int x11_receive(Plug plug, int urgent, char *data, int len)
{
struct X11Private *pr = (struct X11Private *) plug; struct X11Private *pr = (struct X11Private *) plug;
sshfwd_write(pr->c, data, len); sshfwd_write(pr->c, data, len);
@ -132,7 +137,8 @@ static int x11_receive (Plug plug, int urgent, char *data, int len) {
* Returns an error message, or NULL on success. * Returns an error message, or NULL on success.
* also, fills the SocketsStructure * also, fills the SocketsStructure
*/ */
char *x11_init (Socket *s, char *display, void *c) { char *x11_init(Socket * s, char *display, void *c)
{
static struct plug_function_table fn_table = { static struct plug_function_table fn_table = {
x11_closing, x11_closing,
x11_receive x11_receive
@ -188,7 +194,8 @@ char *x11_init (Socket *s, char *display, void *c) {
return NULL; return NULL;
} }
void x11_close (Socket s) { void x11_close(Socket s)
{
struct X11Private *pr; struct X11Private *pr;
if (!s) if (!s)
return; return;
@ -207,7 +214,8 @@ void x11_close (Socket s) {
/* /*
* Called to send data down the raw connection. * Called to send data down the raw connection.
*/ */
void x11_send (Socket s, char *data, int len) { void x11_send(Socket s, char *data, int len)
{
struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s); struct X11Private *pr = (struct X11Private *) sk_get_private_ptr(s);
if (s == NULL) if (s == NULL)

228
xlat.c
View File

@ -2,124 +2,175 @@
#include <stdio.h> #include <stdio.h>
#include "putty.h" #include "putty.h"
static unsigned char win2koi[] = static unsigned char win2koi[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 126, 127,
160,161,162,163,164,189,166,167,179,169,180,171,172,173,174,183, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
176,177,182,166,173,181,182,183,163,185,164,187,188,189,190,167, 142, 143,
225,226,247,231,228,229,246,250,233,234,235,236,237,238,239,240, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
242,243,244,245,230,232,227,254,251,253,255,249,248,252,224,241, 158, 159,
193,194,215,199,196,197,214,218,201,202,203,204,205,206,207,208, 160, 161, 162, 163, 164, 189, 166, 167, 179, 169, 180, 171, 172, 173,
210,211,212,213,198,200,195,222,219,221,223,217,216,220,192,209 174, 183,
176, 177, 182, 166, 173, 181, 182, 183, 163, 185, 164, 187, 188, 189,
190, 167,
225, 226, 247, 231, 228, 229, 246, 250, 233, 234, 235, 236, 237, 238,
239, 240,
242, 243, 244, 245, 230, 232, 227, 254, 251, 253, 255, 249, 248, 252,
224, 241,
193, 194, 215, 199, 196, 197, 214, 218, 201, 202, 203, 204, 205, 206,
207, 208,
210, 211, 212, 213, 198, 200, 195, 222, 219, 221, 223, 217, 216, 220,
192, 209
}; };
static unsigned char koi2win[] = static unsigned char koi2win[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 126, 127,
160,161,162,184,186,165,179,191,168,169,170,171,172,180,174,175, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
176,177,178,168,170,181,178,175,184,185,186,187,188,165,190,191, 142, 143,
254,224,225,246,228,229,244,227,245,232,233,234,235,236,237,238, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
239,255,240,241,242,243,230,226,252,251,231,248,253,249,247,250, 158, 159,
222,192,193,214,196,197,212,195,213,200,201,202,203,204,205,206, 160, 161, 162, 184, 186, 165, 179, 191, 168, 169, 170, 171, 172, 180,
207,223,208,209,210,211,198,194,220,219,199,216,221,217,215,218 174, 175,
176, 177, 178, 168, 170, 181, 178, 175, 184, 185, 186, 187, 188, 165,
190, 191,
254, 224, 225, 246, 228, 229, 244, 227, 245, 232, 233, 234, 235, 236,
237, 238,
239, 255, 240, 241, 242, 243, 230, 226, 252, 251, 231, 248, 253, 249,
247, 250,
222, 192, 193, 214, 196, 197, 212, 195, 213, 200, 201, 202, 203, 204,
205, 206,
207, 223, 208, 209, 210, 211, 198, 194, 220, 219, 199, 216, 221, 217,
215, 218
}; };
static unsigned char xlatWIN1250toISO88592[] = static unsigned char xlatWIN1250toISO88592[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
128,129, 39,131, 34, 46,124,124,136, 47,169, 60,166,171,174,172, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 127,
128, 129, 39, 131, 34, 46, 124, 124, 136, 47, 169, 60, 166, 171, 174,
172,
144, 96, 39, 34, 34, 42, 45, 45, 152, 84, 185, 62, 182, 187, 190, 188, 144, 96, 39, 34, 34, 42, 45, 45, 152, 84, 185, 62, 182, 187, 190, 188,
160,183,162,163,164,161,124,167,168, 99,170, 34, 39,173, 82,175, 160, 183, 162, 163, 164, 161, 124, 167, 168, 99, 170, 34, 39, 173, 82,
176, 63,178,179,180,117,182,255,184,177,186, 34,165,189,181,191, 175,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 176, 63, 178, 179, 180, 117, 182, 255, 184, 177, 186, 34, 165, 189,
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 181, 191,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255
}; };
static unsigned char xlatISO88592toWIN1250[] = static unsigned char xlatISO88592toWIN1250[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 126, 127,
160,165,162,163,164,188,140,167,168,138,170,141,143,173,142,175, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
176,185,178,179,180,190,156,161,184,154,186,157,159,189,158,191, 142, 143,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 158, 159,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 160, 165, 162, 163, 164, 188, 140, 167, 168, 138, 170, 141, 143, 173,
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 142, 175,
176, 185, 178, 179, 180, 190, 156, 161, 184, 154, 186, 157, 159, 189,
158, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255
}; };
static unsigned char xlatISO88592toCP852[] = static unsigned char xlatISO88592toCP852[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 127,
242, 32, 34, 32, 34, 46, 43, 35, 32, 47, 138, 60, 151, 141, 166, 141, 242, 32, 34, 32, 34, 46, 43, 35, 32, 47, 138, 60, 151, 141, 166, 141,
032, 34, 34, 34, 34, 254, 45, 45, 32, 126, 154, 62, 152, 157, 167, 171, 032, 34, 34, 34, 34, 254, 45, 45, 32, 126, 154, 62, 152, 157, 167, 171,
255,164,244,157,207,149,151,245,249,230,184,155,141,240,166,189, 255, 164, 244, 157, 207, 149, 151, 245, 249, 230, 184, 155, 141, 240,
248,165,247,136,239,150,152,243,242,231,173,156,171,241,167,190, 166, 189,
232,181,182,198,142,145,143,128,172,144,168,211,183,214,215,210, 248, 165, 247, 136, 239, 150, 152, 243, 242, 231, 173, 156, 171, 241,
209,227,213,224,226,138,153,158,252,222,233,235,154,237,221,225, 167, 190,
234,160,131,199,132,146,134,135,159,130,169,137,216,161,140,212, 232, 181, 182, 198, 142, 145, 143, 128, 172, 144, 168, 211, 183, 214,
208,228,229,162,147,139,148,246,253,133,163,251,129,236,238,250, 215, 210,
209, 227, 213, 224, 226, 138, 153, 158, 252, 222, 233, 235, 154, 237,
221, 225,
234, 160, 131, 199, 132, 146, 134, 135, 159, 130, 169, 137, 216, 161,
140, 212,
208, 228, 229, 162, 147, 139, 148, 246, 253, 133, 163, 251, 129, 236,
238, 250,
}; };
static unsigned char xlatCP852toISO88592[] = static unsigned char xlatCP852toISO88592[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 111,
199,252,233,226,228,249,230,231,179,235,138,245,238,141,196,198, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
201,197,229,244,246,165,181,140,156,214,154,171,187,157,215,232, 126, 127,
225,237,243,250,161,177,142,158,202,234,170,159,200,186,174,175, 199, 252, 233, 226, 228, 249, 230, 231, 179, 235, 138, 245, 238, 141,
176,177,178,179,180,193,194,204,170,185,186,187,188,175,191,191, 196, 198,
192,193,194,195,196,197,195,227,200,201,202,203,204,205,206,164, 201, 197, 229, 244, 246, 165, 181, 140, 156, 214, 154, 171, 187, 157,
240,208,207,203,239,210,205,206,236,217,218,219,220,222,217,223, 215, 232,
211,223,212,209,241,242,169,185,192,218,224,219,253,221,254,180, 225, 237, 243, 250, 161, 177, 142, 158, 202, 234, 170, 159, 200, 186,
173,189,128,183,162,167,247,178,176,168,255,251,216,248,149,160, 174, 175,
176, 177, 178, 179, 180, 193, 194, 204, 170, 185, 186, 187, 188, 175,
191, 191,
192, 193, 194, 195, 196, 197, 195, 227, 200, 201, 202, 203, 204, 205,
206, 164,
240, 208, 207, 203, 239, 210, 205, 206, 236, 217, 218, 219, 220, 222,
217, 223,
211, 223, 212, 209, 241, 242, 169, 185, 192, 218, 224, 219, 253, 221,
254, 180,
173, 189, 128, 183, 162, 167, 247, 178, 176, 168, 255, 251, 216, 248,
149, 160,
}; };
unsigned char xlat_kbd2tty(unsigned char c) unsigned char xlat_kbd2tty(unsigned char c)
@ -143,24 +194,35 @@ unsigned char xlat_tty2scr(unsigned char c)
} }
static unsigned char latkbd2_win[]= static unsigned char latkbd2_win[] = {
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 221, 35, 36, 37, 38, 253, 40, 41, 42, 178, 225, 186, 254, 46, 32, 33, 221, 35, 36, 37, 38, 253, 40, 41, 42, 178, 225, 186, 254, 46,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 198, 230, 193, 179, 222, 44, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 198, 230, 193, 179, 222, 44,
64,212,200,209,194,211,192,207,208,216,206,203,196,220,210,217, 64, 212, 200, 209, 194, 211, 192, 207, 208, 216, 206, 203, 196, 220,
199,201,202,219,197,195,204,214,215,205,223,245,191,250, 94,170, 210, 217,
96,244,232,241,226,243,224,239,240,248,238,235,228,252,242,249, 199, 201, 202, 219, 197, 195, 204, 214, 215, 205, 223, 245, 191, 250,
231,233,234,251,229,227,236,246,247,237,255,213,175,218,126,127, 94, 170,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 96, 244, 232, 241, 226, 243, 224, 239, 240, 248, 238, 235, 228, 252,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 242, 249,
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 231, 233, 234, 251, 229, 227, 236, 246, 247, 237, 255, 213, 175, 218,
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 126, 127,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 142, 143,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173,
174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255
}; };
unsigned char xlat_latkbd2win(unsigned char c) unsigned char xlat_latkbd2win(unsigned char c)