📄 tlv.c
字号:
/** * Adds the given userinfo struct to a TLV chain. * * @param list Destination chain. * @param type TLV type to add. * @return The size of the value added. */int aim_tlvlist_add_userinfo(GSList **list, guint16 type, aim_userinfo_t *userinfo){ guint8 buf[1024]; /* TODO: Don't use a fixed length buffer */ ByteStream bs; byte_stream_init(&bs, buf, sizeof(buf)); aim_putuserinfo(&bs, userinfo); return aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), buf);}/** * Adds the given chatroom info to a TLV chain. * * @param list Destination chain. * @param type TLV type to add. * @param roomname The name of the chat. * @param instance The instance. * @return The size of the value added. */int aim_tlvlist_add_chatroom(GSList **list, guint16 type, guint16 exchange, const char *roomname, guint16 instance){ int len; ByteStream bs; byte_stream_new(&bs, 2 + 1 + strlen(roomname) + 2); byte_stream_put16(&bs, exchange); byte_stream_put8(&bs, strlen(roomname)); byte_stream_putstr(&bs, roomname); byte_stream_put16(&bs, instance); len = aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), bs.data); g_free(bs.data); return len;}/** * Adds a TLV with a zero length to a TLV chain. * * @param list Destination chain. * @param type TLV type to add. * @return The size of the value added. */int aim_tlvlist_add_noval(GSList **list, const guint16 type){ return aim_tlvlist_add_raw(list, type, 0, NULL);}/* * Note that the inner TLV chain will not be modifiable as a tlvchain once * it is written using this. Or rather, it can be, but updates won't be * made to this. * * TODO: Should probably support sublists for real. * * This is so neat. * * @param list Destination chain. * @param type TLV type to add. * @param t1 The TLV chain you want to write. * @return The number of bytes written to the destination TLV chain. * 0 is returned if there was an error or if the destination * TLV chain has length 0. */int aim_tlvlist_add_frozentlvlist(GSList **list, guint16 type, GSList **tlvlist){ int buflen; ByteStream bs; buflen = aim_tlvlist_size(*tlvlist); if (buflen <= 0) return 0; byte_stream_new(&bs, buflen); aim_tlvlist_write(&bs, tlvlist); aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), bs.data); g_free(bs.data); return buflen;}/** * Substitute a TLV of a given type with a new TLV of the same type. If * you attempt to replace a TLV that does not exist, this function will * just add a new TLV as if you called aim_tlvlist_add_raw(). * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. * @param length Length of string to add (not including %NULL). * @param value String to add. * @return The length of the TLV. */int aim_tlvlist_replace_raw(GSList **list, const guint16 type, const guint16 length, const guint8 *value){ GSList *cur; aim_tlv_t *tlv; if (list == NULL) return 0; for (cur = *list; cur != NULL; cur = cur->next) { tlv = cur->data; if (tlv->type == type) break; } if (cur == NULL) /* TLV does not exist, so add a new one */ return aim_tlvlist_add_raw(list, type, length, value); g_free(tlv->value); tlv->length = length; if (tlv->length > 0) { tlv->value = g_memdup(value, length); } else tlv->value = NULL; return tlv->length;}/** * Substitute a TLV of a given type with a new TLV of the same type. If * you attempt to replace a TLV that does not exist, this function will * just add a new TLV as if you called aim_tlvlist_add_str(). * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. * @param str String to add. * @return The length of the TLV. */int aim_tlvlist_replace_str(GSList **list, const guint16 type, const char *str){ return aim_tlvlist_replace_raw(list, type, strlen(str), (const guchar *)str);}/** * Substitute a TLV of a given type with a new TLV of the same type. If * you attempt to replace a TLV that does not exist, this function will * just add a new TLV as if you called aim_tlvlist_add_raw(). * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. * @return The length of the TLV. */int aim_tlvlist_replace_noval(GSList **list, const guint16 type){ return aim_tlvlist_replace_raw(list, type, 0, NULL);}/** * Substitute a TLV of a given type with a new TLV of the same type. If * you attempt to replace a TLV that does not exist, this function will * just add a new TLV as if you called aim_tlvlist_add_raw(). * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. * @param value 8 bit value to add. * @return The length of the TLV. */int aim_tlvlist_replace_8(GSList **list, const guint16 type, const guint8 value){ guint8 v8[1]; aimutil_put8(v8, value); return aim_tlvlist_replace_raw(list, type, 1, v8);}/** * Substitute a TLV of a given type with a new TLV of the same type. If * you attempt to replace a TLV that does not exist, this function will * just add a new TLV as if you called aim_tlvlist_add_raw(). * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. * @param value 32 bit value to add. * @return The length of the TLV. */int aim_tlvlist_replace_32(GSList **list, const guint16 type, const guint32 value){ guint8 v32[4]; aimutil_put32(v32, value); return aim_tlvlist_replace_raw(list, type, 4, v32);}/** * Remove all TLVs of a given type. If you attempt to remove a TLV * that does not exist, nothing happens. * * @param list Desination chain (%NULL pointer if empty). * @param type TLV type. */void aim_tlvlist_remove(GSList **list, const guint16 type){ GSList *cur, *next; aim_tlv_t *tlv; if (list == NULL || *list == NULL) return; cur = *list; while (cur != NULL) { tlv = cur->data; next = cur->next; if (tlv->type == type) { /* Delete this TLV */ *list = g_slist_delete_link(*list, cur); g_free(tlv->value); g_free(tlv); } cur = next; }}/** * Write a TLV chain into a data buffer. * * Copies a TLV chain into a raw data buffer, writing only the number * of bytes specified. This operation does not free the chain; * aim_tlvlist_free() must still be called to free up the memory used * by the chain structures. * * TODO: Clean this up, make better use of bstreams * * @param bs Input bstream * @param list Source TLV chain * @return Return 0 if the destination bstream is too small. */int aim_tlvlist_write(ByteStream *bs, GSList **list){ int goodbuflen; GSList *cur; aim_tlv_t *tlv; /* do an initial run to test total length */ goodbuflen = aim_tlvlist_size(*list); if (goodbuflen > byte_stream_empty(bs)) return 0; /* not enough buffer */ /* do the real write-out */ for (cur = *list; cur; cur = cur->next) { tlv = cur->data; byte_stream_put16(bs, tlv->type); byte_stream_put16(bs, tlv->length); if (tlv->length > 0) byte_stream_putraw(bs, tlv->value, tlv->length); } return 1; /* TODO: This is a nonsensical return */}/** * Grab the Nth TLV of type type in the TLV list list. * * Returns a pointer to an aim_tlv_t of the specified type; * %NULL on error. The @nth parameter is specified starting at %1. * In most cases, there will be no more than one TLV of any type * in a chain. * * @param list Source chain. * @param type Requested TLV type. * @param nth Index of TLV of type to get. * @return The TLV you were looking for, or NULL if one could not be found. */aim_tlv_t *aim_tlv_gettlv(GSList *list, const guint16 type, const int nth){ GSList *cur; aim_tlv_t *tlv; int i; for (cur = list, i = 0; cur != NULL; cur = cur->next) { tlv = cur->data; if (tlv != NULL) { /* TODO: This NULL check shouldn't be needed */ if (tlv->type == type) i++; if (i >= nth) return tlv; } } return NULL;}/** * Get the length of the data of the nth TLV in the given TLV chain. * * @param list Source chain. * @param type Requested TLV type. * @param nth Index of TLV of type to get. * @return The length of the data in this TLV, or -1 if the TLV could not be * found. Unless -1 is returned, this value will be 2 bytes. */int aim_tlv_getlength(GSList *list, const guint16 type, const int nth){ aim_tlv_t *tlv; tlv = aim_tlv_gettlv(list, type, nth); if (tlv == NULL) return -1; return tlv->length;}char *aim_tlv_getvalue_as_string(aim_tlv_t *tlv){ char *ret; ret = g_malloc(tlv->length + 1); memcpy(ret, tlv->value, tlv->length); ret[tlv->length] = '\0'; return ret;}/** * Retrieve the data from the nth TLV in the given TLV chain as a string. * * @param list Source TLV chain. * @param type TLV type to search for. * @param nth Index of TLV to return. * @return The value of the TLV you were looking for, or NULL if one could * not be found. This is a dynamic buffer and must be freed by the * caller. */char *aim_tlv_getstr(GSList *list, const guint16 type, const int nth){ aim_tlv_t *tlv; tlv = aim_tlv_gettlv(list, type, nth); if (tlv == NULL) return NULL; return aim_tlv_getvalue_as_string(tlv);}/** * Retrieve the data from the nth TLV in the given TLV chain as an 8bit * integer. * * @param list Source TLV chain. * @param type TLV type to search for. * @param nth Index of TLV to return. * @return The value the TLV you were looking for, or 0 if one could * not be found. */guint8 aim_tlv_get8(GSList *list, const guint16 type, const int nth){ aim_tlv_t *tlv; tlv = aim_tlv_gettlv(list, type, nth); if (tlv == NULL) return 0; /* erm */ return aimutil_get8(tlv->value);}/** * Retrieve the data from the nth TLV in the given TLV chain as a 16bit * integer. * * @param list Source TLV chain. * @param type TLV type to search for. * @param nth Index of TLV to return. * @return The value the TLV you were looking for, or 0 if one could * not be found. */guint16 aim_tlv_get16(GSList *list, const guint16 type, const int nth){ aim_tlv_t *tlv; tlv = aim_tlv_gettlv(list, type, nth); if (tlv == NULL) return 0; /* erm */ return aimutil_get16(tlv->value);}/** * Retrieve the data from the nth TLV in the given TLV chain as a 32bit * integer. * * @param list Source TLV chain. * @param type TLV type to search for. * @param nth Index of TLV to return. * @return The value the TLV you were looking for, or 0 if one could * not be found. */guint32 aim_tlv_get32(GSList *list, const guint16 type, const int nth){ aim_tlv_t *tlv; tlv = aim_tlv_gettlv(list, type, nth); if (tlv == NULL) return 0; /* erm */ return aimutil_get32(tlv->value);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -