summaryrefslogtreecommitdiff
path: root/hangul/hanja.c
blob: 92b9c2c65181197773f20fb7c53c3b4203ad14d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
/* libhangul
 * Copyright (C) 2005-2008 Choe Hwanjin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hangul.h"
#include "hangulinternals.h"

#ifndef TRUE
#define TRUE  1
#endif

#ifndef FALSE
#define FALSE 0
#endif

enum {
    HANGUL_ERROR_NOERROR,
    HANGUL_ERROR_INVALID,
    HANGUL_ERROR_RANGE,
    HANGUL_ERROR_CANTOPEN,
};

enum {
    HANJA_TABLE_TYPE_VECTOR,
    HANJA_TABLE_TYPE_MMAP
};

typedef struct _PtrVector        PtrVector;
typedef struct _HanjaKeyEntry    HanjaKeyEntry;

typedef struct _HanjaVectorTable HanjaVectorTable;
typedef struct _HanjaMMapTable   HanjaMMapTable;

typedef struct _HanjaPair      HanjaPair;
typedef struct _HanjaPairArray HanjaPairArray;

struct _Hanja {
    uint32_t key_offset;
    uint32_t value_offset;
    uint32_t comment_offset;
};

struct _HanjaList {
    char*         key;
    size_t        len;
    size_t        alloc;
    const Hanja** items; 
};

typedef void (*HanjaTableDelete)(HanjaTable*);
typedef void (*HanjaTableMatch)(const HanjaTable*, const char*, HanjaList**);

struct _HanjaTable {
    int              type;
    HanjaTableDelete destroy;
    HanjaTableMatch  match;
};

struct _PtrVector {
    void** ptrs; 
    size_t len;
    size_t alloc;
};

struct _HanjaVectorTable {
    HanjaTable     parent;

    PtrVector*     keytable;
};

struct _HanjaKeyEntry {
    uint32_t hanja_offset;
    uint32_t nitems;
};

struct _HanjaMMapTable {
    HanjaTable     parent;

    HanjaKeyEntry* keytable;
    unsigned int   nkeys;
    unsigned int   ndata;

    void*          map;
    size_t         map_length;
};

struct _HanjaPair {
    ucschar first;
    ucschar second;
};

struct _HanjaPairArray {
    ucschar          key;
    const HanjaPair* pairs;
};

#include "hanjacompatible.h"

enum {
    HANJA_STREAM_MEMORY,
    HANJA_STREAM_FILE
};

typedef struct {
    int            type;
    unsigned char* data;
    unsigned char* current;
    size_t         length;
} HanjaMemoryStream;

typedef struct {
    int                  type;
    FILE*                file;
} HanjaFileStream;

typedef union {
    int                  type;
    HanjaMemoryStream    memory;
    HanjaFileStream      file;
} HangulStream;

static void hanja_vector_table_delete(HanjaTable* hanja_table);
static void hanja_vector_table_match(const HanjaTable* hanja_table,
				     const char* key, HanjaList** list);

static void hanja_mmap_table_delete(HanjaTable* hanja_table);
static void hanja_mmap_table_match(const HanjaTable* hanja_table,
				   const char* key, HanjaList** list);

static inline int
hangul_stream_init_as_memory(HangulStream* stream, void* data, size_t length)
{
    stream->type           = HANJA_STREAM_MEMORY;
    stream->memory.data    = data;
    stream->memory.current = data;
    stream->memory.length  = length;
    return 0;
}

static inline int
hangul_stream_init_as_file(HangulStream* stream, FILE* file)
{
    stream->type      = HANJA_STREAM_FILE;
    stream->file.file = file;
    return 0;
}

static inline bool
hangul_stream_check_range(HanjaMemoryStream* stream, unsigned char* p)
{
    if (p >= stream->data && p < stream->data + stream->length)
	return true;
    else
	return false;
}

static inline int
hangul_stream_seek(HangulStream* stream, size_t offset)
{
    if (stream->type == HANJA_STREAM_MEMORY) {
	HanjaMemoryStream* mstream = &stream->memory;
	if (!hangul_stream_check_range(mstream, mstream->current + offset))
	    return HANGUL_ERROR_RANGE;
	    
	stream->memory.current += offset;
	return 0;
    }

    return HANGUL_ERROR_INVALID;
}

static inline int
hangul_stream_read_uint32(HangulStream* stream, uint32_t* value)
{
    if (stream->type == HANJA_STREAM_MEMORY) {
	HanjaMemoryStream* mstream = &stream->memory;
	if (!hangul_stream_check_range(mstream, mstream->current + sizeof(*value)))
	    return HANGUL_ERROR_RANGE;

	memcpy(value, mstream->current, sizeof(*value));
	mstream->current += sizeof(*value);
	return 0;
    }

    return HANGUL_ERROR_INVALID;
}

static inline int
hangul_stream_write(HangulStream* stream, const void* ptr, size_t len)
{
    if (stream->type == HANJA_STREAM_MEMORY) {
	HanjaMemoryStream* mstream = &stream->memory;
	if (!hangul_stream_check_range(mstream, mstream->current + len))
	    return HANGUL_ERROR_RANGE;

	memcpy(mstream->current, ptr, len);
	mstream->current += len;
    } else if (stream->type == HANJA_STREAM_FILE) {
	fwrite(ptr, len, 1, stream->file.file);
    }

    return 0;
}

static const char utf8_skip_table[256] = {
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
};

static inline int utf8_char_len(const char *p)
{
    return utf8_skip_table[*(const unsigned char*)p];
}

static inline const char* utf8_next(const char *str)
{
    int n = utf8_char_len(str);

    while (n > 0) {
	str++;
	if (*str == '\0')
	    return str;
	n--;
    }

    return str;
}

static inline char* utf8_prev(const char *str, const char *p)
{
    for (--p; p >= str; --p) {
	if ((*p & 0xc0) != 0x80)
	    break;
    }
    return (char*)p;
}

#ifndef HAVE_MMAP

#define PROT_READ 0
#define MAP_SHARED 0
static void*
mmap(void *start, size_t length, int prot, int flags, int fd, size_t offset)
{
    start = malloc(length);
    if (start != NULL) {
	read(fd, start, length);
    }
    return start;
}

static int
munmap(void *start, size_t length)
{
    free(start);
}

#endif

static PtrVector*
ptr_vector_new(size_t initial_size)
{
    PtrVector* vector;

    if (initial_size == 0)
	initial_size = 2;

    if (initial_size > SIZE_MAX / sizeof(vector->ptrs[0]))
	return NULL;

    vector = malloc(sizeof(*vector));
    vector->len = 0;
    vector->alloc = initial_size;
    vector->ptrs = malloc(initial_size * sizeof(vector->ptrs[0]));;

    if (vector->ptrs == NULL) {
	free(vector);
	return NULL;
    }

    return vector;
}

static void
ptr_vector_delete(PtrVector* vector)
{
    if (vector != NULL) {
	free(vector->ptrs);
	free(vector);
    }
}

static inline size_t
ptr_vector_get_length(PtrVector* vector)
{
    return vector->len;
}

static void
ptr_vector_append(PtrVector* vector, void* data)
{
    if (vector->alloc > SIZE_MAX / sizeof(vector->ptrs[0]) / 2)
	return;

    if (vector->alloc < vector->len + 1) {
	size_t alloc = vector->alloc * 2;
	void** ptrs;

	ptrs = realloc(vector->ptrs, alloc * sizeof(vector->ptrs[0]));
	if (ptrs != NULL) {
	    vector->alloc = alloc;
	    vector->ptrs  = ptrs;
	}
    }

    if (vector->len + 1 <= vector->alloc) {
	vector->ptrs[vector->len] = data;
	vector->len++;
    }
}

/* hanja searching functions */
static Hanja *
hanja_new(const char *key, const char *value, const char *comment)
{
    Hanja* hanja;
    size_t size;
    size_t keylen;
    size_t valuelen;
    size_t commentlen;
    char*  p;

    keylen = strlen(key) + 1;
    valuelen = strlen(value) + 1;
    if (comment != NULL)
	commentlen = strlen(comment) + 1;
    else
	commentlen = 1;

    size = sizeof(*hanja) + keylen + valuelen + commentlen;
    hanja = malloc(size);
    if (hanja == NULL)
	return NULL;

    p = (char*)hanja + sizeof(*hanja);
    strcpy(p, key);
    p += keylen;
    strcpy(p, value);
    p += valuelen;
    if (comment != NULL)
	strcpy(p, comment);
    else
	*p = '\0';
    p += valuelen;

    hanja->key_offset     = sizeof(*hanja);
    hanja->value_offset   = sizeof(*hanja) + keylen;
    hanja->comment_offset = sizeof(*hanja) + keylen + valuelen;

    return hanja;
}

