Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * SHIFT_JIS_2004 <--> UTF8
4 : *
5 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
6 : * Portions Copyright (c) 1994, Regents of the University of California
7 : *
8 : * IDENTIFICATION
9 : * src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : #include "postgres.h"
15 : #include "fmgr.h"
16 : #include "mb/pg_wchar.h"
17 : #include "../../Unicode/shift_jis_2004_to_utf8.map"
18 : #include "../../Unicode/utf8_to_shift_jis_2004.map"
19 :
166 tgl@sss.pgh.pa.us 20 CBC 6 : PG_MODULE_MAGIC_EXT(
21 : .name = "utf8_and_sjis2004",
22 : .version = PG_VERSION
23 : );
24 :
6742 ishii@postgresql.org 25 6 : PG_FUNCTION_INFO_V1(shift_jis_2004_to_utf8);
26 3 : PG_FUNCTION_INFO_V1(utf8_to_shift_jis_2004);
27 :
28 : /* ----------
29 : * conv_proc(
30 : * INTEGER, -- source encoding id
31 : * INTEGER, -- destination encoding id
32 : * CSTRING, -- source string (null terminated C string)
33 : * CSTRING, -- destination string (null terminated C string)
34 : * INTEGER, -- source string length
35 : * BOOL -- if true, don't throw an error if conversion fails
36 : * ) returns INTEGER;
37 : *
38 : * Returns the number of bytes successfully converted.
39 : * ----------
40 : */
41 : Datum
42 129 : shift_jis_2004_to_utf8(PG_FUNCTION_ARGS)
43 : {
44 129 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
45 129 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
46 129 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 47 129 : bool noError = PG_GETARG_BOOL(5);
48 : int converted;
49 :
6066 tgl@sss.pgh.pa.us 50 129 : CHECK_ENCODING_CONVERSION_ARGS(PG_SHIFT_JIS_2004, PG_UTF8);
51 :
1621 heikki.linnakangas@i 52 129 : converted = LocalToUtf(src, len, dest,
53 : &shift_jis_2004_to_unicode_tree,
54 : LUmapSHIFT_JIS_2004_combined, lengthof(LUmapSHIFT_JIS_2004_combined),
55 : NULL,
56 : PG_SHIFT_JIS_2004,
57 : noError);
58 :
59 84 : PG_RETURN_INT32(converted);
60 : }
61 :
62 : Datum
6742 ishii@postgresql.org 63 3 : utf8_to_shift_jis_2004(PG_FUNCTION_ARGS)
64 : {
65 3 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
66 3 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
67 3 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 68 3 : bool noError = PG_GETARG_BOOL(5);
69 : int converted;
70 :
6066 tgl@sss.pgh.pa.us 71 3 : CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_SHIFT_JIS_2004);
72 :
1621 heikki.linnakangas@i 73 3 : converted = UtfToLocal(src, len, dest,
74 : &shift_jis_2004_from_unicode_tree,
75 : ULmapSHIFT_JIS_2004_combined, lengthof(ULmapSHIFT_JIS_2004_combined),
76 : NULL,
77 : PG_SHIFT_JIS_2004,
78 : noError);
79 :
80 3 : PG_RETURN_INT32(converted);
81 : }
|