PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/lib/charcnv.c

https://gitlab.com/miztake/samba
C | 535 lines | 294 code | 62 blank | 179 comment | 83 complexity | afda8661a8c4f4215b3a73130c814ebf MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Character set conversion Extensions
  4. Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
  5. Copyright (C) Andrew Tridgell 2001
  6. Copyright (C) Simo Sorce 2001
  7. Copyright (C) Martin Pool 2003
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "includes.h"
  20. /**
  21. * Destroy global objects allocated by init_iconv()
  22. **/
  23. void gfree_charcnv(void)
  24. {
  25. free_iconv_handle();
  26. }
  27. /**
  28. * Copy a string from a char* unix src to a dos codepage string destination.
  29. *
  30. * @return the number of bytes occupied by the string in the destination.
  31. *
  32. * @param flags can include
  33. * <dl>
  34. * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
  35. * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
  36. * </dl>
  37. *
  38. * @param dest_len the maximum length in bytes allowed in the
  39. * destination.
  40. **/
  41. size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
  42. {
  43. size_t src_len = 0;
  44. char *tmpbuf = NULL;
  45. size_t size = 0;
  46. bool ret;
  47. /* No longer allow a length of -1. */
  48. if (dest_len == (size_t)-1) {
  49. smb_panic("push_ascii - dest_len == -1");
  50. }
  51. if (flags & STR_UPPER) {
  52. tmpbuf = SMB_STRDUP(src);
  53. if (!tmpbuf) {
  54. smb_panic("malloc fail");
  55. }
  56. if (!strupper_m(tmpbuf)) {
  57. if ((flags & (STR_TERMINATE|STR_TERMINATE_ASCII)) &&
  58. dest &&
  59. dest_len > 0) {
  60. *(char *)dest = 0;
  61. }
  62. SAFE_FREE(tmpbuf);
  63. return 0;
  64. }
  65. src = tmpbuf;
  66. }
  67. src_len = strlen(src);
  68. if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
  69. src_len++;
  70. }
  71. ret = convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, &size);
  72. SAFE_FREE(tmpbuf);
  73. if (ret == false) {
  74. if ((flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) &&
  75. dest_len > 0) {
  76. ((char *)dest)[0] = '\0';
  77. }
  78. return 0;
  79. }
  80. return size;
  81. }
  82. /********************************************************************
  83. Push and malloc an ascii string. src and dest null terminated.
  84. ********************************************************************/
  85. /**
  86. * Copy a string from a dos codepage source to a unix char* destination.
  87. *
  88. * The resulting string in "dest" is always null terminated.
  89. *
  90. * @param flags can have:
  91. * <dl>
  92. * <dt>STR_TERMINATE</dt>
  93. * <dd>STR_TERMINATE means the string in @p src
  94. * is null terminated, and src_len is ignored.</dd>
  95. * </dl>
  96. *
  97. * @param src_len is the length of the source area in bytes.
  98. * @returns the number of bytes occupied by the string in @p src.
  99. **/
  100. size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
  101. {
  102. bool ret;
  103. size_t size = 0;
  104. if (dest_len == (size_t)-1) {
  105. /* No longer allow dest_len of -1. */
  106. smb_panic("pull_ascii - invalid dest_len of -1");
  107. }
  108. if (flags & STR_TERMINATE) {
  109. if (src_len == (size_t)-1) {
  110. src_len = strlen((const char *)src) + 1;
  111. } else {
  112. size_t len = strnlen((const char *)src, src_len);
  113. if (len < src_len)
  114. len++;
  115. src_len = len;
  116. }
  117. }
  118. ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, &size);
  119. if (ret == false) {
  120. size = 0;
  121. dest_len = 0;
  122. }
  123. if (dest_len && size) {
  124. /* Did we already process the terminating zero ? */
  125. if (dest[MIN(size-1, dest_len-1)] != 0) {
  126. dest[MIN(size, dest_len-1)] = 0;
  127. }
  128. } else {
  129. dest[0] = 0;
  130. }
  131. return src_len;
  132. }
  133. /**
  134. * Copy a string from a dos codepage source to a unix char* destination.
  135. * Talloc version.
  136. *
  137. * The resulting string in "dest" is always null terminated.
  138. *
  139. * @param flags can have:
  140. * <dl>
  141. * <dt>STR_TERMINATE</dt>
  142. * <dd>STR_TERMINATE means the string in @p src
  143. * is null terminated, and src_len is ignored.</dd>
  144. * </dl>
  145. *
  146. * @param src_len is the length of the source area in bytes.
  147. * @returns the number of bytes occupied by the string in @p src.
  148. **/
  149. static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx,
  150. char **ppdest,
  151. const void *src,
  152. size_t src_len,
  153. int flags)
  154. {
  155. char *dest = NULL;
  156. size_t dest_len;
  157. *ppdest = NULL;
  158. if (!src_len) {
  159. return 0;
  160. }
  161. if (src_len == (size_t)-1) {
  162. smb_panic("src_len == -1 in pull_ascii_base_talloc");
  163. }
  164. if (flags & STR_TERMINATE) {
  165. size_t len = strnlen((const char *)src, src_len);
  166. if (len < src_len)
  167. len++;
  168. src_len = len;
  169. /* Ensure we don't use an insane length from the client. */
  170. if (src_len >= 1024*1024) {
  171. char *msg = talloc_asprintf(ctx,
  172. "Bad src length (%u) in "
  173. "pull_ascii_base_talloc",
  174. (unsigned int)src_len);
  175. smb_panic(msg);
  176. }
  177. }
  178. /* src_len != -1 here. */
  179. if (!convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, &dest,
  180. &dest_len)) {
  181. dest_len = 0;
  182. }
  183. if (dest_len && dest) {
  184. /* Did we already process the terminating zero ? */
  185. if (dest[dest_len-1] != 0) {
  186. size_t size = talloc_get_size(dest);
  187. /* Have we got space to append the '\0' ? */
  188. if (size <= dest_len) {
  189. /* No, realloc. */
  190. dest = talloc_realloc(ctx, dest, char,
  191. dest_len+1);
  192. if (!dest) {
  193. /* talloc fail. */
  194. dest_len = (size_t)-1;
  195. return 0;
  196. }
  197. }
  198. /* Yay - space ! */
  199. dest[dest_len] = '\0';
  200. dest_len++;
  201. }
  202. } else if (dest) {
  203. dest[0] = 0;
  204. }
  205. *ppdest = dest;
  206. return src_len;
  207. }
  208. /**
  209. * Copy a string from a char* src to a unicode destination.
  210. *
  211. * @returns the number of bytes occupied by the string in the destination.
  212. *
  213. * @param flags can have:
  214. *
  215. * <dl>
  216. * <dt>STR_TERMINATE <dd>means include the null termination.
  217. * <dt>STR_UPPER <dd>means uppercase in the destination.
  218. * <dt>STR_NOALIGN <dd>means don't do alignment.
  219. * </dl>
  220. *
  221. * @param dest_len is the maximum length allowed in the
  222. * destination.
  223. **/
  224. static size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
  225. {
  226. size_t len=0;
  227. size_t src_len;
  228. size_t size = 0;
  229. bool ret;
  230. if (dest_len == (size_t)-1) {
  231. /* No longer allow dest_len of -1. */
  232. smb_panic("push_ucs2 - invalid dest_len of -1");
  233. }
  234. if (flags & STR_TERMINATE)
  235. src_len = (size_t)-1;
  236. else
  237. src_len = strlen(src);
  238. if (ucs2_align(base_ptr, dest, flags)) {
  239. *(char *)dest = 0;
  240. dest = (void *)((char *)dest + 1);
  241. if (dest_len)
  242. dest_len--;
  243. len++;
  244. }
  245. /* ucs2 is always a multiple of 2 bytes */
  246. dest_len &= ~1;
  247. ret = convert_string(CH_UNIX, CH_UTF16LE, src, src_len, dest, dest_len, &size);
  248. if (ret == false) {
  249. if ((flags & STR_TERMINATE) &&
  250. dest &&
  251. dest_len) {
  252. *(char *)dest = 0;
  253. }
  254. return len;
  255. }
  256. len += size;
  257. if (flags & STR_UPPER) {
  258. smb_ucs2_t *dest_ucs2 = (smb_ucs2_t *)dest;
  259. size_t i;
  260. /* We check for i < (size / 2) below as the dest string isn't null
  261. terminated if STR_TERMINATE isn't set. */
  262. for (i = 0; i < (size / 2) && i < (dest_len / 2) && dest_ucs2[i]; i++) {
  263. smb_ucs2_t v = toupper_w(dest_ucs2[i]);
  264. if (v != dest_ucs2[i]) {
  265. dest_ucs2[i] = v;
  266. }
  267. }
  268. }
  269. return len;
  270. }
  271. /**
  272. Copy a string from a ucs2 source to a unix char* destination.
  273. Talloc version with a base pointer.
  274. Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
  275. needs fixing. JRA).
  276. Flags can have:
  277. STR_TERMINATE means the string in src is null terminated.
  278. STR_NOALIGN means don't try to align.
  279. if STR_TERMINATE is set then src_len is ignored if it is -1.
  280. src_len is the length of the source area in bytes
  281. Return the number of bytes occupied by the string in src.
  282. The resulting string in "dest" is always null terminated.
  283. **/
  284. static size_t pull_ucs2_base_talloc(TALLOC_CTX *ctx,
  285. const void *base_ptr,
  286. char **ppdest,
  287. const void *src,
  288. size_t src_len,
  289. int flags)
  290. {
  291. char *dest;
  292. size_t dest_len;
  293. size_t ucs2_align_len = 0;
  294. *ppdest = NULL;
  295. #ifdef DEVELOPER
  296. /* Ensure we never use the braindead "malloc" varient. */
  297. if (ctx == NULL) {
  298. smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
  299. }
  300. #endif
  301. if (!src_len) {
  302. return 0;
  303. }
  304. if (src_len == (size_t)-1) {
  305. /* no longer used anywhere, but worth checking */
  306. smb_panic("sec_len == -1 in pull_ucs2_base_talloc");
  307. }
  308. if (ucs2_align(base_ptr, src, flags)) {
  309. src = (const void *)((const char *)src + 1);
  310. src_len--;
  311. ucs2_align_len = 1;
  312. }
  313. if (flags & STR_TERMINATE) {
  314. /* src_len -1 is the default for null terminated strings. */
  315. size_t len = strnlen_w((const smb_ucs2_t *)src,
  316. src_len/2);
  317. if (len < src_len/2)
  318. len++;
  319. src_len = len*2;
  320. /* Ensure we don't use an insane length from the client. */
  321. if (src_len >= 1024*1024) {
  322. smb_panic("Bad src length in pull_ucs2_base_talloc\n");
  323. }
  324. }
  325. /* ucs2 is always a multiple of 2 bytes */
  326. src_len &= ~1;
  327. if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len,
  328. (void *)&dest, &dest_len)) {
  329. dest_len = 0;
  330. }
  331. if (dest_len) {
  332. /* Did we already process the terminating zero ? */
  333. if (dest[dest_len-1] != 0) {
  334. size_t size = talloc_get_size(dest);
  335. /* Have we got space to append the '\0' ? */
  336. if (size <= dest_len) {
  337. /* No, realloc. */
  338. dest = talloc_realloc(ctx, dest, char,
  339. dest_len+1);
  340. if (!dest) {
  341. /* talloc fail. */
  342. dest_len = (size_t)-1;
  343. return 0;
  344. }
  345. }
  346. /* Yay - space ! */
  347. dest[dest_len] = '\0';
  348. dest_len++;
  349. }
  350. } else if (dest) {
  351. dest[0] = 0;
  352. }
  353. *ppdest = dest;
  354. return src_len + ucs2_align_len;
  355. }
  356. /**
  357. Copy a string from a char* src to a unicode or ascii
  358. dos codepage destination choosing unicode or ascii based on the
  359. flags supplied
  360. Return the number of bytes occupied by the string in the destination.
  361. flags can have:
  362. STR_TERMINATE means include the null termination.
  363. STR_UPPER means uppercase in the destination.
  364. STR_ASCII use ascii even with unicode packet.
  365. STR_NOALIGN means don't do alignment.
  366. dest_len is the maximum length allowed in the destination. If dest_len
  367. is -1 then no maxiumum is used.
  368. **/
  369. size_t push_string_check_fn(void *dest, const char *src,
  370. size_t dest_len, int flags)
  371. {
  372. if (!(flags & STR_ASCII) && (flags & STR_UNICODE)) {
  373. return push_ucs2(NULL, dest, src, dest_len, flags);
  374. }
  375. return push_ascii(dest, src, dest_len, flags);
  376. }
  377. /**
  378. Copy a string from a char* src to a unicode or ascii
  379. dos codepage destination choosing unicode or ascii based on the
  380. flags in the SMB buffer starting at base_ptr.
  381. Return the number of bytes occupied by the string in the destination.
  382. flags can have:
  383. STR_TERMINATE means include the null termination.
  384. STR_UPPER means uppercase in the destination.
  385. STR_ASCII use ascii even with unicode packet.
  386. STR_NOALIGN means don't do alignment.
  387. dest_len is the maximum length allowed in the destination. If dest_len
  388. is -1 then no maxiumum is used.
  389. **/
  390. size_t push_string_base(const char *base, uint16_t flags2,
  391. void *dest, const char *src,
  392. size_t dest_len, int flags)
  393. {
  394. if (!(flags & STR_ASCII) && \
  395. ((flags & STR_UNICODE || \
  396. (flags2 & FLAGS2_UNICODE_STRINGS)))) {
  397. return push_ucs2(base, dest, src, dest_len, flags);
  398. }
  399. return push_ascii(dest, src, dest_len, flags);
  400. }
  401. /**
  402. Copy a string from a unicode or ascii source (depending on
  403. the packet flags) to a char* destination.
  404. Variant that uses talloc.
  405. Flags can have:
  406. STR_TERMINATE means the string in src is null terminated.
  407. STR_UNICODE means to force as unicode.
  408. STR_ASCII use ascii even with unicode packet.
  409. STR_NOALIGN means don't do alignment.
  410. if STR_TERMINATE is set then src_len is ignored is it is -1
  411. src_len is the length of the source area in bytes.
  412. Return the number of bytes occupied by the string in src.
  413. The resulting string in "dest" is always null terminated.
  414. **/
  415. size_t pull_string_talloc(TALLOC_CTX *ctx,
  416. const void *base_ptr,
  417. uint16_t smb_flags2,
  418. char **ppdest,
  419. const void *src,
  420. size_t src_len,
  421. int flags)
  422. {
  423. if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
  424. smb_panic("No base ptr to get flg2 and neither ASCII nor "
  425. "UNICODE defined");
  426. }
  427. if (!(flags & STR_ASCII) && \
  428. ((flags & STR_UNICODE || \
  429. (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
  430. return pull_ucs2_base_talloc(ctx,
  431. base_ptr,
  432. ppdest,
  433. src,
  434. src_len,
  435. flags);
  436. }
  437. return pull_ascii_base_talloc(ctx,
  438. ppdest,
  439. src,
  440. src_len,
  441. flags);
  442. }
  443. /*******************************************************************
  444. Write a string in (little-endian) unicode format. src is in
  445. the current DOS codepage. len is the length in bytes of the
  446. string pointed to by dst.
  447. if null_terminate is True then null terminate the packet (adds 2 bytes)
  448. the return value is the length in bytes consumed by the string, including the
  449. null termination if applied
  450. ********************************************************************/
  451. size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
  452. {
  453. int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
  454. : STR_UNICODE|STR_NOALIGN;
  455. return push_ucs2(NULL, dst, src, len, flags);
  456. }
  457. /* Converts a string from internal samba format to unicode. Always terminates.
  458. * Actually just a wrapper round push_ucs2_talloc().
  459. */
  460. int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
  461. {
  462. size_t size;
  463. if (push_ucs2_talloc(ctx, dest, src, &size))
  464. return size;
  465. else
  466. return -1;
  467. }