/src/samba/lib/util/charset/pull_push.c
Line | Count | Source |
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 | | |
9 | | This program is free software; you can redistribute it and/or modify |
10 | | it under the terms of the GNU General Public License as published by |
11 | | the Free Software Foundation; either version 3 of the License, or |
12 | | (at your option) any later version. |
13 | | |
14 | | This program is distributed in the hope that it will be useful, |
15 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | GNU General Public License for more details. |
18 | | |
19 | | You should have received a copy of the GNU General Public License |
20 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | | |
22 | | */ |
23 | | |
24 | | #include "replace.h" |
25 | | #include "system/locale.h" |
26 | | #include "charset.h" |
27 | | |
28 | | /** |
29 | | * Copy a string from a unix char* src to a UCS2 destination, |
30 | | * allocating a buffer using talloc(). |
31 | | * |
32 | | * @param dest always set at least to NULL |
33 | | * @param converted_size set to the number of bytes occupied by the string in |
34 | | * the destination on success. |
35 | | * |
36 | | * @return true if new buffer was correctly allocated, and string was |
37 | | * converted. |
38 | | **/ |
39 | | bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, |
40 | | size_t *converted_size) |
41 | 55.9k | { |
42 | 55.9k | size_t src_len = strlen(src)+1; |
43 | | |
44 | 55.9k | *dest = NULL; |
45 | 55.9k | return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len, |
46 | 55.9k | (void **)dest, converted_size); |
47 | 55.9k | } |
48 | | |
49 | | /** |
50 | | * @brief Create a UTF-8 string from a unix charset string. |
51 | | * |
52 | | * The resulting UTF-8 string is talloc'ed. |
53 | | * |
54 | | * @param[in] ctx The talloc memory context. |
55 | | * |
56 | | * @param[in] dest A pointer to store the pointer to the talloc'ed UTF-8 |
57 | | * string. |
58 | | * |
59 | | * @param[in] src The unix charset string to convert. |
60 | | * |
61 | | * @param[in] converted_size A pointer to store the length of the talloc'ed |
62 | | * UTF-8 string including the nul-termination bytes. |
63 | | * |
64 | | * The destination string should be free'd using talloc_free() if no longer |
65 | | * needed. |
66 | | * |
67 | | * @return True on success, false otherwise. |
68 | | */ |
69 | | bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, |
70 | | size_t *converted_size) |
71 | 0 | { |
72 | 0 | size_t src_len = strlen(src)+1; |
73 | |
|
74 | 0 | *dest = NULL; |
75 | 0 | return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, |
76 | 0 | (void**)dest, converted_size); |
77 | 0 | } |
78 | | |
79 | | /** |
80 | | * Copy a string from a unix char* src to an ASCII destination, |
81 | | * allocating a buffer using talloc(). |
82 | | * |
83 | | * @param dest always set at least to NULL |
84 | | * |
85 | | * @param converted_size The number of bytes occupied by the string in the destination |
86 | | * @returns boolean indicating if the conversion was successful |
87 | | **/ |
88 | | bool push_ascii_talloc(TALLOC_CTX *mem_ctx, char **dest, const char *src, size_t *converted_size) |
89 | 0 | { |
90 | 0 | size_t src_len = strlen(src)+1; |
91 | |
|
92 | 0 | *dest = NULL; |
93 | 0 | return convert_string_talloc(mem_ctx, CH_UNIX, CH_DOS, src, src_len, |
94 | 0 | (void **)dest, converted_size); |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc |
99 | | * |
100 | | * @param dest always set at least to NULL |
101 | | * @param converted_size set to the number of bytes occupied by the string in |
102 | | * the destination on success. |
103 | | * |
104 | | * @return true if new buffer was correctly allocated, and string was |
105 | | * converted. |
106 | | **/ |
107 | | |
108 | | bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, |
109 | | size_t *converted_size) |
110 | 18.8k | { |
111 | 18.8k | size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t); |
112 | | |
113 | 18.8k | *dest = NULL; |
114 | 18.8k | return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, |
115 | 18.8k | (void **)dest, converted_size); |
116 | 18.8k | } |
117 | | |
118 | | |
119 | | /** |
120 | | * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc |
121 | | * |
122 | | * @param dest always set at least to NULL |
123 | | * @param converted_size set to the number of bytes occupied by the string in |
124 | | * the destination on success. |
125 | | * |
126 | | * @return true if new buffer was correctly allocated, and string was |
127 | | * converted. |
128 | | **/ |
129 | | |
130 | | bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, |
131 | | size_t *converted_size) |
132 | 0 | { |
133 | 0 | size_t src_len = strlen(src)+1; |
134 | |
|
135 | 0 | *dest = NULL; |
136 | 0 | return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, |
137 | 0 | (void **)dest, converted_size); |
138 | 0 | } |
139 | | |
140 | | |
141 | | /** |
142 | | * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc |
143 | | * |
144 | | * @param dest always set at least to NULL |
145 | | * @param converted_size set to the number of bytes occupied by the string in |
146 | | * the destination on success. |
147 | | * |
148 | | * @return true if new buffer was correctly allocated, and string was |
149 | | * converted. |
150 | | **/ |
151 | | |
152 | | bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, |
153 | | size_t *converted_size) |
154 | 0 | { |
155 | 0 | size_t src_len = strlen(src)+1; |
156 | |
|
157 | | *dest = NULL; |
158 | 0 | return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, |
159 | 0 | (void **)dest, converted_size); |
160 | 0 | } |