structure optimizations again.
authorPekka Riikonen <priikone@silcnet.org>
Sat, 30 Mar 2002 19:24:47 +0000 (19:24 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 30 Mar 2002 19:24:47 +0000 (19:24 +0000)
CHANGES
lib/silccore/silcid.c
lib/silccore/silcpacket.c
lib/silccore/silcpacket.h

diff --git a/CHANGES b/CHANGES
index b38d57e34f9cbc24072a76f19a0a6b8cbce89776..404c90ba9b0dd0afabade76b437a279256d637da 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,12 @@
+Sat Mar 30 21:06:45 EET 2002  Pekka Riikonen <priikone@silcnet.org>
+
+       * 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 <priikone@silcnet.org>
 
        * Changed the object argument for silc_cipher_register,
index 6a7fb8f2c72c38c20902cd95f137045b61b32c00..6e892c087b2a8dedb675bbf4206408873fe48637 100644 (file)
@@ -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:
index 7c3f2b0d90cb40a5f0a49d5646f3b3209f923571..89aae80b2edb362caa740e190fa2ce8e726f128a 100644 (file)
@@ -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), 
index 7912362811eed97c2bb208a6bc36c44ddea334cf..ea7dbbfb9d792b0dd42a91aa73c8ef17450a7c6c 100644 (file)
@@ -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;