source: projects/specs/trunk/lib/libb/libbtctl/libbtctl-0.11.1-unicode.patch @ 9078

Revision 9078, 12.5 KB checked in by inagaki, 9 years ago (diff)

2014-11-11 Ryoichi INAGAKI <ryo1@…>

  • libbtctl: added patch
  • obex-data-server, obexftp, openobex: updated


  • libbtctl-0.11.1/src/Makefile.am

    old new  
    7070        obex-server-source.c obex-server-source.h \ 
    7171        obex-server-source-private.h \ 
    7272        btobex.c btobex.h obexsdp.h obexsdp.c \ 
    73         obex-client-source.c obex-client-source.h \ 
     73        obex-client-source.c obex-client-source.h unicode.c unicode.h common.h \ 
    7474        btobex-client.c btobex-client.h \ 
    7575        btctl-types.h btctl-types.c 
    7676 
  • libbtctl-0.11.1/src/btobex.c

    old new  
    2929#include "btctl-types.h" 
    3030#include "btctl-marshal.h" 
    3131#include "obexsdp.h" 
     32#include "unicode.h" 
    3233 
    3334static gpointer          parent_class = NULL; 
    3435 
     
    310311                                if ((namebuf = g_new0 (gchar, hlen/2))) { 
    311312                                        /* FIXME: figure out which encoding of unicode is 
    312313                                         * being used and handle it properly */ 
    313                                         OBEX_UnicodeToChar (namebuf, hv.bs, hlen); 
     314                                        UnicodeToChar (namebuf, hv.bs, hlen); 
    314315                                        name = namebuf; 
    315316                                } 
    316317                                break; 
  • libbtctl-0.11.1/src/obex-client-source.c

    old new  
    3434 
    3535#include "obex-client-source.h" 
    3636#include "obex-server-source-private.h" 
     37#include "unicode.h" 
    3738 
    3839#define OBEX_STREAM_CHUNK       4096 
    3940 
     
    327328        bfname = g_path_get_basename (fname); 
    328329        uname_size = (strlen (bfname)+1)*2; 
    329330        uname = g_malloc (uname_size); 
    330         OBEX_CharToUnicode (uname, bfname, uname_size); 
     331        CharToUnicode (uname, bfname, uname_size); 
    331332 
    332333        hd.bs = uname; 
    333334        OBEX_ObjectAddHeader (bc->handle, object, 
  • new file libbtctl-0.11.1/src/unicode.h

    - +  
     1/** 
     2        \file obexftp/unicode.h 
     3        Unicode charset and encoding conversions. 
     4        ObexFTP library - language bindings for OBEX file transfer. 
     5 
     6        Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net> 
     7 
     8        ObexFTP is free software; you can redistribute it and/or modify 
     9        it under the terms of the GNU Lesser General Public License as 
     10        published by the Free Software Foundation; either version 2 of 
     11        the License, or (at your option) any later version. 
     12 
     13        This program is distributed in the hope that it will be useful, 
     14        but WITHOUT ANY WARRANTY; without even the implied warranty of 
     15        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     16        GNU Lesser General Public License for more details. 
     17 
     18        You should have received a copy of the GNU Lesser General Public 
     19        License along with ObexFTP. If not, see <http://www.gnu.org/>. 
     20 */ 
     21 
     22#ifndef OBEXFTP_UNICODE_H 
     23#define OBEXFTP_UNICODE_H 
     24 
     25#include <inttypes.h> 
     26 
     27#ifdef __cplusplus 
     28extern "C" { 
     29#endif 
     30 
     31int CharToUnicode(uint8_t *uc, const uint8_t *c, int size); 
     32int UnicodeToChar(uint8_t *c, const uint8_t *uc, int size); 
     33int Utf8ToChar(uint8_t *c, const uint8_t *uc, int size); 
     34 
     35#ifdef __cplusplus 
     36} 
     37#endif 
     38 
     39#endif /* OBEXFTP_UNICODE_H */ 
  • new file libbtctl-0.11.1/src/unicode.c

    - +  
     1/** 
     2        \file obexftp/unicode.c 
     3        Unicode charset and encoding conversions. 
     4        ObexFTP library - language bindings for OBEX file transfer. 
     5 
     6        Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net> 
     7 
     8        ObexFTP is free software; you can redistribute it and/or modify 
     9        it under the terms of the GNU Lesser General Public License as 
     10        published by the Free Software Foundation; either version 2 of 
     11        the License, or (at your option) any later version. 
     12 
     13        This program is distributed in the hope that it will be useful, 
     14        but WITHOUT ANY WARRANTY; without even the implied warranty of 
     15        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     16        GNU Lesser General Public License for more details. 
     17 
     18        You should have received a copy of the GNU Lesser General Public 
     19        License along with ObexFTP. If not, see <http://www.gnu.org/>. 
     20 */ 
     21 
     22#ifdef HAVE_CONFIG_H 
     23#include <config.h> 
     24#endif 
     25 
     26#include <stdio.h> 
     27#include <stdlib.h> 
     28#include <string.h> 
     29 
     30#ifdef _WIN32 /* no need for iconv */ 
     31#include <windows.h> /* pulls in Winnls.h */ 
     32#else 
     33#ifdef HAVE_ICONV 
     34#include <iconv.h> 
     35#include <locale.h> 
     36#ifndef ICONV_CONST 
     37#define ICONV_CONST 
     38#endif 
     39#ifdef HAVE_LANGINFO_H 
     40#include <langinfo.h> 
     41#define locale_charset nl_langinfo(CODESET) 
     42#else 
     43#define locale_charset "" 
     44#endif /* HAVE_LANGINFO_H */ 
     45#endif /* HAVE_ICONV */ 
     46#endif /* _WIN32 */ 
     47 
     48#include "unicode.h" 
     49 
     50#include <common.h> 
     51 
     52 
     53/** 
     54        Convert a string to UTF-16BE, tries to guess charset and encoding. 
     55 
     56        As a lib we can't be sure what the input charset and encoding is. 
     57        Try to read the input as UTF-8, this will also work for plain ASCII (7bit). 
     58        On errors fall back to the environment locale, which again could be UTF-8. 
     59        As last resort try to copy verbatim, i.e. as ISO-8859-1. 
     60 
     61        \note This is a quick hack until OpenOBEX is iconv-ready. 
     62 */ 
     63int CharToUnicode(uint8_t *uc, const uint8_t *c, int size) 
     64{ 
     65#ifdef _WIN32 /* no need for iconv */ 
     66        int ret, i; 
     67        char tmp; 
     68 
     69        return_val_if_fail(uc != NULL, -1); 
     70        return_val_if_fail(c != NULL, -1); 
     71 
     72        /* ANSI to UTF-16LE */ 
     73        ret = MultiByteToWideChar(CP_ACP, 0, c, -1, (LPWSTR)uc, size); 
     74        /* turn the eggs the right way around now */ 
     75        for (i=0; i < ret; i++) { 
     76                tmp = uc[2*i]; 
     77                uc[2*i] = uc[2*i+1]; 
     78                uc[2*i+1] = tmp; 
     79        } 
     80        return ret * 2; /* 0 on error */ 
     81#else /* _WIN32 */ 
     82 
     83#ifdef HAVE_ICONV 
     84        iconv_t utf16; 
     85        size_t ni, no, nrc; 
     86        /* avoid type-punned dereferecing (breaks strict aliasing) */ 
     87        ICONV_CONST char *cc = (ICONV_CONST char *)c; 
     88        char *ucc = (char *)uc; 
     89 
     90        return_val_if_fail(uc != NULL, -1); 
     91        return_val_if_fail(c != NULL, -1); 
     92 
     93        /* try UTF-8 to UTF-16BE */ 
     94        ni = strlen(cc) + 1; 
     95        no = size; 
     96        utf16 = iconv_open("UTF-16BE", "UTF-8"); 
     97        nrc = iconv(utf16, &cc, &ni, &ucc, &no); 
     98                (void)iconv_close(utf16); 
     99        if (nrc == (size_t)(-1)) { 
     100                DEBUG(3, "Iconv from UTF-8 conversion error: '%s'\n", cc); 
     101        } else { 
     102                return size-no; 
     103        } 
     104 
     105        /* try current locale charset to UTF-16BE */ 
     106        setlocale(LC_CTYPE, ""); 
     107        DEBUG(2, "Iconv from locale \"%s\"\n", locale_charset); 
     108        cc = (ICONV_CONST char *)c; 
     109        ucc = (char *)uc; 
     110        ni = strlen(cc) + 1; 
     111        no = size; 
     112        utf16 = iconv_open("UTF-16BE", locale_charset); 
     113        nrc = iconv(utf16, &cc, &ni, &ucc, &no); 
     114                (void)iconv_close(utf16); 
     115        if (nrc == (size_t)(-1)) { 
     116                DEBUG(3, "Iconv from locale conversion error: '%s'\n", cc); 
     117        } else { 
     118                return size-no; 
     119        } 
     120 
     121        /* fallback to ISO-8859-1 to UTF-16BE (every byte is valid here) */ 
     122        cc = (ICONV_CONST char *)c; 
     123        ucc = (char *)uc; 
     124        ni = strlen(cc) + 1; 
     125        no = size; 
     126        utf16 = iconv_open("UTF-16BE", "ISO-8859-1"); 
     127        nrc = iconv(utf16, &cc, &ni, &ucc, &no); 
     128                (void)iconv_close(utf16); 
     129        if (nrc == (size_t)(-1)) { 
     130                DEBUG(2, "Iconv internal conversion error: '%s'\n", cc); 
     131                return -1; 
     132        } 
     133 
     134        return size-no; 
     135#else /* HAVE_ICONV */ 
     136        int len, n; 
     137 
     138        if (uc == NULL || c == NULL) 
     139                return -1; 
     140 
     141        len = n = strlen((char *) c); 
     142        if (n*2+2 > size) 
     143                return -1; 
     144 
     145        uc[n*2+1] = 0; 
     146        uc[n*2] = 0; 
     147 
     148        while (n--) { 
     149                uc[n*2+1] = c[n]; 
     150                uc[n*2] = 0; 
     151        } 
     152 
     153        return (len * 2) + 2; 
     154#endif /* HAVE_ICONV */ 
     155 
     156#endif /* _WIN32 */ 
     157} 
     158 
     159 
     160/** 
     161        Convert a string from UTF-16BE to locale charset. 
     162 
     163        Plain ASCII (7bit) and basic ISO-8859-1 will always work. 
     164        This conversion supports UTF-8 and single byte locales. 
     165 
     166        \note This is a quick hack until OpenOBEX is iconv-ready. 
     167 */ 
     168int UnicodeToChar(uint8_t *c, const uint8_t *uc, int size) 
     169{ 
     170#ifdef _WIN32 /* no need for iconv */ 
     171        int ret, n, i; 
     172        uint8_t *le; 
     173 
     174        return_val_if_fail(uc != NULL, -1); 
     175        return_val_if_fail(c != NULL, -1); 
     176 
     177        /* turn the eggs around, pointy side up */ 
     178        for (n=0; uc[2*n] != 0 || uc[2*n+1] != 0; n++); 
     179        le = malloc(2*n+2); 
     180        for (i=0; i <= n; i++) { 
     181                le[2*i] = uc[2*i+1]; 
     182                le[2*i+1] = uc[2*i]; 
     183        } 
     184        /* UTF-16LE to ANSI */ 
     185        ret = WideCharToMultiByte(CP_ACP, 0, le, -1, c, size, NULL, NULL); 
     186        free(le); 
     187        return ret; /* 0 on error */ 
     188#else /* _WIN32 */ 
     189 
     190#ifdef HAVE_ICONV 
     191        iconv_t utf16; 
     192        size_t ni, no, nrc; 
     193        /* avoid type-punned dereferecing (breaks strict aliasing) */ 
     194        char *cc = (char *)c; 
     195        ICONV_CONST char *ucc = (ICONV_CONST char *)uc; 
     196 
     197        return_val_if_fail(uc != NULL, -1); 
     198        return_val_if_fail(c != NULL, -1); 
     199 
     200        /* UTF-16BE to current locale charset */ 
     201        setlocale(LC_CTYPE, ""); 
     202        DEBUG(3, "Iconv to locale \"%s\"\n", locale_charset); 
     203        for (ni=0; ucc[2*ni] != 0 || ucc[2*ni+1] != 0; ni++); 
     204        ni = 2*ni+2; 
     205        no = size; 
     206        utf16 = iconv_open(locale_charset, "UTF-16BE"); 
     207        nrc = iconv(utf16, &ucc, &ni, &cc, &no); 
     208                (void)iconv_close(utf16); 
     209        if (nrc == (size_t)(-1)) { 
     210                DEBUG(2, "Iconv from locale conversion error: '%s'\n", cc); 
     211        } 
     212        return size-no; 
     213#else /* HAVE_ICONV */ 
     214        int n; 
     215 
     216        if (uc == NULL || c == NULL) 
     217                return -1; 
     218 
     219        /* Make sure buffer is big enough! */ 
     220        for (n = 0; uc[n*2+1] != 0; n++); 
     221 
     222        if (n >= size) 
     223                return -1; 
     224 
     225        for (n = 0; uc[n*2+1] != 0; n++) 
     226                c[n] = uc[n*2+1]; 
     227        c[n] = 0; 
     228 
     229        return 0; 
     230#endif /* HAVE_ICONV */ 
     231 
     232#endif /* _WIN32 */ 
     233} 
     234 
     235 
     236/** 
     237        Convert a (xml) string from UTF-8 to locale charset. 
     238 
     239        Plain ASCII (7bit) and basic ISO-8859-1 will always work. 
     240        This conversion supports UTF-8 and single byte locales. 
     241 
     242        \note This is a quick hack until OpenOBEX is iconv-ready. 
     243 */ 
     244int Utf8ToChar(uint8_t *c, const uint8_t *uc, int size) 
     245{ 
     246#ifdef _WIN32 /* no need for iconv */ 
     247        int ret, n, i; 
     248        uint8_t *le; 
     249 
     250        return_val_if_fail(uc != NULL, -1); 
     251        return_val_if_fail(c != NULL, -1); 
     252 
     253        n = strlen(uc)*2+2; 
     254        le = malloc(n); 
     255        /* UTF-8 to UTF-16LE */ 
     256        ret = MultiByteToWideChar(CP_UTF8, 0, uc, -1, (LPWSTR)le, n); 
     257 
     258        /* UTF-16LE to ANSI */ 
     259        ret = WideCharToMultiByte(CP_ACP, 0, le, -1, c, size, NULL, NULL); 
     260        free(le); 
     261        return ret; /* 0 on error */ 
     262#else /* _WIN32 */ 
     263 
     264#ifdef HAVE_ICONV 
     265        iconv_t utf8; 
     266        size_t ni, no, nrc; 
     267        /* avoid type-punned dereferecing (breaks strict aliasing) */ 
     268        char *cc = (char *)c; 
     269        ICONV_CONST char *ucc = (ICONV_CONST char *)uc; 
     270 
     271        return_val_if_fail(uc != NULL, -1); 
     272        return_val_if_fail(c != NULL, -1); 
     273 
     274        setlocale(LC_CTYPE, ""); 
     275        DEBUG(2, "Iconv to \"%s\"\n", locale_charset); 
     276        ni = strlen(ucc); 
     277        no = size; 
     278        utf8 = iconv_open(locale_charset, "UTF-8"); 
     279        nrc = iconv(utf8, &ucc, &ni, &cc, &no); 
     280                (void)iconv_close(utf8); 
     281        if (nrc != (size_t)(-1)) { 
     282                DEBUG(2, "Iconv from locale conversion error: '%s'\n", cc); 
     283        } 
     284        return size-no; 
     285#else /* HAVE_ICONV */ 
     286        int n, i; 
     287        n = strlen(uc); 
     288        strncpy(c, uc, size); 
     289        c[size] = '\0'; 
     290        return n; 
     291#endif /* HAVE_ICONV */ 
     292 
     293#endif /* _WIN32 */ 
     294} 
  • new file libbtctl-0.11.1/src/common.h

    - +  
     1/** 
     2        \file includes/common.h 
     3        ObexFTP common macros and debugging. 
     4        ObexFTP library - language bindings for OBEX file transfer. 
     5 */ 
     6 
     7#ifndef _OBEXFTP_COMMON_H 
     8#define _OBEXFTP_COMMON_H 
     9 
     10#ifdef HAVE_CONFIG_H 
     11#include "config.h" 
     12#endif 
     13#include <stdio.h> 
     14 
     15#ifdef UNUSED 
     16#elif defined(__GNUC__) 
     17# define UNUSED(x) UNUSED_ ## x __attribute__((unused)) 
     18#elif defined(__LCLINT__) 
     19# define UNUSED(x) /*@unused@*/ x 
     20#else 
     21# define UNUSED(x) x 
     22#endif 
     23 
     24#ifndef FALSE 
     25#define FALSE   (0) 
     26#endif 
     27 
     28#ifndef TRUE 
     29#define TRUE    (!FALSE) 
     30#endif 
     31 
     32/* these are not asserts! dont define to nothing */ 
     33#define return_if_fail(expr)    do { if (!(expr)) return; } while(0); 
     34#define return_val_if_fail(expr,val)    do { if (!(expr)) return val; } while(0); 
     35 
     36#ifdef _WIN32 
     37#define snprintf _snprintf 
     38#endif /* _WIN32 */ 
     39 
     40/* use 0 for production, 1 for verification, >2 for debug */ 
     41#ifndef OBEXFTP_DEBUG 
     42#define OBEXFTP_DEBUG 0 
     43#endif 
     44 
     45#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 
     46#define DEBUG(n, ...)   if (OBEXFTP_DEBUG >= (n)) fprintf(stderr, __VA_ARGS__) 
     47#elif defined (__GNUC__) 
     48#define DEBUG(n, format...)     if (OBEXFTP_DEBUG >= (n)) fprintf (stderr, format) 
     49#else   /* !__GNUC__ */ 
     50static void 
     51DEBUG (int n, const char *format, ...) 
     52{ 
     53        va_list args; 
     54        if (OBEXFTP_DEBUG >= (n)) { 
     55                va_start (args, format); 
     56                fprintf (stderr, format, args); 
     57                va_end (args); 
     58        } 
     59} 
     60#endif  /* !__GNUC__ */ 
     61 
     62#if OBEXFTP_DEBUG > 4 
     63#define DEBUGBUFFER(b,l) do { \ 
     64        int i; \ 
     65        for (i=0; i < (l); i++) \ 
     66                fprintf (stderr, "%02x ", ((uint8_t *)(b))[i]); \ 
     67        fprintf (stderr, "\n"); \ 
     68} while (0) 
     69#else 
     70#define DEBUGBUFFER(b,l) do { } while (0) 
     71#endif 
     72 
     73#endif /* _OBEXFTP_COMMON_H */ 
Note: See TracBrowser for help on using the repository browser.