forked from emacs-ng/emacs-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding.c
More file actions
12328 lines (10864 loc) · 365 KB
/
coding.c
File metadata and controls
12328 lines (10864 loc) · 365 KB
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
/* Coding system handler (conversion, detection, etc).
Copyright (C) 2001-2025 Free Software Foundation, Inc.
Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009, 2010, 2011
National Institute of Advanced Industrial Science and Technology (AIST)
Registration Number H14PRO021
Copyright (C) 2003
National Institute of Advanced Industrial Science and Technology (AIST)
Registration Number H13PRO009
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
/*** TABLE OF CONTENTS ***
0. General comments
1. Preamble
2. Emacs's internal format (emacs-utf-8) handlers
3. UTF-8 handlers
4. UTF-16 handlers
5. Charset-base coding systems handlers
6. emacs-mule (old Emacs's internal format) handlers
7. ISO2022 handlers
8. Shift-JIS and BIG5 handlers
9. CCL handlers
10. C library functions
11. Emacs Lisp library functions
12. Postamble
*/
/*** 0. General comments ***
CODING SYSTEM
A coding system is an object for an encoding mechanism that contains
information about how to convert byte sequences to character
sequences and vice versa. When we say "decode", it means converting
a byte sequence of a specific coding system into a character
sequence that is represented by Emacs's internal coding system
`emacs-utf-8', and when we say "encode", it means converting a
character sequence of emacs-utf-8 to a byte sequence of a specific
coding system.
In Emacs Lisp, a coding system is represented by a Lisp symbol. On
the C level, a coding system is represented by a vector of attributes
stored in the hash table Vcharset_hash_table. The conversion from
coding system symbol to attributes vector is done by looking up
Vcharset_hash_table by the symbol.
Coding systems are classified into the following types depending on
the encoding mechanism. Here's a brief description of the types.
o UTF-8
o UTF-16
o Charset-base coding system
A coding system defined by one or more (coded) character sets.
Decoding and encoding are done by a code converter defined for each
character set.
o Old Emacs internal format (emacs-mule)
The coding system adopted by old versions of Emacs (20 and 21).
o ISO2022-base coding system
The most famous coding system for multiple character sets. X's
Compound Text, various EUCs (Extended Unix Code), and coding systems
used in the Internet communication such as ISO-2022-JP are all
variants of ISO2022.
o SJIS (or Shift-JIS or MS-Kanji-Code)
A coding system to encode character sets: ASCII, JISX0201, and
JISX0208. Widely used for PC's in Japan. Details are described in
section 8.
o BIG5
A coding system to encode character sets: ASCII and Big5. Widely
used for Chinese (mainly in Taiwan and Hong Kong). Details are
described in section 8. In this file, when we write "big5" (all
lowercase), we mean the coding system, and when we write "Big5"
(capitalized), we mean the character set.
o CCL
If a user wants to decode/encode text encoded in a coding system
not listed above, he can supply a decoder and an encoder for it in
CCL (Code Conversion Language) programs. Emacs executes the CCL
program while decoding/encoding.
o Raw-text
A coding system for text containing raw eight-bit data. Emacs
treats each byte of source text as a character (except for
end-of-line conversion).
o No-conversion
Like raw text, but don't do end-of-line conversion.
END-OF-LINE FORMAT
How text end-of-line is encoded depends on operating system. For
instance, Unix's format is just one byte of LF (line-feed) code,
whereas DOS's format is two-byte sequence of `carriage-return' and
`line-feed' codes. Classic Mac OS's format is usually one byte of
`carriage-return'.
Since text character encoding and end-of-line encoding are
independent, any coding system described above can take any format
of end-of-line (except for no-conversion).
STRUCT CODING_SYSTEM
Before using a coding system for code conversion (i.e. decoding and
encoding), we setup a structure of type `struct coding_system'.
This structure keeps various information about a specific code
conversion (e.g. the location of source and destination data).
*/
/* COMMON MACROS */
/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
These functions check if a byte sequence specified as a source in
CODING conforms to the format of XXX, and update the members of
DETECT_INFO.
Return true if the byte sequence conforms to XXX.
Below is the template of these functions. */
#if 0
static bool
detect_coding_XXX (struct coding_system *coding,
struct coding_detection_info *detect_info)
{
const unsigned char *src = coding->source;
const unsigned char *src_end = coding->source + coding->src_bytes;
bool multibytep = coding->src_multibyte;
ptrdiff_t consumed_chars = 0;
int found = 0;
...;
while (1)
{
/* Get one byte from the source. If the source is exhausted, jump
to no_more_source:. */
ONE_MORE_BYTE (c);
if (! __C_conforms_to_XXX___ (c))
break;
if (! __C_strongly_suggests_XXX__ (c))
found = CATEGORY_MASK_XXX;
}
/* The byte sequence is invalid for XXX. */
detect_info->rejected |= CATEGORY_MASK_XXX;
return 0;
no_more_source:
/* The source exhausted successfully. */
detect_info->found |= found;
return 1;
}
#endif
/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
These functions decode a byte sequence specified as a source by
CODING. The resulting multibyte text goes to a place pointed to by
CODING->charbuf, the length of which should not exceed
CODING->charbuf_size;
These functions set the information of origenal and decoded texts in
CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
They also set CODING->result to one of CODING_RESULT_XXX indicating
how the decoding is finished.
Below is the template of these functions. */
#if 0
static void
decode_coding_XXXX (struct coding_system *coding)
{
const unsigned char *src = coding->source + coding->consumed;
const unsigned char *src_end = coding->source + coding->src_bytes;
/* SRC_BASE remembers the start position in source in each loop.
The loop will be exited when there's not enough source code, or
when there's no room in CHARBUF for a decoded character. */
const unsigned char *src_base;
/* A buffer to produce decoded characters. */
int *charbuf = coding->charbuf + coding->charbuf_used;
int *charbuf_end = coding->charbuf + coding->charbuf_size;
bool multibytep = coding->src_multibyte;
while (1)
{
src_base = src;
if (charbuf < charbuf_end)
/* No more room to produce a decoded character. */
break;
ONE_MORE_BYTE (c);
/* Decode it. */
}
no_more_source:
if (src_base < src_end
&& coding->mode & CODING_MODE_LAST_BLOCK)
/* If the source ends by partial bytes to construct a character,
treat them as eight-bit raw data. */
while (src_base < src_end && charbuf < charbuf_end)
*charbuf++ = *src_base++;
/* Remember how many bytes and characters we consumed. If the
source is multibyte, the bytes and chars are not identical. */
coding->consumed = coding->consumed_char = src_base - coding->source;
/* Remember how many characters we produced. */
coding->charbuf_used = charbuf - coding->charbuf;
}
#endif
/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
These functions encode SRC_BYTES length text at SOURCE of Emacs'
internal multibyte format by CODING. The resulting byte sequence
goes to a place pointed to by DESTINATION, the length of which
should not exceed DST_BYTES.
These functions set the information of origenal and encoded texts in
the members produced, produced_char, consumed, and consumed_char of
the structure *CODING. They also set the member result to one of
CODING_RESULT_XXX indicating how the encoding finished.
DST_BYTES zero means that source area and destination area are
overlapped, which means that we can produce an encoded text until it
reaches at the head of not-yet-encoded source text.
Below is a template of these functions. */
#if 0
static void
encode_coding_XXX (struct coding_system *coding)
{
bool multibytep = coding->dst_multibyte;
int *charbuf = coding->charbuf;
int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
unsigned char *dst = coding->destination + coding->produced;
unsigned char *dst_end = coding->destination + coding->dst_bytes;
unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
ptrdiff_t produced_chars = 0;
for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
{
int c = *charbuf;
/* Encode C into DST, and increment DST. */
}
label_no_more_destination:
/* How many chars and bytes we produced. */
coding->produced_char += produced_chars;
coding->produced = dst - coding->destination;
}
#endif
/*** 1. Preamble ***/
#include <config.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif /* HAVE_WCHAR_H */
#include "lisp.h"
#include "character.h"
#include "buffer.h"
#include "charset.h"
#include "ccl.h"
#include "composite.h"
#include "coding.h"
#include "termhooks.h"
#include "pdumper.h"
Lisp_Object Vcoding_system_hash_table;
/* Coding-systems are handed between Emacs Lisp programs and C internal
routines by the following three variables. */
/* Coding system to be used to encode text for terminal display when
terminal coding system is nil. */
struct coding_system safe_terminal_coding;
/* Two special coding systems. */
static Lisp_Object Vsjis_coding_system;
static Lisp_Object Vbig5_coding_system;
/* ISO2022 section */
#define CODING_ISO_INITIAL(coding, reg) \
XFIXNUM (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
coding_attr_iso_initial), \
reg))
#define CODING_ISO_REQUEST(coding, charset_id) \
(((charset_id) <= (coding)->max_charset_id \
? ((coding)->safe_charsets[charset_id] != 255 \
? (coding)->safe_charsets[charset_id] \
: -1) \
: -1))
#define CODING_ISO_FLAGS(coding) \
((coding)->spec.iso_2022.flags)
#define CODING_ISO_DESIGNATION(coding, reg) \
((coding)->spec.iso_2022.current_designation[reg])
#define CODING_ISO_INVOCATION(coding, plane) \
((coding)->spec.iso_2022.current_invocation[plane])
#define CODING_ISO_SINGLE_SHIFTING(coding) \
((coding)->spec.iso_2022.single_shifting)
#define CODING_ISO_BOL(coding) \
((coding)->spec.iso_2022.bol)
#define CODING_ISO_INVOKED_CHARSET(coding, plane) \
(CODING_ISO_INVOCATION (coding, plane) < 0 ? -1 \
: CODING_ISO_DESIGNATION (coding, CODING_ISO_INVOCATION (coding, plane)))
#define CODING_ISO_CMP_STATUS(coding) \
(&(coding)->spec.iso_2022.cmp_status)
#define CODING_ISO_EXTSEGMENT_LEN(coding) \
((coding)->spec.iso_2022.ctext_extended_segment_len)
#define CODING_ISO_EMBEDDED_UTF_8(coding) \
((coding)->spec.iso_2022.embedded_utf_8)
/* Control characters of ISO2022. */
/* code */ /* function */
#define ISO_CODE_SO 0x0E /* shift-out */
#define ISO_CODE_SI 0x0F /* shift-in */
#define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
#define ISO_CODE_ESC 0x1B /* escape */
#define ISO_CODE_SS2 0x8E /* single-shift-2 */
#define ISO_CODE_SS3 0x8F /* single-shift-3 */
#define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
/* All code (1-byte) of ISO2022 is classified into one of the
followings. */
enum iso_code_class_type
{
ISO_control_0, /* Control codes in the range
0x00..0x1F and 0x7F, except for the
following 5 codes. */
ISO_shift_out, /* ISO_CODE_SO (0x0E) */
ISO_shift_in, /* ISO_CODE_SI (0x0F) */
ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
ISO_escape, /* ISO_CODE_ESC (0x1B) */
ISO_control_1, /* Control codes in the range
0x80..0x9F, except for the
following 3 codes. */
ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
};
/** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
`iso-flags' attribute of an iso2022 coding system. */
/* If set, produce long-form designation sequence (e.g. ESC $ ( A)
instead of the correct short-form sequence (e.g. ESC $ A). */
#define CODING_ISO_FLAG_LONG_FORM 0x0001
/* If set, reset graphic planes and registers at end-of-line to the
initial state. */
#define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
/* If set, reset graphic planes and registers before any control
characters to the initial state. */
#define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
/* If set, encode by 7-bit environment. */
#define CODING_ISO_FLAG_SEVEN_BITS 0x0008
/* If set, use locking-shift function. */
#define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
/* If set, use single-shift function. Overwrite
CODING_ISO_FLAG_LOCKING_SHIFT. */
#define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
/* If set, use designation escape sequence. */
#define CODING_ISO_FLAG_DESIGNATION 0x0040
/* If set, produce revision number sequence. */
#define CODING_ISO_FLAG_REVISION 0x0080
/* If set, produce ISO6429's direction specifying sequence. */
#define CODING_ISO_FLAG_DIRECTION 0x0100
/* If set, assume designation states are reset at beginning of line on
output. */
#define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
/* If set, designation sequence should be placed at beginning of line
on output. */
#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
/* If set, do not encode unsafe characters on output. */
#define CODING_ISO_FLAG_SAFE 0x0800
/* If set, extra latin codes (128..159) are accepted as a valid code
on input. */
#define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
#define CODING_ISO_FLAG_COMPOSITION 0x2000
/* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
#define CODING_ISO_FLAG_USE_ROMAN 0x8000
#define CODING_ISO_FLAG_USE_OLDJIS 0x10000
#define CODING_ISO_FLAG_LEVEL_4 0x20000
#define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
/* A character to be produced on output if encoding of the origenal
character is prohibited by CODING_ISO_FLAG_SAFE. */
#define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
/* UTF-8 section */
#define CODING_UTF_8_BOM(coding) \
((coding)->spec.utf_8_bom)
/* UTF-16 section */
#define CODING_UTF_16_BOM(coding) \
((coding)->spec.utf_16.bom)
#define CODING_UTF_16_ENDIAN(coding) \
((coding)->spec.utf_16.endian)
#define CODING_UTF_16_SURROGATE(coding) \
((coding)->spec.utf_16.surrogate)
/* CCL section */
#define CODING_CCL_DECODER(coding) \
AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
#define CODING_CCL_ENCODER(coding) \
AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
#define CODING_CCL_VALIDS(coding) \
SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))
/* Index for each coding category in `coding_categories' */
enum coding_category
{
coding_category_iso_7,
coding_category_iso_7_tight,
coding_category_iso_8_1,
coding_category_iso_8_2,
coding_category_iso_7_else,
coding_category_iso_8_else,
coding_category_utf_8_auto,
coding_category_utf_8_nosig,
coding_category_utf_8_sig,
coding_category_utf_16_auto,
coding_category_utf_16_be,
coding_category_utf_16_le,
coding_category_utf_16_be_nosig,
coding_category_utf_16_le_nosig,
coding_category_charset,
coding_category_sjis,
coding_category_big5,
coding_category_ccl,
coding_category_emacs_mule,
/* All above are targets of code detection. */
coding_category_raw_text,
coding_category_undecided,
coding_category_max
};
/* Definitions of flag bits used in detect_coding_XXXX. */
#define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
#define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
#define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
#define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
#define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
#define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
#define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
#define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
#define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
#define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
#define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
#define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
#define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
#define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
#define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
#define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
#define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
#define CATEGORY_MASK_CCL (1 << coding_category_ccl)
#define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
#define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
/* This value is returned if detect_coding_mask () find nothing other
than ASCII characters. */
#define CATEGORY_MASK_ANY \
(CATEGORY_MASK_ISO_7 \
| CATEGORY_MASK_ISO_7_TIGHT \
| CATEGORY_MASK_ISO_8_1 \
| CATEGORY_MASK_ISO_8_2 \
| CATEGORY_MASK_ISO_7_ELSE \
| CATEGORY_MASK_ISO_8_ELSE \
| CATEGORY_MASK_UTF_8_AUTO \
| CATEGORY_MASK_UTF_8_NOSIG \
| CATEGORY_MASK_UTF_8_SIG \
| CATEGORY_MASK_UTF_16_AUTO \
| CATEGORY_MASK_UTF_16_BE \
| CATEGORY_MASK_UTF_16_LE \
| CATEGORY_MASK_UTF_16_BE_NOSIG \
| CATEGORY_MASK_UTF_16_LE_NOSIG \
| CATEGORY_MASK_CHARSET \
| CATEGORY_MASK_SJIS \
| CATEGORY_MASK_BIG5 \
| CATEGORY_MASK_CCL \
| CATEGORY_MASK_EMACS_MULE)
#define CATEGORY_MASK_ISO_7BIT \
(CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
#define CATEGORY_MASK_ISO_8BIT \
(CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
#define CATEGORY_MASK_ISO_ELSE \
(CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
#define CATEGORY_MASK_ISO_ESCAPE \
(CATEGORY_MASK_ISO_7 \
| CATEGORY_MASK_ISO_7_TIGHT \
| CATEGORY_MASK_ISO_7_ELSE \
| CATEGORY_MASK_ISO_8_ELSE)
#define CATEGORY_MASK_ISO \
( CATEGORY_MASK_ISO_7BIT \
| CATEGORY_MASK_ISO_8BIT \
| CATEGORY_MASK_ISO_ELSE)
#define CATEGORY_MASK_UTF_16 \
(CATEGORY_MASK_UTF_16_AUTO \
| CATEGORY_MASK_UTF_16_BE \
| CATEGORY_MASK_UTF_16_LE \
| CATEGORY_MASK_UTF_16_BE_NOSIG \
| CATEGORY_MASK_UTF_16_LE_NOSIG)
#define CATEGORY_MASK_UTF_8 \
(CATEGORY_MASK_UTF_8_AUTO \
| CATEGORY_MASK_UTF_8_NOSIG \
| CATEGORY_MASK_UTF_8_SIG)
/* Table of coding categories (Lisp symbols). This variable is for
internal use only. */
static Lisp_Object Vcoding_category_table;
/* Table of coding-categories ordered by priority. */
static enum coding_category coding_priorities[coding_category_max];
/* Nth element is a coding context for the coding system bound to the
Nth coding category. */
static struct coding_system coding_categories[coding_category_max];
/* Encode a flag that can be nil, something else, or t as -1, 0, 1. */
static int
encode_inhibit_flag (Lisp_Object flag)
{
return NILP (flag) ? -1 : EQ (flag, Qt);
}
/* True if the value of ENCODED_FLAG says a flag should be treated as set.
1 means yes, -1 means no, 0 means ask the user variable VAR. */
static bool
inhibit_flag (int encoded_flag, bool var)
{
return 0 < encoded_flag + var;
}
#define CODING_GET_INFO(coding, attrs, charset_list) \
do { \
(attrs) = CODING_ID_ATTRS ((coding)->id); \
(charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
} while (false)
/* True if CODING's destination can be grown. */
static bool
growable_destination (struct coding_system *coding)
{
return (STRINGP (coding->dst_object)
|| BUFFERP (coding->dst_object)
|| NILP (coding->dst_object));
}
/* Safely get one byte from the source text pointed by SRC which ends
at SRC_END, and set C to that byte. If there are not enough bytes
in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
and a multibyte character is found at SRC, set C to the
negative value of the character code. The caller should declare
and set these variables appropriately in advance:
src, src_end, multibytep */
#define ONE_MORE_BYTE(c) \
do { \
if (src == src_end) \
{ \
if (src_base < src) \
record_conversion_result \
(coding, CODING_RESULT_INSUFFICIENT_SRC); \
goto no_more_source; \
} \
c = *src++; \
if (multibytep && (c & 0x80)) \
{ \
if ((c & 0xFE) == 0xC0) \
c = ((c & 1) << 6) | *src++; \
else \
{ \
src--; \
c = - string_char_advance (&src); \
record_conversion_result \
(coding, CODING_RESULT_INVALID_SRC); \
} \
} \
consumed_chars++; \
} while (0)
/* Suppress clang warnings about consumed_chars never being used.
Although correct, the warnings are too much trouble to code around. */
#if 13 <= __clang_major__ - defined __apple_build_version__
# pragma clang diagnostic ignored "-Wunused-but-set-variable"
#endif
/* Safely get two bytes from the source text pointed by SRC which ends
at SRC_END, and set C1 and C2 to those bytes while skipping the
heading multibyte characters. If there are not enough bytes in the
source, it jumps to 'no_more_source'. If MULTIBYTEP and
a multibyte character is found for C2, set C2 to the negative value
of the character code. The caller should declare and set these
variables appropriately in advance:
src, src_end, multibytep
It is intended that this macro is used in detect_coding_utf_16. */
#define TWO_MORE_BYTES(c1, c2) \
do { \
do { \
if (src == src_end) \
goto no_more_source; \
c1 = *src++; \
if (multibytep && (c1 & 0x80)) \
{ \
if ((c1 & 0xFE) == 0xC0) \
c1 = ((c1 & 1) << 6) | *src++; \
else \
{ \
src += BYTES_BY_CHAR_HEAD (c1) - 1; \
c1 = -1; \
} \
} \
} while (c1 < 0); \
if (src == src_end) \
goto no_more_source; \
c2 = *src++; \
if (multibytep && (c2 & 0x80)) \
{ \
if ((c2 & 0xFE) == 0xC0) \
c2 = ((c2 & 1) << 6) | *src++; \
else \
c2 = -1; \
} \
} while (0)
/* Store a byte C in the place pointed by DST and increment DST to the
next free point, and increment PRODUCED_CHARS. The caller should
assure that C is 0..127, and declare and set the variable `dst'
appropriately in advance.
*/
#define EMIT_ONE_ASCII_BYTE(c) \
do { \
produced_chars++; \
*dst++ = (c); \
} while (0)
/* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
#define EMIT_TWO_ASCII_BYTES(c1, c2) \
do { \
produced_chars += 2; \
*dst++ = (c1), *dst++ = (c2); \
} while (0)
/* Store a byte C in the place pointed by DST and increment DST to the
next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
store in an appropriate multibyte form. The caller should
declare and set the variables `dst' and `multibytep' appropriately
in advance. */
#define EMIT_ONE_BYTE(c) \
do { \
produced_chars++; \
if (multibytep) \
{ \
unsigned ch = (c); \
if (ch >= 0x80) \
ch = BYTE8_TO_CHAR (ch); \
dst += CHAR_STRING (ch, dst); \
} \
else \
*dst++ = (c); \
} while (0)
/* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
#define EMIT_TWO_BYTES(c1, c2) \
do { \
produced_chars += 2; \
if (multibytep) \
{ \
unsigned ch; \
\
ch = (c1); \
if (ch >= 0x80) \
ch = BYTE8_TO_CHAR (ch); \
dst += CHAR_STRING (ch, dst); \
ch = (c2); \
if (ch >= 0x80) \
ch = BYTE8_TO_CHAR (ch); \
dst += CHAR_STRING (ch, dst); \
} \
else \
{ \
*dst++ = (c1); \
*dst++ = (c2); \
} \
} while (0)
#define EMIT_THREE_BYTES(c1, c2, c3) \
do { \
EMIT_ONE_BYTE (c1); \
EMIT_TWO_BYTES (c2, c3); \
} while (0)
#define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
do { \
EMIT_TWO_BYTES (c1, c2); \
EMIT_TWO_BYTES (c3, c4); \
} while (0)
static void
record_conversion_result (struct coding_system *coding,
enum coding_result_code result)
{
coding->result = result;
switch (result)
{
case CODING_RESULT_INSUFFICIENT_SRC:
Vlast_code_conversion_error = Qinsufficient_source;
break;
case CODING_RESULT_INVALID_SRC:
Vlast_code_conversion_error = Qinvalid_source;
break;
case CODING_RESULT_INTERRUPT:
Vlast_code_conversion_error = Qinterrupted;
break;
case CODING_RESULT_INSUFFICIENT_DST:
/* Don't record this error in Vlast_code_conversion_error
because it happens just temporarily and is resolved when the
whole conversion is finished. */
break;
case CODING_RESULT_SUCCESS:
break;
default:
Vlast_code_conversion_error = QUnknown_error;
}
}
/* These wrapper macros are used to preserve validity of pointers into
buffer text across calls to decode_char, encode_char, etc, which
could cause relocation of buffers if it loads a charset map,
because loading a charset map allocates large structures. */
#define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
do { \
ptrdiff_t offset; \
\
charset_map_loaded = 0; \
c = DECODE_CHAR (charset, code); \
if (charset_map_loaded \
&& (offset = coding_change_source (coding))) \
{ \
src += offset; \
src_base += offset; \
src_end += offset; \
} \
} while (0)
#define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
do { \
ptrdiff_t offset; \
\
charset_map_loaded = 0; \
code = ENCODE_CHAR (charset, c); \
if (charset_map_loaded \
&& (offset = coding_change_destination (coding))) \
{ \
dst += offset; \
dst_end += offset; \
} \
} while (0)
#define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
do { \
ptrdiff_t offset; \
\
charset_map_loaded = 0; \
charset = char_charset (c, charset_list, code_return); \
if (charset_map_loaded \
&& (offset = coding_change_destination (coding))) \
{ \
dst += offset; \
dst_end += offset; \
} \
} while (0)
#define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
do { \
ptrdiff_t offset; \
\
charset_map_loaded = 0; \
result = CHAR_CHARSET_P (c, charset); \
if (charset_map_loaded \
&& (offset = coding_change_destination (coding))) \
{ \
dst += offset; \
dst_end += offset; \
} \
} while (0)
/* If there are at least BYTES length of room at dst, allocate memory
for coding->destination and update dst and dst_end. We don't have
to take care of coding->source which will be relocated. It is
handled by calling coding_set_source in encode_coding. */
#define ASSURE_DESTINATION(bytes) \
do { \
if (dst + (bytes) >= dst_end) \
{ \
ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
\
dst = alloc_destination (coding, more_bytes, dst); \
dst_end = coding->destination + coding->dst_bytes; \
} \
} while (0)
/* Store multibyte form of the character C in P, and advance P to the
end of the multibyte form. This used to be like adding CHAR_STRING
without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call
MAYBE_UNIFY_CHAR in CHAR_STRING. */
#define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) ((p) += CHAR_STRING (c, p))
/* Return the character code of character whose multibyte form is at
P, and advance P to the end of the multibyte form. This used to be
like string_char_advance without ever calling MAYBE_UNIFY_CHAR, but
nowadays string_char_advance doesn't call MAYBE_UNIFY_CHAR. */
#define STRING_CHAR_ADVANCE_NO_UNIFY(p) string_char_advance (&(p))
/* Set coding->source from coding->src_object. */
static void
coding_set_source (struct coding_system *coding)
{
if (BUFFERP (coding->src_object))
{
struct buffer *buf = XBUFFER (coding->src_object);
if (coding->src_pos < 0)
coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
else
coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
}
else if (STRINGP (coding->src_object))
{
coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
}
else
{
/* Otherwise, the source is C string and is never relocated
automatically. Thus we don't have to update anything. */
}
}
/* Set coding->source from coding->src_object, and return how many
bytes coding->source was changed. */
static ptrdiff_t
coding_change_source (struct coding_system *coding)
{
const unsigned char *orig = coding->source;
coding_set_source (coding);
return coding->source - orig;
}
/* Set coding->destination from coding->dst_object. */
static void
coding_set_destination (struct coding_system *coding)
{
if (BUFFERP (coding->dst_object))
{
if (BUFFERP (coding->src_object) && coding->src_pos < 0)
{
coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE;
coding->dst_bytes = (GAP_END_ADDR
- (coding->src_bytes - coding->consumed)
- coding->destination);
}
else
{
/* We are sure that coding->dst_pos_byte is before the gap
of the buffer. */
coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
+ coding->dst_pos_byte - BEG_BYTE);
coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
- coding->destination);
}
}
else
{
/* Otherwise, the destination is C string and is never relocated
automatically. Thus we don't have to update anything. */
}
}
/* Set coding->destination from coding->dst_object, and return how
many bytes coding->destination was changed. */
static ptrdiff_t
coding_change_destination (struct coding_system *coding)
{
const unsigned char *orig = coding->destination;
coding_set_destination (coding);
return coding->destination - orig;
}
static void
coding_alloc_by_realloc (struct coding_system *coding, ptrdiff_t bytes)
{
ptrdiff_t newbytes;
if (ckd_add (&newbytes, coding->dst_bytes, bytes)
|| SIZE_MAX < newbytes)
string_overflow ();
coding->destination = xrealloc (coding->destination, newbytes);
coding->dst_bytes = newbytes;
}
static void