1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Replace PuTTY's 2-3-4 tree implementation with the shiny new counted

one, in preparation for using it to speed up scrollback.

[originally from svn r1053]
This commit is contained in:
Simon Tatham 2001-04-16 17:18:24 +00:00
parent cdf972d9f1
commit 03c9b6107b
7 changed files with 862 additions and 320 deletions

View File

@ -224,11 +224,11 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
static void keylist_update(void) {
struct RSAKey *rkey;
struct ssh2_userkey *skey;
enum234 e;
int i;
if (keylist) {
SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e)) {
for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
char listentry[512], *p;
/*
* Replace two spaces in the fingerprint with tabs, for
@ -242,7 +242,7 @@ static void keylist_update(void) {
SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
0, (LPARAM)listentry);
}
for (skey = first234(ssh2keys, &e); skey; skey = next234(&e)) {
for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
char listentry[512], *p;
int len;
/*
@ -438,15 +438,15 @@ static void answer_msg(void *msg) {
* Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
*/
{
enum234 e;
struct RSAKey *key;
int len, nkeys;
int i;
/*
* Count up the number and length of keys we hold.
*/
len = nkeys = 0;
for (key = first234(rsakeys, &e); key; key = next234(&e)) {
for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
nkeys++;
len += 4; /* length field */
len += ssh1_bignum_length(key->exponent);
@ -465,7 +465,7 @@ static void answer_msg(void *msg) {
ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
PUT_32BIT(ret+5, nkeys);
p = ret + 5 + 4;
for (key = first234(rsakeys, &e); key; key = next234(&e)) {
for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
PUT_32BIT(p, bignum_bitcount(key->modulus));
p += 4;
p += ssh1_write_bignum(p, key->exponent);
@ -481,17 +481,17 @@ static void answer_msg(void *msg) {
* Reply with SSH2_AGENT_IDENTITIES_ANSWER.
*/
{
enum234 e;
struct ssh2_userkey *key;
int len, nkeys;
unsigned char *blob;
int bloblen;
int i;
/*
* Count up the number and length of keys we hold.
*/
len = nkeys = 0;
for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
nkeys++;
len += 4; /* length field */
blob = key->alg->public_blob(key->data, &bloblen);
@ -511,7 +511,7 @@ static void answer_msg(void *msg) {
ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
PUT_32BIT(ret+5, nkeys);
p = ret + 5 + 4;
for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
blob = key->alg->public_blob(key->data, &bloblen);
PUT_32BIT(p, bloblen);
p += 4;
@ -743,9 +743,8 @@ static void answer_msg(void *msg) {
*/
{
struct RSAKey *rkey;
enum234 e;
while ( (rkey = first234(rsakeys, &e)) != NULL ) {
while ( (rkey = index234(rsakeys, 0)) != NULL ) {
del234(rsakeys, rkey);
freersakey(rkey);
sfree(rkey);
@ -762,9 +761,8 @@ static void answer_msg(void *msg) {
*/
{
struct ssh2_userkey *skey;
enum234 e;
while ( (skey = first234(ssh2keys, &e)) != NULL ) {
while ( (skey = index234(ssh2keys, 0)) != NULL ) {
del234(ssh2keys, skey);
skey->alg->freekey(skey->data);
sfree(skey);
@ -925,7 +923,6 @@ static void prompt_add_keyfile(void) {
*/
static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
enum234 e;
struct RSAKey *rkey;
struct ssh2_userkey *skey;
@ -970,11 +967,12 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
if (HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED) {
int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
int i;
if (n == LB_ERR) {
MessageBeep(0);
break;
}
for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e))
for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
if (n-- == 0)
break;
if (rkey) {
@ -982,7 +980,7 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
freersakey(rkey);
sfree(rkey);
} else {
for (skey = first234(ssh2keys, &e); skey; skey = next234(&e))
for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++)
if (n-- == 0)
break;
if (skey) {

13
plink.c
View File

@ -568,11 +568,10 @@ int main(int argc, char **argv) {
n = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
if (n == 0) {
WSANETWORKEVENTS things;
enum234 e;
SOCKET socket;
extern SOCKET first_socket(enum234 *), next_socket(enum234 *);
extern SOCKET first_socket(int *), next_socket(int *);
extern int select_result(WPARAM, LPARAM);
int i;
int i, socketstate;
/*
* We must not call select_result() for any socket
@ -582,8 +581,8 @@ int main(int argc, char **argv) {
*/
/* Count the active sockets. */
i = 0;
for (socket = first_socket(&e); socket != INVALID_SOCKET;
socket = next_socket(&e))
for (socket = first_socket(&socketstate); socket != INVALID_SOCKET;
socket = next_socket(&socketstate))
i++;
/* Expand the buffer if necessary. */
@ -594,8 +593,8 @@ int main(int argc, char **argv) {
/* Retrieve the sockets into sklist. */
skcount = 0;
for (socket = first_socket(&e); socket != INVALID_SOCKET;
socket = next_socket(&e)) {
for (socket = first_socket(&socketstate); socket != INVALID_SOCKET;
socket = next_socket(&socketstate)) {
sklist[skcount++] = socket;
}

75
ssh.c
View File

@ -365,6 +365,42 @@ static int ssh_channelfind(void *av, void *bv) {
return 0;
}
static int alloc_channel_id(void) {
const int CHANNEL_NUMBER_OFFSET = 256;
int low, high, mid;
int tsize;
struct ssh_channel *c;
/*
* First-fit allocation of channel numbers: always pick the
* lowest unused one. To do this, binary-search using the
* counted B-tree to find the largest channel ID which is in a
* contiguous sequence from the beginning. (Precisely
* everything in that sequence must have ID equal to its tree
* index plus CHANNEL_NUMBER_OFFSET.)
*/
tsize = count234(ssh_channels);
low = -1; high = tsize;
while (high - low > 1) {
mid = (high + low) / 2;
c = index234(ssh_channels, mid);
if (c->localid == mid + CHANNEL_NUMBER_OFFSET)
low = mid; /* this one is fine */
else
high = mid; /* this one is past it */
}
/*
* Now low points to either -1, or the tree index of the
* largest ID in the initial sequence.
*/
{
unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET;
assert(NULL == find234(ssh_channels, &i, ssh_channelfind));
}
return low + 1 + CHANNEL_NUMBER_OFFSET;
}
static void c_write (char *buf, int len) {
if ((flags & FLAG_STDERR)) {
int i;
@ -2234,9 +2270,7 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
} else if (pktin.type == SSH1_SMSG_X11_OPEN) {
/* Remote side is trying to open a channel to talk to our
* X-Server. Give them back a local channel number. */
unsigned i;
struct ssh_channel *c, *d;
enum234 e;
logevent("Received X11 connect request");
/* Refuse if X11 forwarding is disabled. */
@ -2256,13 +2290,8 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
PKT_END);
} else {
logevent("opening X11 forward connection succeeded");
for (i=1, d = first234(ssh_channels, &e); d; d = next234(&e)) {
if (d->localid > i)
break; /* found a free number */
i = d->localid + 1;
}
c->remoteid = GET_32BIT(pktin.body);
c->localid = i;
c->localid = alloc_channel_id();
c->closes = 0;
c->type = CHAN_X11; /* identify channel type */
add234(ssh_channels, c);
@ -2277,7 +2306,6 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
* agent. Give them back a local channel number. */
unsigned i;
struct ssh_channel *c;
enum234 e;
/* Refuse if agent forwarding is disabled. */
if (!ssh_agentfwd_enabled) {
@ -2285,15 +2313,9 @@ static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) {
PKT_INT, GET_32BIT(pktin.body),
PKT_END);
} else {
i = 1;
for (c = first234(ssh_channels, &e); c; c = next234(&e)) {
if (c->localid > i)
break; /* found a free number */
i = c->localid + 1;
}
c = smalloc(sizeof(struct ssh_channel));
c->remoteid = GET_32BIT(pktin.body);
c->localid = i;
c->localid = alloc_channel_id();
c->closes = 0;
c->type = CHAN_AGENT; /* identify channel type */
c->u.a.lensofar = 0;
@ -3512,8 +3534,9 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
/*
* So now create a channel with a session in it.
*/
ssh_channels = newtree234(ssh_channelcmp);
mainchan = smalloc(sizeof(struct ssh_channel));
mainchan->localid = 100; /* as good as any */
mainchan->localid = alloc_channel_id();
ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
ssh2_pkt_addstring("session");
ssh2_pkt_adduint32(mainchan->localid);
@ -3537,7 +3560,6 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
mainchan->v2.remmaxpkt = ssh2_pkt_getuint32();
mainchan->v2.outbuffer = NULL;
mainchan->v2.outbuflen = mainchan->v2.outbufsize = 0;
ssh_channels = newtree234(ssh_channelcmp);
add234(ssh_channels, mainchan);
logevent("Opened channel for session");
@ -3816,7 +3838,6 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
} else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) {
unsigned i = ssh2_pkt_getuint32();
struct ssh_channel *c;
enum234 e;
c = find234(ssh_channels, &i, ssh_channelfind);
if (!c)
@ -3842,8 +3863,7 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
/*
* See if that was the last channel left open.
*/
c = first234(ssh_channels, &e);
if (!c) {
if (count234(ssh_channels) == 0) {
logevent("All channels closed. Disconnecting");
ssh2_pkt_init(SSH2_MSG_DISCONNECT);
ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION);
@ -3902,15 +3922,8 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
} else {
struct ssh_channel *d;
unsigned i;
enum234 e;
for (i=1, d = first234(ssh_channels, &e); d;
d = next234(&e)) {
if (d->localid > i)
break; /* found a free number */
i = d->localid + 1;
}
c->localid = i;
c->localid = alloc_channel_id();
c->closes = 0;
c->v2.remwindow = ssh2_pkt_getuint32();
c->v2.remmaxpkt = ssh2_pkt_getuint32();
@ -3936,12 +3949,12 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
try_send = TRUE;
}
if (try_send) {
enum234 e;
int i;
struct ssh_channel *c;
/*
* Try to send data on all channels if we can.
*/
for (c = first234(ssh_channels, &e); c; c = next234(&e))
for (i = 0; NULL != (c = index234(ssh_channels, i)); i++)
ssh2_try_send(c);
}
}

View File

@ -95,12 +95,12 @@ static void lz77_compress(struct LZ77Context *ctx,
#define INVALID -1 /* invalid hash _and_ invalid offset */
struct WindowEntry {
int next, prev; /* array indices within the window */
int hashval;
short next, prev; /* array indices within the window */
short hashval;
};
struct HashEntry {
int first; /* window index of first in chain */
short first; /* window index of first in chain */
};
struct Match {
@ -393,7 +393,8 @@ static const unsigned char mirrorbytes[256] = {
};
typedef struct {
int code, extrabits, min, max;
short code, extrabits;
int min, max;
} coderecord;
static const coderecord lencodes[] = {
@ -760,7 +761,7 @@ struct zlib_tableentry;
struct zlib_tableentry {
unsigned char nbits;
int code;
short code;
struct zlib_table *nexttable;
};

892
tree234.c

File diff suppressed because it is too large Load Diff

151
tree234.h
View File

@ -1,33 +1,44 @@
/*
* tree234.h: header defining functions in tree234.c.
*
* This file is copyright 1999-2001 Simon Tatham.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef TREE234_H
#define TREE234_H
/*
* These typedefs are notionally opaque outside tree234.c itself.
* This typedef is opaque outside tree234.c itself.
*/
typedef struct node234_Tag node234;
typedef struct tree234_Tag tree234;
typedef struct enum234_Tag enum234;
/*
* enum234 must be declared here because client code needs to be
* able to create automatic instances of it. This declaration does
* not constitute licence to use its internals outside tree234.c.
* The contents of this structure may change without notice. YOU
* HAVE BEEN WARNED.
*/
struct enum234_Tag {
node234 *node;
int posn;
};
typedef int (*cmpfn234)(void *, void *);
/*
* Create a 2-3-4 tree.
* Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and
* lookups by key will fail: you can only look things up by numeric
* index, and you have to use addpos234() and delpos234().
*/
tree234 *newtree234(cmpfn234 cmp);
@ -37,33 +48,113 @@ tree234 *newtree234(cmpfn234 cmp);
void freetree234(tree234 *t);
/*
* Add an element e to a 2-3-4 tree t. Returns e on success, or if
* an existing element compares equal, returns that.
* Add an element e to a sorted 2-3-4 tree t. Returns e on success,
* or if an existing element compares equal, returns that.
*/
void *add234(tree234 *t, void *e);
/*
* Find an element e in a 2-3-4 tree t. Returns NULL if not found.
* e is always passed as the first argument to cmp, so cmp can be
* an asymmetric function if desired. cmp can also be passed as
* NULL, in which case the compare function from the tree proper
* will be used.
* Add an element e to an unsorted 2-3-4 tree t. Returns e on
* success, NULL on failure. (Failure should only occur if the
* index is out of range or the tree is sorted.)
*
* Index range can be from 0 to the tree's current element count,
* inclusive.
*/
void *addpos234(tree234 *t, void *e, int index);
/*
* Look up the element at a given numeric index in a 2-3-4 tree.
* Returns NULL if the index is out of range.
*
* One obvious use for this function is in iterating over the whole
* of a tree (sorted or unsorted):
*
* for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);
*
* or
*
* int maxcount = count234(tree);
* for (i = 0; i < maxcount; i++) {
* p = index234(tree, i);
* assert(p != NULL);
* consume(p);
* }
*/
void *index234(tree234 *t, int index);
/*
* Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
* found. e is always passed as the first argument to cmp, so cmp
* can be an asymmetric function if desired. cmp can also be passed
* as NULL, in which case the compare function from the tree proper
* will be used.
*
* Three of these functions are special cases of findrelpos234. The
* non-`pos' variants lack the `index' parameter: if the parameter
* is present and non-NULL, it must point to an integer variable
* which will be filled with the numeric index of the returned
* element.
*
* The non-`rel' variants lack the `relation' parameter. This
* parameter allows you to specify what relation the element you
* provide has to the element you're looking for. This parameter
* can be:
*
* REL234_EQ - find only an element that compares equal to e
* REL234_LT - find the greatest element that compares < e
* REL234_LE - find the greatest element that compares <= e
* REL234_GT - find the smallest element that compares > e
* REL234_GE - find the smallest element that compares >= e
*
* Non-`rel' variants assume REL234_EQ.
*
* If `rel' is REL234_GT or REL234_LT, the `e' parameter may be
* NULL. In this case, REL234_GT will return the smallest element
* in the tree, and REL234_LT will return the greatest. This gives
* an alternative means of iterating over a sorted tree, instead of
* using index234:
*
* // to loop forwards
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)
* consume(p);
*
* // to loop backwards
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)
* consume(p);
*/
enum {
REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE
};
void *find234(tree234 *t, void *e, cmpfn234 cmp);
void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation);
void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index);
void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp, int relation,
int *index);
/*
* 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.
*
* delpos234 deletes the element at a particular tree index: it
* works on both sorted and unsorted trees.
*
* del234 deletes the element passed to it, so it only works on
* sorted trees. (It's equivalent to using findpos234 to determine
* the index of an element, and then passing that index to
* delpos234.)
*
* Both functions return a pointer to the element they delete, for
* the user to free or pass on elsewhere or whatever. If the index
* is out of range (delpos234) or the element is already not in the
* tree (del234) then they return NULL.
*/
void del234(tree234 *t, void *e);
void *del234(tree234 *t, void *e);
void *delpos234(tree234 *t, int index);
/*
* Iterate over the elements of a tree234, in order.
*
* enum234 e;
* for (p = first234(tree, &e); p; p = next234(&e)) consume(p);
* Return the total element count of a tree234.
*/
void *first234(tree234 *t, enum234 *e);
void *next234(enum234 *e);
int count234(tree234 *t);
#endif /* TREE234_H */

View File

@ -747,11 +747,13 @@ static char *sk_tcp_socket_error(Socket sock) {
/*
* For Plink: enumerate all sockets currently active.
*/
SOCKET first_socket(enum234 *e) {
Actual_Socket s = first234(sktree, e);
SOCKET first_socket(int *state) {
Actual_Socket s;
*state = 0;
s = index234(sktree, (*state)++);
return s ? s->s : INVALID_SOCKET;
}
SOCKET next_socket(enum234 *e) {
Actual_Socket s = next234(e);
SOCKET next_socket(int *state) {
Actual_Socket s = index234(sktree, (*state)++);
return s ? s->s : INVALID_SOCKET;
}