static void
hanja_delete(Hanja* hanja)
{
    free(hanja);
}

const char*
hanja_get_key(const Hanja* hanja)
{
    if (hanja != NULL) {
	const char* p  = (const char*)hanja;
	return p + hanja->key_offset;
    }
    return NULL;
}

const char*
hanja_get_value(const Hanja* hanja)
{
    if (hanja != NULL) {
	const char* p  = (const char*)hanja;
	return p + hanja->value_offset;
    }
    return NULL;
}

const char*
hanja_get_comment(const Hanja* hanja)
{
    if (hanja != NULL) {
	const char* p  = (const char*)hanja;
	return p + hanja->comment_offset;
    }
    return NULL;
}

static const Hanja*
hanja_keyentry_get_hanja(const HanjaKeyEntry* entry)
{
    const char* p = (const char*)entry;
    return (const Hanja*)(p + entry->hanja_offset);
}

static HanjaList *
hanja_list_new(const char *key)
{
    HanjaList *list;

    list = malloc(sizeof(*list));
    if (list != NULL) {
	list->key = strdup(key);
	list->len = 0;
	list->alloc = 1;
	list->items = malloc(list->alloc * sizeof(list->items[0]));
	if (list->items == NULL) {
	    free(list);
	    list = NULL;
	}
    }

    return list;
}

