]> andersk Git - libfaim.git/blob - aim_tlv.c
Lots of minor cleanups. Adds new (disabled) SNAC-based login.
[libfaim.git] / aim_tlv.c
1 #include <faim/aim.h>
2
3 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen)
4 {
5   int pos;
6   struct aim_tlvlist_t *list;
7   struct aim_tlvlist_t *cur;
8   
9   u_short type;
10   u_short length;
11
12   if (!buf)
13     return NULL;
14
15   list = NULL;
16   
17   pos = 0;
18
19   while (pos < maxlen)
20     {
21       type = aimutil_get16(buf+pos);
22       pos += 2;
23
24       if (pos < maxlen)
25         {
26           length = aimutil_get16(buf+pos);
27           pos += 2;
28           
29           if ((pos+length) <= maxlen)
30             {
31               cur = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
32               memset(cur, 0x00, sizeof(struct aim_tlvlist_t));
33
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);
39               
40               cur->next = list;
41               list = cur;
42               
43               pos += length;
44             }
45         }
46     }
47
48   return list;
49 }
50
51 void aim_freetlvchain(struct aim_tlvlist_t **list)
52 {
53   struct aim_tlvlist_t *cur, *cur2;
54
55   if (!list || !(*list))
56     return;
57
58   cur = *list;
59   while (cur)
60     {
61       aim_freetlv(&cur->tlv);
62       cur2 = cur->next;
63       free(cur);
64       cur = cur2;
65     }
66   list = NULL;
67   return;
68 }
69
70 /*
71  * Grab the Nth TLV of type type in the TLV list list.
72  */
73 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth)
74 {
75   int i;
76   struct aim_tlvlist_t *cur;
77   
78   i = 0;
79   for (cur = list; cur != NULL; cur = cur->next)
80     {
81       if (cur && cur->tlv)
82         {
83           if (cur->tlv->type == type)
84             i++;
85           if (i >= nth)
86             return cur->tlv;
87         }
88     }
89   return NULL;
90 }
91
92 char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth)
93 {
94   struct aim_tlv_t *tlv;
95   char *newstr;
96
97   if (!(tlv = aim_gettlv(list, type, nth)))
98     return NULL;
99   
100   newstr = (char *) malloc(tlv->length + 1);
101   memcpy(newstr, tlv->value, tlv->length);
102   *(newstr + tlv->length) = '\0';
103
104   return newstr;
105 }
106
107 struct aim_tlv_t *aim_grabtlv(u_char *src)
108 {
109   struct aim_tlv_t *dest = NULL;
110
111   dest = aim_createtlv();
112
113   dest->type = src[0] << 8;
114   dest->type += src[1];
115
116   dest->length = src[2] << 8;
117   dest->length += src[3];
118
119   dest->value = (u_char *) malloc(dest->length*sizeof(u_char));
120   memset(dest->value, 0, dest->length*sizeof(u_char));
121
122   memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
123   
124   return dest;
125 }
126
127 struct aim_tlv_t *aim_grabtlvstr(u_char *src)
128 {
129   struct aim_tlv_t *dest = NULL;
130
131   dest = aim_createtlv();
132
133   dest->type = src[0] << 8;
134   dest->type += src[1];
135
136   dest->length = src[2] << 8;
137   dest->length += src[3];
138
139   dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char));
140   memset(dest->value, 0, (dest->length+1)*sizeof(u_char));
141
142   memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
143   dest->value[dest->length] = '\0';
144
145   return dest;
146 }
147
148 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv)
149 {
150   int i=0;
151
152   dest[i++] = newtlv->type >> 8;
153   dest[i++] = newtlv->type & 0x00FF;
154   dest[i++] = newtlv->length >> 8;
155   dest[i++] = newtlv->length & 0x00FF;
156   memcpy(&(dest[i]), newtlv->value, newtlv->length);
157   i+=newtlv->length;
158   return i;
159 }
160
161 struct aim_tlv_t *aim_createtlv(void)
162 {
163   struct aim_tlv_t *newtlv = NULL;
164   newtlv = (struct aim_tlv_t *)malloc(sizeof(struct aim_tlv_t));
165   memset(newtlv, 0, sizeof(struct aim_tlv_t));
166   return newtlv;
167 }
168
169 int aim_freetlv(struct aim_tlv_t **oldtlv)
170 {
171   if (!oldtlv)
172     return -1;
173   if (!*oldtlv)
174     return -1;
175   if ((*oldtlv)->value)
176     free((*oldtlv)->value);
177   free(*(oldtlv));
178   (*oldtlv) = NULL;
179
180   return 0;
181 }
182
183 int aim_puttlv_16(u_char *buf, u_short t, u_short v)
184 {
185   int curbyte=0;
186   curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
187   curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002);
188   curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff));
189   return curbyte;
190 }
191
192 int aim_puttlv_32(u_char *buf, u_short t, u_long v)
193 {
194   int curbyte=0;
195   curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
196   curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004);
197   curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff));
198   return curbyte;
199 }
200
201 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v)
202 {
203   int curbyte;
204   if (!v || !buf)
205     return 0;
206   
207   curbyte  = 0;
208   curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
209   curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff));
210   memcpy(buf+curbyte, v, l);
211   curbyte += l;
212   return curbyte;
213 }
This page took 0.050926 seconds and 5 git commands to generate.