Mercurial > pidgin
annotate src/protocols/oscar/tlv.c @ 4809:71ef8462630e
[gaim-migrate @ 5129]
Resume ass-kicking.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 17 Mar 2003 05:21:09 +0000 |
parents | 858979ab3867 |
children | e4a5085fb870 |
rev | line source |
---|---|
2086 | 1 |
2 #define FAIM_INTERNAL | |
3 #include <aim.h> | |
4 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
5 static aim_tlv_t *createtlv(void) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
6 { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
7 aim_tlv_t *newtlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
8 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
9 if (!(newtlv = (aim_tlv_t *)malloc(sizeof(aim_tlv_t)))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
10 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
11 memset(newtlv, 0, sizeof(aim_tlv_t)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
12 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
13 return newtlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
14 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
15 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
16 static void freetlv(aim_tlv_t **oldtlv) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
17 { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
18 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
19 if (!oldtlv || !*oldtlv) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
20 return; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
21 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
22 free((*oldtlv)->value); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
23 free(*oldtlv); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
24 *oldtlv = NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
25 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
26 return; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
27 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
28 |
2086 | 29 /** |
30 * aim_readtlvchain - Read a TLV chain from a buffer. | |
3952 | 31 * @param bs Input bstream |
2086 | 32 * |
33 * Reads and parses a series of TLV patterns from a data buffer; the | |
34 * returned structure is manipulatable with the rest of the TLV | |
35 * routines. When done with a TLV chain, aim_freetlvchain() should | |
36 * be called to free the dynamic substructures. | |
37 * | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
38 * XXX There should be a flag setable here to have the tlvlist contain |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
39 * bstream references, so that at least the ->value portion of each |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
40 * element doesn't need to be malloc/memcpy'd. This could prove to be |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
41 * just as effecient as the in-place TLV parsing used in a couple places |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
42 * in libfaim. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
43 * |
2086 | 44 */ |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
45 faim_internal aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs) |
2086 | 46 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
47 aim_tlvlist_t *list = NULL, *cur; |
3459 | 48 |
49 while (aim_bstream_empty(bs) > 0) { | |
50 fu16_t type, length; | |
2086 | 51 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
52 type = aimbs_get16(bs); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
53 length = aimbs_get16(bs); |
2086 | 54 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
55 #if 0 /* temporarily disabled until I know if they're still doing it or not */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
56 /* |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
57 * Okay, so now AOL has decided that any TLV of |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
58 * type 0x0013 can only be two bytes, despite |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
59 * what the actual given length is. So here |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
60 * we dump any invalid TLVs of that sort. Hopefully |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
61 * theres no special cases to this special case. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 * - mid (30jun2000) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
63 */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
64 if ((type == 0x0013) && (length != 0x0002)) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
65 length = 0x0002; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
66 #else |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
67 if (0) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
68 ; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
69 #endif |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
70 else { |
2086 | 71 |
3459 | 72 if (length > aim_bstream_empty(bs)) { |
73 aim_freetlvchain(&list); | |
74 return NULL; | |
75 } | |
76 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
77 cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t)); |
3459 | 78 if (!cur) { |
79 aim_freetlvchain(&list); | |
80 return NULL; | |
81 } | |
82 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
83 memset(cur, 0, sizeof(aim_tlvlist_t)); |
2086 | 84 |
3459 | 85 cur->tlv = createtlv(); |
86 if (!cur->tlv) { | |
87 free(cur); | |
88 aim_freetlvchain(&list); | |
89 return NULL; | |
90 } | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
91 cur->tlv->type = type; |
3459 | 92 if ((cur->tlv->length = length)) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
93 cur->tlv->value = aimbs_getraw(bs, length); |
3459 | 94 if (!cur->tlv->value) { |
95 freetlv(&cur->tlv); | |
96 free(cur); | |
97 aim_freetlvchain(&list); | |
98 return NULL; | |
99 } | |
100 } | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
102 cur->next = list; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
103 list = cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
104 } |
2086 | 105 } |
106 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
107 return list; |
2086 | 108 } |
109 | |
110 /** | |
3952 | 111 * aim_readtlvchain_num - Read a TLV chain from a buffer. |
112 * @param bs Input bstream | |
113 * @param num The max number of TLVs that will be read, or -1 if unlimited. | |
114 * There are a number of places where you want to read in a tlvchain, | |
115 * but the chain is not at the end of the SNAC, and the chain is | |
4230 | 116 * preceeded by the number of TLVs. So you can limit that with this. |
117 * | |
3952 | 118 * Reads and parses a series of TLV patterns from a data buffer; the |
119 * returned structure is manipulatable with the rest of the TLV | |
120 * routines. When done with a TLV chain, aim_freetlvchain() should | |
121 * be called to free the dynamic substructures. | |
122 * | |
123 * XXX There should be a flag setable here to have the tlvlist contain | |
124 * bstream references, so that at least the ->value portion of each | |
125 * element doesn't need to be malloc/memcpy'd. This could prove to be | |
126 * just as effecient as the in-place TLV parsing used in a couple places | |
127 * in libfaim. | |
128 * | |
129 */ | |
130 faim_internal aim_tlvlist_t *aim_readtlvchain_num(aim_bstream_t *bs, fu16_t num) | |
131 { | |
132 aim_tlvlist_t *list = NULL, *cur; | |
4317 | 133 |
3952 | 134 while ((aim_bstream_empty(bs) > 0) && (num != 0)) { |
135 fu16_t type, length; | |
136 | |
137 type = aimbs_get16(bs); | |
138 length = aimbs_get16(bs); | |
139 | |
140 if (length > aim_bstream_empty(bs)) { | |
141 aim_freetlvchain(&list); | |
142 return NULL; | |
143 } | |
144 | |
145 cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t)); | |
146 if (!cur) { | |
147 aim_freetlvchain(&list); | |
148 return NULL; | |
149 } | |
150 | |
151 memset(cur, 0, sizeof(aim_tlvlist_t)); | |
152 | |
153 cur->tlv = createtlv(); | |
154 if (!cur->tlv) { | |
155 free(cur); | |
156 aim_freetlvchain(&list); | |
157 return NULL; | |
158 } | |
159 cur->tlv->type = type; | |
160 if ((cur->tlv->length = length)) { | |
4317 | 161 cur->tlv->value = aimbs_getraw(bs, length); |
3952 | 162 if (!cur->tlv->value) { |
163 freetlv(&cur->tlv); | |
164 free(cur); | |
165 aim_freetlvchain(&list); | |
166 return NULL; | |
167 } | |
168 } | |
169 | |
170 num--; | |
171 cur->next = list; | |
172 list = cur; | |
173 } | |
174 | |
175 return list; | |
176 } | |
177 | |
178 /** | |
4317 | 179 * aim_readtlvchain_len - Read a TLV chain from a buffer. |
4230 | 180 * @param bs Input bstream |
181 * @param len The max length in bytes that will be read. | |
182 * There are a number of places where you want to read in a tlvchain, | |
183 * but the chain is not at the end of the SNAC, and the chain is | |
184 * preceeded by the length of the TLVs. So you can limit that with this. | |
185 * | |
186 * Reads and parses a series of TLV patterns from a data buffer; the | |
187 * returned structure is manipulatable with the rest of the TLV | |
188 * routines. When done with a TLV chain, aim_freetlvchain() should | |
189 * be called to free the dynamic substructures. | |
190 * | |
191 * XXX There should be a flag setable here to have the tlvlist contain | |
192 * bstream references, so that at least the ->value portion of each | |
193 * element doesn't need to be malloc/memcpy'd. This could prove to be | |
194 * just as effecient as the in-place TLV parsing used in a couple places | |
195 * in libfaim. | |
196 * | |
197 */ | |
198 faim_internal aim_tlvlist_t *aim_readtlvchain_len(aim_bstream_t *bs, fu16_t len) | |
199 { | |
200 aim_tlvlist_t *list = NULL, *cur; | |
4317 | 201 |
4230 | 202 while ((aim_bstream_empty(bs) > 0) && (len > 0)) { |
203 fu16_t type, length; | |
204 | |
205 type = aimbs_get16(bs); | |
206 length = aimbs_get16(bs); | |
207 | |
208 if (length > aim_bstream_empty(bs)) { | |
209 aim_freetlvchain(&list); | |
210 return NULL; | |
211 } | |
212 | |
213 cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t)); | |
214 if (!cur) { | |
215 aim_freetlvchain(&list); | |
216 return NULL; | |
217 } | |
218 | |
219 memset(cur, 0, sizeof(aim_tlvlist_t)); | |
220 | |
221 cur->tlv = createtlv(); | |
222 if (!cur->tlv) { | |
223 free(cur); | |
224 aim_freetlvchain(&list); | |
225 return NULL; | |
226 } | |
227 cur->tlv->type = type; | |
228 if ((cur->tlv->length = length)) { | |
4317 | 229 cur->tlv->value = aimbs_getraw(bs, length); |
4230 | 230 if (!cur->tlv->value) { |
231 freetlv(&cur->tlv); | |
232 free(cur); | |
233 aim_freetlvchain(&list); | |
234 return NULL; | |
235 } | |
236 } | |
237 | |
238 len -= aim_sizetlvchain(&cur); | |
239 cur->next = list; | |
240 list = cur; | |
241 } | |
242 | |
243 return list; | |
244 } | |
245 | |
246 /** | |
247 * aim_tlvlist_copy - Duplicate a TLV chain. | |
248 * @param orig | |
249 * | |
250 * This is pretty pelf exslanatory. | |
251 * | |
252 */ | |
253 faim_internal aim_tlvlist_t *aim_tlvlist_copy(aim_tlvlist_t *orig) | |
254 { | |
255 aim_tlvlist_t *new = NULL; | |
256 | |
257 while (orig) { | |
258 aim_addtlvtochain_raw(&new, orig->tlv->type, orig->tlv->length, orig->tlv->value); | |
259 orig = orig->next; | |
260 } | |
261 | |
262 return new; | |
263 } | |
264 | |
265 /** | |
2086 | 266 * aim_freetlvchain - Free a TLV chain structure |
267 * @list: Chain to be freed | |
268 * | |
269 * Walks the list of TLVs in the passed TLV chain and | |
270 * frees each one. Note that any references to this data | |
271 * should be removed before calling this. | |
272 * | |
273 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
274 faim_internal void aim_freetlvchain(aim_tlvlist_t **list) |
2086 | 275 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
276 aim_tlvlist_t *cur; |
2086 | 277 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
278 if (!list || !*list) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
279 return; |
2086 | 280 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
281 for (cur = *list; cur; ) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
282 aim_tlvlist_t *tmp; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
283 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
284 freetlv(&cur->tlv); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
285 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
286 tmp = cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
287 free(cur); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
288 cur = tmp; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
289 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
290 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
291 list = NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
292 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
293 return; |
2086 | 294 } |
295 | |
296 /** | |
297 * aim_counttlvchain - Count the number of TLVs in a chain | |
298 * @list: Chain to be counted | |
299 * | |
300 * Returns the number of TLVs stored in the passed chain. | |
301 * | |
302 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
303 faim_internal int aim_counttlvchain(aim_tlvlist_t **list) |
2086 | 304 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
305 aim_tlvlist_t *cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
306 int count; |
2086 | 307 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
308 if (!list || !*list) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
309 return 0; |
2086 | 310 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
311 for (cur = *list, count = 0; cur; cur = cur->next) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
312 count++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
313 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
314 return count; |
2086 | 315 } |
316 | |
317 /** | |
318 * aim_sizetlvchain - Count the number of bytes in a TLV chain | |
319 * @list: Chain to be sized | |
320 * | |
321 * Returns the number of bytes that would be needed to | |
322 * write the passed TLV chain to a data buffer. | |
323 * | |
324 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
325 faim_internal int aim_sizetlvchain(aim_tlvlist_t **list) |
2086 | 326 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
327 aim_tlvlist_t *cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
328 int size; |
2086 | 329 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
330 if (!list || !*list) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
331 return 0; |
2086 | 332 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
333 for (cur = *list, size = 0; cur; cur = cur->next) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
334 size += (4 + cur->tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
335 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
336 return size; |
2086 | 337 } |
338 | |
339 /** | |
3912 | 340 * aim_addtlvtochain_raw - Add a string to a TLV chain |
2086 | 341 * @list: Desination chain (%NULL pointer if empty) |
3912 | 342 * @t: TLV type |
343 * @l: Length of string to add (not including %NULL) | |
344 * @v: String to add | |
2086 | 345 * |
346 * Adds the passed string as a TLV element of the passed type | |
347 * to the TLV chain. | |
348 * | |
349 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
350 faim_internal int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v) |
2086 | 351 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
352 aim_tlvlist_t *newtlv, *cur; |
2086 | 353 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
354 if (!list) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
355 return 0; |
2086 | 356 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
357 if (!(newtlv = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t)))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
358 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
359 memset(newtlv, 0x00, sizeof(aim_tlvlist_t)); |
2086 | 360 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
361 if (!(newtlv->tlv = createtlv())) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
362 free(newtlv); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
363 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
364 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
365 newtlv->tlv->type = t; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
366 if ((newtlv->tlv->length = l)) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
367 newtlv->tlv->value = (fu8_t *)malloc(newtlv->tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
368 memcpy(newtlv->tlv->value, v, newtlv->tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
369 } |
2086 | 370 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
371 if (!*list) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
372 *list = newtlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
373 else { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
374 for(cur = *list; cur->next; cur = cur->next) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
375 ; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
376 cur->next = newtlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
377 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
378 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
379 return newtlv->tlv->length; |
2086 | 380 } |
381 | |
382 /** | |
2821
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
383 * aim_addtlvtochain8 - Add a 8bit integer to a TLV chain |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
384 * @list: Destination chain |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
385 * @type: TLV type to add |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
386 * @val: Value to add |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
387 * |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
388 * Adds a one-byte unsigned integer to a TLV chain. |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
389 * |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
390 */ |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
391 faim_internal int aim_addtlvtochain8(aim_tlvlist_t **list, const fu16_t t, const fu8_t v) |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
392 { |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
393 fu8_t v8[1]; |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
394 |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
395 aimutil_put8(v8, v); |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
396 |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
397 return aim_addtlvtochain_raw(list, t, 1, v8); |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
398 } |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
399 |
9467e4ee81be
[gaim-migrate @ 2834]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2421
diff
changeset
|
400 /** |
2086 | 401 * aim_addtlvtochain16 - Add a 16bit integer to a TLV chain |
402 * @list: Destination chain | |
3952 | 403 * @t: TLV type to add |
404 * @v: Value to add | |
2086 | 405 * |
406 * Adds a two-byte unsigned integer to a TLV chain. | |
407 * | |
408 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
409 faim_internal int aim_addtlvtochain16(aim_tlvlist_t **list, const fu16_t t, const fu16_t v) |
2086 | 410 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
411 fu8_t v16[2]; |
2086 | 412 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
413 aimutil_put16(v16, v); |
2086 | 414 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
415 return aim_addtlvtochain_raw(list, t, 2, v16); |
2086 | 416 } |
417 | |
418 /** | |
419 * aim_addtlvtochain32 - Add a 32bit integer to a TLV chain | |
420 * @list: Destination chain | |
421 * @type: TLV type to add | |
422 * @val: Value to add | |
423 * | |
424 * Adds a four-byte unsigned integer to a TLV chain. | |
425 * | |
426 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
427 faim_internal int aim_addtlvtochain32(aim_tlvlist_t **list, const fu16_t t, const fu32_t v) |
2086 | 428 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
429 fu8_t v32[4]; |
2086 | 430 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
431 aimutil_put32(v32, v); |
2086 | 432 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
433 return aim_addtlvtochain_raw(list, t, 4, v32); |
2086 | 434 } |
435 | |
436 /** | |
437 * aim_addtlvtochain_caps - Add a capability block to a TLV chain | |
438 * @list: Destination chain | |
439 * @type: TLV type to add | |
440 * @caps: Bitfield of capability flags to send | |
441 * | |
442 * Adds a block of capability blocks to a TLV chain. The bitfield | |
443 * passed in should be a bitwise %OR of any of the %AIM_CAPS constants: | |
444 * | |
445 * %AIM_CAPS_BUDDYICON Supports Buddy Icons | |
446 * | |
447 * %AIM_CAPS_VOICE Supports Voice Chat | |
448 * | |
449 * %AIM_CAPS_IMIMAGE Supports DirectIM/IMImage | |
450 * | |
451 * %AIM_CAPS_CHAT Supports Chat | |
452 * | |
453 * %AIM_CAPS_GETFILE Supports Get File functions | |
454 * | |
455 * %AIM_CAPS_SENDFILE Supports Send File functions | |
456 * | |
457 */ | |
2866
6d62d4520460
[gaim-migrate @ 2879]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2821
diff
changeset
|
458 faim_internal int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu32_t caps) |
2086 | 459 { |
2866
6d62d4520460
[gaim-migrate @ 2879]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2821
diff
changeset
|
460 fu8_t buf[16*16]; /* XXX icky fixed length buffer */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
461 aim_bstream_t bs; |
2086 | 462 |
2421
95b4ec08abec
[gaim-migrate @ 2434]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2270
diff
changeset
|
463 if (!caps) |
95b4ec08abec
[gaim-migrate @ 2434]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2270
diff
changeset
|
464 return 0; /* nothing there anyway */ |
95b4ec08abec
[gaim-migrate @ 2434]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2270
diff
changeset
|
465 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
466 aim_bstream_init(&bs, buf, sizeof(buf)); |
2086 | 467 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
468 aim_putcap(&bs, caps); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
469 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
470 return aim_addtlvtochain_raw(list, t, aim_bstream_curpos(&bs), buf); |
2086 | 471 } |
472 | |
2421
95b4ec08abec
[gaim-migrate @ 2434]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2270
diff
changeset
|
473 faim_internal int aim_addtlvtochain_userinfo(aim_tlvlist_t **list, fu16_t type, aim_userinfo_t *ui) |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
474 { |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
475 fu8_t buf[1024]; /* bleh */ |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
476 aim_bstream_t bs; |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
477 |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
478 aim_bstream_init(&bs, buf, sizeof(buf)); |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
479 |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
480 aim_putuserinfo(&bs, ui); |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
481 |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
482 return aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf); |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
483 } |
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
484 |
2086 | 485 /** |
486 * aim_addtlvtochain_noval - Add a blank TLV to a TLV chain | |
487 * @list: Destination chain | |
488 * @type: TLV type to add | |
489 * | |
490 * Adds a TLV with a zero length to a TLV chain. | |
491 * | |
492 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
493 faim_internal int aim_addtlvtochain_noval(aim_tlvlist_t **list, const fu16_t t) |
2086 | 494 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
495 return aim_addtlvtochain_raw(list, t, 0, NULL); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
496 } |
2086 | 497 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
498 /* |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
499 * Note that the inner TLV chain will not be modifiable as a tlvchain once |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
500 * it is written using this. Or rather, it can be, but updates won't be |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
501 * made to this. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
502 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
503 * XXX should probably support sublists for real. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
504 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
505 * This is so neat. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
506 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
507 */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
508 faim_internal int aim_addtlvtochain_frozentlvlist(aim_tlvlist_t **list, fu16_t type, aim_tlvlist_t **tl) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
509 { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
510 fu8_t *buf; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
511 int buflen; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
512 aim_bstream_t bs; |
2086 | 513 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
514 buflen = aim_sizetlvchain(tl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
515 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
516 if (buflen <= 0) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
517 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
518 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
519 if (!(buf = malloc(buflen))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
520 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
521 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
522 aim_bstream_init(&bs, buf, buflen); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
523 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
524 aim_writetlvchain(&bs, tl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
525 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
526 aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
527 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
528 free(buf); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
529 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
530 return buflen; |
2086 | 531 } |
532 | |
533 /** | |
534 * aim_writetlvchain - Write a TLV chain into a data buffer. | |
535 * @buf: Destination buffer | |
536 * @buflen: Maximum number of bytes that will be written to buffer | |
537 * @list: Source TLV chain | |
538 * | |
539 * Copies a TLV chain into a raw data buffer, writing only the number | |
540 * of bytes specified. This operation does not free the chain; | |
541 * aim_freetlvchain() must still be called to free up the memory used | |
542 * by the chain structures. | |
543 * | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
544 * XXX clean this up, make better use of bstreams |
2086 | 545 */ |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
546 faim_internal int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list) |
2086 | 547 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
548 int goodbuflen; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
549 aim_tlvlist_t *cur; |
2086 | 550 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
551 /* do an initial run to test total length */ |
4617 | 552 goodbuflen = aim_sizetlvchain(list); |
2086 | 553 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
554 if (goodbuflen > aim_bstream_empty(bs)) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
555 return 0; /* not enough buffer */ |
2086 | 556 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
557 /* do the real write-out */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
558 for (cur = *list; cur; cur = cur->next) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
559 aimbs_put16(bs, cur->tlv->type); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
560 aimbs_put16(bs, cur->tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
561 if (cur->tlv->length) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
562 aimbs_putraw(bs, cur->tlv->value, cur->tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
563 } |
2086 | 564 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
565 return 1; /* XXX this is a nonsensical return */ |
2086 | 566 } |
567 | |
568 | |
569 /** | |
570 * aim_gettlv - Grab the Nth TLV of type type in the TLV list list. | |
571 * @list: Source chain | |
572 * @type: Requested TLV type | |
573 * @nth: Index of TLV of type to get | |
574 * | |
575 * Returns a pointer to an aim_tlv_t of the specified type; | |
576 * %NULL on error. The @nth parameter is specified starting at %1. | |
577 * In most cases, there will be no more than one TLV of any type | |
578 * in a chain. | |
579 * | |
580 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
581 faim_internal aim_tlv_t *aim_gettlv(aim_tlvlist_t *list, const fu16_t t, const int n) |
2086 | 582 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
583 aim_tlvlist_t *cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
584 int i; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
585 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
586 for (cur = list, i = 0; cur; cur = cur->next) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
587 if (cur && cur->tlv) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
588 if (cur->tlv->type == t) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
589 i++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
590 if (i >= n) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
591 return cur->tlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
592 } |
2086 | 593 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
594 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
595 return NULL; |
2086 | 596 } |
597 | |
598 /** | |
599 * aim_gettlv_str - Retrieve the Nth TLV in chain as a string. | |
600 * @list: Source TLV chain | |
601 * @type: TLV type to search for | |
602 * @nth: Index of TLV to return | |
603 * | |
604 * Same as aim_gettlv(), except that the return value is a %NULL- | |
605 * terminated string instead of an aim_tlv_t. This is a | |
606 * dynamic buffer and must be freed by the caller. | |
607 * | |
608 */ | |
2270
d82efea341ef
[gaim-migrate @ 2280]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2246
diff
changeset
|
609 faim_internal char *aim_gettlv_str(aim_tlvlist_t *list, const fu16_t t, const int n) |
2086 | 610 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
611 aim_tlv_t *tlv; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
612 char *newstr; |
2086 | 613 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
614 if (!(tlv = aim_gettlv(list, t, n))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
615 return NULL; |
2086 | 616 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
617 newstr = (char *) malloc(tlv->length + 1); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
618 memcpy(newstr, tlv->value, tlv->length); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
619 *(newstr + tlv->length) = '\0'; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
620 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
621 return newstr; |
2086 | 622 } |
623 | |
624 /** | |
625 * aim_gettlv8 - Retrieve the Nth TLV in chain as a 8bit integer. | |
626 * @list: Source TLV chain | |
627 * @type: TLV type to search for | |
628 * @nth: Index of TLV to return | |
629 * | |
630 * Same as aim_gettlv(), except that the return value is a | |
631 * 8bit integer instead of an aim_tlv_t. | |
632 * | |
633 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
634 faim_internal fu8_t aim_gettlv8(aim_tlvlist_t *list, const fu16_t t, const int n) |
2086 | 635 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
636 aim_tlv_t *tlv; |
2086 | 637 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
638 if (!(tlv = aim_gettlv(list, t, n))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
639 return 0; /* erm */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
640 return aimutil_get8(tlv->value); |
2086 | 641 } |
642 | |
643 /** | |
644 * aim_gettlv16 - Retrieve the Nth TLV in chain as a 16bit integer. | |
645 * @list: Source TLV chain | |
646 * @type: TLV type to search for | |
647 * @nth: Index of TLV to return | |
648 * | |
649 * Same as aim_gettlv(), except that the return value is a | |
650 * 16bit integer instead of an aim_tlv_t. | |
651 * | |
652 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
653 faim_internal fu16_t aim_gettlv16(aim_tlvlist_t *list, const fu16_t t, const int n) |
2086 | 654 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
655 aim_tlv_t *tlv; |
2086 | 656 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
657 if (!(tlv = aim_gettlv(list, t, n))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
658 return 0; /* erm */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
659 return aimutil_get16(tlv->value); |
2086 | 660 } |
661 | |
662 /** | |
663 * aim_gettlv32 - Retrieve the Nth TLV in chain as a 32bit integer. | |
664 * @list: Source TLV chain | |
665 * @type: TLV type to search for | |
666 * @nth: Index of TLV to return | |
667 * | |
668 * Same as aim_gettlv(), except that the return value is a | |
669 * 32bit integer instead of an aim_tlv_t. | |
670 * | |
671 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
672 faim_internal fu32_t aim_gettlv32(aim_tlvlist_t *list, const fu16_t t, const int n) |
2086 | 673 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
674 aim_tlv_t *tlv; |
2086 | 675 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
676 if (!(tlv = aim_gettlv(list, t, n))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
677 return 0; /* erm */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
678 return aimutil_get32(tlv->value); |
2086 | 679 } |
680 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
681 #if 0 |
2086 | 682 /** |
683 * aim_puttlv_8 - Write a one-byte TLV. | |
684 * @buf: Destination buffer | |
685 * @t: TLV type | |
686 * @v: Value | |
687 * | |
688 * Writes a TLV with a one-byte integer value portion. | |
689 * | |
690 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
691 faim_export int aim_puttlv_8(fu8_t *buf, const fu16_t t, const fu8_t v) |
2086 | 692 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
693 fu8_t v8[1]; |
2086 | 694 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
695 aimutil_put8(v8, v); |
2086 | 696 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
697 return aim_puttlv_raw(buf, t, 1, v8); |
2086 | 698 } |
699 | |
700 /** | |
701 * aim_puttlv_16 - Write a two-byte TLV. | |
702 * @buf: Destination buffer | |
703 * @t: TLV type | |
704 * @v: Value | |
705 * | |
706 * Writes a TLV with a two-byte integer value portion. | |
707 * | |
708 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
709 faim_export int aim_puttlv_16(fu8_t *buf, const fu16_t t, const fu16_t v) |
2086 | 710 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
711 fu8_t v16[2]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
712 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
713 aimutil_put16(v16, v); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
714 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
715 return aim_puttlv_raw(buf, t, 2, v16); |
2086 | 716 } |
717 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
718 |
2086 | 719 /** |
720 * aim_puttlv_32 - Write a four-byte TLV. | |
721 * @buf: Destination buffer | |
722 * @t: TLV type | |
723 * @v: Value | |
724 * | |
725 * Writes a TLV with a four-byte integer value portion. | |
726 * | |
727 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
728 faim_export int aim_puttlv_32(fu8_t *buf, const fu16_t t, const fu32_t v) |
2086 | 729 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
730 fu8_t v32[4]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
731 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
732 aimutil_put32(v32, v); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
733 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
734 return aim_puttlv_raw(buf, t, 4, v32); |
2086 | 735 } |
736 | |
737 /** | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
738 * aim_puttlv_raw - Write a raw TLV. |
2086 | 739 * @buf: Destination buffer |
740 * @t: TLV type | |
741 * @l: Length of string | |
742 * @v: String to write | |
743 * | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
744 * Writes a TLV with a raw value portion. (Only the first @l |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
745 * bytes of the passed buffer will be written, which should not |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
746 * include a terminating NULL.) |
2086 | 747 * |
748 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
749 faim_export int aim_puttlv_raw(fu8_t *buf, const fu16_t t, const fu16_t l, const fu8_t *v) |
2086 | 750 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
751 int i; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
752 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
753 i = aimutil_put16(buf, t); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
754 i += aimutil_put16(buf+i, l); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
755 if (l) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
756 memcpy(buf+i, v, l); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
757 i += l; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
758 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
759 return i; |
2086 | 760 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
761 #endif |