static void
hanja_list_reserve(HanjaList* list, size_t n)
{
    size_t size = list->alloc;

    if (n > SIZE_MAX / sizeof(list->items[0]) - list->len)
	return;

    while (size < list->len + n)
	size *= 2;

    if (size > SIZE_MAX / sizeof(list->items[0]))
	return;

    if (list->alloc < list->len + n) {
	const Hanja** data;

	data = realloc(list->items, size * sizeof(list->items[0]));
	if (data != NULL) {
	    list->alloc = size;
	    list->items = data;
	}
    }
}

static void
hanja_list_append_n(HanjaList* list, const Hanja* hanja, int n)
{
    hanja_list_reserve(list, n);

    if (list->alloc >= list->len + n) {
	unsigned int i;
	for (i = 0; i < n ; i++)
	    list->items[list->len + i] = hanja + i;
	list->len += n;
    }
}

static void
hanja_list_append_nptrs(HanjaList* list, const Hanja** hanja, int n)
{
    hanja_list_reserve(list, n);

    if (list->alloc >= list->len + n) {
	unsigned int i;
	for (i = 0; i < n ; i++)
	    list->items[list->len + i] = hanja[i];
	list->len += n;
    }
}


static PtrVector*
hanja_vectors_from_txt(const char *filename)
{
    char *save_ptr = NULL;
    char *key;
    char *value;
    char *comment;
    char lastkey[64] = { 0, };
    char buf[1024];

    FILE *file;
    PtrVector* keys = NULL;
    PtrVector* data = NULL;

    if (filename == NULL)
	return NULL;

    file = fopen(filename, "r");
    if (file == NULL) {
	return NULL;
    }

    while (fgets(buf, sizeof(buf), file) != NULL) {
	Hanja* hanja;

	/* skip comments and empty lines */
	if (buf[0] == '#' || buf[0] == '\r' || buf[0] == '\n' || buf[0] == '\0')
	    continue;

	save_ptr = NULL;
	key = strtok_r(buf, ":", &save_ptr);
	value = strtok_r(NULL, ":", &save_ptr);
	comment = strtok_r(NULL, "\r\n", &save_ptr);

	if (key == NULL || strlen(key) == 0)
	    continue;

	if (value == NULL || strlen(value) == 0)
	    continue;

	if (comment == NULL)
	    comment = "";

	if (data != NULL && strcmp(key, lastkey) != 0) {
	    if (keys == NULL)
		keys = ptr_vector_new(32);

	    ptr_vector_append(keys, data);
	    strncpy(lastkey, key, sizeof(lastkey));
	    data = NULL;
	}

	hanja = hanja_new(key, value, comment);
	if (hanja != NULL) {
	    if (data == NULL)
		data = ptr_vector_new(1);
	    ptr_vector_append(data, hanja);
	}
    }

    if (data != NULL) {
	if (keys == NULL)
	    keys = ptr_vector_new(1);
	ptr_vector_append(keys, data);
    }

    fclose(file);

    return keys;
}

