- int i = 0;
- int tlvcnt = 0;
- int curtlv = 0;
- int tlv1 = 0;
- u_short curtype;
- int lastvalid;
-
-
- if (!buf || !outinfo)
- return -1;
-
- /* Clear out old data first */
- memset(outinfo, 0x00, sizeof(struct aim_userinfo_s));
-
- /*
- * Screen name. Stored as an unterminated string prepended
- * with an unsigned byte containing its length.
- */
- if (buf[i] < MAXSNLEN) {
- memcpy(outinfo->sn, &(buf[i+1]), buf[i]);
- outinfo->sn[(int)buf[i]] = '\0';
- } else {
- memcpy(outinfo->sn, &(buf[i+1]), MAXSNLEN-1);
- outinfo->sn[MAXSNLEN] = '\0';
- }
- i = 1 + (int)buf[i];
-
- /*
- * Warning Level. Stored as an unsigned short.
- */
- outinfo->warnlevel = aimutil_get16(&buf[i]);
- i += 2;
-
- /*
- * TLV Count. Unsigned short representing the number of
- * Type-Length-Value triples that follow.
- */
- tlvcnt = aimutil_get16(&buf[i]);
- i += 2;
-
- /*
- * Parse out the Type-Length-Value triples as they're found.
- */
- while (curtlv < tlvcnt) {
- lastvalid = 1;
- curtype = aimutil_get16(&buf[i]);
- switch (curtype) {
- /*
- * Type = 0x0000: Invalid
- *
- * AOL has been trying to throw these in just to break us.
- * They're real nice guys over there at AOL.
- *
- * Just skip the two zero bytes and continue on. (This doesn't
- * count towards tlvcnt!)
- */
- case 0x0000:
- lastvalid = 0;
- i += 2;
- break;
-
- /*
- * Type = 0x0001: User flags
- *
- * Specified as any of the following bitwise ORed together:
- * 0x0001 Trial (user less than 60days)
- * 0x0002 Unknown bit 2
- * 0x0004 AOL Main Service user
- * 0x0008 Unknown bit 4
- * 0x0010 Free (AIM) user
- * 0x0020 Away
- *
- * In some odd cases, we can end up with more
- * than one of these. We only want the first,
- * as the others may not be something we want.
- *
- */
- case 0x0001:
- if (tlv1) /* use only the first */
- break;
- outinfo->flags = aimutil_get16(&buf[i+4]);
- tlv1++;
- break;
-
- /*
- * Type = 0x0002: Member-Since date.
- *
- * The time/date that the user originally
- * registered for the service, stored in
- * time_t format
- */
- case 0x0002:
- outinfo->membersince = aimutil_get32(&buf[i+4]);
- break;
-
- /*
- * Type = 0x0003: On-Since date.
- *
- * The time/date that the user started
- * their current session, stored in time_t
- * format.
- */
- case 0x0003:
- outinfo->onlinesince = aimutil_get32(&buf[i+4]);
- break;
-
- /*
- * Type = 0x0004: Idle time.
- *
- * Number of seconds since the user
- * actively used the service.
- */
- case 0x0004:
- outinfo->idletime = aimutil_get16(&buf[i+4]);
- break;
-
- /*
- * Type = 0x0006: ICQ Online Status
- *
- * ICQ's Away/DND/etc "enriched" status
- * Some decoding of values done by Scott <darkagl@pcnet.com>
- */
- case 0x0006:
- outinfo->icqinfo.status = aimutil_get16(buf+i+2+2+2);
- break;
-
-
- /*
- * Type = 0x000a
- *
- * ICQ User IP Address.
- * Ahh, the joy of ICQ security.
- */
- case 0x000a:
- outinfo->icqinfo.ipaddr = aimutil_get32(&buf[i+4]);
- break;
-
- /* Type = 0x000c
- *
- * random crap containing the IP address,
- * apparently a port number, and some Other Stuff.
- *
- */
- case 0x000c:
- memcpy(outinfo->icqinfo.crap, &buf[i+4], 0x25);
- break;
-
- /*
- * Type = 0x000d
- *
- * Capability information. Not real sure of
- * actual decoding. See comment on aim_bos_setprofile()
- * in aim_misc.c about the capability block, its the same.
- *
- */
- case 0x000d:
- {
- int len;
- len = aimutil_get16(buf+i+2);
- if (!len)
- break;
-
- outinfo->capabilities = aim_getcap(sess, buf+i+4, len);
- }
- break;
-
- /*
- * Type = 0x000e
- *
- * Unknown. Always of zero length, and always only
- * on AOL users.
- *
- * Ignore.
- *
- */
- case 0x000e:
- break;
-
- /*
- * Type = 0x000f: Session Length. (AIM)
- * Type = 0x0010: Session Length. (AOL)
- *
- * The duration, in seconds, of the user's
- * current session.
- *
- * Which TLV type this comes in depends
- * on the service the user is using (AIM or AOL).
- *
- */
- case 0x000f:
- case 0x0010:
- outinfo->sessionlen = aimutil_get32(&buf[i+4]);
- break;
-
- /*
- * Reaching here indicates that either AOL has
- * added yet another TLV for us to deal with,
- * or the parsing has gone Terribly Wrong.
- *
- * Either way, inform the owner and attempt
- * recovery.
- *
- */
- default:
- {
- int len,z = 0, y = 0, x = 0;
- char tmpstr[160];
-
- faimdprintf(sess, 0, "userinfo: **warning: unexpected TLV:\n");
- faimdprintf(sess, 0, "userinfo: sn =%s\n", outinfo->sn);
- faimdprintf(sess, 0, "userinfo: curtlv=0x%04x\n", curtlv);
- faimdprintf(sess, 0, "userinfo: type =0x%04x\n",aimutil_get16(&buf[i]));
- faimdprintf(sess, 0, "userinfo: length=0x%04x\n", len = aimutil_get16(&buf[i+2]));
- faimdprintf(sess, 0, "userinfo: data: \n");
- while (z<len)
- {
- x = snprintf(tmpstr, sizeof(tmpstr), "userinfo: ");
- for (y = 0; y < 8; y++)
- {
- if (z<len)
- {
- snprintf(tmpstr+x, sizeof(tmpstr)-x, "%02x ", buf[i+4+z]);
- z++;
- x += 3;
- }
- else
- break;
- }
- faimdprintf(sess, 0, "%s\n", tmpstr);
- }
- }
- break;
- }
- /*
- * No matter what, TLV triplets should always look like this:
- *
- * u_short type;
- * u_short length;
- * u_char data[length];
- *
- */
- if (lastvalid) {
- i += (2 + 2 + aimutil_get16(&buf[i+2]));
- curtlv++;
- }
- }
-
- return i;
+ int curtlv, tlvcnt;
+ fu8_t snlen;
+
+ if (!bs || !outinfo)
+ return -EINVAL;
+
+ /* Clear out old data first */
+ memset(outinfo, 0x00, sizeof(struct aim_userinfo_s));
+
+ /*
+ * Screen name. Stored as an unterminated string prepended with a
+ * byte containing its length.
+ */
+ snlen = aimbs_get8(bs);
+ aimbs_getrawbuf(bs, outinfo->sn, snlen);
+
+ /*
+ * Warning Level. Stored as an unsigned short.
+ */
+ outinfo->warnlevel = aimbs_get16(bs);
+
+ /*
+ * TLV Count. Unsigned short representing the number of
+ * Type-Length-Value triples that follow.
+ */
+ tlvcnt = aimbs_get16(bs);
+
+ /*
+ * Parse out the Type-Length-Value triples as they're found.
+ */
+ for (curtlv = 0; curtlv < tlvcnt; curtlv++) {
+ int endpos;
+ fu16_t type, length;
+
+ type = aimbs_get16(bs);
+ length = aimbs_get16(bs);
+
+ endpos = aim_bstream_curpos(bs) + length;
+
+ if (type == 0x0001) {
+ /*
+ * Type = 0x0001: User flags
+ *
+ * Specified as any of the following ORed together:
+ * 0x0001 Trial (user less than 60days)
+ * 0x0002 Unknown bit 2
+ * 0x0004 AOL Main Service user
+ * 0x0008 Unknown bit 4
+ * 0x0010 Free (AIM) user
+ * 0x0020 Away
+ * 0x0400 ActiveBuddy
+ *
+ */
+ outinfo->flags = aimbs_get16(bs);
+
+ } else if (type == 0x0002) {
+ /*
+ * Type = 0x0002: Member-Since date.
+ *
+ * The time/date that the user originally registered for
+ * the service, stored in time_t format.
+ */
+ outinfo->membersince = aimbs_get32(bs);
+
+ } else if (type == 0x0003) {
+ /*
+ * Type = 0x0003: On-Since date.
+ *
+ * The time/date that the user started their current
+ * session, stored in time_t format.
+ */
+ outinfo->onlinesince = aimbs_get32(bs);
+
+ } else if (type == 0x0004) {
+ /*
+ * Type = 0x0004: Idle time.
+ *
+ * Number of seconds since the user actively used the
+ * service.
+ *
+ * Note that the client tells the server when to start
+ * counting idle times, so this may or may not be
+ * related to reality.
+ */
+ outinfo->idletime = aimbs_get16(bs);
+
+ } else if (type == 0x0006) {
+ /*
+ * Type = 0x0006: ICQ Online Status
+ *
+ * ICQ's Away/DND/etc "enriched" status. Some decoding
+ * of values done by Scott <darkagl@pcnet.com>
+ */
+ aimbs_get16(bs);
+ outinfo->icqinfo.status = aimbs_get16(bs);
+
+ } else if (type == 0x000a) {
+ /*
+ * Type = 0x000a
+ *
+ * ICQ User IP Address.
+ * Ahh, the joy of ICQ security.
+ */
+ outinfo->icqinfo.ipaddr = aimbs_get32(bs);
+
+ } else if (type == 0x000c) {
+ /*
+ * Type = 0x000c
+ *
+ * random crap containing the IP address,
+ * apparently a port number, and some Other Stuff.
+ *
+ */
+ aimbs_getrawbuf(bs, outinfo->icqinfo.crap, 0x25);
+
+ } else if (type == 0x000d) {
+ /*
+ * Type = 0x000d
+ *
+ * Capability information.
+ *
+ */
+ outinfo->capabilities = aim_getcap(sess, bs, length);
+
+ } else if (type == 0x000e) {
+ /*
+ * Type = 0x000e
+ *
+ * Unknown. Always of zero length, and always only
+ * on AOL users.
+ *
+ * Ignore.
+ *
+ */
+
+ } else if ((type == 0x000f) || (type == 0x0010)) {
+ /*
+ * Type = 0x000f: Session Length. (AIM)
+ * Type = 0x0010: Session Length. (AOL)
+ *
+ * The duration, in seconds, of the user's current
+ * session.
+ *
+ * Which TLV type this comes in depends on the
+ * service the user is using (AIM or AOL).
+ *
+ */
+ outinfo->sessionlen = aimbs_get32(bs);
+
+ } else {
+
+ /*
+ * Reaching here indicates that either AOL has
+ * added yet another TLV for us to deal with,
+ * or the parsing has gone Terribly Wrong.
+ *
+ * Either way, inform the owner and attempt
+ * recovery.
+ *
+ */
+ faimdprintf(sess, 0, "userinfo: **warning: unexpected TLV:\n");
+ faimdprintf(sess, 0, "userinfo: sn =%s\n", outinfo->sn);
+ faimdprintf(sess, 0, "userinfo: type =0x%04x\n",type);
+ faimdprintf(sess, 0, "userinfo: length=0x%04x\n", length);
+
+ }
+
+ /* Save ourselves. */
+ aim_bstream_setpos(bs, endpos);
+ }
+
+ return 0;