libbtctl-0.11.1-unicode.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. --- libbtctl-0.11.1/src/Makefile.am.unicode 2014-11-11 18:43:29.000000000 -0800
  2. +++ libbtctl-0.11.1/src/Makefile.am 2014-11-11 19:00:55.000000000 -0800
  3. @@ -70,7 +70,7 @@
  4. obex-server-source.c obex-server-source.h \
  5. obex-server-source-private.h \
  6. btobex.c btobex.h obexsdp.h obexsdp.c \
  7. - obex-client-source.c obex-client-source.h \
  8. + obex-client-source.c obex-client-source.h unicode.c unicode.h common.h \
  9. btobex-client.c btobex-client.h \
  10. btctl-types.h btctl-types.c
  11. --- libbtctl-0.11.1/src/btobex.c.unicode 2007-07-24 07:53:51.000000000 -0700
  12. +++ libbtctl-0.11.1/src/btobex.c 2014-11-11 18:56:25.000000000 -0800
  13. @@ -29,6 +29,7 @@
  14. #include "btctl-types.h"
  15. #include "btctl-marshal.h"
  16. #include "obexsdp.h"
  17. +#include "unicode.h"
  18. static gpointer parent_class = NULL;
  19. @@ -310,7 +311,7 @@
  20. if ((namebuf = g_new0 (gchar, hlen/2))) {
  21. /* FIXME: figure out which encoding of unicode is
  22. * being used and handle it properly */
  23. - OBEX_UnicodeToChar (namebuf, hv.bs, hlen);
  24. + UnicodeToChar (namebuf, hv.bs, hlen);
  25. name = namebuf;
  26. }
  27. break;
  28. --- libbtctl-0.11.1/src/obex-client-source.c.unicode 2007-03-05 15:07:19.000000000 -0800
  29. +++ libbtctl-0.11.1/src/obex-client-source.c 2014-11-11 18:57:30.000000000 -0800
  30. @@ -34,6 +34,7 @@
  31. #include "obex-client-source.h"
  32. #include "obex-server-source-private.h"
  33. +#include "unicode.h"
  34. #define OBEX_STREAM_CHUNK 4096
  35. @@ -327,7 +328,7 @@
  36. bfname = g_path_get_basename (fname);
  37. uname_size = (strlen (bfname)+1)*2;
  38. uname = g_malloc (uname_size);
  39. - OBEX_CharToUnicode (uname, bfname, uname_size);
  40. + CharToUnicode (uname, bfname, uname_size);
  41. hd.bs = uname;
  42. OBEX_ObjectAddHeader (bc->handle, object,
  43. --- /dev/null 2014-11-11 18:16:49.229711166 -0800
  44. +++ libbtctl-0.11.1/src/unicode.h 2013-03-05 12:43:50.000000000 -0800
  45. @@ -0,0 +1,39 @@
  46. +/**
  47. + \file obexftp/unicode.h
  48. + Unicode charset and encoding conversions.
  49. + ObexFTP library - language bindings for OBEX file transfer.
  50. +
  51. + Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net>
  52. +
  53. + ObexFTP is free software; you can redistribute it and/or modify
  54. + it under the terms of the GNU Lesser General Public License as
  55. + published by the Free Software Foundation; either version 2 of
  56. + the License, or (at your option) any later version.
  57. +
  58. + This program is distributed in the hope that it will be useful,
  59. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  61. + GNU Lesser General Public License for more details.
  62. +
  63. + You should have received a copy of the GNU Lesser General Public
  64. + License along with ObexFTP. If not, see <http://www.gnu.org/>.
  65. + */
  66. +
  67. +#ifndef OBEXFTP_UNICODE_H
  68. +#define OBEXFTP_UNICODE_H
  69. +
  70. +#include <inttypes.h>
  71. +
  72. +#ifdef __cplusplus
  73. +extern "C" {
  74. +#endif
  75. +
  76. +int CharToUnicode(uint8_t *uc, const uint8_t *c, int size);
  77. +int UnicodeToChar(uint8_t *c, const uint8_t *uc, int size);
  78. +int Utf8ToChar(uint8_t *c, const uint8_t *uc, int size);
  79. +
  80. +#ifdef __cplusplus
  81. +}
  82. +#endif
  83. +
  84. +#endif /* OBEXFTP_UNICODE_H */
  85. --- /dev/null 2014-11-11 18:16:49.229711166 -0800
  86. +++ libbtctl-0.11.1/src/unicode.c 2013-03-05 12:43:50.000000000 -0800
  87. @@ -0,0 +1,294 @@
  88. +/**
  89. + \file obexftp/unicode.c
  90. + Unicode charset and encoding conversions.
  91. + ObexFTP library - language bindings for OBEX file transfer.
  92. +
  93. + Copyright (c) 2007 Christian W. Zuckschwerdt <zany@triq.net>
  94. +
  95. + ObexFTP is free software; you can redistribute it and/or modify
  96. + it under the terms of the GNU Lesser General Public License as
  97. + published by the Free Software Foundation; either version 2 of
  98. + the License, or (at your option) any later version.
  99. +
  100. + This program is distributed in the hope that it will be useful,
  101. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  102. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  103. + GNU Lesser General Public License for more details.
  104. +
  105. + You should have received a copy of the GNU Lesser General Public
  106. + License along with ObexFTP. If not, see <http://www.gnu.org/>.
  107. + */
  108. +
  109. +#ifdef HAVE_CONFIG_H
  110. +#include <config.h>
  111. +#endif
  112. +
  113. +#include <stdio.h>
  114. +#include <stdlib.h>
  115. +#include <string.h>
  116. +
  117. +#ifdef _WIN32 /* no need for iconv */
  118. +#include <windows.h> /* pulls in Winnls.h */
  119. +#else
  120. +#ifdef HAVE_ICONV
  121. +#include <iconv.h>
  122. +#include <locale.h>
  123. +#ifndef ICONV_CONST
  124. +#define ICONV_CONST
  125. +#endif
  126. +#ifdef HAVE_LANGINFO_H
  127. +#include <langinfo.h>
  128. +#define locale_charset nl_langinfo(CODESET)
  129. +#else
  130. +#define locale_charset ""
  131. +#endif /* HAVE_LANGINFO_H */
  132. +#endif /* HAVE_ICONV */
  133. +#endif /* _WIN32 */
  134. +
  135. +#include "unicode.h"
  136. +
  137. +#include <common.h>
  138. +
  139. +
  140. +/**
  141. + Convert a string to UTF-16BE, tries to guess charset and encoding.
  142. +
  143. + As a lib we can't be sure what the input charset and encoding is.
  144. + Try to read the input as UTF-8, this will also work for plain ASCII (7bit).
  145. + On errors fall back to the environment locale, which again could be UTF-8.
  146. + As last resort try to copy verbatim, i.e. as ISO-8859-1.
  147. +
  148. + \note This is a quick hack until OpenOBEX is iconv-ready.
  149. + */
  150. +int CharToUnicode(uint8_t *uc, const uint8_t *c, int size)
  151. +{
  152. +#ifdef _WIN32 /* no need for iconv */
  153. + int ret, i;
  154. + char tmp;
  155. +
  156. + return_val_if_fail(uc != NULL, -1);
  157. + return_val_if_fail(c != NULL, -1);
  158. +
  159. + /* ANSI to UTF-16LE */
  160. + ret = MultiByteToWideChar(CP_ACP, 0, c, -1, (LPWSTR)uc, size);
  161. + /* turn the eggs the right way around now */
  162. + for (i=0; i < ret; i++) {
  163. + tmp = uc[2*i];
  164. + uc[2*i] = uc[2*i+1];
  165. + uc[2*i+1] = tmp;
  166. + }
  167. + return ret * 2; /* 0 on error */
  168. +#else /* _WIN32 */
  169. +
  170. +#ifdef HAVE_ICONV
  171. + iconv_t utf16;
  172. + size_t ni, no, nrc;
  173. + /* avoid type-punned dereferecing (breaks strict aliasing) */
  174. + ICONV_CONST char *cc = (ICONV_CONST char *)c;
  175. + char *ucc = (char *)uc;
  176. +
  177. + return_val_if_fail(uc != NULL, -1);
  178. + return_val_if_fail(c != NULL, -1);
  179. +
  180. + /* try UTF-8 to UTF-16BE */
  181. + ni = strlen(cc) + 1;
  182. + no = size;
  183. + utf16 = iconv_open("UTF-16BE", "UTF-8");
  184. + nrc = iconv(utf16, &cc, &ni, &ucc, &no);
  185. + (void)iconv_close(utf16);
  186. + if (nrc == (size_t)(-1)) {
  187. + DEBUG(3, "Iconv from UTF-8 conversion error: '%s'\n", cc);
  188. + } else {
  189. + return size-no;
  190. + }
  191. +
  192. + /* try current locale charset to UTF-16BE */
  193. + setlocale(LC_CTYPE, "");
  194. + DEBUG(2, "Iconv from locale \"%s\"\n", locale_charset);
  195. + cc = (ICONV_CONST char *)c;
  196. + ucc = (char *)uc;
  197. + ni = strlen(cc) + 1;
  198. + no = size;
  199. + utf16 = iconv_open("UTF-16BE", locale_charset);
  200. + nrc = iconv(utf16, &cc, &ni, &ucc, &no);
  201. + (void)iconv_close(utf16);
  202. + if (nrc == (size_t)(-1)) {
  203. + DEBUG(3, "Iconv from locale conversion error: '%s'\n", cc);
  204. + } else {
  205. + return size-no;
  206. + }
  207. +
  208. + /* fallback to ISO-8859-1 to UTF-16BE (every byte is valid here) */
  209. + cc = (ICONV_CONST char *)c;
  210. + ucc = (char *)uc;
  211. + ni = strlen(cc) + 1;
  212. + no = size;
  213. + utf16 = iconv_open("UTF-16BE", "ISO-8859-1");
  214. + nrc = iconv(utf16, &cc, &ni, &ucc, &no);
  215. + (void)iconv_close(utf16);
  216. + if (nrc == (size_t)(-1)) {
  217. + DEBUG(2, "Iconv internal conversion error: '%s'\n", cc);
  218. + return -1;
  219. + }
  220. +
  221. + return size-no;
  222. +#else /* HAVE_ICONV */
  223. + int len, n;
  224. +
  225. + if (uc == NULL || c == NULL)
  226. + return -1;
  227. +
  228. + len = n = strlen((char *) c);
  229. + if (n*2+2 > size)
  230. + return -1;
  231. +
  232. + uc[n*2+1] = 0;
  233. + uc[n*2] = 0;
  234. +
  235. + while (n--) {
  236. + uc[n*2+1] = c[n];
  237. + uc[n*2] = 0;
  238. + }
  239. +
  240. + return (len * 2) + 2;
  241. +#endif /* HAVE_ICONV */
  242. +
  243. +#endif /* _WIN32 */
  244. +}
  245. +
  246. +
  247. +/**
  248. + Convert a string from UTF-16BE to locale charset.
  249. +
  250. + Plain ASCII (7bit) and basic ISO-8859-1 will always work.
  251. + This conversion supports UTF-8 and single byte locales.
  252. +
  253. + \note This is a quick hack until OpenOBEX is iconv-ready.
  254. + */
  255. +int UnicodeToChar(uint8_t *c, const uint8_t *uc, int size)
  256. +{
  257. +#ifdef _WIN32 /* no need for iconv */
  258. + int ret, n, i;
  259. + uint8_t *le;
  260. +
  261. + return_val_if_fail(uc != NULL, -1);
  262. + return_val_if_fail(c != NULL, -1);
  263. +
  264. + /* turn the eggs around, pointy side up */
  265. + for (n=0; uc[2*n] != 0 || uc[2*n+1] != 0; n++);
  266. + le = malloc(2*n+2);
  267. + for (i=0; i <= n; i++) {
  268. + le[2*i] = uc[2*i+1];
  269. + le[2*i+1] = uc[2*i];
  270. + }
  271. + /* UTF-16LE to ANSI */
  272. + ret = WideCharToMultiByte(CP_ACP, 0, le, -1, c, size, NULL, NULL);
  273. + free(le);
  274. + return ret; /* 0 on error */
  275. +#else /* _WIN32 */
  276. +
  277. +#ifdef HAVE_ICONV
  278. + iconv_t utf16;
  279. + size_t ni, no, nrc;
  280. + /* avoid type-punned dereferecing (breaks strict aliasing) */
  281. + char *cc = (char *)c;
  282. + ICONV_CONST char *ucc = (ICONV_CONST char *)uc;
  283. +
  284. + return_val_if_fail(uc != NULL, -1);
  285. + return_val_if_fail(c != NULL, -1);
  286. +
  287. + /* UTF-16BE to current locale charset */
  288. + setlocale(LC_CTYPE, "");
  289. + DEBUG(3, "Iconv to locale \"%s\"\n", locale_charset);
  290. + for (ni=0; ucc[2*ni] != 0 || ucc[2*ni+1] != 0; ni++);
  291. + ni = 2*ni+2;
  292. + no = size;
  293. + utf16 = iconv_open(locale_charset, "UTF-16BE");
  294. + nrc = iconv(utf16, &ucc, &ni, &cc, &no);
  295. + (void)iconv_close(utf16);
  296. + if (nrc == (size_t)(-1)) {
  297. + DEBUG(2, "Iconv from locale conversion error: '%s'\n", cc);
  298. + }
  299. + return size-no;
  300. +#else /* HAVE_ICONV */
  301. + int n;
  302. +
  303. + if (uc == NULL || c == NULL)
  304. + return -1;
  305. +
  306. + /* Make sure buffer is big enough! */
  307. + for (n = 0; uc[n*2+1] != 0; n++);
  308. +
  309. + if (n >= size)
  310. + return -1;
  311. +
  312. + for (n = 0; uc[n*2+1] != 0; n++)
  313. + c[n] = uc[n*2+1];
  314. + c[n] = 0;
  315. +
  316. + return 0;
  317. +#endif /* HAVE_ICONV */
  318. +
  319. +#endif /* _WIN32 */
  320. +}
  321. +
  322. +
  323. +/**
  324. + Convert a (xml) string from UTF-8 to locale charset.
  325. +
  326. + Plain ASCII (7bit) and basic ISO-8859-1 will always work.
  327. + This conversion supports UTF-8 and single byte locales.
  328. +
  329. + \note This is a quick hack until OpenOBEX is iconv-ready.
  330. + */
  331. +int Utf8ToChar(uint8_t *c, const uint8_t *uc, int size)
  332. +{
  333. +#ifdef _WIN32 /* no need for iconv */
  334. + int ret, n, i;
  335. + uint8_t *le;
  336. +
  337. + return_val_if_fail(uc != NULL, -1);
  338. + return_val_if_fail(c != NULL, -1);
  339. +
  340. + n = strlen(uc)*2+2;
  341. + le = malloc(n);
  342. + /* UTF-8 to UTF-16LE */
  343. + ret = MultiByteToWideChar(CP_UTF8, 0, uc, -1, (LPWSTR)le, n);
  344. +
  345. + /* UTF-16LE to ANSI */
  346. + ret = WideCharToMultiByte(CP_ACP, 0, le, -1, c, size, NULL, NULL);
  347. + free(le);
  348. + return ret; /* 0 on error */
  349. +#else /* _WIN32 */
  350. +
  351. +#ifdef HAVE_ICONV
  352. + iconv_t utf8;
  353. + size_t ni, no, nrc;
  354. + /* avoid type-punned dereferecing (breaks strict aliasing) */
  355. + char *cc = (char *)c;
  356. + ICONV_CONST char *ucc = (ICONV_CONST char *)uc;
  357. +
  358. + return_val_if_fail(uc != NULL, -1);
  359. + return_val_if_fail(c != NULL, -1);
  360. +
  361. + setlocale(LC_CTYPE, "");
  362. + DEBUG(2, "Iconv to \"%s\"\n", locale_charset);
  363. + ni = strlen(ucc);
  364. + no = size;
  365. + utf8 = iconv_open(locale_charset, "UTF-8");
  366. + nrc = iconv(utf8, &ucc, &ni, &cc, &no);
  367. + (void)iconv_close(utf8);
  368. + if (nrc != (size_t)(-1)) {
  369. + DEBUG(2, "Iconv from locale conversion error: '%s'\n", cc);
  370. + }
  371. + return size-no;
  372. +#else /* HAVE_ICONV */
  373. + int n, i;
  374. + n = strlen(uc);
  375. + strncpy(c, uc, size);
  376. + c[size] = '\0';
  377. + return n;
  378. +#endif /* HAVE_ICONV */
  379. +
  380. +#endif /* _WIN32 */
  381. +}
  382. --- /dev/null 2014-11-11 18:16:49.229711166 -0800
  383. +++ libbtctl-0.11.1/src/common.h 2013-03-05 12:43:50.000000000 -0800
  384. @@ -0,0 +1,73 @@
  385. +/**
  386. + \file includes/common.h
  387. + ObexFTP common macros and debugging.
  388. + ObexFTP library - language bindings for OBEX file transfer.
  389. + */
  390. +
  391. +#ifndef _OBEXFTP_COMMON_H
  392. +#define _OBEXFTP_COMMON_H
  393. +
  394. +#ifdef HAVE_CONFIG_H
  395. +#include "config.h"
  396. +#endif
  397. +#include <stdio.h>
  398. +
  399. +#ifdef UNUSED
  400. +#elif defined(__GNUC__)
  401. +# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  402. +#elif defined(__LCLINT__)
  403. +# define UNUSED(x) /*@unused@*/ x
  404. +#else
  405. +# define UNUSED(x) x
  406. +#endif
  407. +
  408. +#ifndef FALSE
  409. +#define FALSE (0)
  410. +#endif
  411. +
  412. +#ifndef TRUE
  413. +#define TRUE (!FALSE)
  414. +#endif
  415. +
  416. +/* these are not asserts! dont define to nothing */
  417. +#define return_if_fail(expr) do { if (!(expr)) return; } while(0);
  418. +#define return_val_if_fail(expr,val) do { if (!(expr)) return val; } while(0);
  419. +
  420. +#ifdef _WIN32
  421. +#define snprintf _snprintf
  422. +#endif /* _WIN32 */
  423. +
  424. +/* use 0 for production, 1 for verification, >2 for debug */
  425. +#ifndef OBEXFTP_DEBUG
  426. +#define OBEXFTP_DEBUG 0
  427. +#endif
  428. +
  429. +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  430. +#define DEBUG(n, ...) if (OBEXFTP_DEBUG >= (n)) fprintf(stderr, __VA_ARGS__)
  431. +#elif defined (__GNUC__)
  432. +#define DEBUG(n, format...) if (OBEXFTP_DEBUG >= (n)) fprintf (stderr, format)
  433. +#else /* !__GNUC__ */
  434. +static void
  435. +DEBUG (int n, const char *format, ...)
  436. +{
  437. + va_list args;
  438. + if (OBEXFTP_DEBUG >= (n)) {
  439. + va_start (args, format);
  440. + fprintf (stderr, format, args);
  441. + va_end (args);
  442. + }
  443. +}
  444. +#endif /* !__GNUC__ */
  445. +
  446. +#if OBEXFTP_DEBUG > 4
  447. +#define DEBUGBUFFER(b,l) do { \
  448. + int i; \
  449. + for (i=0; i < (l); i++) \
  450. + fprintf (stderr, "%02x ", ((uint8_t *)(b))[i]); \
  451. + fprintf (stderr, "\n"); \
  452. +} while (0)
  453. +#else
  454. +#define DEBUGBUFFER(b,l) do { } while (0)
  455. +#endif
  456. +
  457. +#endif /* _OBEXFTP_COMMON_H */