--- KeyRing/keyring.c.original	2006-06-24 21:49:51.000000000 -0700
+++ KeyRing/keyring.c	2006-12-17 20:02:08.000000000 -0800
@@ -160,6 +160,8 @@
 
 static int keyring_find(int unique_id);
 
+int plugin_unpack_cai_from_ai(struct CategoryAppInfo *cai, unsigned char *ai_raw, int len);
+
 /****************************** Main Code *************************************/
 static int pack_KeyRing(struct KeyRing *kr, unsigned char *buf, int buf_size,
 			int *wrote_size)
@@ -1348,17 +1350,12 @@
 
    /* This gets the application specific data out of the database for us.
     * We still need to write a function to unpack it from its blob form. */
+
+   memset (&ai, 0, sizeof (ai));
    
    jp_get_app_info("Keys-Gtkr", &buf, &buf_size);
 
-   /* This call should work, but the appinfo is too small, so we do it */
-   /* Keyring is not using a legal category app info structure */
-   /* unpack_CategoryAppInfo(&ai, buf, buf_size+4); */
-   
-   /* I'm going to be lazy and only get the names, since that's all I use */
-   for (i=0; i<NUM_KEYRING_CAT_ITEMS; i++) {
-      memcpy(&ai.name[i][0], buf+i*16+2, 16);
-   }
+   plugin_unpack_cai_from_ai (&ai, buf, buf_size);
 
    free(buf);
    
@@ -2238,3 +2235,62 @@
 
    return EXIT_SUCCESS;
 }
+
+/* Stolen from pilot-link and modified slightly. */
+int plugin_unpack_cai_from_ai(struct CategoryAppInfo *ai, unsigned char *record, int len)
+{
+	int 	i, rec;
+
+	if (len < 2 + 16 * 16 + 16 + 2)
+		return 0;
+	rec = get_short(record);
+	for (i = 0; i < 16; i++) {
+		if (rec & (1 << i))
+			ai->renamed[i] = 1;
+		else
+			ai->renamed[i] = 0;
+	}
+	record += 2;
+	for (i = 0; i < 16; i++) {
+		memcpy(ai->name[i], record, 16);
+		record += 16;
+	}
+	memcpy(ai->ID, record, 16);
+	record += 16;
+	ai->lastUniqueID = get_byte(record);
+	record += 2;
+
+	return 2 + 16 * 16 + 16 + 2;
+}
+
+int plugin_pack_cai_into_ai(struct CategoryAppInfo *ai, unsigned char *record, int len)
+{
+	int 	i, rec;
+	
+	unsigned char *start = record;
+
+	if (!record) {
+		return 2 + 16 * 16 + 16 + 2;
+	}
+	if (len < (2 + 16 * 16 + 16 + 2))
+		return 0;	/* not enough room */
+	rec = 0;
+	for (i = 0; i < 16; i++) {
+		if (ai->renamed[i])
+			rec |= (1 << i);
+	}
+	set_short(record, rec);
+	record += 2;
+	for (i = 0; i < 16; i++) {
+		memcpy(record, ai->name[i], 16);
+		record += 16;
+	}
+	memcpy(record, ai->ID, 16);
+	record += 16;
+	set_byte(record, ai->lastUniqueID);
+	record++;
+	set_byte(record, 0);		/* gapfill */
+	record++;
+
+	return (record - start);
+}