3 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen)
6 struct aim_tlvlist_t *list;
7 struct aim_tlvlist_t *cur;
21 type = aimutil_get16(buf+pos);
26 length = aimutil_get16(buf+pos);
29 if ((pos+length) <= maxlen)
31 cur = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
32 memset(cur, 0x00, sizeof(struct aim_tlvlist_t));
34 cur->tlv = aim_createtlv();
35 cur->tlv->type = type;
36 cur->tlv->length = length;
37 cur->tlv->value = (u_char *)malloc(length*sizeof(u_char));
38 memcpy(cur->tlv->value, buf+pos, length);
51 void aim_freetlvchain(struct aim_tlvlist_t **list)
53 struct aim_tlvlist_t *cur, *cur2;
55 if (!list || !(*list))
61 aim_freetlv(&cur->tlv);
70 int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str)
72 struct aim_tlvlist_t *new;
73 struct aim_tlvlist_t *cur;
78 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
79 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
81 new->tlv = aim_createtlv();
82 new->tlv->type = type;
83 new->tlv->length = strlen(str);
84 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
85 memcpy(new->tlv->value, str, new->tlv->length);
91 } else if ((*list)->next == NULL) {
94 for(cur = *list; cur->next; cur = cur->next)
98 return new->tlv->length;
101 int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val)
103 struct aim_tlvlist_t *new;
104 struct aim_tlvlist_t *cur;
109 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
110 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
112 new->tlv = aim_createtlv();
113 new->tlv->type = type;
114 new->tlv->length = 2;
115 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
116 aimutil_put16(new->tlv->value, val);
122 } else if ((*list)->next == NULL) {
125 for(cur = *list; cur->next; cur = cur->next)
132 int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val)
134 struct aim_tlvlist_t *new;
135 struct aim_tlvlist_t *cur;
140 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
141 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
143 new->tlv = aim_createtlv();
144 new->tlv->type = type;
145 new->tlv->length = 4;
146 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
147 aimutil_put32(new->tlv->value, val);
153 } else if ((*list)->next == NULL) {
156 for(cur = *list; cur->next; cur = cur->next)
163 int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list)
167 struct aim_tlvlist_t *cur;
169 if (!list || !buf || !buflen)
172 /* do an initial run to test total length */
173 for (cur = *list; cur; cur = cur->next) {
174 goodbuflen += 2 + 2; /* type + len */
175 goodbuflen += cur->tlv->length;
178 if (goodbuflen > buflen)
179 return 0; /* not enough buffer */
181 /* do the real write-out */
182 for (cur = *list; cur; cur = cur->next) {
183 i += aimutil_put16(buf+i, cur->tlv->type);
184 i += aimutil_put16(buf+i, cur->tlv->length);
185 memcpy(buf+i, cur->tlv->value, cur->tlv->length);
186 i += cur->tlv->length;
194 * Grab the Nth TLV of type type in the TLV list list.
196 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth)
199 struct aim_tlvlist_t *cur;
202 for (cur = list; cur != NULL; cur = cur->next)
206 if (cur->tlv->type == type)
215 char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth)
217 struct aim_tlv_t *tlv;
220 if (!(tlv = aim_gettlv(list, type, nth)))
223 newstr = (char *) malloc(tlv->length + 1);
224 memcpy(newstr, tlv->value, tlv->length);
225 *(newstr + tlv->length) = '\0';
230 struct aim_tlv_t *aim_grabtlv(u_char *src)
232 struct aim_tlv_t *dest = NULL;
234 dest = aim_createtlv();
236 dest->type = src[0] << 8;
237 dest->type += src[1];
239 dest->length = src[2] << 8;
240 dest->length += src[3];
242 dest->value = (u_char *) malloc(dest->length*sizeof(u_char));
243 memset(dest->value, 0, dest->length*sizeof(u_char));
245 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
250 struct aim_tlv_t *aim_grabtlvstr(u_char *src)
252 struct aim_tlv_t *dest = NULL;
254 dest = aim_createtlv();
256 dest->type = src[0] << 8;
257 dest->type += src[1];
259 dest->length = src[2] << 8;
260 dest->length += src[3];
262 dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char));
263 memset(dest->value, 0, (dest->length+1)*sizeof(u_char));
265 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
266 dest->value[dest->length] = '\0';
271 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv)
275 dest[i++] = newtlv->type >> 8;
276 dest[i++] = newtlv->type & 0x00FF;
277 dest[i++] = newtlv->length >> 8;
278 dest[i++] = newtlv->length & 0x00FF;
279 memcpy(&(dest[i]), newtlv->value, newtlv->length);
284 struct aim_tlv_t *aim_createtlv(void)
286 struct aim_tlv_t *newtlv = NULL;
287 newtlv = (struct aim_tlv_t *)malloc(sizeof(struct aim_tlv_t));
288 memset(newtlv, 0, sizeof(struct aim_tlv_t));
292 int aim_freetlv(struct aim_tlv_t **oldtlv)
298 if ((*oldtlv)->value)
299 free((*oldtlv)->value);
306 int aim_puttlv_16(u_char *buf, u_short t, u_short v)
309 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
310 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002);
311 curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff));
315 int aim_puttlv_32(u_char *buf, u_short t, u_long v)
318 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
319 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004);
320 curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff));
324 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v)
329 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
330 curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff));
332 memcpy(buf+curbyte, v, l);