Added silc_argument_get_decoded.
authorPekka Riikonen <priikone@silcnet.org>
Tue, 18 Jul 2006 13:15:14 +0000 (13:15 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Tue, 18 Jul 2006 13:15:14 +0000 (13:15 +0000)
lib/silccore/silcargument.c
lib/silccore/silcargument.h
lib/silccore/silcattrs.c
lib/silccore/silccommand.c

index aa6083e3f084321684890d96a091cb7a207bce24..18446fbe2f9242ca559051b619bddcf5441f64fa 100644 (file)
@@ -297,3 +297,93 @@ unsigned char *silc_argument_get_arg_type(SilcArgumentPayload payload,
 
   return payload->argv[i];
 }
+
+/* Return argument already decoded */
+
+SilcBool silc_argument_get_decoded(SilcArgumentPayload payload,
+                                  SilcUInt32 type,
+                                  SilcArgumentDecodeType dec_type,
+                                  void *ret_arg,
+                                  void **ret_arg_alloc)
+{
+  unsigned char *tmp;
+  SilcUInt32 tmp_len;
+
+  tmp = silc_argument_get_arg_type(payload, type, &tmp_len);
+  if (!tmp)
+    return FALSE;
+
+  switch (dec_type) {
+
+  case SILC_ARGUMENT_ID:
+    if (ret_arg)
+      if (!silc_id_payload_parse_id(tmp, tmp_len, (SilcID *)ret_arg))
+       return FALSE;
+
+    if (ret_arg_alloc) {
+      SilcID id;
+      if (!silc_id_payload_parse_id(tmp, tmp_len, &id))
+       return FALSE;
+      *ret_arg_alloc = silc_memdup(&id, sizeof(id));
+    }
+    break;
+
+  case SILC_ARGUMENT_PUBLIC_KEY:
+    {
+      SilcPublicKey public_key;
+
+      if (!ret_arg_alloc)
+       return FALSE;
+
+      if (!silc_public_key_payload_decode(tmp, tmp_len, &public_key))
+       return FALSE;
+
+      *ret_arg_alloc = public_key;
+    }
+    break;
+
+  case SILC_ARGUMENT_ATTRIBUTES:
+    if (!ret_arg_alloc)
+      return FALSE;
+
+    *ret_arg_alloc = silc_attribute_payload_parse(tmp, tmp_len);
+    break;
+
+  case SILC_ARGUMENT_UINT32:
+    if (tmp_len != 4)
+      return FALSE;
+
+    if (ret_arg) {
+      SilcUInt32 *i = ret_arg;
+      SILC_GET32_MSB(*i, tmp);
+    }
+
+    if (ret_arg_alloc) {
+      SilcUInt32 i;
+      SILC_GET32_MSB(i, tmp);
+      *ret_arg_alloc = silc_memdup(&i, sizeof(i));
+    }
+    break;
+
+  case SILC_ARGUMENT_BOOL:
+    if (tmp_len != 1)
+      return FALSE;
+
+    if (ret_arg) {
+      SilcBool *b = ret_arg;
+      *b = (tmp[0] == 0x01 ? TRUE : FALSE);
+    }
+
+    if (ret_arg_alloc) {
+      SilcBool b;
+      b = (tmp[0] == 0x01 ? TRUE : FALSE);
+      *ret_arg_alloc = silc_memdup(&b, sizeof(b));
+    }
+    break;
+
+  default:
+    return FALSE;
+  }
+
+  return TRUE;
+}
index 50da74b6ba68aaebeef2b62168a85047003af56d..430872f34f685b10752e9a1b5e9f85b4eb490500 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  silcargument.h 
+  silcargument.h
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2002 Pekka Riikonen
+  Copyright (C) 2001 - 2006 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
@@ -21,7 +21,7 @@
  *
  * DESCRIPTION
  *
- * Implementation of the Argument Payload, that is used to include 
+ * Implementation of the Argument Payload, that is used to include
  * argument to other payload that needs arguments.
  *
  ***/
@@ -32,7 +32,7 @@
 /****s* silccore/SilcArgumentAPI/SilcArgumentPayload
  *
  * NAME
- * 
+ *
  *    typedef struct SilcArgumentPayloadStruct *SilcArgumentPayload;
  *
  * DESCRIPTION
@@ -49,7 +49,7 @@ typedef struct SilcArgumentPayloadStruct *SilcArgumentPayload;
  *
  * SYNOPSIS
  *
- *    SilcArgumentPayload 
+ *    SilcArgumentPayload
  *    silc_argument_payload_parse(const unsigned char *payload,
  *                                SilcUInt32 payload_len,
  *                                SilcUInt32 argc);
@@ -82,7 +82,7 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload,
  *    Encodes arguments in to Argument Paylods returning them to SilcBuffer.
  *    The `argv' is the array of the arguments, the `argv_lens' array of
  *    the length of the `argv' arguments and the `argv_types' array of
- *    the argument types of the `argv' arguments. The `argc' is the 
+ *    the argument types of the `argv' arguments. The `argc' is the
  *    number of arguments.
  *
  ***/
@@ -117,7 +117,7 @@ SilcBuffer silc_argument_payload_encode_one(SilcBuffer args,
  *
  * SYNOPSIS
  *
- *    SilcBuffer 
+ *    SilcBuffer
  *    silc_argument_payload_encode_payload(SilcArgumentPayload payload);
  *
  * DESCRIPTION
@@ -213,4 +213,68 @@ unsigned char *silc_argument_get_arg_type(SilcArgumentPayload payload,
                                          SilcUInt32 type,
                                          SilcUInt32 *ret_len);
 
+/****d* silccore/SilcArgumentAPI/SilcArgumentDecodeType
+ *
+ * NAME
+ *
+ *    typedef enum { ... } SilcArgumentDecodeType;
+ *
+ * DESCRIPTION
+ *
+ *    Argument decode types used with silc_argument_get_decoded.
+ *
+ * SOURCE
+ */
+typedef enum {
+  SILC_ARGUMENT_ID,            /* SilcID */
+  SILC_ARGUMENT_PUBLIC_KEY,    /* SilcPublicKey (always alloc) */
+  SILC_ARGUMENT_ATTRIBUTES,    /* SilcDList (always alloc) */
+  SILC_ARGUMENT_UINT32,                /* SilcUInt32 */
+  SILC_ARGUMENT_BOOL,          /* SilcBool */
+} SilcArgumentDecodeType;
+/***/
+
+/****f* silccore/SilcArgumentAPI/silc_argument_get_decoded
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_argument_get_decoded(SilcArgumentPayload payload,
+ *                                       SilcUInt32 type,
+ *                                       SilcArgumentDecodeType dec_type,
+ *                                       void *ret_arg,
+ *                                       void *ret_arg_alloc);
+ *
+ * DESCRIPTION
+ *
+ *    Returns decoded argument by type.  This is a helper function to
+ *    decode common argument types directly.  The `type' is the argument
+ *    type number in the payload, and the `dec_type' is the type the
+ *    argument is decoded to.  If the `ret_arg' is non-NULL then the
+ *    decodec data is returned into that pointer.  If the `ret_arg_alloc'
+ *    is non-NULL then this function will allocate the decoded data and
+ *    will return the pointer into `ret_arg_alloc'.  Some types must always
+ *    be allocated; see SilcArgumentDecodeType.
+ *
+ *    Return TRUE if the argument was present and waa successfully decoded.
+ *    FALSE if it is not present, or could not be decoded.
+ *
+ * EXAMPLE
+ *
+ *    SilcID id;
+ *    SilcPublicKey public_key;
+ *
+ *    if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
+ *      error;
+ *
+ *    if (!silc_argument_get_decoded(args, 4, SILC_ARGUMENT_PUBLIC_KEY,
+ *                                   NULL, &public_key))
+ *      error;
+ *
+ ***/
+SilcBool silc_argument_get_decoded(SilcArgumentPayload payload,
+                                  SilcUInt32 type,
+                                  SilcArgumentDecodeType dec_type,
+                                  void *ret_arg,
+                                  void **ret_arg_alloc);
+
 #endif
index 3dd0b79b9731ceeead70e0d3f34bf804fb3e713b..ebef5eeca7e94b6cce6d61283dcf0650f7a87bde 100644 (file)
@@ -468,7 +468,7 @@ unsigned char *silc_attribute_get_verify_data(SilcDList attrs,
 /* Return parsed attribute object */
 
 SilcBool silc_attribute_get_object(SilcAttributePayload payload,
-                              void *object, SilcUInt32 object_size)
+                                  void *object, SilcUInt32 object_size)
 {
   SilcUInt16 len;
   SilcBool ret = FALSE;
index 83dbde8cf6263e1f218019c410123a572747e1cc..0f575f64f4f6898e1142595d1b3215f56867f7ce 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005 Pekka Riikonen
+  Copyright (C) 1997 - 2006 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
@@ -401,8 +401,8 @@ SilcUInt16 silc_command_get_ident(SilcCommandPayload payload)
 /* Return command status */
 
 SilcBool silc_command_get_status(SilcCommandPayload payload,
-                            SilcStatus *status,
-                            SilcStatus *error)
+                                SilcStatus *status,
+                                SilcStatus *error)
 {
   unsigned char *tmp;
   SilcUInt32 tmp_len;