static void
hanja_vectors_delete(PtrVector* vectors)
{
    unsigned int i, j;
    for (i = 0; i < vectors->len; i++) {
	PtrVector* vector = vectors->ptrs[i];

	for (j = 0; j < vector->len; j++)
	    hanja_delete(vector->ptrs[j]);

	ptr_vector_delete(vector);
    }
    ptr_vector_delete(vectors);
}

static int
hanja_vectors_save(PtrVector* vectors, HangulStream* stream)
{
    unsigned int i, j, k;
    uint32_t nkeys;
    uint32_t ndata;
    uint32_t keytable_size;
    uint32_t datatable_size;
    uint32_t last_offset;

    /* signature */
    hangul_stream_write(stream, "HANJADB\x0", 8);

    nkeys = vectors->len;
    hangul_stream_write(stream, &nkeys, sizeof(nkeys));

    ndata = 0;
    for (i = 0; i < nkeys; i++)
	ndata += ptr_vector_get_length(vectors->ptrs[i]);
    hangul_stream_write(stream, &ndata, sizeof(ndata));

    keytable_size = nkeys * sizeof(HanjaKeyEntry);
    datatable_size = ndata * sizeof(Hanja);

    /* key table */
    last_offset = keytable_size;
    for (i = 0; i < nkeys; i++) {
	HanjaKeyEntry entry;

	entry.hanja_offset = last_offset - i * sizeof(entry);
	entry.nitems       = ptr_vector_get_length(vectors->ptrs[i]);

	hangul_stream_write(stream, &entry, sizeof(entry));

	last_offset += entry.nitems * sizeof(Hanja);
    }

    /* data table */
    last_offset = datatable_size;
    k = 0;
    for (i = 0; i < nkeys; i++) {
	PtrVector* items = vectors->ptrs[i];
	for (j = 0; j < items->len; j++) {
	    const char* key;
	    const char* value;
	    const char* comment;
	    size_t key_len;
	    size_t value_len;
	    size_t comment_len;
	    Hanja hanja;
	    Hanja* item = items->ptrs[j];

	    key     = hanja_get_key(item);
	    value   = hanja_get_value(item);
	    comment = hanja_get_comment(item);

	    hanja.key_offset     = last_offset - k * sizeof(hanja);
	    hanja.value_offset   = last_offset - k * sizeof(hanja);
	    hanja.comment_offset = last_offset - k * sizeof(hanja);

	    key_len     = strlen(key) + 1;
	    value_len   = strlen(value) + 1;
	    comment_len = strlen(comment) + 1;

	    hanja.value_offset   += key_len;
	    hanja.comment_offset += key_len + value_len;

	    hangul_stream_write(stream, &hanja, sizeof(hanja));

	    last_offset += key_len + value_len + comment_len;
	    k++;
	}
    }

    /* data */
    for (i = 0; i < nkeys; i++) {
	PtrVector* items = vectors->ptrs[i];
	for (j = 0; j < items->len; j++) {
	    size_t len;
	    const char* key;
	    const char* value;
	    const char* comment;
	    Hanja* hanja = items->ptrs[j];

	    key     = hanja_get_key(hanja);
	    value   = hanja_get_value(hanja);
	    comment = hanja_get_comment(hanja);

	    len = strlen(key) + 1;
	    hangul_stream_write(stream, key, len);

	    len = strlen(value) + 1;
	    hangul_stream_write(stream, value, len);

	    len = strlen(comment) + 1;
	    hangul_stream_write(stream, comment, len);
	}
    }

    return 0;
}

