Mercurial > pidgin.yaz
comparison libpurple/protocols/jabber/caps.c @ 30005:12ba4c648872
jabber: Space efficiency.
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Wed, 17 Mar 2010 02:27:28 +0000 |
parents | 9ae3e70a327b |
children | 6362579b3d2e |
comparison
equal
deleted
inserted
replaced
30004:22faf27397d2 | 30005:12ba4c648872 |
---|---|
843 | 843 |
844 fields = g_list_sort(fields, jabber_caps_xdata_field_compare); | 844 fields = g_list_sort(fields, jabber_caps_xdata_field_compare); |
845 return fields; | 845 return fields; |
846 } | 846 } |
847 | 847 |
848 static GString* | 848 static void |
849 jabber_caps_verification_append(GString *verification, const gchar *str) | 849 append_escaped_string(PurpleCipherContext *context, const gchar *str) |
850 { | 850 { |
851 char *tmp = g_markup_escape_text(str, -1); | 851 char *tmp = g_markup_escape_text(str, -1); |
852 verification = g_string_append(verification, tmp); | 852 purple_cipher_context_append(context, (const guchar *)tmp, strlen(tmp)); |
853 g_free(tmp); | 853 g_free(tmp); |
854 return g_string_append_c(verification, '<'); | 854 purple_cipher_context_append(context, (const guchar *)"<", 1); |
855 } | 855 } |
856 | 856 |
857 gchar *jabber_caps_calculate_hash(JabberCapsClientInfo *info, const char *hash) | 857 gchar *jabber_caps_calculate_hash(JabberCapsClientInfo *info, const char *hash) |
858 { | 858 { |
859 GList *node; | 859 GList *node; |
860 GString *verification; | |
861 PurpleCipherContext *context; | 860 PurpleCipherContext *context; |
862 guint8 checksum[20]; | 861 guint8 checksum[20]; |
863 gsize checksum_size = 20; | 862 gsize checksum_size = 20; |
864 gboolean success; | 863 gboolean success; |
865 | 864 |
869 /* sort identities, features and x-data forms */ | 868 /* sort identities, features and x-data forms */ |
870 info->identities = g_list_sort(info->identities, jabber_identity_compare); | 869 info->identities = g_list_sort(info->identities, jabber_identity_compare); |
871 info->features = g_list_sort(info->features, (GCompareFunc)strcmp); | 870 info->features = g_list_sort(info->features, (GCompareFunc)strcmp); |
872 info->forms = g_list_sort(info->forms, jabber_xdata_compare); | 871 info->forms = g_list_sort(info->forms, jabber_xdata_compare); |
873 | 872 |
874 verification = g_string_new(""); | 873 /* Add identities to the hash data */ |
875 | |
876 /* concat identities to the verification string */ | |
877 for (node = info->identities; node; node = node->next) { | 874 for (node = info->identities; node; node = node->next) { |
878 JabberIdentity *id = (JabberIdentity*)node->data; | 875 JabberIdentity *id = (JabberIdentity*)node->data; |
879 char *category = g_markup_escape_text(id->category, -1); | 876 char *category = g_markup_escape_text(id->category, -1); |
880 char *type = g_markup_escape_text(id->type, -1); | 877 char *type = g_markup_escape_text(id->type, -1); |
881 char *lang = NULL; | 878 char *lang = NULL; |
882 char *name = NULL; | 879 char *name = NULL; |
880 char *tmp; | |
883 | 881 |
884 if (id->lang) | 882 if (id->lang) |
885 lang = g_markup_escape_text(id->lang, -1); | 883 lang = g_markup_escape_text(id->lang, -1); |
886 if (id->name) | 884 if (id->name) |
887 name = g_markup_escape_text(id->name, -1); | 885 name = g_markup_escape_text(id->name, -1); |
888 | 886 |
889 g_string_append_printf(verification, "%s/%s/%s/%s<", category, | 887 tmp = g_strconcat(category, "/", type, "/", lang ? lang : "", |
890 type, lang ? lang : "", name ? name : ""); | 888 "/", name ? name : "", "<", NULL); |
891 | 889 |
890 purple_cipher_context_append(context, (const guchar *)tmp, strlen(tmp)); | |
891 | |
892 g_free(tmp); | |
892 g_free(category); | 893 g_free(category); |
893 g_free(type); | 894 g_free(type); |
894 g_free(lang); | 895 g_free(lang); |
895 g_free(name); | 896 g_free(name); |
896 } | 897 } |
897 | 898 |
898 /* concat features to the verification string */ | 899 /* concat features to the verification string */ |
899 for (node = info->features; node; node = node->next) { | 900 for (node = info->features; node; node = node->next) { |
900 verification = jabber_caps_verification_append(verification, node->data); | 901 append_escaped_string(context, node->data); |
901 } | 902 } |
902 | 903 |
903 /* concat x-data forms to the verification string */ | 904 /* concat x-data forms to the verification string */ |
904 for(node = info->forms; node; node = node->next) { | 905 for(node = info->forms; node; node = node->next) { |
905 xmlnode *data = (xmlnode *)node->data; | 906 xmlnode *data = (xmlnode *)node->data; |
906 gchar *formtype = jabber_caps_get_formtype(data); | 907 gchar *formtype = jabber_caps_get_formtype(data); |
907 GList *fields = jabber_caps_xdata_get_fields(data); | 908 GList *fields = jabber_caps_xdata_get_fields(data); |
908 | 909 |
909 /* append FORM_TYPE's field value to the verification string */ | 910 /* append FORM_TYPE's field value to the verification string */ |
910 verification = jabber_caps_verification_append(verification, formtype); | 911 append_escaped_string(context, formtype); |
911 g_free(formtype); | 912 g_free(formtype); |
912 | 913 |
913 while (fields) { | 914 while (fields) { |
914 GList *value; | 915 GList *value; |
915 JabberDataFormField *field = (JabberDataFormField*)fields->data; | 916 JabberDataFormField *field = (JabberDataFormField*)fields->data; |
916 | 917 |
917 if (strcmp(field->var, "FORM_TYPE")) { | 918 if (!g_str_equal(field->var, "FORM_TYPE")) { |
918 /* Append the "var" attribute */ | 919 /* Append the "var" attribute */ |
919 verification = jabber_caps_verification_append(verification, field->var); | 920 append_escaped_string(context, field->var); |
920 /* Append <value/> elements' cdata */ | 921 /* Append <value/> elements' cdata */ |
921 for(value = field->values; value; value = value->next) { | 922 for (value = field->values; value; value = value->next) { |
922 verification = jabber_caps_verification_append(verification, value->data); | 923 append_escaped_string(context, value->data); |
923 g_free(value->data); | 924 g_free(value->data); |
924 } | 925 } |
925 } | 926 } |
926 | 927 |
927 g_free(field->var); | 928 g_free(field->var); |
930 fields = g_list_delete_link(fields, fields); | 931 fields = g_list_delete_link(fields, fields); |
931 } | 932 } |
932 } | 933 } |
933 | 934 |
934 /* generate hash */ | 935 /* generate hash */ |
935 purple_cipher_context_append(context, (guchar*)verification->str, verification->len); | 936 success = purple_cipher_context_digest(context, checksum_size, |
936 | |
937 success = purple_cipher_context_digest(context, verification->len, | |
938 checksum, &checksum_size); | 937 checksum, &checksum_size); |
939 | 938 |
940 g_string_free(verification, TRUE); | |
941 purple_cipher_context_destroy(context); | 939 purple_cipher_context_destroy(context); |
942 | 940 |
943 return (success ? purple_base64_encode(checksum, checksum_size) : NULL); | 941 return (success ? purple_base64_encode(checksum, checksum_size) : NULL); |
944 } | 942 } |
945 | 943 |