PostgreSQL Source Code git master
big5.c File Reference
#include "postgres_fe.h"
#include "mb/pg_wchar.h"
Include dependency graph for big5.c:

Go to the source code of this file.

Data Structures

struct  codes_t
 

Functions

static unsigned short BinarySearchRange (const codes_t *array, int high, unsigned short code)
 
unsigned short BIG5toCNS (unsigned short big5, unsigned char *lc)
 
unsigned short CNStoBIG5 (unsigned short cns, unsigned char lc)
 

Variables

static const codes_t big5Level1ToCnsPlane1 [25]
 
static const codes_t cnsPlane1ToBig5Level1 [26]
 
static const codes_t big5Level2ToCnsPlane2 [48]
 
static const codes_t cnsPlane2ToBig5Level2 [49]
 
static const unsigned short b1c4 [][2]
 
static const unsigned short b2c3 [][2]
 

Function Documentation

◆ BIG5toCNS()

unsigned short BIG5toCNS ( unsigned short  big5,
unsigned char *  lc 
)

Definition at line 292 of file big5.c.

293{
294 unsigned short cns = 0;
295 int i;
296
297 if (big5 < 0xc940U)
298 {
299 /* level 1 */
300
301 for (i = 0; i < sizeof(b1c4) / (sizeof(unsigned short) * 2); i++)
302 {
303 if (b1c4[i][0] == big5)
304 {
305 *lc = LC_CNS11643_4;
306 return (b1c4[i][1] | 0x8080U);
307 }
308 }
309
310 if (0 < (cns = BinarySearchRange(big5Level1ToCnsPlane1, 23, big5)))
311 *lc = LC_CNS11643_1;
312 }
313 else if (big5 == 0xc94aU)
314 {
315 /* level 2 */
316 *lc = LC_CNS11643_1;
317 cns = 0x4442;
318 }
319 else
320 {
321 /* level 2 */
322 for (i = 0; i < sizeof(b2c3) / (sizeof(unsigned short) * 2); i++)
323 {
324 if (b2c3[i][0] == big5)
325 {
326 *lc = LC_CNS11643_3;
327 return (b2c3[i][1] | 0x8080U);
328 }
329 }
330
331 if (0 < (cns = BinarySearchRange(big5Level2ToCnsPlane2, 46, big5)))
332 *lc = LC_CNS11643_2;
333 }
334
335 if (0 == cns)
336 { /* no mapping Big5 to CNS 11643-1992 */
337 *lc = 0;
338 return (unsigned short) '?';
339 }
340
341 return cns | 0x8080;
342}
static const codes_t big5Level1ToCnsPlane1[25]
Definition: big5.c:25
static const codes_t big5Level2ToCnsPlane2[48]
Definition: big5.c:84
static unsigned short BinarySearchRange(const codes_t *array, int high, unsigned short code)
Definition: big5.c:208
static const unsigned short b2c3[][2]
Definition: big5.c:197
static const unsigned short b1c4[][2]
Definition: big5.c:189
int i
Definition: isn.c:72
#define LC_CNS11643_3
Definition: pg_wchar.h:192
#define LC_CNS11643_1
Definition: pg_wchar.h:137
#define LC_CNS11643_4
Definition: pg_wchar.h:193
#define LC_CNS11643_2
Definition: pg_wchar.h:138

References b1c4, b2c3, big5Level1ToCnsPlane1, big5Level2ToCnsPlane2, BinarySearchRange(), i, LC_CNS11643_1, LC_CNS11643_2, LC_CNS11643_3, and LC_CNS11643_4.

Referenced by big52euc_tw(), and big52mic().

◆ BinarySearchRange()

static unsigned short BinarySearchRange ( const codes_t array,
int  high,
unsigned short  code 
)
static

Definition at line 207 of file big5.c.