int
hanja_table_txt_to_bin(const char *txtfilename, const char* binfilename)
{
    PtrVector* vectors;

    vectors = hanja_vectors_from_txt(txtfilename);
    if (vectors != NULL) {
	FILE* file;
	HangulStream stream;

	file = fopen(binfilename, "w");
	if (file != NULL) {
	    hangul_stream_init_as_file(&stream, file);
	    hanja_vectors_save(vectors, &stream);
	    fclose(file);
	}

	hanja_vectors_delete(vectors);
    }

    return 0;
}

HanjaTable*
hanja_vector_table_load(PtrVector* vector)
{
    HanjaVectorTable* table;

    table = malloc(sizeof(*table));
    if (table != NULL) {
	table->parent.type = HANJA_TABLE_TYPE_VECTOR;
	table->parent.destroy = hanja_vector_table_delete;
	table->parent.match = hanja_vector_table_match;
	table->keytable = vector;
    }

    return (HanjaTable*)table;
}

static void
hanja_vector_table_delete(HanjaTable* hanja_table)
{
    if (hanja_table != NULL) {
	HanjaVectorTable* table = (HanjaVectorTable*)hanja_table;
	hanja_vectors_delete(table->keytable);
	free(table);
    }
}

static int
vector_table_cmp(const void* m1, const void* m2)
{
    const char*  key = m1;
    const PtrVector* vector = *(const void**)m2;
    const Hanja* hanja = vector->ptrs[0];
    const char*  hanja_key = hanja_get_key(hanja);

    return strcmp(key, hanja_key);
}

static void
hanja_vector_table_match(const HanjaTable* hanja_table,
		         const char* key, HanjaList** list)
{
    const HanjaVectorTable* table;
    const PtrVector** res;

    table = (const HanjaVectorTable*)hanja_table;
    res = bsearch(key, table->keytable->ptrs, table->keytable->len,
		  sizeof(PtrVector*), vector_table_cmp);
    if (res != NULL && *res != NULL) {
	const Hanja** hanja;

	if (*list == NULL)
	    *list = hanja_list_new(key);

	hanja = (const Hanja**)res[0]->ptrs;
	hanja_list_append_nptrs(*list, hanja, res[0]->len);
    }
}

HanjaMMapTable*
hanja_mmap_table_load(void* data, size_t length)
{
    unsigned int i, j;
    uint32_t nkeys = 0;
    uint32_t ndata = 0;
    HanjaKeyEntry*  keytable = NULL;
    HanjaMMapTable* table = NULL;
    int res = 0;
    const char* end;

    HangulStream stream;

    /* signature */
    if (memcmp("HANJADB\x0", data, 8) != 0)
	goto error;

    res = hangul_stream_init_as_memory(&stream, data, length);

    res = hangul_stream_seek(&stream, 8);

    res = hangul_stream_read_uint32(&stream, &nkeys);
    if (res != 0)
	goto error;

    res = hangul_stream_read_uint32(&stream, &ndata);
    if (res != 0)
	goto error;

    end = (const char*)data + length;

    keytable = (HanjaKeyEntry*)stream.memory.current;
    if ((const char*)keytable > end)
	goto error;

    /* check integrity here.
     * If the data file is wrong, the program may access the wrong address
     * and it will be killed by segmentation fault.
     * So we check it here, before to use. */
    for (i = 0; i < nkeys; i++) {
	const Hanja*         hanja;
	const HanjaKeyEntry* entry;

	entry = &keytable[i];
	if ((const char*)entry > end)
	    goto error;

	hanja = hanja_keyentry_get_hanja(entry);
	if ((const char*)hanja > end)
	    goto error;

	for (j = 0; j < entry->nitems; j++) {
	    const char* key     = hanja_get_key(hanja + j);
	    const char* value   = hanja_get_value(hanja + j);
	    const char* comment = hanja_get_comment(hanja + j);

	    if (key > end)
		goto error;

	    if (value > end)
		goto error;

	    if (comment > end)
		goto error;
	}
    }

    /* the last byte should be nul, or the last comment will be over the
     * boundary */
    end--;
    if (end[0] != '\0')
	goto error;

    table = malloc(sizeof(*table));
    if (table == NULL)
	goto error;

    table->parent.type = HANJA_TABLE_TYPE_MMAP;
    table->parent.destroy = hanja_mmap_table_delete;
    table->parent.match = hanja_mmap_table_match;
    table->keytable = keytable;
    table->nkeys = nkeys;
    table->ndata = ndata;
    table->map = data;
    table->map_length = length;

    return table;

error:
    return NULL;
}

