reduce number of memory allocations

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@9504 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
2001-01-26 15:08:36 +00:00
parent e7f3feb49c
commit c4395a890d

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999 Kungliga Tekniska H<>gskolan
* Copyright (c) 1999-2001 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -75,9 +75,14 @@ append_hex(char *str, krb5_data *data)
p[data->length + 1] = '\"';
memcpy(p + 1, data->data, data->length);
}else{
p = calloc(1, data->length * 2 + 1);
for(i = 0; i < data->length; i++)
sprintf(p + 2 * i, "%02x", ((u_char*)data->data)[i]);
const char *xchars = "0123456789abcdef";
char *q = p = malloc(data->length * 2 + 1);
for(i = 0; i < data->length; i++) {
unsigned char c = ((u_char*)data->data)[i];
*q++ = xchars[(c & 0xf0) >> 4];
*q++ = xchars[(c & 0xf)];
}
*q = '\0';
}
strcat(str, p);
free(p);
@@ -123,6 +128,7 @@ hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
{
char *p;
char buf[1024] = "";
char tmp[32];
int i;
krb5_error_code ret;
@@ -134,29 +140,26 @@ hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
strlcat(buf, " ", sizeof(buf));
free(p);
/* --- kvno */
asprintf(&p, "%d", ent->kvno);
strlcat(buf, p, sizeof(buf));
free(p);
snprintf(tmp, sizeof(tmp), "%d", ent->kvno);
strlcat(buf, tmp, sizeof(buf));
/* --- keys */
for(i = 0; i < ent->keys.len; i++){
/* --- mkvno, keytype */
if(ent->keys.val[i].mkvno)
asprintf(&p, ":%d:%d:",
snprintf(tmp, sizeof(tmp), ":%d:%d:",
*ent->keys.val[i].mkvno,
ent->keys.val[i].key.keytype);
else
asprintf(&p, "::%d:",
snprintf(tmp, sizeof(tmp), "::%d:",
ent->keys.val[i].key.keytype);
strlcat(buf, p, sizeof(buf));
free(p);
strlcat(buf, tmp, sizeof(buf));
/* --- keydata */
append_hex(buf, &ent->keys.val[i].key.keyvalue);
strlcat(buf, ":", sizeof(buf));
/* --- salt */
if(ent->keys.val[i].salt){
asprintf(&p, "%u/", ent->keys.val[i].salt->type);
strlcat(buf, p, sizeof(buf));
free(p);
snprintf(tmp, sizeof(tmp), "%u/", ent->keys.val[i].salt->type);
strlcat(buf, tmp, sizeof(buf));
append_hex(buf, &ent->keys.val[i].salt->salt);
}else
strlcat(buf, "-", sizeof(buf));
@@ -196,28 +199,25 @@ hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
/* --- max life */
if(ent->max_life){
asprintf(&p, "%d", *ent->max_life);
strlcat(buf, p, sizeof(buf));
free(p);
snprintf(tmp, sizeof(tmp), "%d", *ent->max_life);
strlcat(buf, tmp, sizeof(buf));
}else
strlcat(buf, "-", sizeof(buf));
strlcat(buf, " ", sizeof(buf));
/* --- max renewable life */
if(ent->max_renew){
asprintf(&p, "%d", *ent->max_renew);
strlcat(buf, p, sizeof(buf));
free(p);
snprintf(tmp, sizeof(tmp), "%d", *ent->max_renew);
strlcat(buf, tmp, sizeof(buf));
}else
strlcat(buf, "-", sizeof(buf));
strlcat(buf, " ", sizeof(buf));
/* --- flags */
asprintf(&p, "%d", HDBFlags2int(ent->flags));
strlcat(buf, p, sizeof(buf));
free(p);
snprintf(tmp, sizeof(tmp), "%d", HDBFlags2int(ent->flags));
strlcat(buf, tmp, sizeof(buf));
*str = strdup(buf);
return 0;