From: Pekka Riikonen Date: Sat, 30 Mar 2002 19:24:47 +0000 (+0000) Subject: structure optimizations again. X-Git-Tag: silc.client.0.8.6~4^2~53 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=c1ad9c3e8978b0e1537b5bbdf89e385029429516 structure optimizations again. --- diff --git a/CHANGES b/CHANGES index b38d57e3..404c90ba 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,12 @@ +Sat Mar 30 21:06:45 EET 2002 Pekka Riikonen + + * Optimized even more the SilcPacketContext structure. Now + totally saved 16 bytes of memory per context after optimization. + Affected files are lib/silccore/silcpacket.[ch]. + + * Made strict checks for valid SILC IDs. Affected file is + lib/silccore/silcid.c. + Sat Mar 30 18:15:55 EET 2002 Pekka Riikonen * Changed the object argument for silc_cipher_register, diff --git a/lib/silccore/silcid.c b/lib/silccore/silcid.c index 6a7fb8f2..6e892c08 100644 --- a/lib/silccore/silcid.c +++ b/lib/silccore/silcid.c @@ -62,6 +62,9 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload, if (ret == -1) goto err; + if (newp->type > SILC_ID_CHANNEL) + goto err; + silc_buffer_pull(&buffer, 4); if (newp->len > buffer.len || newp->len > SILC_PACKET_MAX_ID_LEN) @@ -102,6 +105,9 @@ void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len, if (ret == -1) return NULL; + if (type > SILC_ID_CHANNEL) + return NULL; + silc_buffer_pull(&buffer, 4); if (idlen > buffer.len || idlen > SILC_PACKET_MAX_ID_LEN) @@ -209,6 +215,9 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type) SilcChannelID *channel_id; SilcUInt32 id_len = silc_id_get_len(id, type); + if (id_len > SILC_PACKET_MAX_ID_LEN) + return NULL; + switch(type) { case SILC_ID_SERVER: server_id = (SilcServerID *)id; @@ -251,6 +260,8 @@ unsigned char *silc_id_id2str(const void *id, SilcIdType type) void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type) { + if (id_len > SILC_PACKET_MAX_ID_LEN) + return NULL; switch(type) { case SILC_ID_SERVER: diff --git a/lib/silccore/silcpacket.c b/lib/silccore/silcpacket.c index 7c3f2b0d..89aae80b 100644 --- a/lib/silccore/silcpacket.c +++ b/lib/silccore/silcpacket.c @@ -585,6 +585,7 @@ SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher) SilcBuffer buffer = ctx->buffer; SilcUInt8 tmp; int len, ret; + SilcUInt8 src_id_len, src_id_type, dst_id_len, dst_id_type, padlen; SILC_LOG_DEBUG(("Parsing incoming packet")); @@ -599,34 +600,46 @@ SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher) SILC_STR_UI_SHORT(&ctx->truelen), SILC_STR_UI_CHAR(&ctx->flags), SILC_STR_UI_CHAR(&ctx->type), - SILC_STR_UI_CHAR(&ctx->padlen), + SILC_STR_UI_CHAR(&padlen), SILC_STR_UI_CHAR(&tmp), - SILC_STR_UI_CHAR(&ctx->src_id_len), - SILC_STR_UI_CHAR(&ctx->dst_id_len), - SILC_STR_UI_CHAR(&ctx->src_id_type), + SILC_STR_UI_CHAR(&src_id_len), + SILC_STR_UI_CHAR(&dst_id_len), + SILC_STR_UI_CHAR(&src_id_type), SILC_STR_END); if (len == -1 || tmp != 0) return SILC_PACKET_NONE; - if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN || - ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) { + if (src_id_len > SILC_PACKET_MAX_ID_LEN || + dst_id_len > SILC_PACKET_MAX_ID_LEN) { SILC_LOG_ERROR(("Bad ID lengths in packet (%d and %d)", - ctx->src_id_len, ctx->dst_id_len)); + src_id_len, dst_id_len)); return SILC_PACKET_NONE; } silc_buffer_pull(buffer, len); ret = silc_buffer_unformat(buffer, SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id, - ctx->src_id_len), - SILC_STR_UI_CHAR(&ctx->dst_id_type), + src_id_len), + SILC_STR_UI_CHAR(&dst_id_type), SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id, - ctx->dst_id_len), - SILC_STR_UI_XNSTRING(NULL, ctx->padlen), + dst_id_len), + SILC_STR_UI_XNSTRING(NULL, padlen), SILC_STR_END); if (ret == -1) return SILC_PACKET_NONE; + if (src_id_type > SILC_ID_CHANNEL || dst_id_type > SILC_ID_CHANNEL) { + SILC_LOG_ERROR(("Bad ID types in packet (%d and %d", + src_id_type, dst_id_type)); + return SILC_PACKET_NONE; + } + + ctx->src_id_len = src_id_len; + ctx->dst_id_len = dst_id_len; + ctx->src_id_type = src_id_type; + ctx->dst_id_type = dst_id_type; + ctx->padlen = padlen; + silc_buffer_push(buffer, len); SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len), @@ -653,6 +666,7 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx, SilcBuffer buffer = ctx->buffer; SilcUInt8 tmp; int len, ret; + SilcUInt8 src_id_len, src_id_type, dst_id_len, dst_id_type, padlen; SILC_LOG_DEBUG(("Parsing incoming packet")); @@ -667,38 +681,50 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx, SILC_STR_UI_SHORT(&ctx->truelen), SILC_STR_UI_CHAR(&ctx->flags), SILC_STR_UI_CHAR(&ctx->type), - SILC_STR_UI_CHAR(&ctx->padlen), + SILC_STR_UI_CHAR(&padlen), SILC_STR_UI_CHAR(&tmp), - SILC_STR_UI_CHAR(&ctx->src_id_len), - SILC_STR_UI_CHAR(&ctx->dst_id_len), - SILC_STR_UI_CHAR(&ctx->src_id_type), + SILC_STR_UI_CHAR(&src_id_len), + SILC_STR_UI_CHAR(&dst_id_len), + SILC_STR_UI_CHAR(&src_id_type), SILC_STR_END); if (len == -1 || tmp != 0) { SILC_LOG_ERROR(("Malformed packet header, packet dropped")); return SILC_PACKET_NONE; } - if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN || - ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) { + if (src_id_len > SILC_PACKET_MAX_ID_LEN || + dst_id_len > SILC_PACKET_MAX_ID_LEN) { SILC_LOG_ERROR(("Bad ID lengths in packet (%d and %d)", - ctx->src_id_len, ctx->dst_id_len)); + src_id_len, dst_id_len)); return SILC_PACKET_NONE; } silc_buffer_pull(buffer, len); ret = silc_buffer_unformat(buffer, SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id, - ctx->src_id_len), - SILC_STR_UI_CHAR(&ctx->dst_id_type), + src_id_len), + SILC_STR_UI_CHAR(&dst_id_type), SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id, - ctx->dst_id_len), - SILC_STR_UI_XNSTRING(NULL, ctx->padlen), + dst_id_len), + SILC_STR_UI_XNSTRING(NULL, padlen), SILC_STR_END); if (ret == -1) { SILC_LOG_ERROR(("Malformed packet header, packet dropped")); return SILC_PACKET_NONE; } + if (src_id_type > SILC_ID_CHANNEL || dst_id_type > SILC_ID_CHANNEL) { + SILC_LOG_ERROR(("Bad ID types in packet (%d and %d", + src_id_type, dst_id_type)); + return SILC_PACKET_NONE; + } + + ctx->src_id_len = src_id_len; + ctx->dst_id_len = dst_id_len; + ctx->src_id_type = src_id_type; + ctx->dst_id_type = dst_id_type; + ctx->padlen = padlen; + silc_buffer_push(buffer, len); SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len), diff --git a/lib/silccore/silcpacket.h b/lib/silccore/silcpacket.h index 79123628..ea7dbbfb 100644 --- a/lib/silccore/silcpacket.h +++ b/lib/silccore/silcpacket.h @@ -180,44 +180,46 @@ typedef unsigned char SilcPacketFlags; * * unsigned char *src_id * SilcUInt8 src_id_len - * unsigned char src_id_type + * SilcUInt8 src_id_type * * Source ID, its length and type. On packet reception retuned ID's * are always the hash values of the ID's from the packet. * * unsigned char *dst_id; * SilcUInt8 dst_id_len; - * unsigned char src_id_type; + * SilcUInt8 src_id_type; * * Destination ID, its length and type. On packet reception retuned * ID's are always the hash values of the ID's from the packet. * - * SilcUInt8 padlen - * - * The padded length of the packet. This may be set by the caller - * before calling any of the silc_packet_* routines. If not provided - * the library will calculate the values. - * - * unsigned int long_pad + * bool long_pad * * If set to TRUE the packet will include the maximum padding allowed * in SILC packet, which is 128 bytes. If FALSE only the amount of * padding needed will be applied. * - * unsigned int users; + * SilcUInt16 users; * * Reference counter for this context. The context is freed only * after the reference counter hits zero. The counter is added * calling silc_packet_context_dup and decreased by calling the * silc_packet_context_free. * + * SilcUInt8 padlen + * + * The padded length of the packet. This may be set by the caller + * before calling any of the silc_packet_* routines. If not provided + * the library will calculate the values. + * * SilcUInt32 sequence; * - * Packet sequence number. + * Packet sequence number. Set only when this context is a parsed + * packet. * * SilcBuffer buffer * - * The actual packet data. + * The actual packet data. Set only when this context is a parsed + * packet. * ***/ typedef struct { @@ -227,14 +229,13 @@ typedef struct { unsigned char *src_id; unsigned char *dst_id; - SilcUInt8 src_id_len; - SilcUInt8 src_id_type; - SilcUInt8 dst_id_len; - SilcUInt8 dst_id_type; - - SilcUInt8 padlen; - unsigned int long_pad : 1; /* Set when maximum padding in packet */ - unsigned int users : 23; /* Reference counter */ + unsigned int src_id_len : 5; + unsigned int src_id_type : 2; + unsigned int dst_id_len : 5; + unsigned int dst_id_type : 2; + unsigned int long_pad : 1; /* Set when maximum padding in packet */ + unsigned int users : 9; /* Reference counter */ + unsigned int padlen : 8; SilcUInt32 sequence; SilcBuffer buffer;