3
3
* client encoding and server internal encoding.
4
4
* (currently mule internal code (mic) is used)
5
5
* Tatsuo Ishii
6
- * $Id: mbutils.c,v 1.25 2001/10/25 05:49:51 momjian Exp $
6
+ * $Id: mbutils.c,v 1.26 2001/11/19 06:48:39 ishii Exp $
7
7
*/
8
8
#include "postgres.h"
9
9
@@ -220,6 +220,8 @@ pg_convert(PG_FUNCTION_ARGS)
220
220
from_mic_converter dest ;
221
221
unsigned char * result ;
222
222
text * retval ;
223
+ unsigned char * str ;
224
+ int len ;
223
225
224
226
if (encoding < 0 )
225
227
elog (ERROR , "Invalid encoding name %s" , NameStr (* s ));
@@ -231,14 +233,28 @@ pg_convert(PG_FUNCTION_ARGS)
231
233
elog (ERROR , "Conversion from %s to %s is not possible" , NameStr (* s ), encoding_name );
232
234
}
233
235
234
- result = pg_do_encoding_conversion (VARDATA (string ), VARSIZE (string ) - VARHDRSZ ,
235
- src , dest );
236
+ /* make sure that source string is null terminated */
237
+ len = VARSIZE (string ) - VARHDRSZ ;
238
+ str = palloc (len + 1 );
239
+ memcpy (str , VARDATA (string ), len );
240
+ * (str + len ) = '\0' ;
241
+
242
+ result = pg_do_encoding_conversion (str , len , src , dest );
236
243
if (result == NULL )
237
244
elog (ERROR , "Encoding conversion failed" );
238
245
239
- retval = DatumGetTextP (DirectFunctionCall1 (textin , CStringGetDatum (result )));
240
- if (result != (unsigned char * ) VARDATA (string ))
246
+ /* build text data type structre. we cannot use textin() here,
247
+ since textin assumes that input string encoding is same as
248
+ database encoding. */
249
+ len = strlen (result ) + VARHDRSZ ;
250
+ retval = palloc (len );
251
+ VARATT_SIZEP (retval ) = len ;
252
+ memcpy (VARDATA (retval ), result , len - VARHDRSZ );
253
+
254
+ /* free memory allocated by pg_do_encoding_conversion */
255
+ if (result != str )
241
256
pfree (result );
257
+ pfree (str );
242
258
243
259
/* free memory if allocated by the toaster */
244
260
PG_FREE_IF_COPY (string , 0 );
@@ -263,6 +279,8 @@ pg_convert2(PG_FUNCTION_ARGS)
263
279
from_mic_converter dest ;
264
280
unsigned char * result ;
265
281
text * retval ;
282
+ unsigned char * str ;
283
+ int len ;
266
284
267
285
if (src_encoding < 0 )
268
286
elog (ERROR , "Invalid source encoding name %s" , src_encoding_name );
@@ -275,14 +293,27 @@ pg_convert2(PG_FUNCTION_ARGS)
275
293
src_encoding_name , dest_encoding_name );
276
294
}
277
295
278
- result = pg_do_encoding_conversion (VARDATA (string ), VARSIZE (string ) - VARHDRSZ ,
279
- src , dest );
296
+ /* make sure that source string is null terminated */
297
+ len = VARSIZE (string ) - VARHDRSZ ;
298
+ str = palloc (len + 1 );
299
+ memcpy (str , VARDATA (string ), len );
300
+ * (str + len ) = '\0' ;
301
+
302
+ result = pg_do_encoding_conversion (str , len , src , dest );
280
303
if (result == NULL )
281
304
elog (ERROR , "Encoding conversion failed" );
282
305
283
- retval = DatumGetTextP (DirectFunctionCall1 (textin , CStringGetDatum (result )));
284
- if (result != (unsigned char * ) VARDATA (string ))
306
+ /* build text data type structre. we cannot use textin() here,
307
+ since textin assumes that input string encoding is same as
308
+ database encoding. */
309
+ len = strlen (result ) + VARHDRSZ ;
310
+ retval = palloc (len );
311
+ VARATT_SIZEP (retval ) = len ;
312
+ memcpy (VARDATA (retval ), result , len - VARHDRSZ );
313
+
314
+ if (result != str )
285
315
pfree (result );
316
+ pfree (str );
286
317
287
318
/* free memory if allocated by the toaster */
288
319
PG_FREE_IF_COPY (string , 0 );
0 commit comments