209{
210 int low,
211 mid,
212 distance,
213 tmp;
214
215 low = 0;
216 mid = high >> 1;
217
218 for (; low <= high; mid = (low + high) >> 1)
219 {
220 if ((array[mid].code <= code) && (array[mid + 1].code > code))
221 {
222 if (0 == array[mid].peer)
223 return 0;
224 if (code >= 0xa140U)
225 {
226 /* big5 to cns */
227 tmp = ((code & 0xff00) - (array[mid].code & 0xff00)) >> 8;
228 high = code & 0x00ff;
229 low = array[mid].code & 0x00ff;
230
231 /*
232 * NOTE: big5 high_byte: 0xa1-0xfe, low_byte: 0x40-0x7e,
233 * 0xa1-0xfe (radicals: 0x00-0x3e, 0x3f-0x9c) big5 radix is
234 * 0x9d. [region_low, region_high] We
235 * should remember big5 has two different regions (above).
236 * There is a bias for the distance between these regions.
237 * 0xa1 - 0x7e + bias = 1 (Distance between 0xa1 and 0x7e is
238 * 1.) bias = - 0x22.
239 */
240 distance = tmp * 0x9d + high - low +
241 (high >= 0xa1 ? (low >= 0xa1 ? 0 : -0x22)
242 : (low >= 0xa1 ? +0x22 : 0));
243
244 /*
245 * NOTE: we have to convert the distance into a code point.
246 * The code point's low_byte is 0x21 plus mod_0x5e. In the
247 * first, we extract the mod_0x5e of the starting code point,
248 * subtracting 0x21, and add distance to it. Then we calculate
249 * again mod_0x5e of them, and restore the final codepoint,
250 * adding 0x21.
251 */
252 tmp = (array[mid].peer & 0x00ff) + distance - 0x21;
253 tmp = (array[mid].peer & 0xff00) + ((tmp / 0x5e) << 8)
254 + 0x21 + tmp % 0x5e;
255 return tmp;
256 }
257 else
258 {
259 /* cns to big5 */
260 tmp = ((code & 0xff00) - (array[mid].code & 0xff00)) >> 8;
261
262 /*
263 * NOTE: ISO charsets ranges between 0x21-0xfe (94charset).
264 * Its radix is 0x5e. But there is no distance bias like big5.
265 */
266 distance = tmp * 0x5e
267 + ((int) (code & 0x00ff) - (int) (array[mid].code & 0x00ff));
268
269 /*
270 * NOTE: Similar to big5 to cns conversion, we extract
271 * mod_0x9d and restore mod_0x9d into a code point.
272 */
273 low = array[mid].peer & 0x00ff;
274 tmp = low + distance - (low >= 0xa1 ? 0x62 : 0x40);
275 low = tmp % 0x9d;
276 tmp = (array[mid].peer & 0xff00) + ((tmp / 0x9d) << 8)
277 + (low > 0x3e ? 0x62 : 0x40) + low;
278 return tmp;
279 }
280 }
281 else if (array[mid].code > code)
282 high = mid - 1;
283 else
284 low = mid + 1;
285 }
286
287 return 0;
288}
unsigned short code
Definition: big5.c:20
unsigned short peer
Definition: big5.c:21

References codes_t::code, and codes_t::peer.

Referenced by BIG5toCNS(), and CNStoBIG5().

◆ CNStoBIG5()

unsigned short CNStoBIG5 ( unsigned short  cns,
unsigned char  lc 
)

Definition at line 345 of file big5.c.

346{
347 int i;
348 unsigned int big5 = 0;
349
350 cns &= 0x7f7f;
351
352 switch (lc)
353 {
354 case LC_CNS11643_1:
356 break;
357 case LC_CNS11643_2:
359 break;
360 case LC_CNS11643_3:
361 for (i = 0; i < sizeof(b2c3) / (sizeof(unsigned short) * 2); i++)
362 {
363 if (b2c3[i][1] == cns)
364 return b2c3[i][0];
365 }
366 break;
367 case LC_CNS11643_4:
368 for (i = 0; i < sizeof(b1c4) / (sizeof(unsigned short) * 2); i++)
369 {
370 if (b1c4[i][1] == cns)
371 return b1c4[i][0];
372 }
373 default:
374 break;
375 }
376 return big5;
377}
static const codes_t cnsPlane2ToBig5Level2[49]
Definition: big5.c:136
static const codes_t cnsPlane1ToBig5Level1[26]
Definition: big5.c:54

