Index: libfaim/tlv.c
===================================================================
--- libfaim/tlv.c	(revision e374dee70435f8c596358571092675644a9ba731)
+++ libfaim/tlv.c	(revision cf02dd64c267036375fdf872c3528766fe980d54)
@@ -3,13 +3,15 @@
 #include <aim.h>
 
-static aim_tlv_t *createtlv(void)
-{
-	aim_tlv_t *newtlv;
-
-	if (!(newtlv = (aim_tlv_t *)malloc(sizeof(aim_tlv_t))))
+static aim_tlv_t *createtlv(fu16_t type, fu16_t length, fu8_t *value)
+{
+	aim_tlv_t *ret;
+
+	if (!(ret = (aim_tlv_t *)malloc(sizeof(aim_tlv_t))))
 		return NULL;
-	memset(newtlv, 0, sizeof(aim_tlv_t));
-
-	return newtlv;
+	ret->type = type;
+	ret->length = length;
+	ret->value = value;
+
+	return ret;
 }
 
@@ -28,10 +30,9 @@
 
 /**
- * aim_readtlvchain - Read a TLV chain from a buffer.
- * @param bs Input bstream
+ * Read a TLV chain from a buffer.
  *
  * Reads and parses a series of TLV patterns from a data buffer; the
  * returned structure is manipulatable with the rest of the TLV
- * routines.  When done with a TLV chain, aim_freetlvchain() should
+ * routines.  When done with a TLV chain, aim_tlvlist_free() should
  * be called to free the dynamic substructures.
  *
@@ -42,6 +43,7 @@
  * in libfaim.
  *
- */
-faim_internal aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs)
+ * @param bs Input bstream
+ */
+faim_internal aim_tlvlist_t *aim_tlvlist_read(aim_bstream_t *bs)
 {
 	aim_tlvlist_t *list = NULL, *cur;
@@ -71,5 +73,5 @@
 
 			if (length > aim_bstream_empty(bs)) {
-				aim_freetlvchain(&list);
+				aim_tlvlist_free(&list);
 				return NULL;
 			}
@@ -77,5 +79,5 @@
 			cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
 			if (!cur) {
-				aim_freetlvchain(&list);
+				aim_tlvlist_free(&list);
 				return NULL;
 			}
@@ -83,19 +85,18 @@
 			memset(cur, 0, sizeof(aim_tlvlist_t));
 
-			cur->tlv = createtlv();
+			cur->tlv = createtlv(type, length, NULL);
 			if (!cur->tlv) {
 				free(cur);
-				aim_freetlvchain(&list);
+				aim_tlvlist_free(&list);
 				return NULL;
 			}
-			cur->tlv->type = type;
-			if ((cur->tlv->length = length)) {
-			       cur->tlv->value = aimbs_getraw(bs, length);	
-			       if (!cur->tlv->value) {
-				       freetlv(&cur->tlv);
-				       free(cur);
-				       aim_freetlvchain(&list);
-				       return NULL;
-			       }
+			if (cur->tlv->length > 0) {
+				cur->tlv->value = aimbs_getraw(bs, length);	
+				if (!cur->tlv->value) {
+					freetlv(&cur->tlv);
+					free(cur);
+					aim_tlvlist_free(&list);
+					return NULL;
+				}
 			}
 
@@ -109,5 +110,17 @@
 
 /**
- * aim_readtlvchain_num - Read a TLV chain from a buffer.
+ * Read a TLV chain from a buffer.
+ *
+ * Reads and parses a series of TLV patterns from a data buffer; the
+ * returned structure is manipulatable with the rest of the TLV
+ * routines.  When done with a TLV chain, aim_tlvlist_free() should
+ * be called to free the dynamic substructures.
+ *
+ * XXX There should be a flag setable here to have the tlvlist contain
+ * bstream references, so that at least the ->value portion of each 
+ * element doesn't need to be malloc/memcpy'd.  This could prove to be
+ * just as effecient as the in-place TLV parsing used in a couple places
+ * in libfaim.
+ *
  * @param bs Input bstream
  * @param num The max number of TLVs that will be read, or -1 if unlimited.  
@@ -115,8 +128,59 @@
  *        but the chain is not at the end of the SNAC, and the chain is 
  *        preceeded by the number of TLVs.  So you can limit that with this.
+ */
+faim_internal aim_tlvlist_t *aim_tlvlist_readnum(aim_bstream_t *bs, fu16_t num)
+{
+	aim_tlvlist_t *list = NULL, *cur;
+
+	while ((aim_bstream_empty(bs) > 0) && (num != 0)) {
+		fu16_t type, length;
+
+		type = aimbs_get16(bs);
+		length = aimbs_get16(bs);
+
+		if (length > aim_bstream_empty(bs)) {
+			aim_tlvlist_free(&list);
+			return NULL;
+		}
+
+		cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
+		if (!cur) {
+			aim_tlvlist_free(&list);
+			return NULL;
+		}
+
+		memset(cur, 0, sizeof(aim_tlvlist_t));
+
+		cur->tlv = createtlv(type, length, NULL);
+		if (!cur->tlv) {
+			free(cur);
+			aim_tlvlist_free(&list);
+			return NULL;
+		}
+		if (cur->tlv->length > 0) {
+			cur->tlv->value = aimbs_getraw(bs, length);
+			if (!cur->tlv->value) {
+				freetlv(&cur->tlv);
+				free(cur);
+				aim_tlvlist_free(&list);
+				return NULL;
+			}
+		}
+
+		if (num > 0)
+			num--;
+		cur->next = list;
+		list = cur;
+	}
+
+	return list;
+}
+
+/**
+ * Read a TLV chain from a buffer.
  *
  * Reads and parses a series of TLV patterns from a data buffer; the
  * returned structure is manipulatable with the rest of the TLV
- * routines.  When done with a TLV chain, aim_freetlvchain() should
+ * routines.  When done with a TLV chain, aim_tlvlist_free() should
  * be called to free the dynamic substructures.
  *
@@ -127,55 +191,4 @@
  * in libfaim.
  *
- */
-faim_internal aim_tlvlist_t *aim_readtlvchain_num(aim_bstream_t *bs, fu16_t num)
-{
-	aim_tlvlist_t *list = NULL, *cur;
-
-	while ((aim_bstream_empty(bs) > 0) && (num != 0)) {
-		fu16_t type, length;
-
-		type = aimbs_get16(bs);
-		length = aimbs_get16(bs);
-
-		if (length > aim_bstream_empty(bs)) {
-			aim_freetlvchain(&list);
-			return NULL;
-		}
-
-		cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
-		if (!cur) {
-			aim_freetlvchain(&list);
-			return NULL;
-		}
-
-		memset(cur, 0, sizeof(aim_tlvlist_t));
-
-		cur->tlv = createtlv();
-		if (!cur->tlv) {
-			free(cur);
-			aim_freetlvchain(&list);
-			return NULL;
-		}
-		cur->tlv->type = type;
-		if ((cur->tlv->length = length)) {
-		       cur->tlv->value = aimbs_getraw(bs, length);
-		       if (!cur->tlv->value) {
-			       freetlv(&cur->tlv);
-			       free(cur);
-			       aim_freetlvchain(&list);
-			       return NULL;
-		       }
-		}
-
-		num--;
-		cur->next = list;
-		list = cur;
-	}
-
-	return list;
-}
-
-/**
- * aim_readtlvchain_len - Read a TLV chain from a buffer.
  * @param bs Input bstream
  * @param len The max length in bytes that will be read.
@@ -183,18 +196,6 @@
  *        but the chain is not at the end of the SNAC, and the chain is 
  *        preceeded by the length of the TLVs.  So you can limit that with this.
- *
- * Reads and parses a series of TLV patterns from a data buffer; the
- * returned structure is manipulatable with the rest of the TLV
- * routines.  When done with a TLV chain, aim_freetlvchain() should
- * be called to free the dynamic substructures.
- *
- * XXX There should be a flag setable here to have the tlvlist contain
- * bstream references, so that at least the ->value portion of each 
- * element doesn't need to be malloc/memcpy'd.  This could prove to be
- * just as effecient as the in-place TLV parsing used in a couple places
- * in libfaim.
- *
- */
-faim_internal aim_tlvlist_t *aim_readtlvchain_len(aim_bstream_t *bs, fu16_t len)
+ */
+faim_internal aim_tlvlist_t *aim_tlvlist_readlen(aim_bstream_t *bs, fu16_t len)
 {
 	aim_tlvlist_t *list = NULL, *cur;
@@ -207,5 +208,5 @@
 
 		if (length > aim_bstream_empty(bs)) {
-			aim_freetlvchain(&list);
+			aim_tlvlist_free(&list);
 			return NULL;
 		}
@@ -213,5 +214,5 @@
 		cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
 		if (!cur) {
-			aim_freetlvchain(&list);
+			aim_tlvlist_free(&list);
 			return NULL;
 		}
@@ -219,22 +220,21 @@
 		memset(cur, 0, sizeof(aim_tlvlist_t));
 
-		cur->tlv = createtlv();
+		cur->tlv = createtlv(type, length, NULL);
 		if (!cur->tlv) {
 			free(cur);
-			aim_freetlvchain(&list);
+			aim_tlvlist_free(&list);
 			return NULL;
 		}
-		cur->tlv->type = type;
-		if ((cur->tlv->length = length)) {
-		       cur->tlv->value = aimbs_getraw(bs, length);
-		       if (!cur->tlv->value) {
-			       freetlv(&cur->tlv);
-			       free(cur);
-			       aim_freetlvchain(&list);
-			       return NULL;
-		       }
-		}
-
-		len -= aim_sizetlvchain(&cur);
+		if (cur->tlv->length > 0) {
+			cur->tlv->value = aimbs_getraw(bs, length);
+			if (!cur->tlv->value) {
+				freetlv(&cur->tlv);
+				free(cur);
+				aim_tlvlist_free(&list);
+				return NULL;
+			}
+		}
+
+		len -= aim_tlvlist_size(&cur);
 		cur->next = list;
 		list = cur;
@@ -245,9 +245,9 @@
 
 /**
- * aim_tlvlist_copy - Duplicate a TLV chain.
- * @param orig
- *
+ * Duplicate a TLV chain.
  * This is pretty pelf exslanatory.
  *
+ * @param orig The TLV chain you want to make a copy of.
+ * @return A newly allocated TLV chain.
  */
 faim_internal aim_tlvlist_t *aim_tlvlist_copy(aim_tlvlist_t *orig)
@@ -256,5 +256,5 @@
 
 	while (orig) {
-		aim_addtlvtochain_raw(&new, orig->tlv->type, orig->tlv->length, orig->tlv->value);
+		aim_tlvlist_add_raw(&new, orig->tlv->type, orig->tlv->length, orig->tlv->value);
 		orig = orig->next;
 	}
@@ -275,12 +275,12 @@
 	aim_bstream_t bs1, bs2;
 
-	if (aim_sizetlvchain(&one) != aim_sizetlvchain(&two))
+	if (aim_tlvlist_size(&one) != aim_tlvlist_size(&two))
 		return 1;
 
-	aim_bstream_init(&bs1, ((fu8_t *)malloc(aim_sizetlvchain(&one)*sizeof(fu8_t))), aim_sizetlvchain(&one));
-	aim_bstream_init(&bs2, ((fu8_t *)malloc(aim_sizetlvchain(&two)*sizeof(fu8_t))), aim_sizetlvchain(&two));
-
-	aim_writetlvchain(&bs1, &one);
-	aim_writetlvchain(&bs2, &two);
+	aim_bstream_init(&bs1, ((fu8_t *)malloc(aim_tlvlist_size(&one)*sizeof(fu8_t))), aim_tlvlist_size(&one));
+	aim_bstream_init(&bs2, ((fu8_t *)malloc(aim_tlvlist_size(&two)*sizeof(fu8_t))), aim_tlvlist_size(&two));
+
+	aim_tlvlist_write(&bs1, &one);
+	aim_tlvlist_write(&bs2, &two);
 
 	if (memcmp(bs1.data, bs2.data, bs1.len)) {
@@ -297,5 +297,5 @@
 
 /**
- * aim_freetlvchain - Free a TLV chain structure
+ * Free a TLV chain structure
  * @list: Chain to be freed
  *
@@ -305,5 +305,5 @@
  *
  */
-faim_internal void aim_freetlvchain(aim_tlvlist_t **list)
+faim_internal void aim_tlvlist_free(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
@@ -328,11 +328,10 @@
 
 /**
- * aim_counttlvchain - Count the number of TLVs in a chain
- * @list: Chain to be counted
- *
- * Returns the number of TLVs stored in the passed chain.
- *
- */
-faim_internal int aim_counttlvchain(aim_tlvlist_t **list)
+ * Count the number of TLVs in a chain.
+ *
+ * @param list Chain to be counted.
+ * @return The number of TLVs stored in the passed chain.
+ */
+faim_internal int aim_tlvlist_count(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
@@ -349,12 +348,11 @@
 
 /**
- * aim_sizetlvchain - Count the number of bytes in a TLV chain
- * @list: Chain to be sized
- *
- * Returns the number of bytes that would be needed to 
- * write the passed TLV chain to a data buffer.
- *
- */
-faim_internal int aim_sizetlvchain(aim_tlvlist_t **list)
+ * Count the number of bytes in a TLV chain.
+ *
+ * @param list Chain to be sized
+ * @return The number of bytes that would be needed to 
+ *         write the passed TLV chain to a data buffer.
+ */
+faim_internal int aim_tlvlist_size(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
@@ -371,19 +369,18 @@
 
 /**
- * aim_addtlvtochain_raw - Add a string to a TLV chain
- * @list: Desination chain (%NULL pointer if empty)
- * @t: TLV type
- * @l: Length of string to add (not including %NULL)
- * @v: String to add
- *
  * Adds the passed string as a TLV element of the passed type
  * to the TLV chain.
  *
- */
-faim_internal int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v)
+ * @param list Desination chain (%NULL pointer if empty).
+ * @param type TLV type.
+ * @length Length of string to add (not including %NULL).
+ * @value String to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_raw(aim_tlvlist_t **list, const fu16_t type, const fu16_t length, const fu8_t *value)
 {
 	aim_tlvlist_t *newtlv, *cur;
 
-	if (!list)
+	if (list == NULL)
 		return 0;
 
@@ -392,12 +389,11 @@
 	memset(newtlv, 0x00, sizeof(aim_tlvlist_t));
 
-	if (!(newtlv->tlv = createtlv())) {
+	if (!(newtlv->tlv = createtlv(type, length, NULL))) {
 		free(newtlv);
 		return 0;
 	}
-	newtlv->tlv->type = t;
-	if ((newtlv->tlv->length = l)) {
+	if (newtlv->tlv->length > 0) {
 		newtlv->tlv->value = (fu8_t *)malloc(newtlv->tlv->length);
-		memcpy(newtlv->tlv->value, v, newtlv->tlv->length);
+		memcpy(newtlv->tlv->value, value, newtlv->tlv->length);
 	}
 
@@ -414,80 +410,71 @@
 
 /**
- * aim_addtlvtochain8 - Add a 8bit integer to a TLV chain
- * @list: Destination chain
- * @type: TLV type to add
- * @val: Value to add
- *
- * Adds a one-byte unsigned integer to a TLV chain.
- *
- */
-faim_internal int aim_addtlvtochain8(aim_tlvlist_t **list, const fu16_t t, const fu8_t v)
+ * Add a one byte integer to a TLV chain.
+ *
+ * @param list Destination chain.
+ * @param type TLV type to add.
+ * @param value Value to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_8(aim_tlvlist_t **list, const fu16_t type, const fu8_t value)
 {
 	fu8_t v8[1];
 
-	aimutil_put8(v8, v);
-
-	return aim_addtlvtochain_raw(list, t, 1, v8);
-}
-
-/**
- * aim_addtlvtochain16 - Add a 16bit integer to a TLV chain
- * @list: Destination chain
- * @t: TLV type to add
- * @v: Value to add
- *
- * Adds a two-byte unsigned integer to a TLV chain.
- *
- */
-faim_internal int aim_addtlvtochain16(aim_tlvlist_t **list, const fu16_t t, const fu16_t v)
+	aimutil_put8(v8, value);
+
+	return aim_tlvlist_add_raw(list, type, 1, v8);
+}
+
+/**
+ * Add a two byte integer to a TLV chain.
+ *
+ * @param list Destination chain.
+ * @param type TLV type to add.
+ * @param value Value to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_16(aim_tlvlist_t **list, const fu16_t type, const fu16_t value)
 {
 	fu8_t v16[2];
 
-	aimutil_put16(v16, v);
-
-	return aim_addtlvtochain_raw(list, t, 2, v16);
-}
-
-/**
- * aim_addtlvtochain32 - Add a 32bit integer to a TLV chain
- * @list: Destination chain
- * @type: TLV type to add
- * @val: Value to add
- *
- * Adds a four-byte unsigned integer to a TLV chain.
- *
- */
-faim_internal int aim_addtlvtochain32(aim_tlvlist_t **list, const fu16_t t, const fu32_t v)
+	aimutil_put16(v16, value);
+
+	return aim_tlvlist_add_raw(list, type, 2, v16);
+}
+
+/**
+ * Add a four byte integer to a TLV chain.
+ *
+ * @param list Destination chain.
+ * @param type TLV type to add.
+ * @param value Value to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_32(aim_tlvlist_t **list, const fu16_t type, const fu32_t value)
 {
 	fu8_t v32[4];
 
-	aimutil_put32(v32, v);
-
-	return aim_addtlvtochain_raw(list, t, 4, v32);
-}
-
-/**
- * aim_addtlvtochain_caps - Add a capability block to a TLV chain
- * @list: Destination chain
- * @type: TLV type to add
- * @caps: Bitfield of capability flags to send
- *
+	aimutil_put32(v32, value);
+
+	return aim_tlvlist_add_raw(list, type, 4, v32);
+}
+
+/**
  * Adds a block of capability blocks to a TLV chain. The bitfield
  * passed in should be a bitwise %OR of any of the %AIM_CAPS constants:
  *
- *      %AIM_CAPS_BUDDYICON   Supports Buddy Icons
- *
- *      %AIM_CAPS_VOICE       Supports Voice Chat
- *
- *      %AIM_CAPS_IMIMAGE     Supports DirectIM/IMImage
- *
- *      %AIM_CAPS_CHAT        Supports Chat
- *
- *      %AIM_CAPS_GETFILE     Supports Get File functions
- *
- *      %AIM_CAPS_SENDFILE    Supports Send File functions
- *
- */
-faim_internal int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu32_t caps)
+ *     %AIM_CAPS_BUDDYICON   Supports Buddy Icons
+ *     %AIM_CAPS_VOICE       Supports Voice Chat
+ *     %AIM_CAPS_IMIMAGE     Supports DirectIM/IMImage
+ *     %AIM_CAPS_CHAT        Supports Chat
+ *     %AIM_CAPS_GETFILE     Supports Get File functions
+ *     %AIM_CAPS_SENDFILE    Supports Send File functions
+ *
+ * @param list Destination chain
+ * @param type TLV type to add
+ * @param caps Bitfield of capability flags to send
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_caps(aim_tlvlist_t **list, const fu16_t type, const fu32_t caps)
 {
 	fu8_t buf[16*16]; /* XXX icky fixed length buffer */
@@ -501,8 +488,15 @@
 	aim_putcap(&bs, caps);
 
-	return aim_addtlvtochain_raw(list, t, aim_bstream_curpos(&bs), buf);
-}
-
-faim_internal int aim_addtlvtochain_userinfo(aim_tlvlist_t **list, fu16_t type, aim_userinfo_t *ui)
+	return aim_tlvlist_add_raw(list, type, aim_bstream_curpos(&bs), buf);
+}
+
+/**
+ * Adds the given userinfo struct to a TLV chain.
+ *
+ * @param list Destination chain.
+ * @param type TLV type to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_userinfo(aim_tlvlist_t **list, fu16_t type, aim_userinfo_t *userinfo)
 {
 	fu8_t buf[1024]; /* bleh */
@@ -511,20 +505,19 @@
 	aim_bstream_init(&bs, buf, sizeof(buf));
 
-	aim_putuserinfo(&bs, ui);
-
-	return aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
-}
-
-/**
- * aim_addtlvtochain_noval - Add a blank TLV to a TLV chain
- * @list: Destination chain
- * @type: TLV type to add
- *
+	aim_putuserinfo(&bs, userinfo);
+
+	return aim_tlvlist_add_raw(list, type, aim_bstream_curpos(&bs), buf);
+}
+
+/**
  * Adds a TLV with a zero length to a TLV chain.
  *
- */
-faim_internal int aim_addtlvtochain_noval(aim_tlvlist_t **list, const fu16_t t)
-{
-	return aim_addtlvtochain_raw(list, t, 0, NULL);
+ * @param list Destination chain.
+ * @param type TLV type to add.
+ * @retun The size of the value added.
+ */
+faim_internal int aim_tlvlist_add_noval(aim_tlvlist_t **list, const fu16_t type)
+{
+	return aim_tlvlist_add_raw(list, type, 0, NULL);
 }
 
@@ -539,5 +532,5 @@
  *
  */
-faim_internal int aim_addtlvtochain_frozentlvlist(aim_tlvlist_t **list, fu16_t type, aim_tlvlist_t **tl)
+faim_internal int aim_tlvlist_add_frozentlvlist(aim_tlvlist_t **list, fu16_t type, aim_tlvlist_t **tl)
 {
 	fu8_t *buf;
@@ -545,5 +538,5 @@
 	aim_bstream_t bs;
 
-	buflen = aim_sizetlvchain(tl);
+	buflen = aim_tlvlist_size(tl);
 
 	if (buflen <= 0)
@@ -555,7 +548,7 @@
 	aim_bstream_init(&bs, buf, buflen);
 
-	aim_writetlvchain(&bs, tl);
-
-	aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
+	aim_tlvlist_write(&bs, tl);
+
+	aim_tlvlist_add_raw(list, type, aim_bstream_curpos(&bs), buf);
 
 	free(buf);
@@ -565,5 +558,123 @@
 
 /**
- * aim_writetlvchain - Write a TLV chain into a data buffer.
+ * 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.
+ */
+faim_internal int aim_tlvlist_replace_raw(aim_tlvlist_t **list, const fu16_t type, const fu16_t length, const fu8_t *value)
+{
+	aim_tlvlist_t *cur;
+
+	if (list == NULL)
+		return 0;
+
+	for (cur = *list; ((cur != NULL) && (cur->tlv->type != type)); cur = cur->next);
+	if (cur == NULL)
+		return aim_tlvlist_add_raw(list, type, length, value);
+
+	free(cur->tlv->value);
+	cur->tlv->length = length;
+	if (cur->tlv->length > 0) {
+		cur->tlv->value = (fu8_t *)malloc(cur->tlv->length);
+		memcpy(cur->tlv->value, value, cur->tlv->length);
+	} else
+		cur->tlv->value = NULL;
+
+	return cur->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_raw().
+ *
+ * @param list Desination chain (%NULL pointer if empty).
+ * @param type TLV type.
+ * @return The length of the TLV.
+ */
+faim_internal int aim_tlvlist_replace_noval(aim_tlvlist_t **list, const fu16_t 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.
+ */
+faim_internal int aim_tlvlist_replace_8(aim_tlvlist_t **list, const fu16_t type, const fu8_t value)
+{
+	fu8_t 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.
+ */
+faim_internal int aim_tlvlist_replace_32(aim_tlvlist_t **list, const fu16_t type, const fu32_t value)
+{
+	fu8_t v32[4];
+
+	aimutil_put32(v32, value);
+
+	return aim_tlvlist_replace_raw(list, type, 4, v32);
+}
+
+/**
+ * Remove a TLV 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.
+ */
+faim_internal void aim_tlvlist_remove(aim_tlvlist_t **list, const fu16_t type)
+{
+	aim_tlvlist_t *del;
+
+	if (!list || !(*list))
+		return;
+
+	/* Remove the item from the list */
+	if ((*list)->tlv->type == type) {
+		del = *list;
+		*list = (*list)->next;
+	} else {
+		aim_tlvlist_t *cur;
+		for (cur=*list; (cur->next && (cur->next->tlv->type!=type)); cur=cur->next);
+		if (!cur->next)
+			return;
+		del = cur->next;
+		cur->next = del->next;
+	}
+
+	/* Free the removed item */
+	free(del->tlv->value);
+	free(del->tlv);
+	free(del);
+}
+
+/**
+ * aim_tlvlist_write - Write a TLV chain into a data buffer.
  * @buf: Destination buffer
  * @buflen: Maximum number of bytes that will be written to buffer
@@ -572,10 +683,10 @@
  * Copies a TLV chain into a raw data buffer, writing only the number
  * of bytes specified. This operation does not free the chain; 
- * aim_freetlvchain() must still be called to free up the memory used
+ * aim_tlvlist_free() must still be called to free up the memory used
  * by the chain structures.
  *
  * XXX clean this up, make better use of bstreams 
  */
-faim_internal int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list)
+faim_internal int aim_tlvlist_write(aim_bstream_t *bs, aim_tlvlist_t **list)
 {
 	int goodbuflen;
@@ -583,5 +694,5 @@
 
 	/* do an initial run to test total length */
-	goodbuflen = aim_sizetlvchain(list);
+	goodbuflen = aim_tlvlist_size(list);
 
 	if (goodbuflen > aim_bstream_empty(bs))
@@ -601,8 +712,5 @@
 
 /**
- * aim_gettlv - Grab the Nth TLV of type type in the TLV list list.
- * @list: Source chain
- * @type: Requested TLV type
- * @nth: Index of TLV of type to get
+ * Grab the Nth TLV of type type in the TLV list list.
  *
  * Returns a pointer to an aim_tlv_t of the specified type; 
@@ -611,6 +719,10 @@
  * in a chain.
  *
- */
-faim_internal aim_tlv_t *aim_gettlv(aim_tlvlist_t *list, const fu16_t t, const int n)
+ * @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.
+ */
+faim_internal aim_tlv_t *aim_tlv_gettlv(aim_tlvlist_t *list, const fu16_t type, const int nth)
 {
 	aim_tlvlist_t *cur;
@@ -619,7 +731,7 @@
 	for (cur = list, i = 0; cur; cur = cur->next) {
 		if (cur && cur->tlv) {
-			if (cur->tlv->type == t)
+			if (cur->tlv->type == type)
 				i++;
-			if (i >= n)
+			if (i >= nth)
 				return cur->tlv;
 		}
@@ -630,25 +742,24 @@
 
 /**
- * aim_gettlv_str - Retrieve the Nth TLV in chain as a string.
- * @list: Source TLV chain
- * @type: TLV type to search for
- * @nth: Index of TLV to return
- *
- * Same as aim_gettlv(), except that the return value is a %NULL-
- * terminated string instead of an aim_tlv_t.  This is a 
- * dynamic buffer and must be freed by the caller.
- *
- */
-faim_internal char *aim_gettlv_str(aim_tlvlist_t *list, const fu16_t t, const int n)
+ * 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.
+ */
+faim_internal char *aim_tlv_getstr(aim_tlvlist_t *list, const fu16_t type, const int nth)
 {
 	aim_tlv_t *tlv;
 	char *newstr;
 
-	if (!(tlv = aim_gettlv(list, t, n)))
+	if (!(tlv = aim_tlv_gettlv(list, type, nth)))
 		return NULL;
 
 	newstr = (char *) malloc(tlv->length + 1);
 	memcpy(newstr, tlv->value, tlv->length);
-	*(newstr + tlv->length) = '\0';
+	newstr[tlv->length] = '\0';
 
 	return newstr;
@@ -656,18 +767,18 @@
 
 /**
- * aim_gettlv8 - Retrieve the Nth TLV in chain as a 8bit integer.
- * @list: Source TLV chain
- * @type: TLV type to search for
- * @nth: Index of TLV to return
- *
- * Same as aim_gettlv(), except that the return value is a 
- * 8bit integer instead of an aim_tlv_t. 
- *
- */
-faim_internal fu8_t aim_gettlv8(aim_tlvlist_t *list, const fu16_t t, const int n)
+ * 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.
+ */
+faim_internal fu8_t aim_tlv_get8(aim_tlvlist_t *list, const fu16_t type, const int nth)
 {
 	aim_tlv_t *tlv;
 
-	if (!(tlv = aim_gettlv(list, t, n)))
+	if (!(tlv = aim_tlv_gettlv(list, type, nth)))
 		return 0; /* erm */
 	return aimutil_get8(tlv->value);
@@ -675,18 +786,18 @@
 
 /**
- * aim_gettlv16 - Retrieve the Nth TLV in chain as a 16bit integer.
- * @list: Source TLV chain
- * @type: TLV type to search for
- * @nth: Index of TLV to return
- *
- * Same as aim_gettlv(), except that the return value is a 
- * 16bit integer instead of an aim_tlv_t. 
- *
- */
-faim_internal fu16_t aim_gettlv16(aim_tlvlist_t *list, const fu16_t t, const int n)
+ * 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.
+ */
+faim_internal fu16_t aim_tlv_get16(aim_tlvlist_t *list, const fu16_t type, const int nth)
 {
 	aim_tlv_t *tlv;
 
-	if (!(tlv = aim_gettlv(list, t, n)))
+	if (!(tlv = aim_tlv_gettlv(list, type, nth)))
 		return 0; /* erm */
 	return aimutil_get16(tlv->value);
@@ -694,101 +805,19 @@
 
 /**
- * aim_gettlv32 - Retrieve the Nth TLV in chain as a 32bit integer.
- * @list: Source TLV chain
- * @type: TLV type to search for
- * @nth: Index of TLV to return
- *
- * Same as aim_gettlv(), except that the return value is a 
- * 32bit integer instead of an aim_tlv_t. 
- *
- */
-faim_internal fu32_t aim_gettlv32(aim_tlvlist_t *list, const fu16_t t, const int n)
+ * 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.
+ */
+faim_internal fu32_t aim_tlv_get32(aim_tlvlist_t *list, const fu16_t type, const int nth)
 {
 	aim_tlv_t *tlv;
 
-	if (!(tlv = aim_gettlv(list, t, n)))
+	if (!(tlv = aim_tlv_gettlv(list, type, nth)))
 		return 0; /* erm */
 	return aimutil_get32(tlv->value);
 }
-
-#if 0
-/**
- * aim_puttlv_8 - Write a one-byte TLV.
- * @buf: Destination buffer
- * @t: TLV type
- * @v: Value
- *
- * Writes a TLV with a one-byte integer value portion.
- *
- */
-faim_export int aim_puttlv_8(fu8_t *buf, const fu16_t t, const fu8_t v)
-{
-	fu8_t v8[1];
-
-	aimutil_put8(v8, v);
-
-	return aim_puttlv_raw(buf, t, 1, v8);
-}
-
-/**
- * aim_puttlv_16 - Write a two-byte TLV.
- * @buf: Destination buffer
- * @t: TLV type
- * @v: Value
- *
- * Writes a TLV with a two-byte integer value portion.
- *
- */
-faim_export int aim_puttlv_16(fu8_t *buf, const fu16_t t, const fu16_t v)
-{
-	fu8_t v16[2];
-
-	aimutil_put16(v16, v);
-
-	return aim_puttlv_raw(buf, t, 2, v16);
-}
-
-
-/**
- * aim_puttlv_32 - Write a four-byte TLV.
- * @buf: Destination buffer
- * @t: TLV type
- * @v: Value
- *
- * Writes a TLV with a four-byte integer value portion.
- *
- */
-faim_export int aim_puttlv_32(fu8_t *buf, const fu16_t t, const fu32_t v)
-{
-	fu8_t v32[4];
-
-	aimutil_put32(v32, v);
-
-	return aim_puttlv_raw(buf, t, 4, v32);
-}
-
-/**
- * aim_puttlv_raw - Write a raw TLV.
- * @buf: Destination buffer
- * @t: TLV type
- * @l: Length of string
- * @v: String to write
- *
- * Writes a TLV with a raw value portion.  (Only the first @l
- * bytes of the passed buffer will be written, which should not
- * include a terminating NULL.)
- *
- */
-faim_export int aim_puttlv_raw(fu8_t *buf, const fu16_t t, const fu16_t l, const fu8_t *v)
-{
-	int i;
-
-	i = aimutil_put16(buf, t);
-	i += aimutil_put16(buf+i, l);
-	if (l)
-		memcpy(buf+i, v, l);
-	i += l;
-
-	return i;
-}
-#endif