static void
hanja_mmap_table_delete(HanjaTable* hanja_table)
{
    if (hanja_table != NULL) {
	HanjaMMapTable* table = (HanjaMMapTable*)hanja_table;
	if (table->map != NULL) {
	    munmap(table->map, table->map_length);
	}
	free(table);
    }
}

static int
mmap_table_cmp(const void* m1, const void* m2)
{
    const char*  key = m1;
    const Hanja* hanja = hanja_keyentry_get_hanja(m2);
    const char*  hanja_key = hanja_get_key(hanja);

    return strcmp(key, hanja_key);
}

static void
hanja_mmap_table_match(const HanjaTable* hanja_table,
		       const char* key, HanjaList** list)
{
    const HanjaKeyEntry* res;
    const HanjaMMapTable* table;

    table = (const HanjaMMapTable*)hanja_table;
    res = bsearch(key, table->keytable, table->nkeys,
		  sizeof(table->keytable[0]), mmap_table_cmp);
    if (res != NULL) {
	const Hanja* hanja = hanja_keyentry_get_hanja(res); 
	if (*list == NULL)
	    *list = hanja_list_new(key);
	hanja_list_append_n(*list, hanja, res->nitems);
    }
}

HanjaTable*
hanja_table_load_from_txt(const char *filename)
{
    PtrVector*  vectors;
    HanjaTable* table;

    vectors = hanja_vectors_from_txt(filename);
    if (vectors == NULL)
	return NULL;

    table = hanja_vector_table_load(vectors);
    if (table == NULL) {
	hanja_vectors_delete(vectors);
	return NULL;
    }

    return table;
}

HanjaTable*
hanja_table_load_from_bin(const char *filename)
{
    struct stat buf;
    FILE* file;
    void* data;
    size_t length;
    HanjaTable *table = NULL;

    file = fopen(filename, "r");
    if (file == NULL)
	return NULL;

    fstat(fileno(file), &buf);

    length = buf.st_size;
    data = mmap(0, length, PROT_READ, MAP_SHARED, fileno(file), 0);
    fclose(file);

    table = (HanjaTable*)hanja_mmap_table_load(data, length);
    if (table == NULL) {
	munmap(data, length);
	return NULL;
    }

    return table;
}

HanjaTable*
hanja_table_load(const char* filename)
{
    size_t len;
    HanjaTable* table = NULL;

    if (filename == NULL)
	filename = LIBHANGUL_DEFAULT_HANJA_DIC;

    len = strlen(filename);
    if (len > 4 &&
	filename[len - 1] == 't' &&
	filename[len - 2] == 'x' &&
	filename[len - 3] == 't' &&
	filename[len - 4] == '.') {
	table = hanja_table_load_from_txt(filename);
    }

    if (table == NULL)
	table = hanja_table_load_from_bin(filename);

    if (table == NULL)
	table = hanja_table_load_from_txt(filename);

    return table;
}

