From: Pekka Riikonen Date: Tue, 18 Jul 2006 13:15:14 +0000 (+0000) Subject: Added silc_argument_get_decoded. X-Git-Tag: 1.2.beta1~722 X-Git-Url: http://git.silcnet.org/gitweb/?a=commitdiff_plain;h=27126950b0669ffcecd5ecd12b1a813705587d1d;p=runtime.git Added silc_argument_get_decoded. --- diff --git a/lib/silccore/silcargument.c b/lib/silccore/silcargument.c index aa6083e3..18446fbe 100644 --- a/lib/silccore/silcargument.c +++ b/lib/silccore/silcargument.c @@ -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; +} diff --git a/lib/silccore/silcargument.h b/lib/silccore/silcargument.h index 50da74b6..430872f3 100644 --- a/lib/silccore/silcargument.h +++ b/lib/silccore/silcargument.h @@ -1,10 +1,10 @@ /* - silcargument.h + silcargument.h Author: Pekka Riikonen - 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 diff --git a/lib/silccore/silcattrs.c b/lib/silccore/silcattrs.c index 3dd0b79b..ebef5eec 100644 --- a/lib/silccore/silcattrs.c +++ b/lib/silccore/silcattrs.c @@ -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; diff --git a/lib/silccore/silccommand.c b/lib/silccore/silccommand.c index 83dbde8c..0f575f64 100644 --- a/lib/silccore/silccommand.c +++ b/lib/silccore/silccommand.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - 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;