Header And Logo

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

utf8_and_cyrillic.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *    UTF8 and Cyrillic
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_cyrillic/utf8_and_cyrillic.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #include "postgres.h"
00015 #include "fmgr.h"
00016 #include "mb/pg_wchar.h"
00017 #include "../../Unicode/utf8_to_koi8r.map"
00018 #include "../../Unicode/koi8r_to_utf8.map"
00019 #include "../../Unicode/utf8_to_koi8u.map"
00020 #include "../../Unicode/koi8u_to_utf8.map"
00021 
00022 PG_MODULE_MAGIC;
00023 
00024 PG_FUNCTION_INFO_V1(utf8_to_koi8r);
00025 PG_FUNCTION_INFO_V1(koi8r_to_utf8);
00026 
00027 PG_FUNCTION_INFO_V1(utf8_to_koi8u);
00028 PG_FUNCTION_INFO_V1(koi8u_to_utf8);
00029 
00030 extern Datum utf8_to_koi8r(PG_FUNCTION_ARGS);
00031 extern Datum koi8r_to_utf8(PG_FUNCTION_ARGS);
00032 
00033 extern Datum utf8_to_koi8u(PG_FUNCTION_ARGS);
00034 extern Datum koi8u_to_utf8(PG_FUNCTION_ARGS);
00035 
00036 /* ----------
00037  * conv_proc(
00038  *      INTEGER,    -- source encoding id
00039  *      INTEGER,    -- destination encoding id
00040  *      CSTRING,    -- source string (null terminated C string)
00041  *      CSTRING,    -- destination string (null terminated C string)
00042  *      INTEGER     -- source string length
00043  * ) returns VOID;
00044  * ----------
00045  */
00046 
00047 Datum
00048 utf8_to_koi8r(PG_FUNCTION_ARGS)
00049 {
00050     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00051     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00052     int         len = PG_GETARG_INT32(4);
00053 
00054     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R);
00055 
00056     UtfToLocal(src, dest, ULmapKOI8R, NULL,
00057              sizeof(ULmapKOI8R) / sizeof(pg_utf_to_local), 0, PG_KOI8R, len);
00058 
00059     PG_RETURN_VOID();
00060 }
00061 
00062 Datum
00063 koi8r_to_utf8(PG_FUNCTION_ARGS)
00064 {
00065     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00066     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00067     int         len = PG_GETARG_INT32(4);
00068 
00069     CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8);
00070 
00071     LocalToUtf(src, dest, LUmapKOI8R, NULL,
00072              sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), 0, PG_KOI8R, len);
00073 
00074     PG_RETURN_VOID();
00075 }
00076 
00077 Datum
00078 utf8_to_koi8u(PG_FUNCTION_ARGS)
00079 {
00080     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00081     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00082     int         len = PG_GETARG_INT32(4);
00083 
00084     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8U);
00085 
00086     UtfToLocal(src, dest, ULmapKOI8U, NULL,
00087              sizeof(ULmapKOI8U) / sizeof(pg_utf_to_local), 0, PG_KOI8U, len);
00088 
00089     PG_RETURN_VOID();
00090 }
00091 
00092 Datum
00093 koi8u_to_utf8(PG_FUNCTION_ARGS)
00094 {
00095     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00096     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00097     int         len = PG_GETARG_INT32(4);
00098 
00099     CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8);
00100 
00101     LocalToUtf(src, dest, LUmapKOI8U, NULL,
00102              sizeof(LUmapKOI8U) / sizeof(pg_local_to_utf), 0, PG_KOI8U, len);
00103 
00104     PG_RETURN_VOID();
00105 }