comparison libpurple/util.c @ 27791:ab1185e87ca5

- more normalization for window title - apply botch_utf() to outgoing alias name in msn14 protocol - now botch_utf() and sanitize_utf() accept -1 and NULL as the length of source string and the pointer to length of converted string respectively. - now botch_ucs() and botch_utf() skip conversion if _WIN32 is defined.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 13 Mar 2008 14:50:21 +0000
parents 2cb44870580d
children 85d5b6c06a6c
comparison
equal deleted inserted replaced
27790:1aab44eab011 27791:ab1185e87ca5
4707 } 4707 }
4708 4708
4709 return g_string_free(string, FALSE); 4709 return g_string_free(string, FALSE);
4710 } 4710 }
4711 4711
4712
4713 #ifdef _WIN32
4714 void botch_ucs(gchar *ucs_src, gsize len)
4715 {
4716 /* no operation */
4717 }
4718 #else
4712 void botch_ucs(gchar *ucs_src, gsize len) 4719 void botch_ucs(gchar *ucs_src, gsize len)
4713 { 4720 {
4714 gint i; 4721 gint i;
4715 guchar *ucs = (guchar *)ucs_src; 4722 guchar *ucs = (guchar *)ucs_src;
4716 4723
4755 break; 4762 break;
4756 } 4763 }
4757 } 4764 }
4758 4765
4759 } 4766 }
4767 #endif
4760 4768
4761 #ifdef _WIN32 4769 #ifdef _WIN32
4762 void sanitize_ucs(gchar *ucs, gsize len) 4770 void sanitize_ucs(gchar *ucs, gsize len)
4763 { 4771 {
4764 /* no operation */ 4772 /* no operation */
4813 4821
4814 #ifdef _WIN32 4822 #ifdef _WIN32
4815 gchar *sanitize_utf(const gchar *msg, gsize len, gsize *newlen) 4823 gchar *sanitize_utf(const gchar *msg, gsize len, gsize *newlen)
4816 { 4824 {
4817 g_return_val_if_fail(msg != NULL, NULL); 4825 g_return_val_if_fail(msg != NULL, NULL);
4826 if(len == -1)
4827 len = strlen(msg);
4818 g_return_val_if_fail(len > 0, NULL); 4828 g_return_val_if_fail(len > 0, NULL);
4819 4829
4820 *newlen = len; 4830 if(newlen)
4831 *newlen = len;
4821 4832
4822 return g_strndup(msg, len); 4833 return g_strndup(msg, len);
4823 } 4834 }
4824 #else 4835 #else
4825 gchar *sanitize_utf(const gchar *msg, gsize len, gsize *newlen) 4836 gchar *sanitize_utf(const gchar *msg, gsize len, gsize *newlen)
4827 gint i; 4838 gint i;
4828 size_t bytes; 4839 size_t bytes;
4829 guchar *utf; 4840 guchar *utf;
4830 4841
4831 g_return_val_if_fail(msg != NULL, NULL); 4842 g_return_val_if_fail(msg != NULL, NULL);
4843 if(len == -1)
4844 len = strlen(msg);
4832 g_return_val_if_fail(len > 0, NULL); 4845 g_return_val_if_fail(len > 0, NULL);
4833 4846
4834 utf = (guchar *)g_strndup(msg, len); 4847 utf = (guchar *)g_strndup(msg, len);
4835 4848
4836 bytes = len; 4849 bytes = len;
4862 *(utf+i+2) = 0x9c; 4875 *(utf+i+2) = 0x9c;
4863 } 4876 }
4864 break; 4877 break;
4865 case 0xbf: 4878 case 0xbf:
4866 switch(*(utf+i+2)){ 4879 switch(*(utf+i+2)){
4867 case 0xa0:// ¢ 4880 case 0xa0: // ¢
4868 *(utf+i) = 0xc2; 4881 *(utf+i) = 0xc2;
4869 *(utf+i+1) = 0xa2; 4882 *(utf+i+1) = 0xa2;
4870 memmove(utf+i+2, utf+i+3, 4883 memmove(utf+i+2, utf+i+3,
4871 len-i-3); //1byte詰める 4884 len-i-3); //shorten by 1byte
4872 bytes--; 4885 bytes--;
4873 break; 4886 break;
4874 case 0xa1: // £ 4887 case 0xa1: // £
4875 *(utf+i) = 0xc2; 4888 *(utf+i) = 0xc2;
4876 *(utf+i+1) = 0xa3; 4889 *(utf+i+1) = 0xa3;
4877 memmove(utf+i+2, utf+i+3, 4890 memmove(utf+i+2, utf+i+3,
4878 len-i-3); //1byte詰める 4891 len-i-3); //shorten by 1byte
4879 bytes--; 4892 bytes--;
4880 break; 4893 break;
4881 case 0xa2: // ¬ 4894 case 0xa2: // ¬
4882 *(utf+i) = 0xc2; 4895 *(utf+i) = 0xc2;
4883 *(utf+i+1) = 0xac; 4896 *(utf+i+1) = 0xac;
4884 memmove(utf+i+2, utf+i+3, 4897 memmove(utf+i+2, utf+i+3,
4885 len-i-3); //1byte詰める 4898 len-i-3); //shorten by 1byte
4886 bytes--; 4899 bytes--;
4887 break; 4900 break;
4888 } 4901 }
4889 break; 4902 break;
4890 } 4903 }
4891 break; 4904 break;
4892 } 4905 }
4893 } 4906 }
4894 *(utf+bytes)= 0x00; //terminate 4907 *(utf+bytes)= 0x00; //terminate
4895 *newlen = bytes; 4908 if(newlen)
4909 *newlen = bytes;
4896 return (gchar *)utf; 4910 return (gchar *)utf;
4897 } 4911 }
4898 #endif 4912 #endif
4899 4913
4900 4914 #ifdef _WIN32
4915 gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen)
4916 {
4917 g_return_val_if_fail(msg != NULL, NULL);
4918 if(len == -1)
4919 len = strlen(msg);
4920 g_return_val_if_fail(len > 0, NULL);
4921
4922 if(newlen)
4923 *newlen = len;
4924
4925 return g_strndup(msg, len);
4926 }
4927 #else
4901 gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) 4928 gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen)
4902 { 4929 {
4903 int i,bytes; 4930 int i,bytes;
4904 unsigned char *utf; 4931 unsigned char *utf;
4905 4932
4906 g_return_val_if_fail(msg != NULL, NULL); 4933 g_return_val_if_fail(msg != NULL, NULL);
4934 if(len == -1)
4935 len = strlen(msg);
4907 g_return_val_if_fail(len > 0, NULL); 4936 g_return_val_if_fail(len > 0, NULL);
4908 4937
4909 bytes = len; 4938 bytes = len;
4910 4939
4911 utf = g_malloc0(bytes*3/2+1); /* new length might be 3/2 in the worst case */ 4940 utf = g_malloc0(bytes*3/2+1); /* new length might be 3/2 in the worst case */
4912 memcpy(utf, msg, bytes); 4941 memcpy(utf, msg, bytes);
4913 4942
4966 } 4995 }
4967 break; 4996 break;
4968 } //switch 4997 } //switch
4969 } 4998 }
4970 *(utf+bytes) = 0x00; //terminate 4999 *(utf+bytes) = 0x00; //terminate
4971 *newlen = bytes; 5000 if(newlen)
5001 *newlen = bytes;
4972 return (gchar *)utf; 5002 return (gchar *)utf;
4973 } 5003 }
5004 #endif