Header And Logo

PostgreSQL
| The world's most advanced open source database.

utf8_and_win.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *    WIN <--> UTF8
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * IDENTIFICATION
00009  *    src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #include "postgres.h"
00015 #include "fmgr.h"
00016 #include "mb/pg_wchar.h"
00017 #include "../../Unicode/utf8_to_win1250.map"
00018 #include "../../Unicode/utf8_to_win1251.map"
00019 #include "../../Unicode/utf8_to_win1252.map"
00020 #include "../../Unicode/utf8_to_win1253.map"
00021 #include "../../Unicode/utf8_to_win1254.map"
00022 #include "../../Unicode/utf8_to_win1255.map"
00023 #include "../../Unicode/utf8_to_win1256.map"
00024 #include "../../Unicode/utf8_to_win1257.map"
00025 #include "../../Unicode/utf8_to_win1258.map"
00026 #include "../../Unicode/utf8_to_win866.map"
00027 #include "../../Unicode/utf8_to_win874.map"
00028 #include "../../Unicode/win1250_to_utf8.map"
00029 #include "../../Unicode/win1251_to_utf8.map"
00030 #include "../../Unicode/win1252_to_utf8.map"
00031 #include "../../Unicode/win1253_to_utf8.map"
00032 #include "../../Unicode/win1254_to_utf8.map"
00033 #include "../../Unicode/win1255_to_utf8.map"
00034 #include "../../Unicode/win1256_to_utf8.map"
00035 #include "../../Unicode/win1257_to_utf8.map"
00036 #include "../../Unicode/win866_to_utf8.map"
00037 #include "../../Unicode/win874_to_utf8.map"
00038 #include "../../Unicode/win1258_to_utf8.map"
00039 
00040 PG_MODULE_MAGIC;
00041 
00042 PG_FUNCTION_INFO_V1(win_to_utf8);
00043 PG_FUNCTION_INFO_V1(utf8_to_win);
00044 
00045 extern Datum win_to_utf8(PG_FUNCTION_ARGS);
00046 extern Datum utf8_to_win(PG_FUNCTION_ARGS);
00047 
00048 /* ----------
00049  * conv_proc(
00050  *      INTEGER,    -- source encoding id
00051  *      INTEGER,    -- destination encoding id
00052  *      CSTRING,    -- source string (null terminated C string)
00053  *      CSTRING,    -- destination string (null terminated C string)
00054  *      INTEGER     -- source string length
00055  * ) returns VOID;
00056  * ----------
00057  */
00058 
00059 typedef struct
00060 {
00061     pg_enc      encoding;
00062     pg_local_to_utf *map1;      /* to UTF8 map name */
00063     pg_utf_to_local *map2;      /* from UTF8 map name */
00064     int         size1;          /* size of map1 */
00065     int         size2;          /* size of map2 */
00066 } pg_conv_map;
00067 
00068 static pg_conv_map maps[] = {
00069     {PG_WIN866, LUmapWIN866, ULmapWIN866,
00070         sizeof(LUmapWIN866) / sizeof(pg_local_to_utf),
00071     sizeof(ULmapWIN866) / sizeof(pg_utf_to_local)},
00072     {PG_WIN874, LUmapWIN874, ULmapWIN874,
00073         sizeof(LUmapWIN874) / sizeof(pg_local_to_utf),
00074     sizeof(ULmapWIN874) / sizeof(pg_utf_to_local)},
00075     {PG_WIN1250, LUmapWIN1250, ULmapWIN1250,
00076         sizeof(LUmapWIN1250) / sizeof(pg_local_to_utf),
00077     sizeof(ULmapWIN1250) / sizeof(pg_utf_to_local)},
00078     {PG_WIN1251, LUmapWIN1251, ULmapWIN1251,
00079         sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf),
00080     sizeof(ULmapWIN1251) / sizeof(pg_utf_to_local)},
00081     {PG_WIN1252, LUmapWIN1252, ULmapWIN1252,
00082         sizeof(LUmapWIN1252) / sizeof(pg_local_to_utf),
00083     sizeof(ULmapWIN1252) / sizeof(pg_utf_to_local)},
00084     {PG_WIN1253, LUmapWIN1253, ULmapWIN1253,
00085         sizeof(LUmapWIN1253) / sizeof(pg_local_to_utf),
00086     sizeof(ULmapWIN1253) / sizeof(pg_utf_to_local)},
00087     {PG_WIN1254, LUmapWIN1254, ULmapWIN1254,
00088         sizeof(LUmapWIN1254) / sizeof(pg_local_to_utf),
00089     sizeof(ULmapWIN1254) / sizeof(pg_utf_to_local)},
00090     {PG_WIN1255, LUmapWIN1255, ULmapWIN1255,
00091         sizeof(LUmapWIN1255) / sizeof(pg_local_to_utf),
00092     sizeof(ULmapWIN1255) / sizeof(pg_utf_to_local)},
00093     {PG_WIN1256, LUmapWIN1256, ULmapWIN1256,
00094         sizeof(LUmapWIN1256) / sizeof(pg_local_to_utf),
00095     sizeof(ULmapWIN1256) / sizeof(pg_utf_to_local)},
00096     {PG_WIN1257, LUmapWIN1257, ULmapWIN1257,
00097         sizeof(LUmapWIN1257) / sizeof(pg_local_to_utf),
00098     sizeof(ULmapWIN1257) / sizeof(pg_utf_to_local)},
00099     {PG_WIN1258, LUmapWIN1258, ULmapWIN1258,
00100         sizeof(LUmapWIN1258) / sizeof(pg_local_to_utf),
00101     sizeof(ULmapWIN1258) / sizeof(pg_utf_to_local)},
00102 };
00103 
00104 Datum
00105 win_to_utf8(PG_FUNCTION_ARGS)
00106 {
00107     int         encoding = PG_GETARG_INT32(0);
00108     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00109     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00110     int         len = PG_GETARG_INT32(4);
00111     int         i;
00112 
00113     CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
00114 
00115     for (i = 0; i < sizeof(maps) / sizeof(pg_conv_map); i++)
00116     {
00117         if (encoding == maps[i].encoding)
00118         {
00119             LocalToUtf(src, dest, maps[i].map1, NULL, maps[i].size1, 0, encoding, len);
00120             PG_RETURN_VOID();
00121         }
00122     }
00123 
00124     ereport(ERROR,
00125             (errcode(ERRCODE_INTERNAL_ERROR),
00126       errmsg("unexpected encoding ID %d for WIN character sets", encoding)));
00127 
00128     PG_RETURN_VOID();
00129 }
00130 
00131 Datum
00132 utf8_to_win(PG_FUNCTION_ARGS)
00133 {
00134     int         encoding = PG_GETARG_INT32(1);
00135     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00136     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00137     int         len = PG_GETARG_INT32(4);
00138     int         i;
00139 
00140     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
00141 
00142     for (i = 0; i < sizeof(maps) / sizeof(pg_conv_map); i++)
00143     {
00144         if (encoding == maps[i].encoding)
00145         {
00146             UtfToLocal(src, dest, maps[i].map2, NULL, maps[i].size2, 0, encoding, len);
00147             PG_RETURN_VOID();
00148         }
00149     }
00150 
00151     ereport(ERROR,
00152             (errcode(ERRCODE_INTERNAL_ERROR),
00153       errmsg("unexpected encoding ID %d for WIN character sets", encoding)));
00154 
00155     PG_RETURN_VOID();
00156 }