References b1c4, b2c3, BinarySearchRange(), cnsPlane1ToBig5Level1, cnsPlane2ToBig5Level2, i, LC_CNS11643_1, LC_CNS11643_2, LC_CNS11643_3, and LC_CNS11643_4.

Referenced by euc_tw2big5(), and mic2big5().

Variable Documentation

◆ b1c4

const unsigned short b1c4[][2]
static
Initial value:
= {
{0xC879, 0x2123},
{0xC87B, 0x2124},
{0xC87D, 0x212A},
{0xC8A2, 0x2152}
}

Definition at line 189 of file big5.c.

Referenced by BIG5toCNS(), and CNStoBIG5().

◆ b2c3

const unsigned short b2c3[][2]
static
Initial value:
= {
{0xF9D6, 0x4337},
{0xF9D7, 0x4F50},
{0xF9D8, 0x444E},
{0xF9D9, 0x504A},
{0xF9DA, 0x2C5D},
{0xF9DB, 0x3D7E},
{0xF9DC, 0x4B5C}
}

Definition at line 197 of file big5.c.

Referenced by BIG5toCNS(), and CNStoBIG5().

◆ big5Level1ToCnsPlane1

const codes_t big5Level1ToCnsPlane1[25]
static
Initial value:
= {
{0xA140, 0x2121},
{0xA1F6, 0x2258},
{0xA1F7, 0x2257},
{0xA1F8, 0x2259},
{0xA2AF, 0x2421},
{0xA3C0, 0x4221},
{0xa3e1, 0x0000},
{0xA440, 0x4421},
{0xACFE, 0x5753},
{0xacff, 0x0000},
{0xAD40, 0x5323},
{0xAFD0, 0x5754},
{0xBBC8, 0x6B51},
{0xBE52, 0x6B50},
{0xBE53, 0x6F5C},
{0xC1AB, 0x7536},
{0xC2CB, 0x7535},
{0xC2CC, 0x7737},
{0xC361, 0x782E},
{0xC3B9, 0x7865},
{0xC3BA, 0x7864},
{0xC3BB, 0x7866},
{0xC456, 0x782D},
{0xC457, 0x7962},
{0xc67f, 0x0000}
}

Definition at line 25 of file big5.c.

Referenced by BIG5toCNS().

◆ big5Level2ToCnsPlane2

const codes_t big5Level2ToCnsPlane2[48]
static

Definition at line 84 of file big5.c.

Referenced by BIG5toCNS().

◆ cnsPlane1ToBig5Level1

const codes_t cnsPlane1ToBig5Level1[26]
static
Initial value:
= {
{0x2121, 0xA140},
{0x2257, 0xA1F7},
{0x2258, 0xA1F6},
{0x2259, 0xA1F8},
{0x234f, 0x0000},
{0x2421, 0xA2AF},
{0x2571, 0x0000},
{0x4221, 0xA3C0},
{0x4242, 0x0000},
{0x4421, 0xA440},
{0x5323, 0xAD40},
{0x5753, 0xACFE},
{0x5754, 0xAFD0},
{0x6B50, 0xBE52},
{0x6B51, 0xBBC8},
{0x6F5C, 0xBE53},
{0x7535, 0xC2CB},
{0x7536, 0xC1AB},
{0x7737, 0xC2CC},
{0x782D, 0xC456},
{0x782E, 0xC361},
{0x7864, 0xC3BA},
{0x7865, 0xC3B9},
{0x7866, 0xC3BB},
{0x7962, 0xC457},
{0x7d4c, 0x0000}
}

Definition at line 54 of file big5.c.

Referenced by CNStoBIG5().

◆ cnsPlane2ToBig5Level2

const codes_t cnsPlane2ToBig5Level2[49]
static

Definition at line 136 of file big5.c.

Referenced by CNStoBIG5().