MsnObject * msn_object_new_from_string(const gchar *str) { MsnObject *obj; gchar *tag, *c; g_return_val_if_fail(str != NULL, NULL); if (strncmp(str, "<msnobj ", 8)) return NULL; obj = msn_object_new(); GET_STRING_TAG(creator, "Creator"); GET_INT_TAG(size, "Size"); GET_INT_TAG(type, "Type"); GET_STRING_TAG(location, "Location"); GET_STRING_TAG(friendly, "Friendly"); GET_STRING_TAG(sha1d, "SHA1D"); GET_STRING_TAG(sha1c, "SHA1C"); /* If we are missing any of the required elements then discard the object */ /* SHA1C is not always sent anymore */ if (obj->creator == NULL || obj->size == 0 || obj->type == 0 || obj->location == NULL || obj->friendly == NULL || obj->sha1d == NULL /*|| obj->sha1c == NULL*/) { pecan_error ("discarding: str=[%s]", str); msn_object_destroy(obj); obj = NULL; } return obj; }
MsnObject * msn_object_new_from_string(const char *str) { MsnObject *obj; char *tag, *c; g_return_val_if_fail(str != NULL, NULL); if (strncmp(str, "<msnobj ", 8)) return NULL; obj = msn_object_new(); GET_STRING_TAG(creator, "Creator"); GET_INT_TAG(size, "Size"); GET_INT_TAG(type, "Type"); GET_STRING_TAG(location, "Location"); GET_STRING_TAG(friendly, "Friendly"); GET_STRING_TAG(sha1d, "SHA1D"); GET_STRING_TAG(sha1c, "SHA1C"); GET_STRING_TAG(url, "Url"); GET_STRING_TAG(url1, "Url1"); /* If we are missing any of the required elements then discard the object */ if (obj->creator == NULL || obj->size == 0 || obj->type == 0 || obj->sha1d == NULL) { purple_debug_error("msn", "Discarding invalid msnobj: '%s'\n", str); msn_object_destroy(obj, FALSE); return NULL; } if (obj->location == NULL || obj->friendly == NULL) { /* Location/friendly are required for non-buddyicon objects */ if (obj->type != MSN_OBJECT_USERTILE) { purple_debug_error("msn", "Discarding invalid msnobj: '%s'\n", str); msn_object_destroy(obj, FALSE); return NULL; /* Buddy icon object can contain Url/Url1 instead */ } else if (obj->url == NULL || obj->url1 == NULL) { purple_debug_error("msn", "Discarding invalid msnobj: '%s'\n", str); msn_object_destroy(obj, FALSE); return NULL; } } return obj; }
void msn_user_set_buddy_icon(MsnUser *user, const char *filename) { struct stat st; FILE *fp; MsnObject *msnobj = msn_user_get_object(user); g_return_if_fail(user != NULL); if (filename == NULL || g_stat(filename, &st) == -1) { msn_user_set_object(user, NULL); } else if ((fp = g_fopen(filename, "rb")) != NULL) { unsigned char *buf; SHA_CTX ctx; gsize len; char *base64; unsigned char digest[20]; if (msnobj == NULL) { msnobj = msn_object_new(TRUE); msn_object_set_local(msnobj); msn_object_set_type(msnobj, MSN_OBJECT_USERTILE); msn_object_set_location(msnobj, "TFR2C2.tmp"); msn_object_set_creator(msnobj, msn_user_get_passport(user)); msn_user_set_object(user, msnobj); } msn_object_set_real_location(msnobj, filename); buf = g_malloc(st.st_size); len = fread(buf, 1, st.st_size, fp); fclose(fp); /* Compute the SHA1D field. */ memset(digest, 0, sizeof(digest)); shaInit(&ctx); shaUpdate(&ctx, buf, st.st_size); shaFinal(&ctx, digest); g_free(buf); base64 = gaim_base64_encode(digest, sizeof(digest)); msn_object_set_sha1d(msnobj, base64); g_free(base64); msn_object_set_size(msnobj, st.st_size); /* Compute the SHA1C field. */ buf = g_strdup_printf( "Creator%sSize%dType%dLocation%sFriendly%sSHA1D%s", msn_object_get_creator(msnobj), msn_object_get_size(msnobj), msn_object_get_type(msnobj), msn_object_get_location(msnobj), msn_object_get_friendly(msnobj), msn_object_get_sha1d(msnobj)); memset(digest, 0, sizeof(digest)); shaInit(&ctx); shaUpdate(&ctx, buf, strlen(buf)); shaFinal(&ctx, digest); g_free(buf); base64 = gaim_base64_encode(digest, sizeof(digest)); msn_object_set_sha1c(msnobj, base64); g_free(base64); } else { gaim_debug_error("msn", "Unable to open buddy icon %s!\n", filename); msn_user_set_object(user, NULL); } }
MsnObject * msn_object_new_from_image (PecanBuffer *image, const char *location, const char *creator, MsnObjectType type) { MsnObject *msnobj; PurpleCipherContext *ctx; char *buf; char *base64; unsigned char digest[20]; msnobj = NULL; if (!image) return msnobj; /* New object */ msnobj = msn_object_new (); msn_object_set_local (msnobj); msn_object_set_type (msnobj, type); msn_object_set_location (msnobj, location); msn_object_set_creator (msnobj, creator); msn_object_set_image (msnobj, image); /* Compute the SHA1D field. */ memset (digest, 0, sizeof (digest)); ctx = purple_cipher_context_new_by_name ("sha1", NULL); purple_cipher_context_append (ctx, (const gpointer) image->data, image->len); purple_cipher_context_digest (ctx, sizeof (digest), digest, NULL); base64 = purple_base64_encode (digest, sizeof (digest)); msn_object_set_sha1d (msnobj, base64); g_free (base64); msn_object_set_size (msnobj, image->len); /* Compute the SHA1C field. */ buf = g_strdup_printf ("Creator%sSize%dType%dLocation%sFriendly%sSHA1D%s", msn_object_get_creator (msnobj), msn_object_get_size (msnobj), msn_object_get_type (msnobj), msn_object_get_location (msnobj), msn_object_get_friendly (msnobj), msn_object_get_sha1d (msnobj)); memset (digest, 0, sizeof (digest)); purple_cipher_context_reset (ctx, NULL); purple_cipher_context_append (ctx, (const guchar *) buf, strlen (buf)); purple_cipher_context_digest (ctx, sizeof (digest), digest, NULL); purple_cipher_context_destroy (ctx); g_free (buf); base64 = purple_base64_encode (digest, sizeof (digest)); msn_object_set_sha1c (msnobj, base64); g_free (base64); return msnobj; }
MsnObject* msn_object_new_from_image(PurpleStoredImage *img, const char *location, const char *creator, MsnObjectType type) { MsnObject *msnobj; PurpleHash *hash; char *buf; gconstpointer data; size_t size; char *base64; unsigned char digest[20]; msnobj = NULL; if (img == NULL) return msnobj; size = purple_imgstore_get_size(img); data = purple_imgstore_get_data(img); /* New object */ msnobj = msn_object_new(); msn_object_set_local(msnobj); msn_object_set_type(msnobj, type); msn_object_set_location(msnobj, location); msn_object_set_creator(msnobj, creator); msn_object_set_image(msnobj, img); /* Compute the SHA1D field. */ memset(digest, 0, sizeof(digest)); hash = purple_sha1_hash_new(); purple_hash_append(hash, data, size); purple_hash_digest(hash, digest, sizeof(digest)); base64 = purple_base64_encode(digest, sizeof(digest)); msn_object_set_sha1d(msnobj, base64); g_free(base64); msn_object_set_size(msnobj, size); /* Compute the SHA1C field. */ buf = g_strdup_printf( "Creator%sSize%dType%dLocation%sFriendly%sSHA1D%s", msn_object_get_creator(msnobj), msn_object_get_size(msnobj), msn_object_get_type(msnobj), msn_object_get_location(msnobj), msn_object_get_friendly(msnobj), msn_object_get_sha1d(msnobj)); memset(digest, 0, sizeof(digest)); purple_hash_reset(hash); purple_hash_append(hash, (const guchar *)buf, strlen(buf)); purple_hash_digest(hash, digest, sizeof(digest)); g_object_unref(hash); g_free(buf); base64 = purple_base64_encode(digest, sizeof(digest)); msn_object_set_sha1c(msnobj, base64); g_free(base64); return msnobj; }