Header And Logo

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

latin2_and_win1250.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *    LATIN2 and WIN1250
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/latin2_and_win1250/latin2_and_win1250.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #include "postgres.h"
00015 #include "fmgr.h"
00016 #include "mb/pg_wchar.h"
00017 
00018 #define ENCODING_GROWTH_RATE 4
00019 
00020 PG_MODULE_MAGIC;
00021 
00022 PG_FUNCTION_INFO_V1(latin2_to_mic);
00023 PG_FUNCTION_INFO_V1(mic_to_latin2);
00024 PG_FUNCTION_INFO_V1(win1250_to_mic);
00025 PG_FUNCTION_INFO_V1(mic_to_win1250);
00026 PG_FUNCTION_INFO_V1(latin2_to_win1250);
00027 PG_FUNCTION_INFO_V1(win1250_to_latin2);
00028 
00029 extern Datum latin2_to_mic(PG_FUNCTION_ARGS);
00030 extern Datum mic_to_latin2(PG_FUNCTION_ARGS);
00031 extern Datum win1250_to_mic(PG_FUNCTION_ARGS);
00032 extern Datum mic_to_win1250(PG_FUNCTION_ARGS);
00033 extern Datum latin2_to_win1250(PG_FUNCTION_ARGS);
00034 extern Datum win1250_to_latin2(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 static void latin22mic(const unsigned char *l, unsigned char *p, int len);
00048 static void mic2latin2(const unsigned char *mic, unsigned char *p, int len);
00049 static void win12502mic(const unsigned char *l, unsigned char *p, int len);
00050 static void mic2win1250(const unsigned char *mic, unsigned char *p, int len);
00051 
00052 Datum
00053 latin2_to_mic(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_LATIN2, PG_MULE_INTERNAL);
00060 
00061     latin22mic(src, dest, len);
00062 
00063     PG_RETURN_VOID();
00064 }
00065 
00066 Datum
00067 mic_to_latin2(PG_FUNCTION_ARGS)
00068 {
00069     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00070     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00071     int         len = PG_GETARG_INT32(4);
00072 
00073     CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_LATIN2);
00074 
00075     mic2latin2(src, dest, len);
00076 
00077     PG_RETURN_VOID();
00078 }
00079 
00080 Datum
00081 win1250_to_mic(PG_FUNCTION_ARGS)
00082 {
00083     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00084     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00085     int         len = PG_GETARG_INT32(4);
00086 
00087     CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_MULE_INTERNAL);
00088 
00089     win12502mic(src, dest, len);
00090 
00091     PG_RETURN_VOID();
00092 }
00093 
00094 Datum
00095 mic_to_win1250(PG_FUNCTION_ARGS)
00096 {
00097     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00098     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00099     int         len = PG_GETARG_INT32(4);
00100 
00101     CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_WIN1250);
00102 
00103     mic2win1250(src, dest, len);
00104 
00105     PG_RETURN_VOID();
00106 }
00107 
00108 Datum
00109 latin2_to_win1250(PG_FUNCTION_ARGS)
00110 {
00111     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00112     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00113     int         len = PG_GETARG_INT32(4);
00114     unsigned char *buf;
00115 
00116     CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN2, PG_WIN1250);
00117 
00118     buf = palloc(len * ENCODING_GROWTH_RATE + 1);
00119     latin22mic(src, buf, len);
00120     mic2win1250(buf, dest, strlen((char *) buf));
00121     pfree(buf);
00122 
00123     PG_RETURN_VOID();
00124 }
00125 
00126 Datum
00127 win1250_to_latin2(PG_FUNCTION_ARGS)
00128 {
00129     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00130     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00131     int         len = PG_GETARG_INT32(4);
00132     unsigned char *buf;
00133 
00134     CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_LATIN2);
00135 
00136     buf = palloc(len * ENCODING_GROWTH_RATE + 1);
00137     win12502mic(src, buf, len);
00138     mic2latin2(buf, dest, strlen((char *) buf));
00139     pfree(buf);
00140 
00141     PG_RETURN_VOID();
00142 }
00143 
00144 static void
00145 latin22mic(const unsigned char *l, unsigned char *p, int len)
00146 {
00147     latin2mic(l, p, len, LC_ISO8859_2, PG_LATIN2);
00148 }
00149 
00150 static void
00151 mic2latin2(const unsigned char *mic, unsigned char *p, int len)
00152 {
00153     mic2latin(mic, p, len, LC_ISO8859_2, PG_LATIN2);
00154 }
00155 
00156 /*-----------------------------------------------------------------
00157  * WIN1250
00158  * Microsoft's CP1250(windows-1250)
00159  *-----------------------------------------------------------------*/
00160 static void
00161 win12502mic(const unsigned char *l, unsigned char *p, int len)
00162 {
00163     static const unsigned char win1250_2_iso88592[] = {
00164         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00165         0x88, 0x89, 0xA9, 0x8B, 0xA6, 0xAB, 0xAE, 0xAC,
00166         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
00167         0x98, 0x99, 0xB9, 0x9B, 0xB6, 0xBB, 0xBE, 0xBC,
00168         0xA0, 0xB7, 0xA2, 0xA3, 0xA4, 0xA1, 0x00, 0xA7,
00169         0xA8, 0x00, 0xAA, 0x00, 0x00, 0xAD, 0x00, 0xAF,
00170         0xB0, 0x00, 0xB2, 0xB3, 0xB4, 0x00, 0x00, 0x00,
00171         0xB8, 0xB1, 0xBA, 0x00, 0xA5, 0xBD, 0xB5, 0xBF,
00172         0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
00173         0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
00174         0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
00175         0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
00176         0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
00177         0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
00178         0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
00179         0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
00180     };
00181 
00182     latin2mic_with_table(l, p, len, LC_ISO8859_2, PG_WIN1250,
00183                          win1250_2_iso88592);
00184 }
00185 
00186 static void
00187 mic2win1250(const unsigned char *mic, unsigned char *p, int len)
00188 {
00189     static const unsigned char iso88592_2_win1250[] = {
00190         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00191         0x88, 0x89, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x00,
00192         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
00193         0x98, 0x99, 0x00, 0x9B, 0x00, 0x00, 0x00, 0x00,
00194         0xA0, 0xA5, 0xA2, 0xA3, 0xA4, 0xBC, 0x8C, 0xA7,
00195         0xA8, 0x8A, 0xAA, 0x8D, 0x8F, 0xAD, 0x8E, 0xAF,
00196         0xB0, 0xB9, 0xB2, 0xB3, 0xB4, 0xBE, 0x9C, 0xA1,
00197         0xB8, 0x9A, 0xBA, 0x9D, 0x9F, 0xBD, 0x9E, 0xBF,
00198         0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
00199         0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
00200         0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
00201         0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
00202         0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
00203         0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
00204         0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
00205         0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
00206     };
00207 
00208     mic2latin_with_table(mic, p, len, LC_ISO8859_2, PG_WIN1250,
00209                          iso88592_2_win1250);
00210 }