void
hanja_table_delete(HanjaTable *table)
{
    if (table != NULL) {
	table->destroy(table);
    }
}

HanjaList*
hanja_table_match_exact(const HanjaTable* table, const char *key)
{
    HanjaList* ret = NULL;

    if (key == NULL || key[0] == '\0' || table == NULL)
	return NULL;

    table->match(table, key, &ret);

    return ret;
}

HanjaList*
hanja_table_match_prefix(const HanjaTable* table, const char *key)
{
    char* p;
    char* newkey;
    HanjaList* ret = NULL;

    if (key == NULL || key[0] == '\0' || table == NULL)
	return NULL;

    newkey = strdup(key);
    if (newkey == NULL)
	return NULL;

    p = strchr(newkey, '\0');
    while (newkey[0] != '\0') {
	table->match(table, newkey, &ret);
	p = utf8_prev(newkey, p);
	p[0] = '\0';
    }
    free(newkey);

    return ret;
}

HanjaList*
hanja_table_match_suffix(const HanjaTable* table, const char *key)
{
    const char* p;
    HanjaList* ret = NULL;

    if (key == NULL || key[0] == '\0' || table == NULL)
	return NULL;

    p = key;
    while (p[0] != '\0') {
	table->match(table, p, &ret);
	p = utf8_next(p);
    }

    return ret;
}

int
hanja_list_get_size(const HanjaList *list)
{
    if (list != NULL)
	return list->len;
    return 0;
}

const char*
hanja_list_get_key(const HanjaList *list)
{
    if (list != NULL)
	return list->key;
    return NULL;
}

const Hanja*
hanja_list_get_nth(const HanjaList *list, unsigned int n)
{
    if (list != NULL) {
	if (n < list->len)
	    return list->items[n];
    }
    return NULL;
}

const char*
hanja_list_get_nth_key(const HanjaList *list, unsigned int n)
{
    const Hanja* hanja = hanja_list_get_nth(list, n);
    return hanja_get_key(hanja);
}

const char*
hanja_list_get_nth_value(const HanjaList *list, unsigned int n)
{
    const Hanja* hanja = hanja_list_get_nth(list, n);
    return hanja_get_value(hanja);
}

const char*
hanja_list_get_nth_comment(const HanjaList *list, unsigned int n)
{
    const Hanja* hanja = hanja_list_get_nth(list, n);
    return hanja_get_comment(hanja);
}

void
hanja_list_delete(HanjaList *list)
{
    if (list) {
	free(list->items);
	free(list->key);
	free(list);
    }
}

static int
compare_pair(const void* a, const void* b)
{
    const ucschar*   c = a;
    const HanjaPair* y = b;

    return *c - y->first;
}

size_t
hanja_compatibility_form(ucschar* hanja, const ucschar* hangul, size_t n)
{
    size_t i;
    size_t nconverted;

    if (hangul == NULL || hanja == NULL)
	return 0;

    nconverted = 0;
    for (i = 0; i < n && hangul[i] != 0 && hanja[i] != 0; i++) {
	HanjaPairArray* p;

	p = bsearch(&hanja[i],
		    hanja_unified_to_compat_table,
		    N_ELEMENTS(hanja_unified_to_compat_table),
		    sizeof(hanja_unified_to_compat_table[0]),
		    compare_pair);
	if (p != NULL) {
	    const HanjaPair* pair = p->pairs;
	    while (pair->first != 0) {
		if (pair->first == hangul[i]) {
		    hanja[i] = pair->second;
		    nconverted++;
		    break;
		}
		pair++;
	    }
	}
    }

    return nconverted;
}

size_t
hanja_unified_form(ucschar* str, size_t n)
{
    size_t i;
    size_t nconverted;

    if (str == NULL)
	return 0;

    nconverted = 0;
    for (i = 0; i < n && str[i] != 0; i++) {
	if (str[i] >= 0xF900 && str[i] <= 0xFA0B) {
	    str[i] = hanja_compat_to_unified_table[str[i] - 0xF900];
	    nconverted++;
	}
    }

    return nconverted;
}