From 386c883d8774999c6e74d7c6c37e52e4163a4cb1 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sun, 17 Feb 2002 19:50:05 +0000 Subject: [PATCH] updates. --- CHANGES | 6 ++ apps/silcd/command.c | 10 +-- apps/silcd/command_reply.c | 6 +- apps/silcd/packet_receive.c | 3 +- apps/silcd/packet_send.c | 3 +- apps/silcd/protocol.c | 14 ++--- apps/silcd/server.c | 6 +- includes/silcincludes.h | 1 - lib/silcclient/client.c | 9 +-- lib/silcclient/client_channel.c | 6 +- lib/silcclient/client_prvmsg.c | 3 +- lib/silcclient/command_reply.c | 5 +- lib/silcclient/protocol.c | 14 ++--- lib/silccore/silcauth.c | 3 +- lib/silccore/silccommand.c | 9 +-- lib/silccore/silcid.c | 24 +++----- lib/silccore/silcnotify.c | 3 +- lib/silcutil/DIRECTORY | 1 - lib/silcutil/Makefile.am | 1 - lib/silcutil/silcbuffer.h | 85 ++++++++++++++++++++++++-- lib/silcutil/silcbufutil.h | 100 ------------------------------ lib/silcutil/silcconfig.h | 16 ++--- lib/silcutil/silclist.h | 2 +- lib/silcutil/silcmemory.c | 20 ++++-- lib/silcutil/silcmemory.h | 104 +++++++++++++++++++++++++++++--- win32/libsilc/libsilc.def | 1 + 26 files changed, 243 insertions(+), 212 deletions(-) delete mode 100644 lib/silcutil/silcbufutil.h diff --git a/CHANGES b/CHANGES index 7d34d074..01d6bbf5 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,12 @@ Sun Feb 17 19:02:56 EET 2002 Pekka Riikonen docs from all kinds of filenames. Affected files are util/robodoc/Source/generator.c, scripts/silcdoc/silcdoc + * ROBOdoc documented lib/silcutil/silcmemory.h. + Added also new function silc_memdup. + + * Removed lib/silcutil/silcbufutil.h and moved those routines + to the lib/silcutil/silcbuffer.h. + Sun Feb 17 15:52:30 EET 2002 Pekka Riikonen * Added `user_count' to the SilcChannelEntry which now tells the diff --git a/apps/silcd/command.c b/apps/silcd/command.c index 7a4d7aa7..807faee2 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -3141,10 +3141,8 @@ static void silc_server_command_join_channel(SilcServer server, if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) { /* Get passphrase */ tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len); - if (tmp) { - passphrase = silc_calloc(tmp_len, sizeof(*passphrase)); - memcpy(passphrase, tmp, tmp_len); - } + if (tmp) + passphrase = silc_memdup(tmp, tmp_len); if (!passphrase || !channel->passphrase || memcmp(passphrase, channel->passphrase, strlen(channel->passphrase))) { @@ -4109,9 +4107,7 @@ SILC_SERVER_CMD_FUNC(cmode) if (channel->founder_method == SILC_AUTH_PASSWORD) { tmp = silc_auth_get_data(auth, &tmp_len); - channel->founder_passwd = - silc_calloc(tmp_len + 1, sizeof(*channel->founder_passwd)); - memcpy(channel->founder_passwd, tmp, tmp_len); + channel->founder_passwd = silc_memdup(tmp, tmp_len); channel->founder_passwd_len = tmp_len; } else { /* Verify the payload before setting the mode */ diff --git a/apps/silcd/command_reply.c b/apps/silcd/command_reply.c index d191b6b6..dee4e0be 100644 --- a/apps/silcd/command_reply.c +++ b/apps/silcd/command_reply.c @@ -918,16 +918,14 @@ SILC_SERVER_CMD_REPLY_FUNC(join) tmp = silc_argument_get_arg_type(cmd->args, 8, &len); if (tmp) { silc_free(entry->ban_list); - entry->ban_list = silc_calloc(len, sizeof(*entry->ban_list)); - memcpy(entry->ban_list, tmp, len); + entry->ban_list = silc_memdup(tmp, len); } /* Get the invite list */ tmp = silc_argument_get_arg_type(cmd->args, 9, &len); if (tmp) { silc_free(entry->invite_list); - entry->invite_list = silc_calloc(len, sizeof(*entry->invite_list)); - memcpy(entry->invite_list, tmp, len); + entry->invite_list = silc_memdup(tmp, len); } /* Get the topic */ diff --git a/apps/silcd/packet_receive.c b/apps/silcd/packet_receive.c index 7d210e83..65aba731 100644 --- a/apps/silcd/packet_receive.c +++ b/apps/silcd/packet_receive.c @@ -1589,8 +1589,7 @@ SilcClientEntry silc_server_new_client(SilcServer server, int tlen = strcspn(username, "@"); char *phostname = NULL; - hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char)); - memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1); + hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1); if (strcmp(sock->hostname, sock->ip) && strcmp(sock->hostname, hostname)) { diff --git a/apps/silcd/packet_send.c b/apps/silcd/packet_send.c index d2d21517..8062551f 100644 --- a/apps/silcd/packet_send.c +++ b/apps/silcd/packet_send.c @@ -875,8 +875,7 @@ void silc_server_packet_relay_to_channel(SilcServer server, /* If private key mode is not set then decrypt the packet and re-encrypt it */ if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) { - unsigned char *tmp = silc_calloc(data_len, sizeof(*data)); - memcpy(tmp, data, data_len); + unsigned char *tmp = silc_memdup(data, data_len); /* Decrypt the channel message (we don't check the MAC) */ if (channel->channel_key && diff --git a/apps/silcd/protocol.c b/apps/silcd/protocol.c index d92b2c3e..794a2088 100644 --- a/apps/silcd/protocol.c +++ b/apps/silcd/protocol.c @@ -276,11 +276,8 @@ int silc_server_protocol_ke_set_keys(SilcServer server, } idata->rekey = silc_calloc(1, sizeof(*idata->rekey)); - idata->rekey->send_enc_key = - silc_calloc(keymat->enc_key_len / 8, - sizeof(*idata->rekey->send_enc_key)); - memcpy(idata->rekey->send_enc_key, - keymat->send_enc_key, keymat->enc_key_len / 8); + idata->rekey->send_enc_key = silc_memdup(keymat->send_enc_key, + keymat->enc_key_len / 8); idata->rekey->enc_key_len = keymat->enc_key_len / 8; if (ske->prop->flags & SILC_SKE_SP_FLAG_PFS) @@ -1247,11 +1244,8 @@ silc_server_protocol_rekey_validate(SilcServer server, if (!send) { memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len); silc_free(idata->rekey->send_enc_key); - idata->rekey->send_enc_key = - silc_calloc(keymat->enc_key_len / 8, - sizeof(*idata->rekey->send_enc_key)); - memcpy(idata->rekey->send_enc_key, keymat->send_enc_key, - keymat->enc_key_len / 8); + idata->rekey->send_enc_key = silc_memdup(keymat->send_enc_key, + keymat->enc_key_len / 8); idata->rekey->enc_key_len = keymat->enc_key_len / 8; } } diff --git a/apps/silcd/server.c b/apps/silcd/server.c index 04912e9c..fab3d975 100644 --- a/apps/silcd/server.c +++ b/apps/silcd/server.c @@ -3066,8 +3066,7 @@ bool silc_server_create_channel_key(SilcServer server, /* Save the key */ channel->key_len = len * 8; - channel->key = silc_calloc(len, sizeof(*channel->key)); - memcpy(channel->key, channel_key, len); + channel->key = silc_memdup(channel_key, len); memset(channel_key, 0, sizeof(channel_key)); /* Generate HMAC key from the channel key data and set it */ @@ -3178,8 +3177,7 @@ SilcChannelEntry silc_server_save_channel_key(SilcServer server, /* Save the key */ channel->key_len = tmp_len * 8; - channel->key = silc_calloc(tmp_len, sizeof(unsigned char)); - memcpy(channel->key, tmp, tmp_len); + channel->key = silc_memdup(tmp, tmp_len); silc_cipher_set_key(channel->channel_key, tmp, channel->key_len); /* Generate HMAC key from the channel key data and set it */ diff --git a/includes/silcincludes.h b/includes/silcincludes.h index d15953fa..c7182023 100644 --- a/includes/silcincludes.h +++ b/includes/silcincludes.h @@ -263,7 +263,6 @@ typedef uint32 * void *; #include "silclist.h" #include "silcdlist.h" #include "silcbuffer.h" -#include "silcbufutil.h" #include "silcbuffmt.h" #include "silcnet.h" #include "silcfileutil.h" diff --git a/lib/silcclient/client.c b/lib/silcclient/client.c index 4674c092..620e3de0 100644 --- a/lib/silcclient/client.c +++ b/lib/silcclient/client.c @@ -611,8 +611,7 @@ void silc_client_resolve_auth_method(bool success, proto_ctx->auth_meth = auth_meth; if (auth_data && auth_data_len) { - proto_ctx->auth_data = silc_calloc(auth_data_len, sizeof(*auth_data)); - memcpy(proto_ctx->auth_data, auth_data, auth_data_len); + proto_ctx->auth_data = silc_memdup(auth_data, auth_data_len); proto_ctx->auth_data_len = auth_data_len; } @@ -1438,8 +1437,7 @@ void silc_client_disconnected_by_server(SilcClient client, SILC_LOG_DEBUG(("Server disconnected us, sock %d", sock->sock)); - msg = silc_calloc(message->len + 1, sizeof(char)); - memcpy(msg, message->data, message->len); + msg = silc_memdup(message->data, message->len); client->internal->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg); silc_free(msg); @@ -1462,8 +1460,7 @@ void silc_client_error_by_server(SilcClient client, { char *msg; - msg = silc_calloc(message->len + 1, sizeof(char)); - memcpy(msg, message->data, message->len); + msg = silc_memdup(message->data, message->len); client->internal->ops->say(client, sock->user_data, SILC_CLIENT_MESSAGE_AUDIT, msg); silc_free(msg); diff --git a/lib/silcclient/client_channel.c b/lib/silcclient/client_channel.c index 4f89d727..1a4a6d59 100644 --- a/lib/silcclient/client_channel.c +++ b/lib/silcclient/client_channel.c @@ -396,8 +396,7 @@ void silc_client_save_channel_key(SilcClient client, key = silc_channel_key_get_key(payload, &tmp_len); cipher = silc_channel_key_get_cipher(payload, NULL); channel->key_len = tmp_len * 8; - channel->key = silc_calloc(tmp_len, sizeof(*channel->key)); - memcpy(channel->key, key, tmp_len); + channel->key = silc_memdup(key, tmp_len); if (!silc_cipher_alloc(cipher, &channel->channel_key)) { client->internal->ops->say( @@ -520,8 +519,7 @@ int silc_client_add_channel_private_key(SilcClient client, /* Save the key */ entry = silc_calloc(1, sizeof(*entry)); - entry->key = silc_calloc(keymat->enc_key_len / 8, sizeof(*entry->key)); - memcpy(entry->key, keymat->send_enc_key, keymat->enc_key_len / 8); + entry->key = silc_memdup(keymat->send_enc_key, keymat->enc_key_len / 8); entry->key_len = keymat->enc_key_len / 8; /* Allocate the cipher and set the key*/ diff --git a/lib/silcclient/client_prvmsg.c b/lib/silcclient/client_prvmsg.c index 450fb405..88bedabe 100644 --- a/lib/silcclient/client_prvmsg.c +++ b/lib/silcclient/client_prvmsg.c @@ -344,8 +344,7 @@ int silc_client_add_private_message_key(SilcClient client, } /* Save the key */ - client_entry->key = silc_calloc(key_len, sizeof(*client_entry->key)); - memcpy(client_entry->key, key, key_len); + client_entry->key = silc_memdup(key, key_len); client_entry->key_len = key_len; /* Produce the key material as the protocol defines */ diff --git a/lib/silcclient/command_reply.c b/lib/silcclient/command_reply.c index f5658994..9e8d9a9e 100644 --- a/lib/silcclient/command_reply.c +++ b/lib/silcclient/command_reply.c @@ -278,10 +278,7 @@ silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd, } if (fingerprint && !client_entry->fingerprint) { - client_entry->fingerprint = - silc_calloc(fingerprint_len, - sizeof(*client_entry->fingerprint)); - memcpy(client_entry->fingerprint, fingerprint, fingerprint_len); + client_entry->fingerprint = silc_memdup(fingerprint, fingerprint_len); client_entry->fingerprint_len = fingerprint_len; } diff --git a/lib/silcclient/protocol.c b/lib/silcclient/protocol.c index 3354f1d0..da7bc07f 100644 --- a/lib/silcclient/protocol.c +++ b/lib/silcclient/protocol.c @@ -151,11 +151,8 @@ void silc_client_protocol_ke_set_keys(SilcSKE ske, /* Rekey stuff */ conn->rekey = silc_calloc(1, sizeof(*conn->rekey)); - conn->rekey->send_enc_key = - silc_calloc(keymat->enc_key_len / 8, - sizeof(*conn->rekey->send_enc_key)); - memcpy(conn->rekey->send_enc_key, - keymat->send_enc_key, keymat->enc_key_len / 8); + conn->rekey->send_enc_key = silc_memdup(keymat->send_enc_key, + keymat->enc_key_len / 8); conn->rekey->enc_key_len = keymat->enc_key_len / 8; if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS) @@ -785,11 +782,8 @@ silc_client_protocol_rekey_validate(SilcClient client, if (!send) { memset(conn->rekey->send_enc_key, 0, conn->rekey->enc_key_len); silc_free(conn->rekey->send_enc_key); - conn->rekey->send_enc_key = - silc_calloc(keymat->enc_key_len / 8, - sizeof(*conn->rekey->send_enc_key)); - memcpy(conn->rekey->send_enc_key, keymat->send_enc_key, - keymat->enc_key_len / 8); + conn->rekey->send_enc_key = silc_memdup(keymat->send_enc_key, + keymat->enc_key_len / 8); conn->rekey->enc_key_len = keymat->enc_key_len / 8; } } diff --git a/lib/silccore/silcauth.c b/lib/silccore/silcauth.c index e66f9653..3ee76ca4 100644 --- a/lib/silccore/silcauth.c +++ b/lib/silccore/silcauth.c @@ -181,8 +181,7 @@ silc_auth_public_key_encode_data(SilcPublicKey public_key, SILC_STR_UI_XNSTRING(pk, pk_len), SILC_STR_END); - ret = silc_calloc(buf->len + 1, sizeof(*ret)); - memcpy(ret, buf->data, buf->len); + ret = silc_memdup(buf->data, buf->len); if (ret_len) *ret_len = buf->len; diff --git a/lib/silccore/silccommand.c b/lib/silccore/silccommand.c index 0d64787d..04f03d6b 100644 --- a/lib/silccore/silccommand.c +++ b/lib/silccore/silccommand.c @@ -228,8 +228,7 @@ SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, if (!x_type || !x || !x_len) continue; - argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char)); - memcpy(argv[k], x, x_len); + argv[k] = silc_memdup(x, x_len); argv_lens[k] = x_len; argv_types[k] = x_type; k++; @@ -290,8 +289,7 @@ silc_command_reply_payload_encode_vap(SilcCommand cmd, argv_types = silc_calloc(argc, sizeof(uint32)); SILC_PUT16_MSB(status, status_data); - argv[0] = silc_calloc(sizeof(status_data) + 1, sizeof(unsigned char)); - memcpy(argv[0], status_data, sizeof(status_data)); + argv[0] = silc_memdup(status_data, sizeof(status_data)); argv_lens[0] = sizeof(status_data); argv_types[0] = 1; @@ -303,8 +301,7 @@ silc_command_reply_payload_encode_vap(SilcCommand cmd, if (!x_type || !x || !x_len) continue; - argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char)); - memcpy(argv[k], x, x_len); + argv[k] = silc_memdup(x, x_len); argv_lens[k] = x_len; argv_types[k] = x_type; k++; diff --git a/lib/silccore/silcid.c b/lib/silccore/silcid.c index 9a35723a..e3a37d8e 100644 --- a/lib/silccore/silcid.c +++ b/lib/silccore/silcid.c @@ -182,14 +182,10 @@ void *silc_id_payload_get_id(SilcIDPayload payload) unsigned char *silc_id_payload_get_data(SilcIDPayload payload) { - unsigned char *ret; - if (!payload) return NULL; - ret = silc_calloc(payload->len, sizeof(*ret)); - memcpy(ret, payload->id, payload->len); - return ret; + return silc_memdup(payload->id, payload->len); } /* Get length of ID */ @@ -336,26 +332,20 @@ void *silc_id_dup(const void *id, SilcIdType type) switch(type) { case SILC_ID_SERVER: { - SilcServerID *server_id = (SilcServerID *)id, *new; - new = silc_calloc(1, sizeof(*server_id)); - memcpy(new, server_id, sizeof(*server_id)); - return new; + SilcServerID *server_id = (SilcServerID *)id; + return silc_memdup(server_id, sizeof(*server_id)); } break; case SILC_ID_CLIENT: { - SilcClientID *client_id = (SilcClientID *)id, *new; - new = silc_calloc(1, sizeof(*client_id)); - memcpy(new, client_id, sizeof(*client_id)); - return new; + SilcClientID *client_id = (SilcClientID *)id; + return silc_memdup(client_id, sizeof(*client_id)); } break; case SILC_ID_CHANNEL: { - SilcChannelID *channel_id = (SilcChannelID *)id, *new; - new = silc_calloc(1, sizeof(*channel_id)); - memcpy(new, channel_id, sizeof(*channel_id)); - return new; + SilcChannelID *channel_id = (SilcChannelID *)id; + return silc_memdup(channel_id, sizeof(*channel_id)); } break; } diff --git a/lib/silccore/silcnotify.c b/lib/silccore/silcnotify.c index 99f342cf..c3600c52 100644 --- a/lib/silccore/silcnotify.c +++ b/lib/silccore/silcnotify.c @@ -101,8 +101,7 @@ SilcBuffer silc_notify_payload_encode(SilcNotifyType type, uint32 argc, if (!x || !x_len) continue; - argv[k] = silc_calloc(x_len + 1, sizeof(unsigned char)); - memcpy(argv[k], x, x_len); + argv[k] = silc_memdup(x, x_len); argv_lens[k] = x_len; argv_types[k] = i + 1; k++; diff --git a/lib/silcutil/DIRECTORY b/lib/silcutil/DIRECTORY index 1caefbf3..a48ad6b0 100644 --- a/lib/silcutil/DIRECTORY +++ b/lib/silcutil/DIRECTORY @@ -3,7 +3,6 @@ @FILENAME=silcutillib.html @LINK=silcbuffer.html:SILC Buffer Interface @LINK=silcbuffmt.html:SILC Buffer Format Interface -@LINK=silcbufutil.html:SILC Buffer Utility Interface @LINK=silchashtable.html:SILC Hash Table Interface @LINK=silclog.html:SILC Logging Interface @LINK=silcmemory.html:SILC Memory Interface diff --git a/lib/silcutil/Makefile.am b/lib/silcutil/Makefile.am index a88f243b..ac4e73e0 100644 --- a/lib/silcutil/Makefile.am +++ b/lib/silcutil/Makefile.am @@ -46,7 +46,6 @@ if SILC_DIST_TOOLKIT include_HEADERS = \ silcbuffer.h \ silcbuffmt.h \ - silcbufutil.h \ silcconfig.h \ silchashtable.h \ silclog.h \ diff --git a/lib/silcutil/silcbuffer.h b/lib/silcutil/silcbuffer.h index b91d6cb6..9a64329e 100644 --- a/lib/silcutil/silcbuffer.h +++ b/lib/silcutil/silcbuffer.h @@ -1,16 +1,15 @@ /* - silcbuffer.h + silcbuffer.h - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1998 - 2000 Pekka Riikonen + Copyright (C) 1998 - 2002 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - + the Free Software Foundation; version 2 of the License. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -367,4 +366,78 @@ unsigned char *silc_buffer_put_tail(SilcBuffer sb, return (unsigned char *)memcpy(sb->tail, data, len); } +/* Clears and initialiazes the buffer to the state as if it was just + allocated by silc_buffer_alloc. */ + +static inline +void silc_buffer_clear(SilcBuffer sb) +{ + memset(sb->head, 0, sb->truelen); + sb->data = sb->head; + sb->tail = sb->head; + sb->len = 0; +} + +/* Generates copy of a SilcBuffer. This copies everything inside the + currently valid data area, nothing more. Use silc_buffer_clone to + copy entire buffer. */ + +static inline +SilcBuffer silc_buffer_copy(SilcBuffer sb) +{ + SilcBuffer sb_new; + + sb_new = silc_buffer_alloc(sb->len); + silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + silc_buffer_put(sb_new, sb->data, sb->len); + + return sb_new; +} + +/* Clones SilcBuffer. This generates new SilcBuffer and copies + everything from the source buffer. The result is exact clone of + the original buffer. */ + +static inline +SilcBuffer silc_buffer_clone(SilcBuffer sb) +{ + SilcBuffer sb_new; + + sb_new = silc_buffer_alloc(sb->truelen); + silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + silc_buffer_put(sb_new, sb->head, sb->truelen); + sb_new->data = sb_new->head + (sb->data - sb->head); + sb_new->tail = sb_new->data + sb->len; + sb_new->len = sb->len; + + return sb_new; +} + +/* Reallocates buffer. Old data is saved into the new buffer. Returns + new SilcBuffer pointer. The buffer is exact clone of the old one + except that there is now more space at the end of buffer. */ + +static inline +SilcBuffer silc_buffer_realloc(SilcBuffer sb, uint32 newsize) +{ + SilcBuffer sb_new; + + if (!sb) + return silc_buffer_alloc(newsize); + + if (newsize <= sb->truelen) + return sb; + + sb_new = silc_buffer_alloc(newsize); + silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); + silc_buffer_put(sb_new, sb->head, sb->truelen); + sb_new->data = sb_new->head + (sb->data - sb->head); + sb_new->tail = sb_new->data + sb->len; + sb_new->len = sb->len; + + silc_buffer_free(sb); + + return sb_new; +} + #endif diff --git a/lib/silcutil/silcbufutil.h b/lib/silcutil/silcbufutil.h deleted file mode 100644 index ecf99e03..00000000 --- a/lib/silcutil/silcbufutil.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - - silcbufutil.h - - Author: Pekka Riikonen - - Copyright (C) 1997 - 2000 Pekka Riikonen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - -*/ - -#ifndef SILCBUFUTIL_H -#define SILCBUFUTIL_H - -#include "silcbuffer.h" - -/* Clears and initialiazes the buffer to the state as if it was just - allocated by silc_buffer_alloc. */ - -static inline -void silc_buffer_clear(SilcBuffer sb) -{ - memset(sb->head, 0, sb->truelen); - sb->data = sb->head; - sb->tail = sb->head; - sb->len = 0; -} - -/* Generates copy of a SilcBuffer. This copies everything inside the - currently valid data area, nothing more. Use silc_buffer_clone to - copy entire buffer. */ - -static inline -SilcBuffer silc_buffer_copy(SilcBuffer sb) -{ - SilcBuffer sb_new; - - sb_new = silc_buffer_alloc(sb->len); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); - silc_buffer_put(sb_new, sb->data, sb->len); - - return sb_new; -} - -/* Clones SilcBuffer. This generates new SilcBuffer and copies - everything from the source buffer. The result is exact clone of - the original buffer. */ - -static inline -SilcBuffer silc_buffer_clone(SilcBuffer sb) -{ - SilcBuffer sb_new; - - sb_new = silc_buffer_alloc(sb->truelen); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); - silc_buffer_put(sb_new, sb->head, sb->truelen); - sb_new->data = sb_new->head + (sb->data - sb->head); - sb_new->tail = sb_new->data + sb->len; - sb_new->len = sb->len; - - return sb_new; -} - -/* Reallocates buffer. Old data is saved into the new buffer. Returns - new SilcBuffer pointer. The buffer is exact clone of the old one - except that there is now more space at the end of buffer. */ - -static inline -SilcBuffer silc_buffer_realloc(SilcBuffer sb, uint32 newsize) -{ - SilcBuffer sb_new; - - if (!sb) - return silc_buffer_alloc(newsize); - - if (newsize <= sb->truelen) - return sb; - - sb_new = silc_buffer_alloc(newsize); - silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new)); - silc_buffer_put(sb_new, sb->head, sb->truelen); - sb_new->data = sb_new->head + (sb->data - sb->head); - sb_new->tail = sb_new->data + sb->len; - sb_new->len = sb->len; - - silc_buffer_free(sb); - - return sb_new; -} - -#endif diff --git a/lib/silcutil/silcconfig.h b/lib/silcutil/silcconfig.h index 13265734..b4f5a704 100644 --- a/lib/silcutil/silcconfig.h +++ b/lib/silcutil/silcconfig.h @@ -26,20 +26,21 @@ * (or File object) and SilcConfigEntity (or Entity). The File objects are * structs directly corresponding to the real files in the filesystem, while * Entities are a little more abstract. + * * An Entity is composed by delimited area on a File object (it can take the * whole File object or just part of it), plus a group of known options. - * * In order to parse this file, first you need to create a File object with * the silc_config_open() function, and then you need to create the Entity * with the silc_config_init() function. + * * Now you can use the newly created Entity to register a group of expected * known options and sub-blocks, and then you can call the main parsing loop - * with the silc_config_main() function. - * When silc_config_main() will return, if some error encoured the object file - * will point to the file that caused this error (this can be different from - * the originally opened file if it contained `Include' directives). If no - * errors encoured then the File objects will still point to the original - * file. + * with the silc_config_main() function. When silc_config_main() will + * return, if some error encoured the object file will point to the file + * that caused this error (this can be different from the originally + * opened file if it contained `Include' directives). If no errors + * encoured then the File objects will still point to the original file. + * * While silc_config_main() will take care of destroying Entities before * returning, you need to take care that the File object you created is freed * with the silc_config_close() function. @@ -51,6 +52,7 @@ * The config file syntax is pretty straightforward. All lines starting * with `#' will be skipped, while sub-blocks are delimited by braces (see * the example below). + * * Options with argument must have the `=' character between the option * name and the value. Simple words and numbers does not require quoting. * There is a special built-in directive "Include" which allows you to include diff --git a/lib/silcutil/silclist.h b/lib/silcutil/silclist.h index 6f309349..326837e1 100644 --- a/lib/silcutil/silclist.h +++ b/lib/silcutil/silclist.h @@ -18,7 +18,7 @@ */ -/****h* silcutil/SilcList +/****h* silcutil/SilcListAPI * * DESCRIPTION * diff --git a/lib/silcutil/silcmemory.c b/lib/silcutil/silcmemory.c index 2df6fee4..eb246557 100644 --- a/lib/silcutil/silcmemory.c +++ b/lib/silcutil/silcmemory.c @@ -1,16 +1,15 @@ /* - silcmemory.c + silcmemory.c - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1999 - 2000 Pekka Riikonen + Copyright (C) 1999 - 2002 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - + the Free Software Foundation; version 2 of the License. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -49,3 +48,12 @@ void silc_free(void *ptr) { free(ptr); } + +void *silc_memdup(const void *ptr, size_t size) +{ + unsigned char *addr = silc_malloc(size + 1); + assert(addr != NULL); + memcpy((void *)addr, ptr, size); + addr[size] = '\0'; + return (void *)addr; +} diff --git a/lib/silcutil/silcmemory.h b/lib/silcutil/silcmemory.h index 100f511d..f05800be 100644 --- a/lib/silcutil/silcmemory.h +++ b/lib/silcutil/silcmemory.h @@ -1,16 +1,15 @@ /* - silcmemory.h + silcmemory.h - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1999 - 2000 Pekka Riikonen + Copyright (C) 1999 - 2002 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - + the Free Software Foundation; version 2 of the License. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -18,13 +17,104 @@ */ +/****h* silcutil/SilcMemoryAPI + * + * DESCRIPTION + * + * Basic utility functions for allocating memory. All SILC routines, and + * applications use these functions when they need to allocate, manipulate + * and free memory. + * + * Currently all allocation routines assert() that the memory was allocated + * successfully. Hence, if memory allocation fails it is fatal error. + * + ***/ + #ifndef SILCMEMORY_H #define SILCMEMORY_H /* Prototypes */ + +/****f* silcutil/SilcMemoryAPI/silc_malloc + * + * SYNOPSIS + * + * void *silc_malloc(size_t size); + * + * DESCRIPTION + * + * Allocates memory of `size' bytes and returns pointer to the allocated + * memory area. Free the memory by calling silc_free. + * + ***/ void *silc_malloc(size_t size); + +/****f* silcutil/SilcMemoryAPI/silc_calloc + * + * SYNOPSIS + * + * void *silc_calloc(size_t items, size_t size); + * + * DESCRIPTION + * + * Allocates memory of for an array of `items' elements of `size' bytes + * and returns pointer to the allocated memory area. The memory are is + * also zeroed. Free the memory by calling silc_free. + * + ***/ void *silc_calloc(size_t items, size_t size); + +/****f* silcutil/SilcMemoryAPI/silc_realloc + * + * SYNOPSIS + * + * void *silc_realloc(void *ptr, size_t size); + * + * DESCRIPTION + * + * Change the size of the memory block indicated by `ptr' to the new + * size of `size' bytes. The contents of `ptr' will not be changed. + * If `ptr' is NULL the call is equivalent to silc_malloc. If the + * `size' is zero (0) the call is equivalent to silc_free. Free the + * memory by calling silc_free. + * + * NOTES + * + * The pointer returned to the reallocated memory area might not be + * same as `ptr'. + * + ***/ void *silc_realloc(void *ptr, size_t size); + +/****f* silcutil/SilcMemoryAPI/silc_free + * + * SYNOPSIS + * + * void silc_free(void *ptr); + * + * DESCRIPTION + * + * Frees the memory area indicated by the `ptr'. If `ptr' is NULL no + * operation is performed. + * + ***/ void silc_free(void *ptr); -#endif +/****f* silcutil/SilcMemoryAPI/silc_memdup + * + * SYNOPSIS + * + * void *silc_memdup(const void *ptr, size_t size); + * + * DESCRIPTION + * + * Duplicates the memory area indicated by `ptr' which is of size + * of `size' bytes. Returns pointer to the duplicated memory area. + * This NULL terminates the dupped memory area by allocating `size' + 1 + * bytes, so this function can be used to duplicate strings that does + * not have NULL termination. + * + ***/ +void *silc_memdup(const void *ptr, size_t size); + +#endif /* SILCMEMORY_H */ diff --git a/win32/libsilc/libsilc.def b/win32/libsilc/libsilc.def index 81a750d8..3691c4ca 100644 --- a/win32/libsilc/libsilc.def +++ b/win32/libsilc/libsilc.def @@ -557,3 +557,4 @@ EXPORTS silc_log_flushdelay @ 848 DATA ; silc_hash_table_list_reset @ 849 ; silc_debug_hexdump @ 850 DATA ; + silc_memdup @ 851 ; -- 2.24.0