+/******************************************************************************
+
+ ID Payload
+
+******************************************************************************/
+
+struct SilcIDPayloadStruct {
+ SilcIdType type;
+ SilcUInt16 len;
+ unsigned char *id;
+};
+
+/* Parses buffer and return ID payload into payload structure */
+
+SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
+ SilcUInt32 payload_len)
+{
+ SilcBufferStruct buffer;
+ SilcIDPayload newp;
+ int ret;
+
+ silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
+
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI_SHORT(&newp->type),
+ SILC_STR_UI_SHORT(&newp->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ if (newp->type > SILC_ID_CHANNEL)
+ goto err;
+
+ silc_buffer_pull(&buffer, 4);
+
+ if (newp->len > silc_buffer_len(&buffer) ||
+ newp->len > SILC_PACKET_MAX_ID_LEN)
+ goto err;
+
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ return newp;
+
+ err:
+ SILC_LOG_DEBUG(("Error parsing ID payload"));
+ silc_free(newp);
+ return NULL;
+}
+
+/* Return the ID directly from the raw payload data. */
+
+SilcBool silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
+ SilcIdType *ret_type, void *ret_id,
+ SilcUInt32 ret_id_size)
+{
+ SilcBufferStruct buffer;
+ SilcIdType type;
+ SilcUInt16 idlen;
+ unsigned char *id_data;
+ int ret;
+
+ silc_buffer_set(&buffer, (unsigned char *)data, len);
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI_SHORT(&type),
+ SILC_STR_UI_SHORT(&idlen),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ if (type > SILC_ID_CHANNEL)
+ goto err;
+
+ silc_buffer_pull(&buffer, 4);
+
+ if (idlen > silc_buffer_len(&buffer) || idlen > SILC_PACKET_MAX_ID_LEN)
+ goto err;
+
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI_XNSTRING(&id_data, idlen),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ if (!silc_id_str2id(id_data, idlen, type, ret_id, ret_id_size))
+ goto err;
+
+ if (ret_type)
+ *ret_type = type;
+
+ return TRUE;
+
+ err:
+ SILC_LOG_DEBUG(("Error parsing ID payload"));
+ return FALSE;
+}
+
+/* Encodes ID Payload */
+
+SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
+{
+ SilcBuffer buffer;
+ unsigned char id_data[32];
+ SilcUInt32 len;
+
+ if (!silc_id_id2str(id, type, id_data, sizeof(id_data), &len))
+ return NULL;
+ buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
+ len, type);
+ return buffer;
+}
+
+SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
+ SilcUInt32 id_len, SilcIdType type)
+{
+ SilcBuffer buffer;
+
+ buffer = silc_buffer_alloc_size(4 + id_len);
+ if (!buffer)
+ return NULL;
+ silc_buffer_format(buffer,
+ SILC_STR_UI_SHORT(type),
+ SILC_STR_UI_SHORT(id_len),
+ SILC_STR_UI_XNSTRING(id, id_len),
+ SILC_STR_END);
+ return buffer;
+}
+
+/* Free ID Payload */
+
+void silc_id_payload_free(SilcIDPayload payload)
+{
+ if (payload) {
+ silc_free(payload->id);
+ silc_free(payload);
+ }
+}
+
+/* Get ID type */
+
+SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
+{
+ return payload ? payload->type : 0;
+}
+
+/* Get ID */
+
+SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
+ SilcUInt32 ret_id_len)
+{
+ if (!payload)
+ return FALSE;
+ return silc_id_str2id(payload->id, payload->len, payload->type,
+ ret_id, ret_id_len);
+}
+
+/* Get raw ID data. Data is duplicated. */
+
+unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
+{
+ if (!payload)
+ return NULL;
+
+ return silc_memdup(payload->id, payload->len);
+}
+
+/* Get length of ID */
+
+SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload)
+{
+ return payload ? payload->len : 0;
+}
+