{
if (saves[pri].nsaved >= saves[pri].savesize) {
saves[pri].savesize = saves[pri].nsaved + 32;
- saves[pri].params =
- srealloc(saves[pri].params,
- saves[pri].savesize*sizeof(*saves[pri].params));
+ saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
+ struct cmdline_saved_param);
}
saves[pri].params[saves[pri].nsaved].p = p;
saves[pri].params[saves[pri].nsaved].value = value;
d = 0;
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
- command = srealloc(command, cmdsize);
+ command = sresize(command, cmdsize, char);
}
command[cmdlen++] = d;
} while (c != EOF);
charclass_handler, P(ccd));
ccd->listbox->listbox.multisel = 1;
ccd->listbox->listbox.ncols = 4;
- ccd->listbox->listbox.percentages = smalloc(4*sizeof(int));
+ ccd->listbox->listbox.percentages = snewn(4, int);
ccd->listbox->listbox.percentages[0] = 15;
ccd->listbox->listbox.percentages[1] = 25;
ccd->listbox->listbox.percentages[2] = 20;
environ_handler, P(ed));
ed->listbox->listbox.height = 3;
ed->listbox->listbox.ncols = 2;
- ed->listbox->listbox.percentages = smalloc(2*sizeof(int));
+ ed->listbox->listbox.percentages = snewn(2, int);
ed->listbox->listbox.percentages[0] = 30;
ed->listbox->listbox.percentages[1] = 70;
}
portfwd_handler, P(pfd));
pfd->listbox->listbox.height = 3;
pfd->listbox->listbox.ncols = 2;
- pfd->listbox->listbox.percentages = smalloc(2*sizeof(int));
+ pfd->listbox->listbox.percentages = snewn(2, int);
pfd->listbox->listbox.percentages[0] = 20;
pfd->listbox->listbox.percentages[1] = 80;
ctrl_tabdelay(s, pfd->rembutton);
struct controlbox *ctrl_new_box(void)
{
- struct controlbox *ret = smalloc(sizeof(struct controlbox));
+ struct controlbox *ret = snew(struct controlbox);
ret->nctrlsets = ret->ctrlsetsize = 0;
ret->ctrlsets = NULL;
char *path, char *title)
{
- struct controlset *s = smalloc(sizeof(struct controlset));
+ struct controlset *s = snew(struct controlset);
int index = ctrl_find_set(b, path, 1);
s->pathname = dupstr(path);
s->boxname = NULL;
s->ctrls = NULL;
if (b->nctrlsets >= b->ctrlsetsize) {
b->ctrlsetsize = b->nctrlsets + 32;
- b->ctrlsets = srealloc(b->ctrlsets,
- b->ctrlsetsize*sizeof(*b->ctrlsets));
+ b->ctrlsets = sresize(b->ctrlsets, b->ctrlsetsize,struct controlset *);
}
if (index < b->nctrlsets)
memmove(&b->ctrlsets[index+1], &b->ctrlsets[index],
return b->ctrlsets[index];
index++;
}
- s = smalloc(sizeof(struct controlset));
+ s = snew(struct controlset);
s->pathname = dupstr(path);
s->boxname = dupstr(name);
s->boxtitle = boxtitle ? dupstr(boxtitle) : NULL;
s->ctrls = NULL;
if (b->nctrlsets >= b->ctrlsetsize) {
b->ctrlsetsize = b->nctrlsets + 32;
- b->ctrlsets = srealloc(b->ctrlsets,
- b->ctrlsetsize*sizeof(*b->ctrlsets));
+ b->ctrlsets = sresize(b->ctrlsets, b->ctrlsetsize,struct controlset *);
}
if (index < b->nctrlsets)
memmove(&b->ctrlsets[index+1], &b->ctrlsets[index],
void *ctrl_alloc(struct controlbox *b, size_t size)
{
void *p;
+ /*
+ * This is an internal allocation routine, so it's allowed to
+ * use smalloc directly.
+ */
p = smalloc(size);
if (b->nfrees >= b->freesize) {
b->freesize = b->nfrees + 32;
- b->frees = srealloc(b->frees, b->freesize*sizeof(*b->frees));
+ b->frees = sresize(b->frees, b->freesize, void *);
}
b->frees[b->nfrees++] = p;
return p;
intorptr helpctx, handler_fn handler,
intorptr context)
{
- union control *c = smalloc(sizeof(union control));
+ union control *c = snew(union control);
if (s->ncontrols >= s->ctrlsize) {
s->ctrlsize = s->ncontrols + 32;
- s->ctrls = srealloc(s->ctrls, s->ctrlsize * sizeof(*s->ctrls));
+ s->ctrls = sresize(s->ctrls, s->ctrlsize, union control *);
}
s->ctrls[s->ncontrols++] = c;
/*
} else {
va_list ap;
int i;
- c->columns.percentages = smalloc(ncolumns * sizeof(int));
+ c->columns.percentages = snewn(ncolumns, int);
va_start(ap, ncolumns);
for (i = 0; i < ncolumns; i++)
c->columns.percentages[i] = va_arg(ap, int);
va_end(ap);
c->radio.nbuttons = i;
if (c->radio.shortcut == NO_SHORTCUT)
- c->radio.shortcuts = smalloc(c->radio.nbuttons * sizeof(char));
+ c->radio.shortcuts = snewn(c->radio.nbuttons, char);
else
c->radio.shortcuts = NULL;
- c->radio.buttons = smalloc(c->radio.nbuttons * sizeof(char *));
- c->radio.buttondata = smalloc(c->radio.nbuttons * sizeof(intorptr));
+ c->radio.buttons = snewn(c->radio.nbuttons, char *);
+ c->radio.buttondata = snewn(c->radio.nbuttons, intorptr);
/*
* Second pass along variable argument list to actually fill in
* the structure.
char base64_bit[4];
int base64_chars = 0;
- ret = smalloc(sizeof(*ret));
+ ret = snew(struct openssh_key);
ret->keyblob = NULL;
ret->keyblob_len = ret->keyblob_size = 0;
ret->encrypted = 0;
if (ret->keyblob_len + len > ret->keyblob_size) {
ret->keyblob_size = ret->keyblob_len + len + 256;
- ret->keyblob = srealloc(ret->keyblob, ret->keyblob_size);
+ ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
+ unsigned char);
}
memcpy(ret->keyblob + ret->keyblob_len, out, len);
* Space to create key blob in.
*/
blobsize = 256+key->keyblob_len;
- blob = smalloc(blobsize);
+ blob = snewn(blobsize, unsigned char);
PUT_32BIT(blob, 7);
if (key->type == OSSH_DSA)
memcpy(blob+4, "ssh-dss", 7);
* the sanity checks for free.
*/
assert(privptr > 0); /* should have bombed by now if not */
- retkey = smalloc(sizeof(struct ssh2_userkey));
+ retkey = snew(struct ssh2_userkey);
retkey->alg = (key->type == OSSH_RSA ? &ssh_rsa : &ssh_dss);
retkey->data = retkey->alg->createkey(blob, privptr,
blob+privptr, blobptr-privptr);
dmp1.bytes = (bignum_bitcount(bdmp1)+8)/8;
dmq1.bytes = (bignum_bitcount(bdmq1)+8)/8;
sparelen = dmp1.bytes + dmq1.bytes;
- spareblob = smalloc(sparelen);
+ spareblob = snewn(sparelen, unsigned char);
dmp1.start = spareblob;
dmq1.start = spareblob + dmp1.bytes;
for (i = 0; i < dmp1.bytes; i++)
/*
* Now we know how big outblob needs to be. Allocate it.
*/
- outblob = smalloc(outlen);
+ outblob = snewn(outlen, unsigned char);
/*
* And write the data into it.
char base64_bit[4];
int base64_chars = 0;
- ret = smalloc(sizeof(*ret));
+ ret = snew(struct sshcom_key);
ret->comment[0] = '\0';
ret->keyblob = NULL;
ret->keyblob_len = ret->keyblob_size = 0;
if (ret->keyblob_len + len > ret->keyblob_size) {
ret->keyblob_size = ret->keyblob_len + len + 256;
- ret->keyblob = srealloc(ret->keyblob, ret->keyblob_size);
+ ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
+ unsigned char);
}
memcpy(ret->keyblob + ret->keyblob_len, out, len);
* end up feeding them to alg->createkey().
*/
blobsize = cipherlen + 256;
- blob = smalloc(blobsize);
+ blob = snewn(blobsize, unsigned char);
privlen = 0;
if (type == RSA) {
struct mpint_pos n, e, d, u, p, q;
assert(privlen > 0); /* should have bombed by now if not */
- retkey = smalloc(sizeof(struct ssh2_userkey));
+ retkey = snew(struct ssh2_userkey);
retkey->alg = alg;
retkey->data = alg->createkey(blob, publen, blob+publen, privlen);
if (!retkey->data) {
outlen = 512;
for (i = 0; i < nnumbers; i++)
outlen += 4 + numbers[i].bytes;
- outblob = smalloc(outlen);
+ outblob = snewn(outlen, unsigned char);
/*
* Create the unencrypted key blob.
Backend *back, void *backhandle,
void *frontend)
{
- Ldisc ldisc = smalloc(sizeof(*ldisc));
+ Ldisc ldisc = snew(struct ldisc_tag);
ldisc->buf = NULL;
ldisc->buflen = 0;
default_case:
if (ldisc->buflen >= ldisc->bufsiz) {
ldisc->bufsiz = ldisc->buflen + 256;
- ldisc->buf = srealloc(ldisc->buf, ldisc->bufsiz);
+ ldisc->buf = sresize(ldisc->buf, ldisc->bufsiz, char);
}
ldisc->buf[ldisc->buflen++] = c;
if (ECHOING)
}
widesize = len * 2;
- widebuffer = smalloc(widesize * sizeof(wchar_t));
+ widebuffer = snewn(widesize, wchar_t);
wclen = mb_to_wc(codepage, 0, buf, len, widebuffer, widesize);
luni_send(ldisc, widebuffer, wclen, interactive);
char *p;
linesize = len * ratio * 2;
- linebuffer = smalloc(linesize * sizeof(wchar_t));
+ linebuffer = snewn(linesize, char);
if (in_utf(ldisc->term)) {
/* UTF is a simple algorithm */
void *log_init(void *frontend, Config *cfg)
{
- struct LogContext *ctx = smalloc(sizeof(struct LogContext));
+ struct LogContext *ctx = snew(struct LogContext);
ctx->lgfp = NULL;
ctx->frontend = frontend;
ctx->cfg = *cfg; /* STRUCTURE COPY */
char *dupstr(const char *s)
{
int len = strlen(s);
- char *p = smalloc(len + 1);
+ char *p = snewn(len + 1, char);
strcpy(p, s);
return p;
}
}
va_end(ap);
- p = smalloc(len + 1);
+ p = snewn(len + 1, char);
strcpy(p, s1);
q = p + strlen(p);
char *buf;
int len, size;
- buf = smalloc(512);
+ buf = snewn(512, char);
size = 512;
while (1) {
* buffer wasn't big enough, so we enlarge it a bit and hope. */
size += 512;
}
- buf = srealloc(buf, size);
+ buf = sresize(buf, size, char);
}
}
while (len > 0) {
int grainlen = min(len, BUFFER_GRANULE);
struct bufchain_granule *newbuf;
- newbuf = smalloc(sizeof(struct bufchain_granule));
+ newbuf = snew(struct bufchain_granule);
newbuf->bufpos = 0;
newbuf->buflen = grainlen;
memcpy(newbuf->buf, buf, grainlen);
return;
}
/* For our purposes we want the blob prefixed with its length */
- blob2 = smalloc(bloblen+4);
+ blob2 = snewn(bloblen+4, unsigned char);
PUT_32BIT(blob2, bloblen);
memcpy(blob2 + 4, blob, bloblen);
sfree(blob);
needs_pass = ssh2_userkey_encrypted(&filename, &comment);
attempts = 0;
if (type == SSH_KEYTYPE_SSH1)
- rkey = smalloc(sizeof(*rkey));
+ rkey = snew(struct RSAKey);
pps.passphrase = passphrase;
pps.comment = comment;
original_pass = 0;
ssh1_bignum_length(rkey->q) + 4 + clen /* comment */
;
- request = smalloc(reqlen);
+ request = snewn(reqlen, unsigned char);
request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
reqlen = 5;
4 + clen /* comment */
;
- request = smalloc(reqlen);
+ request = snewn(reqlen, unsigned char);
request[4] = SSH2_AGENTC_ADD_IDENTITY;
reqlen = 5;
}
/* Allocate the buffer. */
- p = ret = smalloc(len);
+ p = ret = snewn(len, unsigned char);
if (length) *length = len;
PUT_32BIT(p, nkeys);
}
/* Allocate the buffer. */
- p = ret = smalloc(len);
+ p = ret = snewn(len, unsigned char);
if (length) *length = len;
/*
if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER)
return NULL;
- ret = smalloc(resplen-5);
+ ret = snewn(resplen-5, unsigned char);
memcpy(ret, response+5, resplen-5);
sfree(response);
} else {
if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER)
return NULL;
- ret = smalloc(resplen-5);
+ ret = snewn(resplen-5, unsigned char);
memcpy(ret, response+5, resplen-5);
sfree(response);
} else {
struct RSAKey *key;
char *comment;
int commentlen;
- key = smalloc(sizeof(struct RSAKey));
+ key = snew(struct RSAKey);
memset(key, 0, sizeof(struct RSAKey));
p += makekey(p, key, NULL, 1);
p += makeprivate(p, key);
p += ssh1_read_bignum(p, &key->p); /* p */
p += ssh1_read_bignum(p, &key->q); /* q */
commentlen = GET_32BIT(p);
- comment = smalloc(commentlen+1);
+ comment = snewn(commentlen+1, char);
if (comment) {
memcpy(comment, p + 4, commentlen);
comment[commentlen] = '\0';
int alglen, commlen;
int bloblen;
- key = smalloc(sizeof(struct ssh2_userkey));
+ key = snew(struct ssh2_userkey);
alglen = GET_32BIT(p);
p += 4;
commlen = GET_32BIT(p);
p += 4;
- comment = smalloc(commlen + 1);
+ comment = snewn(commlen + 1, char);
if (comment) {
memcpy(comment, p, commlen);
comment[commlen] = '\0';
{
OPENFILENAME of;
char filename[FILENAME_MAX];
- char *filelist = smalloc(8192);
+ char *filelist = snewn(8192, char);
char *filewalker;
int n, dirlen;
}
/* get item indices in an array */
- selectedArray = smalloc(numSelected * sizeof(int));
+ selectedArray = snewn(numSelected, int);
SendDlgItemMessage(hwnd, 100, LB_GETSELITEMS,
numSelected, (WPARAM)selectedArray);
if (id > 0) {
retlen = 4 + GET_32BIT(p);
debug(("len is %d\n", retlen));
- ret = smalloc(retlen);
+ ret = snewn(retlen, unsigned char);
if (ret) {
memcpy(ret, p, retlen);
*out = ret;
while (*p) {
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
- command = srealloc(command, cmdsize);
+ command = sresize(command, cmdsize, char);
}
command[cmdlen++]=*p++;
}
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
- command = srealloc(command, cmdsize);
+ command = sresize(command, cmdsize, char);
}
command[cmdlen++]=' '; /* always add trailing space */
if (--argc) p = *++argv;
/* Expand the buffer if necessary. */
if (i > sksize) {
sksize = i + 16;
- sklist = srealloc(sklist, sksize * sizeof(*sklist));
+ sklist = sresize(sklist, sksize, SOCKET);
}
/* Retrieve the sockets into sklist. */
/*
* Open socket.
*/
- pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+ pr = snew(struct PFwdPrivate);
pr->fn = &fn_table;
pr->throttled = pr->throttle_override = 0;
pr->ready = 1;
char *err;
org = (struct PFwdPrivate *)p;
- pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+ pr = snew(struct PFwdPrivate);
pr->fn = &fn_table;
pr->c = NULL;
/*
* Open socket.
*/
- pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
+ pr = snew(struct PFwdPrivate);
pr->fn = &fn_table;
pr->c = NULL;
strcpy(pr->hostname, desthost);
{
DWORD needed, nprinters;
- buffer = srealloc(buffer, offset+512);
+ buffer = sresize(buffer, offset+512, char);
/*
* Exploratory call to EnumPrinters to determine how much space
if (needed < 512)
needed = 512;
- buffer = srealloc(buffer, offset+needed);
+ buffer = sresize(buffer, offset+needed, char);
if (EnumPrinters(param, NULL, ENUM_LEVEL, buffer+offset,
needed, &needed, &nprinters) == 0)
printer_enum *printer_start_enum(int *nprinters_ptr)
{
- printer_enum *ret = smalloc(sizeof(printer_enum));
+ printer_enum *ret = snew(printer_enum);
char *buffer = NULL, *retval;
*nprinters_ptr = 0; /* default return value */
- buffer = smalloc(512);
+ buffer = snewn(512, char);
retval = printer_add_enum(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
buffer, 0, nprinters_ptr);
printer_job *printer_start_job(char *printer)
{
- printer_job *ret = smalloc(sizeof(printer_job));
+ printer_job *ret = snew(printer_job);
DOC_INFO_1 docinfo;
int jobstarted = 0, pagestarted = 0;
SockAddr proxy_addr;
char *proxy_canonical_name, *err;
- ret = smalloc(sizeof(struct Socket_proxy_tag));
+ ret = snew(struct Socket_proxy_tag);
ret->fn = &socket_fn_table;
ret->cfg = *cfg; /* STRUCTURE COPY */
ret->plug = plug;
/* create the proxy plug to map calls from the actual
* socket into our proxy socket layer */
- pplug = smalloc(sizeof(struct Plug_proxy_tag));
+ pplug = snew(struct Plug_proxy_tag);
pplug->fn = &plug_fn_table;
pplug->proxy_socket = ret;
/* get the status line */
len = bufchain_size(&p->pending_input_data);
assert(len > 0); /* or we wouldn't be here */
- data = smalloc(len);
+ data = snewn(len, char);
bufchain_fetch(&p->pending_input_data, data, len);
eol = get_line_end(data, len);
len = bufchain_size(&p->pending_input_data);
assert(len > 0); /* or we wouldn't be here */
- data = smalloc(len);
+ data = snewn(len, char);
datap = data;
bufchain_fetch(&p->pending_input_data, data, len);
}
length = strlen(p->cfg.proxy_username) + namelen + 9;
- command = (char*) smalloc(length);
+ command = snewn(length, char);
strcpy(command + 8, p->cfg.proxy_username);
command[0] = 4; /* version 4 */
if (nnames + names->nnames >= namesize) {
namesize += names->nnames + 128;
- ournames =
- srealloc(ournames, namesize * sizeof(*ournames));
+ ournames = sresize(ournames, namesize, struct fxp_name *);
}
for (i = 0; i < names->nnames; i++)
return 0;
}
- currdir = smalloc(256);
+ currdir = snewn(256, char);
len = GetCurrentDirectory(256, currdir);
if (len > 256)
- currdir = srealloc(currdir, len);
+ currdir = sresize(currdir, len, char);
GetCurrentDirectory(len, currdir);
printf("New local directory is %s\n", currdir);
sfree(currdir);
char *currdir;
int len;
- currdir = smalloc(256);
+ currdir = snewn(256, char);
len = GetCurrentDirectory(256, currdir);
if (len > 256)
- currdir = srealloc(currdir, len);
+ currdir = sresize(currdir, len, char);
GetCurrentDirectory(len, currdir);
printf("Current local directory is %s\n", currdir);
sfree(currdir);
}
fflush(stdout);
- cmd = smalloc(sizeof(struct sftp_command));
+ cmd = snew(struct sftp_command);
cmd->words = NULL;
cmd->nwords = 0;
cmd->wordssize = 0;
char *ret;
linesize += 512;
- line = srealloc(line, linesize);
+ line = sresize(line, linesize, char);
ret = fgets(line + linelen, linesize - linelen, fp);
if (!ret || (linelen == 0 && line[0] == '\0')) {
* containing everything else on the line.
*/
cmd->nwords = cmd->wordssize = 2;
- cmd->words = srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+ cmd->words = sresize(cmd->words, cmd->wordssize, char *);
cmd->words[0] = "!";
cmd->words[1] = p+1;
} else {
*r = '\0';
if (cmd->nwords >= cmd->wordssize) {
cmd->wordssize = cmd->nwords + 16;
- cmd->words =
- srealloc(cmd->words, cmd->wordssize * sizeof(char *));
+ cmd->words = sresize(cmd->words, cmd->wordssize, char *);
}
cmd->words[cmd->nwords++] = q;
}
if (len > 0) {
if (pendsize < pendlen + len) {
pendsize = pendlen + len + 4096;
- pending = (pending ? srealloc(pending, pendsize) :
- smalloc(pendsize));
- if (!pending)
- fatalbox("Out of memory");
+ pending = sresize(pending, pendsize, unsigned char);
}
memcpy(pending + pendlen, p, len);
pendlen += len;
int i;
pub_blob = key->alg->public_blob(key->data, &pub_len);
- buffer = smalloc(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
- strlen(key->comment) + 3);
+ buffer = snewn(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
+ strlen(key->comment) + 3, char);
strcpy(buffer, key->alg->name);
p = buffer + strlen(buffer);
*p++ = ' ';
SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
(LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(200)));
- state = smalloc(sizeof(*state));
+ state = snew(struct MainDlgState);
state->generation_thread_exists = FALSE;
state->collecting_entropy = FALSE;
state->entropy = NULL;
MAKELPARAM(0, PROGRESSRANGE));
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
- params = smalloc(sizeof(*params));
+ params = snew(struct rsa_key_thread_params);
params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
params->dialog = hwnd;
params->keysize = state->keysize;
int len = GetWindowTextLength(editctl);
if (*state->commentptr)
sfree(*state->commentptr);
- *state->commentptr = smalloc(len + 1);
+ *state->commentptr = snewn(len + 1, char);
GetWindowText(editctl, *state->commentptr, len + 1);
if (state->ssh2) {
setupbigedit2(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
state->entropy_required = (state->keysize / 2) * 2;
state->entropy_got = 0;
state->entropy_size = (state->entropy_required *
- sizeof(*state->entropy));
- state->entropy = smalloc(state->entropy_size);
+ sizeof(unsigned));
+ state->entropy = snewn(state->entropy_required, unsigned);
SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
MAKELPARAM(0, state->entropy_required));
* the user will immediately want to change it, which is
* what we want :-)
*/
- *state->commentptr = smalloc(30);
+ *state->commentptr = snewn(30, char);
{
time_t t;
struct tm *tm;
void *saferealloc(void *, size_t);
void safefree(void *);
-
-/* smalloc a thing */
-#define smalloca(type) ((type *) smalloc (sizeof (type)))
-/* smalloc a copy of a thing */
-#define smallocc(ptr) memcpy (smalloc (sizeof (*ptr)), ptr, sizeof (*ptr))
-/* smalloc n things */
-#define smallocn(n,type) ((type *) smalloc ((n) * sizeof (type)))
-
+/*
+ * Direct use of smalloc within the code should be avoided where
+ * possible, in favour of these type-casting macros which ensure
+ * you don't mistakenly allocate enough space for one sort of
+ * structure and assign it to a different sort of pointer.
+ */
+#define snew(type) ((type *)smalloc(sizeof(type)))
+#define snewn(n, type) ((type *)smalloc((n)*sizeof(type)))
+#define sresize(ptr, n, type) ((type *)srealloc(ptr, (n)*sizeof(type)))
#endif
char *err;
Raw raw;
- raw = smalloc(sizeof(*raw));
+ raw = snew(struct raw_backend_data);
raw->fn = &fn_table;
raw->s = NULL;
*backend_handle = raw;
char *err;
Rlogin rlogin;
- rlogin = smalloc(sizeof(*rlogin));
+ rlogin = snew(struct rlogin_tag);
rlogin->fn = &fn_table;
rlogin->s = NULL;
rlogin->frontend = frontend_handle;
if (len > 0) {
if (pendsize < pendlen + len) {
pendsize = pendlen + len + 4096;
- pending = (pending ? srealloc(pending, pendsize) :
- smalloc(pendsize));
+ pending = sresize(pending, pendsize, unsigned char);
if (!pending)
fatalbox("Out of memory");
}
namelen = 0;
if (GetUserName(user, &namelen) == FALSE)
bump("Empty user name");
- user = smalloc(namelen * sizeof(char));
+ user = snewn(namelen, char);
GetUserName(user, &namelen);
if (verbose)
tell_user(stderr, "Guessing user name: %s", user);
if (nnames + names->nnames >= namesize) {
namesize += names->nnames + 128;
- ournames =
- srealloc(ournames, namesize * sizeof(*ournames));
+ ournames = sresize(ournames, namesize, struct fxp_name);
}
for (i = 0; i < names->nnames; i++)
* wildcardness comes before the final slash) and arrange
* things so that a dirstack entry will be set up.
*/
- newsource = smalloc(1+strlen(source));
+ newsource = snewn(1+strlen(source), char);
if (!wc_unescape(newsource, source)) {
/* Yes, here we go; it's a wildcard. Bah. */
char *dupsource, *lastpart, *dirpart, *wildcard;
* wildcard escapes from the directory part, throwing
* an error if it contains a real wildcard.
*/
- dirpart = smalloc(1+strlen(dupsource));
+ dirpart = snewn(1+strlen(dupsource), char);
if (!wc_unescape(dirpart, dupsource)) {
tell_user(stderr, "%s: multiple-level wildcards unsupported",
source);
}
if (nnames + names->nnames >= namesize) {
namesize += names->nnames + 128;
- ournames =
- srealloc(ournames, namesize * sizeof(*ournames));
+ ournames = sresize(ournames, namesize, struct fxp_name);
}
for (i = 0; i < names->nnames; i++)
ournames[nnames++] = names->names[i];
}
fxp_close(dirhandle);
- newitem = smalloc(sizeof(struct scp_sftp_dirstack));
+ newitem = snew(struct scp_sftp_dirstack);
newitem->next = scp_sftp_dirstack_head;
newitem->names = ournames;
newitem->namepos = 0;
bump("Lost connection");
if (i >= bufsize) {
bufsize = i + 128;
- act->buf = srealloc(act->buf, bufsize);
+ act->buf = sresize(act->buf, bufsize, char);
}
act->buf[i++] = ch;
} while (ch != '\n');
user = NULL;
}
- cmd = smalloc(4 * strlen(src) + 100);
+ cmd = snewn(4 * strlen(src) + 100, char);
strcpy(cmd, "ls -la '");
p = cmd + strlen(cmd);
for (q = src; *q; q++) {
int len = strlen(otherbuf) + 1;
if (bufsize < buflen + len) {
bufsize = buflen + len + 2048;
- list->buffer = srealloc(list->buffer, bufsize);
+ list->buffer = sresize(list->buffer, bufsize, char);
}
strcpy(list->buffer + buflen, otherbuf);
buflen += strlen(list->buffer + buflen) + 1;
} while (ret);
enum_settings_finish(handle);
}
- list->buffer = srealloc(list->buffer, buflen + 1);
+ list->buffer = sresize(list->buffer, buflen + 1, char);
list->buffer[buflen] = '\0';
/*
p++;
}
- list->sessions = smalloc((list->nsessions + 1) * sizeof(char *));
+ list->sessions = snewn(list->nsessions + 1, char *);
list->sessions[0] = "Default Settings";
p = list->buffer;
i = 1;
{
if (pkt->maxlen < length) {
pkt->maxlen = length + 256;
- pkt->data = srealloc(pkt->data, pkt->maxlen);
+ pkt->data = sresize(pkt->data, pkt->maxlen, char);
}
}
static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
static struct sftp_packet *sftp_pkt_init(int pkt_type)
{
struct sftp_packet *pkt;
- pkt = smalloc(sizeof(struct sftp_packet));
+ pkt = snew(struct sftp_packet);
pkt->data = NULL;
pkt->savedpos = -1;
pkt->length = 0;
if (!sftp_recvdata(x, 4))
return NULL;
- pkt = smalloc(sizeof(struct sftp_packet));
+ pkt = snew(struct sftp_packet);
pkt->savedpos = 0;
pkt->length = pkt->maxlen = GET_32BIT(x);
- pkt->data = smalloc(pkt->length);
+ pkt->data = snewn(pkt->length, char);
if (!sftp_recvdata(pkt->data, pkt->length)) {
sftp_pkt_free(pkt);
static char *mkstr(char *s, int len)
{
- char *p = smalloc(len + 1);
+ char *p = snewn(len + 1, char);
memcpy(p, s, len);
p[len] = '\0';
return p;
sftp_pkt_free(pktin);
return NULL;
}
- handle = smalloc(sizeof(struct fxp_handle));
+ handle = snew(struct fxp_handle);
handle->hstring = mkstr(hstring, len);
handle->hlen = len;
sftp_pkt_free(pktin);
sftp_pkt_free(pktin);
return NULL;
}
- handle = smalloc(sizeof(struct fxp_handle));
+ handle = snew(struct fxp_handle);
handle->hstring = mkstr(hstring, len);
handle->hlen = len;
sftp_pkt_free(pktin);
if (pktin->type == SSH_FXP_NAME) {
struct fxp_names *ret;
int i;
- ret = smalloc(sizeof(struct fxp_names));
+ ret = snew(struct fxp_names);
ret->nnames = sftp_pkt_getuint32(pktin);
- ret->names = smalloc(ret->nnames * sizeof(struct fxp_name));
+ ret->names = snewn(ret->nnames, struct fxp_name);
for (i = 0; i < ret->nnames; i++) {
char *str;
int len;
struct fxp_name *fxp_dup_name(struct fxp_name *name)
{
struct fxp_name *ret;
- ret = smalloc(sizeof(struct fxp_name));
+ ret = snew(struct fxp_name);
ret->filename = dupstr(name->filename);
ret->longname = dupstr(name->longname);
ret->attrs = name->attrs; /* structure copy */
Rectangle(hdc, cr.left, cr.top, cr.right, cr.bottom);
wtlen = GetWindowTextLength(hWnd);
- wt = (LPTSTR) smalloc((wtlen + 1) * sizeof(TCHAR));
+ wt = (LPTSTR) snewn(wtlen + 1, TCHAR);
GetWindowText(hWnd, wt, wtlen + 1);
SetTextColor(hdc, tip_text);
#define crBegin(v) { int *crLine = &v; switch(v) { case 0:;
#define crState(t) \
struct t *s; \
- if (!ssh->t) ssh->t = smalloc(sizeof(struct t)); \
+ if (!ssh->t) ssh->t = snew(struct t); \
s = ssh->t;
#define crFinish(z) } *crLine = 0; return (z); }
#define crFinishV } *crLine = 0; return; }
if (ssh->pktin.maxlen < st->biglen) {
ssh->pktin.maxlen = st->biglen;
- ssh->pktin.data = srealloc(ssh->pktin.data, st->biglen + APIEXTRA);
+ ssh->pktin.data = sresize(ssh->pktin.data, st->biglen + APIEXTRA,
+ unsigned char);
}
st->to_read = st->biglen;
if (ssh->pktin.maxlen < st->pad + decomplen) {
ssh->pktin.maxlen = st->pad + decomplen;
- ssh->pktin.data = srealloc(ssh->pktin.data,
- ssh->pktin.maxlen + APIEXTRA);
+ ssh->pktin.data = sresize(ssh->pktin.data,
+ ssh->pktin.maxlen + APIEXTRA,
+ unsigned char);
ssh->pktin.body = ssh->pktin.data + st->pad + 1;
}
if (ssh->pktin.maxlen < st->cipherblk) {
ssh->pktin.maxlen = st->cipherblk;
- ssh->pktin.data = srealloc(ssh->pktin.data, st->cipherblk + APIEXTRA);
+ ssh->pktin.data = sresize(ssh->pktin.data, st->cipherblk + APIEXTRA,
+ unsigned char);
}
/*
*/
if (ssh->pktin.maxlen < st->packetlen + st->maclen) {
ssh->pktin.maxlen = st->packetlen + st->maclen;
- ssh->pktin.data = srealloc(ssh->pktin.data,
- ssh->pktin.maxlen + APIEXTRA);
+ ssh->pktin.data = sresize(ssh->pktin.data,
+ ssh->pktin.maxlen + APIEXTRA,
+ unsigned char);
}
/*
&newpayload, &newlen)) {
if (ssh->pktin.maxlen < newlen + 5) {
ssh->pktin.maxlen = newlen + 5;
- ssh->pktin.data = srealloc(ssh->pktin.data,
- ssh->pktin.maxlen + APIEXTRA);
+ ssh->pktin.data = sresize(ssh->pktin.data,
+ ssh->pktin.maxlen + APIEXTRA,
+ unsigned char);
}
ssh->pktin.length = 5 + newlen;
memcpy(ssh->pktin.data + 5, newpayload, newlen);
#ifdef MSCRYPTOAPI
/* Allocate enough buffer space for extra block
* for MS CryptEncrypt() */
- ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 12);
+ ssh->pktout.data = sresize(ssh->pktout.data, biglen + 12,
+ unsigned char);
#else
- ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 4);
+ ssh->pktout.data = sresize(ssh->pktout.data, biglen + 4,
+ unsigned char);
#endif
}
ssh->pktout.body = ssh->pktout.data + 4 + pad + 1;
len = s_wrpkt_prepare(ssh);
if (ssh->deferred_len + len > ssh->deferred_size) {
ssh->deferred_size = ssh->deferred_len + len + 128;
- ssh->deferred_send_data = srealloc(ssh->deferred_send_data,
- ssh->deferred_size);
+ ssh->deferred_send_data = sresize(ssh->deferred_send_data,
+ ssh->deferred_size,
+ unsigned char);
}
memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len);
ssh->deferred_len += len;
{
if (ssh->pktout.maxlen < length) {
ssh->pktout.maxlen = length + 256;
- ssh->pktout.data = srealloc(ssh->pktout.data,
- ssh->pktout.maxlen + APIEXTRA);
+ ssh->pktout.data = sresize(ssh->pktout.data,
+ ssh->pktout.maxlen + APIEXTRA,
+ unsigned char);
if (!ssh->pktout.data)
fatalbox("Out of memory");
}
{
unsigned char *p;
int i, n = (bignum_bitcount(b) + 7) / 8;
- p = smalloc(n + 1);
+ p = snewn(n + 1, unsigned char);
if (!p)
fatalbox("out of memory");
p[0] = 0;
int len = ssh2_pkt_construct(ssh);
if (ssh->deferred_len + len > ssh->deferred_size) {
ssh->deferred_size = ssh->deferred_len + len + 128;
- ssh->deferred_send_data = srealloc(ssh->deferred_send_data,
- ssh->deferred_size);
+ ssh->deferred_send_data = sresize(ssh->deferred_send_data,
+ ssh->deferred_size,
+ unsigned char);
}
memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len);
ssh->deferred_len += len;
}
s->vstrsize = 16;
- s->vstring = smalloc(s->vstrsize);
+ s->vstring = snewn(s->vstrsize, char);
strcpy(s->vstring, "SSH-");
s->vslen = 4;
s->i = 0;
crReturn(1); /* get another char */
if (s->vslen >= s->vstrsize - 1) {
s->vstrsize += 16;
- s->vstring = srealloc(s->vstring, s->vstrsize);
+ s->vstring = sresize(s->vstring, s->vstrsize, char);
}
s->vstring[s->vslen++] = c;
if (s->i >= 0) {
s->vstring[strcspn(s->vstring, "\r\n")] = '\0';/* remove EOL chars */
{
char *vlog;
- vlog = smalloc(20 + s->vslen);
+ vlog = snewn(20 + s->vslen, char);
sprintf(vlog, "Server version: %s", s->vstring);
logevent(vlog);
sfree(vlog);
SockAddr addr;
char *err;
- ssh->savedhost = smalloc(1 + strlen(host));
+ ssh->savedhost = snewn(1 + strlen(host), char);
if (!ssh->savedhost)
fatalbox("Out of memory");
strcpy(ssh->savedhost, host);
s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
- s->rsabuf = smalloc(s->len);
+ s->rsabuf = snewn(s->len, unsigned char);
if (!s->rsabuf)
fatalbox("Out of memory");
*/
int len = rsastr_len(&hostkey);
char fingerprint[100];
- char *keystr = smalloc(len);
+ char *keystr = snewn(len, char);
if (!keystr)
fatalbox("Out of memory");
rsastr_fmt(keystr, &hostkey);
len += ssh1_bignum_length(s->challenge);
len += 16; /* session id */
len += 4; /* response format */
- agentreq = smalloc(4 + len);
+ agentreq = snewn(4 + len, char);
PUT_32BIT(agentreq, len);
q = agentreq + 4;
*q++ = SSH1_AGENTC_RSA_CHALLENGE;
assert(pwlen >= bottom && pwlen <= top);
- randomstr = smalloc(top + 1);
+ randomstr = snewn(top + 1, char);
for (i = bottom; i <= top; i++) {
if (i == pwlen)
dserv, "(", dport, dserv, ")");
} else {
struct ssh_rportfwd *pf;
- pf = smalloc(sizeof(*pf));
+ pf = snew(struct ssh_rportfwd);
strcpy(pf->dhost, host);
pf->dport = dport;
if (saddr) {
PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END);
logevent("Rejected X11 connect request");
} else {
- c = smalloc(sizeof(struct ssh_channel));
+ c = snew(struct ssh_channel);
c->ssh = ssh;
if (x11_init(&c->u.x11.s, ssh->cfg.x11_display, c,
send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END);
} else {
- c = smalloc(sizeof(struct ssh_channel));
+ c = snew(struct ssh_channel);
c->ssh = ssh;
c->remoteid = GET_32BIT(ssh->pktin.body);
c->localid = alloc_channel_id(ssh);
int hostsize, port;
char host[256], buf[1024];
char *p, *h, *e;
- c = smalloc(sizeof(struct ssh_channel));
+ c = snew(struct ssh_channel);
c->ssh = ssh;
hostsize = GET_32BIT(ssh->pktin.body+4);
if (c->u.a.lensofar == 4) {
c->u.a.totallen =
4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = smalloc(c->u.a.totallen);
+ c->u.a.message = snewn(c->u.a.totallen,
+ unsigned char);
memcpy(c->u.a.message, c->u.a.msglen, 4);
}
if (c->u.a.lensofar >= 4 && len > 0) {
s->len += 4 + s->pklen; /* key blob */
s->len += 4 + s->siglen; /* data to sign */
s->len += 4; /* flags */
- s->agentreq = smalloc(4 + s->len);
+ s->agentreq = snewn(4 + s->len, char);
PUT_32BIT(s->agentreq, s->len);
s->q = s->agentreq + 4;
*s->q++ = SSH2_AGENTC_SIGN_REQUEST;
sigdata_len = ssh->pktout.length - 5 + 4 + 20;
if (ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)
sigdata_len -= 4;
- sigdata = smalloc(sigdata_len);
+ sigdata = snewn(sigdata_len, char);
p = 0;
if (!(ssh->remote_bugs & BUG_SSH2_PK_SESSIONID)) {
PUT_32BIT(sigdata+p, 20);
* So now create a channel with a session in it.
*/
ssh->channels = newtree234(ssh_channelcmp);
- ssh->mainchan = smalloc(sizeof(struct ssh_channel));
+ ssh->mainchan = snew(struct ssh_channel);
ssh->mainchan->ssh = ssh;
ssh->mainchan->localid = alloc_channel_id(ssh);
ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN);
dserv, "(", dport, dserv, ")");
} else {
struct ssh_rportfwd *pf;
- pf = smalloc(sizeof(*pf));
+ pf = snew(struct ssh_rportfwd);
strcpy(pf->dhost, host);
pf->dport = dport;
pf->sport = sport;
if (c->u.a.lensofar == 4) {
c->u.a.totallen =
4 + GET_32BIT(c->u.a.msglen);
- c->u.a.message = smalloc(c->u.a.totallen);
+ c->u.a.message = snewn(c->u.a.totallen,
+ unsigned char);
memcpy(c->u.a.message, c->u.a.msglen, 4);
}
if (c->u.a.lensofar >= 4 && length > 0) {
struct ssh_channel *c;
unsigned remid, winsize, pktsize;
ssh2_pkt_getstring(ssh, &type, &typelen);
- c = smalloc(sizeof(struct ssh_channel));
+ c = snew(struct ssh_channel);
c->ssh = ssh;
remid = ssh2_pkt_getuint32(ssh);
port = ssh2_pkt_getuint32(ssh);
if (typelen == 3 && !memcmp(type, "x11", 3)) {
- char *addrstr = smalloc(peeraddrlen+1);
+ char *addrstr = snewn(peeraddrlen+1, char);
memcpy(addrstr, peeraddr, peeraddrlen);
peeraddr[peeraddrlen] = '\0';
char *p;
Ssh ssh;
- ssh = smalloc(sizeof(*ssh));
+ ssh = snew(struct ssh_tag);
ssh->cfg = *cfg; /* STRUCTURE COPY */
ssh->s = NULL;
ssh->cipher = NULL;
{
Ssh ssh = (Ssh) handle;
struct ssh_channel *c;
- c = smalloc(sizeof(struct ssh_channel));
+ c = snew(struct ssh_channel);
c->ssh = ssh;
if (c) {
static void *aes_make_context(void)
{
- return smalloc(sizeof(AESContext));
+ return snew(AESContext);
}
static void aes_free_context(void *handle)
static void *blowfish_make_context(void)
{
- return smalloc(sizeof(BlowfishContext));
+ return snew(BlowfishContext);
}
static void *blowfish_ssh1_make_context(void)
{
/* In SSH1, need one key for each direction */
- return smalloc(2*sizeof(BlowfishContext));
+ return snewn(2, BlowfishContext);
}
static void blowfish_free_context(void *handle)
static Bignum newbn(int length)
{
- Bignum b = smalloc((length + 1) * sizeof(unsigned short));
+ Bignum b = snewn(length + 1, unsigned short);
if (!b)
abort(); /* FIXME */
memset(b, 0, (length + 1) * sizeof(*b));
Bignum copybn(Bignum orig)
{
- Bignum b = smalloc((orig[0] + 1) * sizeof(unsigned short));
+ Bignum b = snewn(orig[0] + 1, unsigned short);
if (!b)
abort(); /* FIXME */
memcpy(b, orig, (orig[0] + 1) * sizeof(*b));
/* Allocate m of size mlen, copy mod to m */
/* We use big endian internally */
mlen = mod[0];
- m = smalloc(mlen * sizeof(unsigned short));
+ m = snewn(mlen, unsigned short);
for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
}
/* Allocate n of size mlen, copy base to n */
- n = smalloc(mlen * sizeof(unsigned short));
+ n = snewn(mlen, unsigned short);
i = mlen - base[0];
for (j = 0; j < i; j++)
n[j] = 0;
n[i + j] = base[base[0] - j];
/* Allocate a and b of size 2*mlen. Set a = 1 */
- a = smalloc(2 * mlen * sizeof(unsigned short));
- b = smalloc(2 * mlen * sizeof(unsigned short));
+ a = snewn(2 * mlen, unsigned short);
+ b = snewn(2 * mlen, unsigned short);
for (i = 0; i < 2 * mlen; i++)
a[i] = 0;
a[2 * mlen - 1] = 1;
/* Allocate m of size mlen, copy mod to m */
/* We use big endian internally */
mlen = mod[0];
- m = smalloc(mlen * sizeof(unsigned short));
+ m = snewn(mlen, unsigned short);
for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
pqlen = (p[0] > q[0] ? p[0] : q[0]);
/* Allocate n of size pqlen, copy p to n */
- n = smalloc(pqlen * sizeof(unsigned short));
+ n = snewn(pqlen, unsigned short);
i = pqlen - p[0];
for (j = 0; j < i; j++)
n[j] = 0;
n[i + j] = p[p[0] - j];
/* Allocate o of size pqlen, copy q to o */
- o = smalloc(pqlen * sizeof(unsigned short));
+ o = snewn(pqlen, unsigned short);
i = pqlen - q[0];
for (j = 0; j < i; j++)
o[j] = 0;
o[i + j] = q[q[0] - j];
/* Allocate a of size 2*pqlen for result */
- a = smalloc(2 * pqlen * sizeof(unsigned short));
+ a = snewn(2 * pqlen, unsigned short);
/* Main computation */
internal_mul(n, o, a, pqlen);
/* Allocate m of size mlen, copy mod to m */
/* We use big endian internally */
mlen = mod[0];
- m = smalloc(mlen * sizeof(unsigned short));
+ m = snewn(mlen, unsigned short);
for (j = 0; j < mlen; j++)
m[j] = mod[mod[0] - j];
plen = mlen + 1;
/* Allocate n of size plen, copy p to n */
- n = smalloc(plen * sizeof(unsigned short));
+ n = snewn(plen, unsigned short);
for (j = 0; j < plen; j++)
n[j] = 0;
for (j = 1; j <= p[0]; j++)
Bignum ret;
/* mlen space for a, mlen space for b, 2*mlen for result */
- workspace = smalloc(mlen * 4 * sizeof(unsigned short));
+ workspace = snewn(mlen * 4, unsigned short);
for (i = 0; i < mlen; i++) {
workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0);
workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0);
i = bignum_bitcount(x);
ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */
ndigits++; /* allow for trailing \0 */
- ret = smalloc(ndigits);
+ ret = snewn(ndigits, char);
/*
* Now allocate some workspace to hold the binary form as we
* repeatedly divide it by ten. Initialise this to the
* big-endian form of the number.
*/
- workspace = smalloc(sizeof(unsigned short) * x[0]);
+ workspace = snewn(x[0], unsigned short);
for (i = 0; i < x[0]; i++)
workspace[i] = x[x[0] - i];
void *crcda_make_context(void)
{
- struct crcda_ctx *ret = smalloc(sizeof(struct crcda_ctx));
+ struct crcda_ctx *ret = snew(struct crcda_ctx);
ret->h = NULL;
ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
return ret;
if (ctx->h == NULL) {
ctx->n = l;
- ctx->h = (uint16 *) smalloc(ctx->n * HASH_ENTRYSIZE);
+ ctx->h = snewn(ctx->n, uint16);
} else {
if (l > ctx->n) {
ctx->n = l;
- ctx->h = (uint16 *) srealloc(ctx->h, ctx->n * HASH_ENTRYSIZE);
+ ctx->h = sresize(ctx->h, ctx->n, uint16);
}
}
static void *des3_make_context(void)
{
- return smalloc(3*sizeof(DESContext));
+ return snewn(3, DESContext);
}
static void *des3_ssh1_make_context(void)
{
/* Need 3 keys for each direction, in SSH1 */
- return smalloc(6*sizeof(DESContext));
+ return snewn(6, DESContext);
}
static void *des_make_context(void)
{
- return smalloc(sizeof(DESContext));
+ return snew(DESContext);
}
static void *des_ssh1_make_context(void)
{
/* Need one key for each direction, in SSH1 */
- return smalloc(2*sizeof(DESContext));
+ return snewn(2, DESContext);
}
static void des3_free_context(void *handle) /* used for both 3DES and DES */
*/
void *dh_setup_group1(void)
{
- struct dh_ctx *ctx = smalloc(sizeof(struct dh_ctx));
+ struct dh_ctx *ctx = snew(struct dh_ctx);
ctx->p = bignum_from_bytes(P, sizeof(P));
ctx->g = bignum_from_bytes(G, sizeof(G));
dh_init(ctx);
*/
void *dh_setup_group(Bignum pval, Bignum gval)
{
- struct dh_ctx *ctx = smalloc(sizeof(struct dh_ctx));
+ struct dh_ctx *ctx = snew(struct dh_ctx);
ctx->p = copybn(pval);
ctx->g = copybn(gval);
dh_init(ctx);
unsigned char *buf;
nbytes = ssh1_bignum_length(ctx->qmask);
- buf = smalloc(nbytes);
+ buf = snewn(nbytes, unsigned char);
do {
/*
int slen;
struct dss_key *dss;
- dss = smalloc(sizeof(struct dss_key));
+ dss = snew(struct dss_key);
if (!dss)
return NULL;
getstring(&data, &len, &p, &slen);
len += 4 * (bignum_bitcount(dss->q) + 15) / 16;
len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
- p = smalloc(len);
+ p = snewn(len, char);
if (!p)
return NULL;
for (i = 0; i < 16; i++)
sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
digest[i]);
- ret = smalloc(strlen(buffer) + 1);
+ ret = snewn(strlen(buffer) + 1, char);
if (ret)
strcpy(ret, buffer);
return ret;
* 27 + sum of lengths. (five length fields, 20+7=27).
*/
bloblen = 27 + plen + qlen + glen + ylen;
- blob = smalloc(bloblen);
+ blob = snewn(bloblen, unsigned char);
p = blob;
PUT_32BIT(p, 7);
p += 4;
* mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
*/
bloblen = 4 + xlen;
- blob = smalloc(bloblen);
+ blob = snewn(bloblen, unsigned char);
p = blob;
PUT_32BIT(p, xlen);
p += 4;
char **b = (char **) blob;
struct dss_key *dss;
- dss = smalloc(sizeof(struct dss_key));
+ dss = snew(struct dss_key);
if (!dss)
return NULL;
* i.e. 4+7 + 4+40 bytes.
*/
nbytes = 4 + 7 + 4 + 40;
- bytes = smalloc(nbytes);
+ bytes = snewn(nbytes, unsigned char);
PUT_32BIT(bytes, 7);
memcpy(bytes + 4, "ssh-dss", 7);
PUT_32BIT(bytes + 4 + 7, 40);
static void *md5_make_context(void)
{
- return smalloc(2*sizeof(struct MD5Context));
+ return snewn(2, struct MD5Context);
}
static void md5_free_context(void *handle)
i += 4;
if (len - i < j)
goto end;
- comment = smalloc(j + 1);
+ comment = snewn(j + 1, char);
if (comment) {
memcpy(comment, buf + i, j);
comment[j] = '\0';
int c;
size = 128;
- text = smalloc(size);
+ text = snewn(size, char);
len = 0;
text[len] = '\0';
}
if (len + 1 > size) {
size += 128;
- text = srealloc(text, size);
+ text = sresize(text, size, char);
}
text[len++] = c;
text[len] = '\0';
int i, j, k;
/* We expect at most 64 base64 characters, ie 48 real bytes, per line. */
- blob = smalloc(48 * nlines);
+ blob = snewn(48 * nlines, unsigned char);
len = 0;
for (i = 0; i < nlines; i++) {
line = read_body(fp);
4 + commlen +
4 + public_blob_len +
4 + private_blob_len);
- macdata = smalloc(maclen);
+ macdata = snewn(maclen, unsigned char);
p = macdata;
#define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
DO_STR(alg->name, namelen);
/*
* Create and return the key.
*/
- ret = smalloc(sizeof(struct ssh2_userkey));
+ ret = snew(struct ssh2_userkey);
ret->alg = alg;
ret->comment = comment;
ret->data = alg->createkey(public_blob, public_blob_len,
}
priv_encrypted_len = priv_blob_len + cipherblk - 1;
priv_encrypted_len -= priv_encrypted_len % cipherblk;
- priv_blob_encrypted = smalloc(priv_encrypted_len);
+ priv_blob_encrypted = snewn(priv_encrypted_len, unsigned char);
memset(priv_blob_encrypted, 0, priv_encrypted_len);
memcpy(priv_blob_encrypted, priv_blob, priv_blob_len);
/* Create padding based on the SHA hash of the unpadded blob. This prevents
4 + commlen +
4 + pub_blob_len +
4 + priv_encrypted_len);
- macdata = smalloc(maclen);
+ macdata = snewn(maclen, unsigned char);
p = macdata;
#define DO_STR(s,len) PUT_32BIT(p,(len));memcpy(p+4,(s),(len));p+=4+(len)
DO_STR(key->alg->name, namelen);
void random_get_savedata(void **data, int *len)
{
- void *buf = smalloc(POOLSIZE / 2);
+ void *buf = snewn(POOLSIZE / 2, char);
random_stir();
memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
*len = POOLSIZE / 2;
length = (ssh1_bignum_length(key->modulus) +
ssh1_bignum_length(key->exponent) + 4);
- ret = smalloc(length);
+ ret = snewn(length, unsigned char);
PUT_32BIT(ret, bignum_bitcount(key->modulus));
pos = 4;
int slen;
struct RSAKey *rsa;
- rsa = smalloc(sizeof(struct RSAKey));
+ rsa = snew(struct RSAKey);
if (!rsa)
return NULL;
getstring(&data, &len, &p, &slen);
int len;
len = rsastr_len(rsa);
- p = smalloc(len);
+ p = snewn(len, char);
rsastr_fmt(p, rsa);
return p;
}
* (three length fields, 12+7=19).
*/
bloblen = 19 + elen + mlen;
- blob = smalloc(bloblen);
+ blob = snewn(bloblen, unsigned char);
p = blob;
PUT_32BIT(p, 7);
p += 4;
* sum of lengths.
*/
bloblen = 16 + dlen + plen + qlen + ulen;
- blob = smalloc(bloblen);
+ blob = snewn(bloblen, unsigned char);
p = blob;
PUT_32BIT(p, dlen);
p += 4;
char **b = (char **) blob;
struct RSAKey *rsa;
- rsa = smalloc(sizeof(struct RSAKey));
+ rsa = snew(struct RSAKey);
if (!rsa)
return NULL;
rsa->comment = NULL;
for (i = 0; i < 16; i++)
sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
digest[i]);
- ret = smalloc(strlen(buffer) + 1);
+ ret = snewn(strlen(buffer) + 1, char);
if (ret)
strcpy(ret, buffer);
return ret;
SHA_Simple(data, datalen, hash);
nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
- bytes = smalloc(nbytes);
+ bytes = snewn(nbytes, unsigned char);
bytes[0] = 1;
for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
freebn(in);
nbytes = (bignum_bitcount(out) + 7) / 8;
- bytes = smalloc(4 + 7 + 4 + nbytes);
+ bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
PUT_32BIT(bytes, 7);
memcpy(bytes + 4, "ssh-rsa", 7);
PUT_32BIT(bytes + 4 + 7, nbytes);
static void *sha1_make_context(void)
{
- return smalloc(2*sizeof(SHA_State));
+ return snewn(2, SHA_State);
}
static void sha1_free_context(void *handle)
struct LZ77InternalContext *st;
int i;
- st = (struct LZ77InternalContext *) smalloc(sizeof(*st));
+ st = snew(struct LZ77InternalContext);
if (!st)
return 0;
while (out->noutbits >= 8) {
if (out->outlen >= out->outsize) {
out->outsize = out->outlen + 64;
- out->outbuf = srealloc(out->outbuf, out->outsize);
+ out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
}
out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
out->outbits >>= 8;
void *zlib_compress_init(void)
{
struct Outbuf *out;
- struct LZ77Context *ectx = smalloc(sizeof(struct LZ77Context));
+ struct LZ77Context *ectx = snew(struct LZ77Context);
lz77_init(ectx);
ectx->literal = zlib_literal;
ectx->match = zlib_match;
- out = smalloc(sizeof(struct Outbuf));
+ out = snew(struct Outbuf);
out->outbits = out->noutbits = 0;
out->firstblock = 1;
out->comp_disabled = FALSE;
int nsyms,
int pfx, int pfxbits, int bits)
{
- struct zlib_table *tab = smalloc(sizeof(struct zlib_table));
+ struct zlib_table *tab = snew(struct zlib_table);
int pfxmask = (1 << pfxbits) - 1;
int nbits, i, j, code;
- tab->table = smalloc((1 << bits) * sizeof(struct zlib_tableentry));
+ tab->table = snewn(1 << bits, struct zlib_tableentry);
tab->mask = (1 << bits) - 1;
for (code = 0; code <= tab->mask; code++) {
void *zlib_decompress_init(void)
{
- struct zlib_decompress_ctx *dctx =
- smalloc(sizeof(struct zlib_decompress_ctx));
+ struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
unsigned char lengths[288];
memset(lengths, 8, 144);
dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
if (dctx->outlen >= dctx->outsize) {
dctx->outsize = dctx->outlen + 512;
- dctx->outblk = srealloc(dctx->outblk, dctx->outsize);
+ dctx->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
}
dctx->outblk[dctx->outlen++] = c;
}
else {
subneg_addchar:
if (telnet->sb_len >= telnet->sb_size) {
- unsigned char *newbuf;
telnet->sb_size += SB_DELTA;
- newbuf = (telnet->sb_buf ?
- srealloc(telnet->sb_buf, telnet->sb_size) :
- smalloc(telnet->sb_size));
- if (newbuf)
- telnet->sb_buf = newbuf;
- else
- telnet->sb_size -= SB_DELTA;
+ telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
+ unsigned char);
}
- if (telnet->sb_len < telnet->sb_size)
- telnet->sb_buf[telnet->sb_len++] = c;
+ telnet->sb_buf[telnet->sb_len++] = c;
telnet->state = SUBNEGOT; /* in case we came here by goto */
}
break;
char *err;
Telnet telnet;
- telnet = smalloc(sizeof(*telnet));
+ telnet = snew(struct telnet_tag);
telnet->fn = &fn_table;
telnet->cfg = *cfg; /* STRUCTURE COPY */
telnet->s = NULL;
*/
oldlen = line[0];
lineattrs = line[oldlen + 1];
- line = srealloc(line, TSIZE * (2 + cols));
+ line = sresize(line, 2 + cols, TTYPE);
line[0] = cols;
for (i = oldlen; i < cols; i++)
line[i + 1] = ERASE_CHAR;
* Allocate a new Terminal structure and initialise the fields
* that need it.
*/
- term = smalloc(sizeof(Terminal));
+ term = snew(Terminal);
term->frontend = frontend;
term->ucsdata = ucsdata;
term->cfg = *mycfg; /* STRUCTURE COPY */
term->savecurs.y += 1;
} else {
/* Add a new blank line at the bottom of the screen. */
- line = smalloc(TSIZE * (newcols + 2));
+ line = snewn(newcols + 2, TTYPE);
line[0] = newcols;
for (j = 0; j < newcols; j++)
line[j + 1] = ERASE_CHAR;
term->disptop = 0;
/* Make a new displayed text buffer. */
- newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
+ newdisp = snewn(newrows * (newcols + 1), TTYPE);
for (i = 0; i < newrows * (newcols + 1); i++)
newdisp[i] = ATTR_INVALID;
sfree(term->disptext);
/* Make a new alternate screen. */
newalt = newtree234(NULL);
for (i = 0; i < newrows; i++) {
- line = smalloc(TSIZE * (newcols + 2));
+ line = snewn(newcols + 2, TTYPE);
line[0] = newcols;
for (j = 0; j < newcols; j++)
line[j + 1] = term->erase_char;
term->alt_screen = newalt;
term->alt_sblines = 0;
- term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
+ term->tabs = sresize(term->tabs, newcols, unsigned char);
{
int i;
for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
if (sblen == term->savelines) {
sblen--, line2 = delpos234(term->scrollback, 0);
} else {
- line2 = smalloc(TSIZE * (term->cols + 2));
+ line2 = snewn(term->cols + 2, TTYPE);
line2[0] = term->cols;
term->tempsblines += 1;
}
term->scrolltail->botline == botline) {
term->scrolltail->lines += lines;
} else {
- newscroll = smalloc(sizeof(struct scrollregion));
+ newscroll = snew(struct scrollregion);
newscroll->topline = topline;
newscroll->botline = botline;
newscroll->lines = lines;
ticks = GETTICKCOUNT();
if (!term->beep_overloaded) {
- newbeep = smalloc(sizeof(struct beeptime));
+ newbeep = snew(struct beeptime);
newbeep->ticks = ticks;
newbeep->next = NULL;
if (!term->beephead)
int buflen; /* amount of memory allocated to workbuf */
buflen = 5120; /* Default size */
- workbuf = smalloc(buflen * sizeof(wchar_t));
+ workbuf = snewn(buflen, wchar_t);
wbptr = workbuf; /* start filling here */
old_top_x = top.x; /* needed for rect==1 */
for (p = cbuf; *p; p++) {
/* Enough overhead for trailing NL and nul */
if (wblen >= buflen - 16) {
- workbuf =
- srealloc(workbuf,
- sizeof(wchar_t) * (buflen += 100));
+ buflen += 100;
+ workbuf = sresize(workbuf, buflen, wchar_t);
wbptr = workbuf + wblen;
}
wblen++;
if (term->paste_buffer)
sfree(term->paste_buffer);
term->paste_pos = term->paste_hold = term->paste_len = 0;
- term->paste_buffer = smalloc(len * sizeof(wchar_t));
+ term->paste_buffer = snewn(len, wchar_t);
p = q = data;
while (p < data + len) {
int beep_overloaded;
long lastbeep;
-#define TSIZE (sizeof(unsigned long))
+#define TTYPE unsigned long
+#define TSIZE (sizeof(TTYPE))
#define fix_cpos do { \
term->cpos = lineptr(term->curs.y) + term->curs.x; \
} while(0)
-/* $Id: testback.c,v 1.6 2003/01/15 23:30:21 ben Exp $ */
+/* $Id: testback.c,v 1.7 2003/03/29 16:14:26 simon Exp $ */
/*
* Copyright (c) 1999 Simon Tatham
* Copyright (c) 1999 Ben Harris
static char *loop_init(void *frontend_handle, void **backend_handle,
Config *cfg, char *host, int port, char **realhost,
int nodelay) {
- struct loop_state *st = smalloc(sizeof(*st));
+ struct loop_state *st = snew(struct loop_state);
st->term = frontend_handle;
*backend_handle = st;
#include <stdlib.h>
#include <assert.h>
+#include "puttymem.h"
#include "tree234.h"
-#define smalloc malloc
-#define sfree free
-
-#define mknew(typ) ( (typ *) smalloc (sizeof (typ)) )
-
#ifdef TEST
#define LOG(x) (printf x)
#else
*/
tree234 *newtree234(cmpfn234 cmp)
{
- tree234 *ret = mknew(tree234);
+ tree234 *ret = snew(tree234);
LOG(("created tree %p\n", ret));
ret->root = NULL;
ret->cmp = cmp;
LOG(("adding node %p to tree %p\n", e, t));
if (t->root == NULL) {
- t->root = mknew(node234);
+ t->root = snew(node234);
t->root->elems[1] = t->root->elems[2] = NULL;
t->root->kids[0] = t->root->kids[1] = NULL;
t->root->kids[2] = t->root->kids[3] = NULL;
LOG((" done\n"));
break;
} else {
- node234 *m = mknew(node234);
+ node234 *m = snew(node234);
m->parent = n->parent;
LOG((" splitting a 4-node; created new node %p\n", m));
/*
}
} else {
LOG((" root is overloaded, split into two\n"));
- t->root = mknew(node234);
+ t->root = snew(node234);
t->root->kids[0] = left;
t->root->counts[0] = lcount;
t->root->elems[0] = e;
#include <stdarg.h>
-#define srealloc realloc
-
/*
* Error reporting function.
*/
if (arraysize < arraylen + 1) {
arraysize = arraylen + 1 + 256;
- array = (array == NULL ? smalloc(arraysize * sizeof(*array)) :
- srealloc(array, arraysize * sizeof(*array)));
+ array = sresize(array, arraysize, void *);
}
i = index;
if (DIRECT_FONT(ucsdata->unitab_line[i]))
continue;
if (!ucsdata->uni_tbl) {
- ucsdata->uni_tbl = smalloc(256 * sizeof(char *));
+ ucsdata->uni_tbl = snewn(256, char *);
memset(ucsdata->uni_tbl, 0, 256 * sizeof(char *));
}
j = ((ucsdata->unitab_line[i] >> 8) & 0xFF);
if (!ucsdata->uni_tbl[j]) {
- ucsdata->uni_tbl[j] = smalloc(256 * sizeof(char));
+ ucsdata->uni_tbl[j] = snewn(256, char);
memset(ucsdata->uni_tbl[j], 0, 256 * sizeof(char));
}
ucsdata->uni_tbl[j][ucsdata->unitab_line[i] & 0xFF] = i;
{
struct dlgparam *dp = (struct dlgparam *)dlg;
struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
+ /*
+ * This is an internal allocation routine, so it's allowed to
+ * use smalloc directly.
+ */
uc->privdata = smalloc(size);
uc->privdata_needs_free = FALSE;
return uc->privdata;
assert(ncols <=
(uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
- percents = smalloc(ncols * sizeof(gint));
+ percents = snewn(ncols, gint);
percents[ncols-1] = 100;
for (i = 0; i < ncols-1; i++) {
percents[i] = uc->ctrl->listbox.percentages[i];
continue; /* no actual control created */
}
- uc = smalloc(sizeof(struct uctrl));
+ uc = snew(struct uctrl);
uc->ctrl = ctrl;
uc->privdata = NULL;
uc->privdata_needs_free = FALSE;
group = NULL;
uc->nbuttons = ctrl->radio.nbuttons;
- uc->buttons = smalloc(uc->nbuttons * sizeof(GtkWidget *));
+ uc->buttons = snewn(uc->nbuttons, GtkWidget *);
for (i = 0; i < ctrl->radio.nbuttons; i++) {
GtkWidget *b;
if (nselparams >= selparamsize) {
selparamsize += 16;
- selparams = srealloc(selparams,
- selparamsize * sizeof(*selparams));
+ selparams = sresize(selparams, selparamsize,
+ struct selparam);
}
selparams[nselparams].dp = &dp;
selparams[nselparams].panels = PANELS(panels);
}
dp.ntreeitems = nselparams;
- dp.treeitems = smalloc(dp.ntreeitems * sizeof(GtkWidget *));
+ dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
for (index = 0; index < nselparams; index++) {
gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
wchar_t *tmp = data;
int tmplen = len;
- inst->pasteout_data_utf8 = smalloc(len*6);
+ inst->pasteout_data_utf8 = snewn(len*6, char);
inst->pasteout_data_utf8_len = len*6;
inst->pasteout_data_utf8_len =
charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
inst->pasteout_data_utf8 = NULL;
} else {
inst->pasteout_data_utf8 =
- srealloc(inst->pasteout_data_utf8,
- inst->pasteout_data_utf8_len);
+ sresize(inst->pasteout_data_utf8,
+ inst->pasteout_data_utf8_len, char);
}
} else {
inst->pasteout_data_utf8 = NULL;
inst->pasteout_data_utf8_len = 0;
}
- inst->pasteout_data = smalloc(len*6);
+ inst->pasteout_data = snewn(len*6, char);
inst->pasteout_data_len = len*6;
inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
data, len, inst->pasteout_data,
inst->pasteout_data = NULL;
} else {
inst->pasteout_data =
- srealloc(inst->pasteout_data, inst->pasteout_data_len);
+ sresize(inst->pasteout_data, inst->pasteout_data_len, char);
}
if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
if (inst->pastein_data)
sfree(inst->pastein_data);
- inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
+ inst->pastein_data = snewn(seldata->length, wchar_t);
inst->pastein_data_len = seldata->length;
inst->pastein_data_len =
mb_to_wc((seldata->type == inst->utf8_string_atom ?
if (!inst->area->window)
return NULL;
- dctx = smalloc(sizeof(*dctx));
+ dctx = snew(struct draw_ctx);
dctx->inst = inst;
dctx->gc = gdk_gc_new(inst->area->window);
return dctx;
wchar_t *wcs;
int i;
- wcs = smalloc(sizeof(wchar_t) * (len+1));
+ wcs = snewn(len+1, wchar_t);
for (i = 0; i < len; i++) {
wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
}
* and (c) the clip rectangle should prevent it causing
* trouble anyway.
*/
- gwcs = smalloc(sizeof(GdkWChar) * (len*2+1));
+ gwcs = snewn(len*2+1, GdkWChar);
memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
/*
* FIXME: when we have a wide-char equivalent of
gwcs, len*2);
sfree(gwcs);
} else {
- gcs = smalloc(sizeof(GdkWChar) * (len+1));
+ gcs = snewn(len+1, gchar);
wc_to_mb(inst->fontinfo[fontid].charset, 0,
wcs, len, gcs, len, ".", NULL, NULL);
gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
if (--argc > 0) {
int i;
- pty_argv = smalloc((argc+1) * sizeof(char *));
+ pty_argv = snewn(argc+1, char *);
++argv;
for (i = 0; i < argc; i++)
pty_argv[i] = argv[i];
/*
* Create an instance structure and initialise to zeroes
*/
- inst = smalloc(sizeof(*inst));
+ inst = snew(struct gui_data);
memset(inst, 0, sizeof(*inst));
inst->alt_keycode = -1; /* this one needs _not_ to be zero */
char *shellname;
if (cfg->login_shell) {
char *p = strrchr(shell, '/');
- shellname = smalloc(2+strlen(shell));
+ shellname = snewn(2+strlen(shell), char);
p = p ? p+1 : shell;
sprintf(shellname, "-%s", p);
} else
if (!fp)
return; /* assume no auth */
- localbuf = smalloc(maxsize);
+ localbuf = snewn(maxsize, char);
while (1) {
/*
}
retsize += 4;
assert(retbuf == sizebuf);
- retbuf = smalloc(retsize);
+ retbuf = snewn(retsize, char);
memcpy(retbuf, sizebuf, 4);
}
}
SockAddr sk_namelookup(const char *host, char **canonicalname)
{
- SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+ SockAddr ret = snew(struct SockAddr_tag);
unsigned long a;
struct hostent *h = NULL;
char realhost[8192];
}
ret->address = ntohl(a);
realhost[lenof(realhost)-1] = '\0';
- *canonicalname = smalloc(1+strlen(realhost));
+ *canonicalname = snewn(1+strlen(realhost), char);
strcpy(*canonicalname, realhost);
return ret;
}
SockAddr sk_nonamelookup(const char *host)
{
- SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+ SockAddr ret = snew(struct SockAddr_tag);
ret->error = NULL;
ret->family = AF_UNSPEC;
strncpy(ret->hostname, host, lenof(ret->hostname));
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
while (*p) {
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
- command = srealloc(command, cmdsize);
+ command = sresize(command, cmdsize, char);
}
command[cmdlen++]=*p++;
}
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
- command = srealloc(command, cmdsize);
+ command = sresize(command, cmdsize, char);
}
command[cmdlen++]=' '; /* always add trailing space */
if (--argc) p = *++argv;
/* Expand the sklist buffer if necessary. */
if (i > sksize) {
sksize = i + 16;
- sklist = srealloc(sklist, sksize * sizeof(*sklist));
+ sklist = sresize(sklist, sksize, int);
}
/*
printer_job *printer_start_job(char *printer)
{
- printer_job *ret = smalloc(sizeof(printer_job));
+ printer_job *ret = snew(printer_job);
/*
* On Unix, we treat the printer string as the name of a
* command to pipe to - typically lpr, of course.
q++;
while (p > string && p[-1] != '.' && p[-1] != '*')
p--;
- xrms = smalloc(sizeof(struct xrm_string));
- key = smalloc(q-p);
+ xrms = snew(struct xrm_string);
+ key = snewn(q-p, char);
memcpy(key, p, q-p);
key[q-p-1] = '\0';
xrms->key = key;
*/
static char *fgetline(FILE *fp)
{
- char *ret = smalloc(512);
+ char *ret = snewn(512, char);
int size = 512, len = 0;
while (fgets(ret + len, size - len, fp)) {
len += strlen(ret + len);
if (ret[len-1] == '\n')
break; /* got a newline, we're done */
size = len + 512;
- ret = srealloc(ret, size);
+ ret = sresize(ret, size, char);
}
if (len == 0) { /* first fgets returned NULL */
sfree(ret);
assert(c->generic.handler == dlg_stdradiobutton_handler);
c->radio.nbuttons++;
c->radio.buttons =
- srealloc(c->radio.buttons,
- c->radio.nbuttons * sizeof(*c->radio.buttons));
+ sresize(c->radio.buttons, c->radio.nbuttons, char *);
c->radio.buttons[c->radio.nbuttons-1] =
dupstr("Play a custom sound file");
c->radio.buttondata =
- srealloc(c->radio.buttondata,
- c->radio.nbuttons * sizeof(*c->radio.buttondata));
+ sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
c->radio.buttondata[c->radio.nbuttons-1] = I(BELL_WAVEFILE);
if (c->radio.shortcuts) {
c->radio.shortcuts =
- srealloc(c->radio.shortcuts,
- (c->radio.nbuttons *
- sizeof(*c->radio.shortcuts)));
+ sresize(c->radio.shortcuts, c->radio.nbuttons, char);
c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;
}
break;
assert(c->generic.handler == dlg_stdradiobutton_handler);
c->radio.nbuttons += 2;
c->radio.buttons =
- srealloc(c->radio.buttons,
- c->radio.nbuttons * sizeof(*c->radio.buttons));
+ sresize(c->radio.buttons, c->radio.nbuttons, char *);
c->radio.buttons[c->radio.nbuttons-2] =
dupstr("Use font in both ANSI and OEM modes");
c->radio.buttons[c->radio.nbuttons-1] =
dupstr("Use font in OEM mode only");
c->radio.buttondata =
- srealloc(c->radio.buttondata,
- c->radio.nbuttons * sizeof(*c->radio.buttondata));
+ sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
c->radio.buttondata[c->radio.nbuttons-2] = I(VT_OEMANSI);
c->radio.buttondata[c->radio.nbuttons-1] = I(VT_OEMONLY);
if (!c->radio.shortcuts) {
int j;
- c->radio.shortcuts =
- smalloc((c->radio.nbuttons *
- sizeof(*c->radio.shortcuts)));
+ c->radio.shortcuts = snewn(c->radio.nbuttons, char);
for (j = 0; j < c->radio.nbuttons; j++)
c->radio.shortcuts[j] = NO_SHORTCUT;
} else {
- c->radio.shortcuts =
- srealloc(c->radio.shortcuts,
- (c->radio.nbuttons *
- sizeof(*c->radio.shortcuts)));
+ c->radio.shortcuts = sresize(c->radio.shortcuts,
+ c->radio.nbuttons, char);
}
c->radio.shortcuts[c->radio.nbuttons-2] = 'b';
c->radio.shortcuts[c->radio.nbuttons-1] = 'e';
bid = va_arg(ap, int);
}
va_end(ap);
- buttons = smalloc(nbuttons * sizeof(struct radio));
+ buttons = snewn(nbuttons, struct radio);
va_start(ap, nacross);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, char *);
bid = va_arg(ap, int);
}
va_end(ap);
- buttons = smalloc(nbuttons * sizeof(struct radio));
+ buttons = snewn(nbuttons, struct radio);
va_start(ap, nacross);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, char *);
bid = va_arg(ap, int);
}
va_end(ap);
- buttons = smalloc(nbuttons * sizeof(struct radio));
+ buttons = snewn(nbuttons, struct radio);
va_start(ap, id);
for (i = 0; i < nbuttons; i++) {
buttons[i].text = va_arg(ap, char *);
RECT r;
HFONT oldfont, newfont;
- ret = smalloc(1+strlen(text));
+ ret = snewn(1+strlen(text), char);
p = text;
q = ret;
- pwidths = smalloc(sizeof(INT)*(1+strlen(text)));
+ pwidths = snewn(1+strlen(text), INT);
/*
* Work out the width the text will need to fit in, by doing
char *txt;
/* Get the item's data. */
tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
- txt = smalloc(tlen+1);
+ txt = snewn(tlen+1, char);
SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
/* Deselect old location. */
if (!text)
return NULL; /* sfree won't choke on this */
- ret = smalloc(2*strlen(text)+1); /* size potentially doubles! */
+ ret = snewn(2*strlen(text)+1, char); /* size potentially doubles! */
shortcut = tolower((unsigned char)shortcut);
p = text;
/* Start a containing box, if we have a boxname. */
if (s->boxname && *s->boxname) {
- struct winctrl *c = smalloc(sizeof(struct winctrl));
+ struct winctrl *c = snew(struct winctrl);
c->ctrl = NULL;
c->base_id = base_id;
c->num_ids = 1;
/* Draw a title, if we have one. */
if (!s->boxname && s->boxtitle) {
- struct winctrl *c = smalloc(sizeof(struct winctrl));
+ struct winctrl *c = snew(struct winctrl);
c->ctrl = NULL;
c->base_id = base_id;
c->num_ids = 1;
ctrl->radio.shortcut);
shortcuts[nshortcuts++] = ctrl->radio.shortcut;
- buttons = smalloc(ctrl->radio.nbuttons * sizeof(struct radio));
+ buttons = snewn(ctrl->radio.nbuttons, struct radio);
for (i = 0; i < ctrl->radio.nbuttons; i++) {
buttons[i].text =
ctrl->listbox.shortcut);
shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
if (ctrl->listbox.draglist) {
- data = smalloc(sizeof(struct prefslist));
+ data = snew(struct prefslist);
num_ids = 4;
prefslist(data, &pos, ctrl->listbox.height, escaped,
base_id, base_id+1, base_id+2, base_id+3);
int *tabarray;
int i, percent;
- tabarray = smalloc((ctrl->listbox.ncols-1) * sizeof(int));
+ tabarray = snewn(ctrl->listbox.ncols-1, int);
percent = 0;
for (i = 0; i < ctrl->listbox.ncols-1; i++) {
percent += ctrl->listbox.percentages[i];
statictext(&pos, escaped, 1, base_id);
staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
sfree(escaped);
- data = smalloc(sizeof(FontSpec));
+ data = snew(FontSpec);
break;
default:
assert(!"Can't happen");
* (and isn't tabdelayed).
*/
if (pos.hwnd) {
- struct winctrl *c = smalloc(sizeof(struct winctrl));
+ struct winctrl *c = snew(struct winctrl);
c->ctrl = ctrl;
c->base_id = actual_base_id;
CB_GETCURSEL, 0, 0);
len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
CB_GETLBTEXTLEN, index, 0);
- text = smalloc(len+1);
+ text = snewn(len+1, char);
SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
index, (LPARAM)text);
SetDlgItemText(dp->hwnd, c->base_id+1, text);
tmp.ctrl = ctrl;
p = find234(dp->privdata, &tmp, NULL);
if (!p) {
- p = smalloc(sizeof(struct perctrl_privdata));
+ p = snew(struct perctrl_privdata);
p->ctrl = ctrl;
p->needs_free = FALSE;
add234(dp->privdata, p);
tmp.ctrl = ctrl;
p = find234(dp->privdata, &tmp, NULL);
if (!p) {
- p = smalloc(sizeof(struct perctrl_privdata));
+ p = snew(struct perctrl_privdata);
p->ctrl = ctrl;
p->needs_free = FALSE;
add234(dp->privdata, p);
}
assert(!p->needs_free);
p->needs_free = TRUE;
+ /*
+ * This is an internal allocation routine, so it's allowed to
+ * use smalloc directly.
+ */
p->data = smalloc(size);
return p->data;
}
break;
}
- selitems = smalloc(selcount * sizeof(int));
+ selitems = snewn(selcount, int);
if (selitems) {
int count = SendDlgItemMessage(hwnd, IDN_LIST,
LB_GETSELITEMS,
size +=
strlen(events[selitems[i]]) + sizeof(sel_nl);
- clipdata = smalloc(size);
+ clipdata = snewn(size, char);
if (clipdata) {
char *p = clipdata;
for (i = 0; i < count; i++) {
if (nevents >= negsize) {
negsize += 64;
- events = srealloc(events, negsize * sizeof(*events));
+ events = sresize(events, negsize, char *);
}
time(&t);
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t",
localtime(&t));
- events[nevents] = smalloc(strlen(timebuf) + strlen(string) + 1);
+ events[nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
strcpy(events[nevents], timebuf);
strcat(events[nevents], string);
if (logbox) {
{
char *bits;
int size = (font_width + 15) / 16 * 2 * font_height;
- bits = smalloc(size);
+ bits = snewn(size, char);
memset(bits, 0, size);
caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
sfree(bits);
HDC hdc = GetDC(hwnd);
if (hdc) {
if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
+ /*
+ * This is a genuine case where we must use smalloc
+ * because the snew macros can't cope.
+ */
logpal = smalloc(sizeof(*logpal)
- sizeof(logpal->palPalEntry)
+ NCOLOURS * sizeof(PALETTEENTRY));
if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) {
char *session =
sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16];
- cl = smalloc(16 + strlen(session));
+ cl = snewn(16 + strlen(session), char);
/* 8, but play safe */
if (!cl)
cl = NULL;
if (n > 0) {
int i;
- buff = (char*) smalloc(n);
+ buff = snewn(n, char);
ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
/*
* Jaeyoun Chung reports that Korean character
int i;
if (len > IpDxLEN) {
sfree(IpDx);
- IpDx = smalloc((len + 16) * sizeof(int));
+ IpDx = snewn(len + 16, int);
IpDxLEN = (len + 16);
}
for (i = 0; i < IpDxLEN; i++)
int nlen, mptr;
if (len > uni_len) {
sfree(uni_buf);
- uni_buf = smalloc((uni_len = len) * sizeof(wchar_t));
+ uni_len = len;
+ uni_buf = snewn(uni_len, wchar_t);
}
for(nlen = mptr = 0; mptr<len; mptr++) {
if (wlen < len) {
sfree(wbuf);
wlen = len;
- wbuf = smalloc(wlen * sizeof(WCHAR));
+ wbuf = snewn(wlen, WCHAR);
}
for (i = 0; i < len; i++)
wbuf[i] = (WCHAR) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
void set_title(void *frontend, char *title)
{
sfree(window_name);
- window_name = smalloc(1 + strlen(title));
+ window_name = snewn(1 + strlen(title), char);
strcpy(window_name, title);
if (cfg.win_name_always || !IsIconic(hwnd))
SetWindowText(hwnd, title);
void set_icon(void *frontend, char *title)
{
sfree(icon_name);
- icon_name = smalloc(1 + strlen(title));
+ icon_name = snewn(1 + strlen(title), char);
strcpy(icon_name, title);
if (!cfg.win_name_always && IsIconic(hwnd))
SetWindowText(hwnd, title);
get_unitab(CP_ACP, unitab, 0);
rtfsize = 100 + strlen(cfg.font.name);
- rtf = smalloc(rtfsize);
+ rtf = snewn(rtfsize, char);
sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
GetACP(), cfg.font.name);
rtflen = strlen(rtf);
if (rtfsize < rtflen + totallen + 3) {
rtfsize = rtflen + totallen + 512;
- rtf = srealloc(rtf, rtfsize);
+ rtf = sresize(rtf, rtfsize, char);
}
strcpy(rtf + rtflen, before); rtflen += blen;
CloseClipboard();
s = GlobalLock(clipdata);
i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
- *p = converted = smalloc(i * sizeof(wchar_t));
+ *p = converted = snewn(i, wchar_t);
MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
*len = i - 1;
return;
SockAddr sk_namelookup(const char *host, char **canonicalname)
{
- SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+ SockAddr ret = snew(struct SockAddr_tag);
unsigned long a;
struct hostent *h = NULL;
char realhost[8192];
}
ret->address = ntohl(a);
realhost[lenof(realhost)-1] = '\0';
- *canonicalname = smalloc(1+strlen(realhost));
+ *canonicalname = snewn(1+strlen(realhost), char);
strcpy(*canonicalname, realhost);
return ret;
}
SockAddr sk_nonamelookup(const char *host)
{
- SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+ SockAddr ret = snew(struct SockAddr_tag);
ret->error = NULL;
ret->family = AF_UNSPEC;
strncpy(ret->hostname, host, lenof(ret->hostname));
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &fn_table;
ret->error = NULL;
ret->plug = plug;
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &fn_table;
ret->error = NULL;
ret->plug = plug;
/*
* Create Socket structure.
*/
- ret = smalloc(sizeof(struct Socket_tag));
+ ret = snew(struct Socket_tag);
ret->fn = &fn_table;
ret->error = NULL;
ret->plug = plug;
if (!sessionname || !*sessionname)
sessionname = "Default Settings";
- p = smalloc(3 * strlen(sessionname) + 1);
+ p = snewn(3 * strlen(sessionname) + 1, char);
mungestr(sessionname, p);
ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
if (!sessionname || !*sessionname)
sessionname = "Default Settings";
- p = smalloc(3 * strlen(sessionname) + 1);
+ p = snewn(3 * strlen(sessionname) + 1, char);
mungestr(sessionname, p);
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
return;
- p = smalloc(3 * strlen(sessionname) + 1);
+ p = snewn(3 * strlen(sessionname) + 1, char);
mungestr(sessionname, p);
RegDeleteKey(subkey1, p);
sfree(p);
if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
return NULL;
- ret = smalloc(sizeof(*ret));
+ ret = snew(struct enumsettings);
if (ret) {
ret->key = key;
ret->i = 0;
{
struct enumsettings *e = (struct enumsettings *) handle;
char *otherbuf;
- otherbuf = smalloc(3 * buflen);
+ otherbuf = snewn(3 * buflen, char);
if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
unmungestr(otherbuf, buffer, buflen);
sfree(otherbuf);
* Now read a saved key in from the registry and see what it
* says.
*/
- otherstr = smalloc(len);
- regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+ otherstr = snewn(len, char);
+ regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
hostkey_regname(regname, hostname, port, keytype);
* under just the hostname and translate that.
*/
char *justhost = regname + 1 + strcspn(regname, ":");
- char *oldstyle = smalloc(len + 10); /* safety margin */
+ char *oldstyle = snewn(len + 10, char); /* safety margin */
readlen = len;
ret = RegQueryValueEx(rkey, justhost, NULL, &type,
oldstyle, &readlen);
char *regname;
HKEY rkey;
- regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+ regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
hostkey_regname(regname, hostname, port, keytype);
* This will guaranteeably be big enough; we can realloc it
* down later.
*/
- outputline = smalloc(1+strlen(cmdline));
- outputargv = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
- outputargstart = smalloc(sizeof(char *) * (strlen(cmdline)+1 / 2));
+ outputline = snewn(1+strlen(cmdline), char);
+ outputargv = snewn(strlen(cmdline)+1 / 2, char *);
+ outputargstart = snewn(strlen(cmdline)+1 / 2, char *);
p = cmdline; q = outputline; outputargc = 0;
*q++ = '\0';
}
- outputargv = srealloc(outputargv, sizeof(char *) * outputargc);
- outputargstart = srealloc(outputargstart, sizeof(char *) * outputargc);
+ outputargv = sresize(outputargv, outputargc, char *);
+ outputargstart = sresize(outputargstart, outputargc, char *);
if (argc) *argc = outputargc;
if (argv) *argv = outputargv; else sfree(outputargv);
void *x11_invent_auth(char *proto, int protomaxlen,
char *data, int datamaxlen, int proto_id)
{
- struct X11Auth *auth = smalloc(sizeof(struct X11Auth));
+ struct X11Auth *auth = snew(struct X11Auth);
char ourdata[64];
int i;
/*
* Open socket.
*/
- pr = (struct X11Private *) smalloc(sizeof(struct X11Private));
+ pr = snew(struct X11Private);
pr->fn = &fn_table;
pr->auth_protocol = NULL;
pr->auth = (struct X11Auth *)auth;
pr->auth_psize = (pr->auth_plen + 3) & ~3;
pr->auth_dsize = (pr->auth_dlen + 3) & ~3;
/* Leave room for a terminating zero, to make our lives easier. */
- pr->auth_protocol = (char *) smalloc(pr->auth_psize + 1);
- pr->auth_data = (unsigned char *) smalloc(pr->auth_dsize);
+ pr->auth_protocol = snewn(pr->auth_psize + 1, char);
+ pr->auth_data = snewn(pr->auth_dsize, char);
}
/*
message = dupprintf("PuTTY X11 proxy: %s", err);
msglen = strlen(message);
- reply = smalloc(8 + msglen+1 + 4); /* include zero byte */
+ reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */
msgsize = (msglen + 3) & ~3;
reply[0] = 0; /* failure */
reply[1] = msglen; /* length of reason string */