Header And Logo

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

utf8_and_ascii.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *    ASCII <--> 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_ascii/utf8_and_ascii.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #include "postgres.h"
00015 #include "fmgr.h"
00016 #include "mb/pg_wchar.h"
00017 
00018 PG_MODULE_MAGIC;
00019 
00020 PG_FUNCTION_INFO_V1(ascii_to_utf8);
00021 PG_FUNCTION_INFO_V1(utf8_to_ascii);
00022 
00023 extern Datum ascii_to_utf8(PG_FUNCTION_ARGS);
00024 extern Datum utf8_to_ascii(PG_FUNCTION_ARGS);
00025 
00026 /* ----------
00027  * conv_proc(
00028  *      INTEGER,    -- source encoding id
00029  *      INTEGER,    -- destination encoding id
00030  *      CSTRING,    -- source string (null terminated C string)
00031  *      CSTRING,    -- destination string (null terminated C string)
00032  *      INTEGER     -- source string length
00033  * ) returns VOID;
00034  * ----------
00035  */
00036 
00037 Datum
00038 ascii_to_utf8(PG_FUNCTION_ARGS)
00039 {
00040     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00041     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00042     int         len = PG_GETARG_INT32(4);
00043 
00044     CHECK_ENCODING_CONVERSION_ARGS(PG_SQL_ASCII, PG_UTF8);
00045 
00046     /* this looks wrong, but basically we're just rejecting high-bit-set */
00047     pg_ascii2mic(src, dest, len);
00048 
00049     PG_RETURN_VOID();
00050 }
00051 
00052 Datum
00053 utf8_to_ascii(PG_FUNCTION_ARGS)
00054 {
00055     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00056     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00057     int         len = PG_GETARG_INT32(4);
00058 
00059     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_SQL_ASCII);
00060 
00061     /* this looks wrong, but basically we're just rejecting high-bit-set */
00062     pg_mic2ascii(src, dest, len);
00063 
00064     PG_RETURN_VOID();
00065 }