diff options
Diffstat (limited to 'src/coding.c')
| -rw-r--r-- | src/coding.c | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/src/coding.c b/src/coding.c index 8b620af8695..e4b52f6db48 100644 --- a/src/coding.c +++ b/src/coding.c | |||
| @@ -642,15 +642,6 @@ static enum coding_category coding_priorities[coding_category_max]; | |||
| 642 | Nth coding category. */ | 642 | Nth coding category. */ |
| 643 | static struct coding_system coding_categories[coding_category_max]; | 643 | static struct coding_system coding_categories[coding_category_max]; |
| 644 | 644 | ||
| 645 | /*** Commonly used macros and functions ***/ | ||
| 646 | |||
| 647 | #ifndef min | ||
| 648 | #define min(a, b) ((a) < (b) ? (a) : (b)) | ||
| 649 | #endif | ||
| 650 | #ifndef max | ||
| 651 | #define max(a, b) ((a) > (b) ? (a) : (b)) | ||
| 652 | #endif | ||
| 653 | |||
| 654 | /* Encode a flag that can be nil, something else, or t as -1, 0, 1. */ | 645 | /* Encode a flag that can be nil, something else, or t as -1, 0, 1. */ |
| 655 | 646 | ||
| 656 | static int | 647 | static int |
| @@ -690,6 +681,14 @@ CHECK_NATNUM_CDR (Lisp_Object x) | |||
| 690 | XSETCDR (x, tmp); | 681 | XSETCDR (x, tmp); |
| 691 | } | 682 | } |
| 692 | 683 | ||
| 684 | /* True if CODING's destination can be grown. */ | ||
| 685 | |||
| 686 | static bool | ||
| 687 | growable_destination (struct coding_system *coding) | ||
| 688 | { | ||
| 689 | return STRINGP (coding->dst_object) || BUFFERP (coding->dst_object); | ||
| 690 | } | ||
| 691 | |||
| 693 | 692 | ||
| 694 | /* Safely get one byte from the source text pointed by SRC which ends | 693 | /* Safely get one byte from the source text pointed by SRC which ends |
| 695 | at SRC_END, and set C to that byte. If there are not enough bytes | 694 | at SRC_END, and set C to that byte. If there are not enough bytes |
| @@ -3074,8 +3073,13 @@ detect_coding_iso_2022 (struct coding_system *coding, | |||
| 3074 | ONE_MORE_BYTE (c1); | 3073 | ONE_MORE_BYTE (c1); |
| 3075 | if (c1 < ' ' || c1 >= 0x80 | 3074 | if (c1 < ' ' || c1 >= 0x80 |
| 3076 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) | 3075 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) |
| 3077 | /* Invalid designation sequence. Just ignore. */ | 3076 | { |
| 3078 | break; | 3077 | /* Invalid designation sequence. Just ignore. */ |
| 3078 | if (c1 >= 0x80) | ||
| 3079 | rejected |= (CATEGORY_MASK_ISO_7BIT | ||
| 3080 | | CATEGORY_MASK_ISO_7_ELSE); | ||
| 3081 | break; | ||
| 3082 | } | ||
| 3079 | } | 3083 | } |
| 3080 | else if (c == '$') | 3084 | else if (c == '$') |
| 3081 | { | 3085 | { |
| @@ -3089,16 +3093,29 @@ detect_coding_iso_2022 (struct coding_system *coding, | |||
| 3089 | ONE_MORE_BYTE (c1); | 3093 | ONE_MORE_BYTE (c1); |
| 3090 | if (c1 < ' ' || c1 >= 0x80 | 3094 | if (c1 < ' ' || c1 >= 0x80 |
| 3091 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) | 3095 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) |
| 3092 | /* Invalid designation sequence. Just ignore. */ | 3096 | { |
| 3093 | break; | 3097 | /* Invalid designation sequence. Just ignore. */ |
| 3098 | if (c1 >= 0x80) | ||
| 3099 | rejected |= (CATEGORY_MASK_ISO_7BIT | ||
| 3100 | | CATEGORY_MASK_ISO_7_ELSE); | ||
| 3101 | break; | ||
| 3102 | } | ||
| 3094 | } | 3103 | } |
| 3095 | else | 3104 | else |
| 3096 | /* Invalid designation sequence. Just ignore it. */ | 3105 | { |
| 3097 | break; | 3106 | /* Invalid designation sequence. Just ignore it. */ |
| 3107 | if (c >= 0x80) | ||
| 3108 | rejected |= (CATEGORY_MASK_ISO_7BIT | ||
| 3109 | | CATEGORY_MASK_ISO_7_ELSE); | ||
| 3110 | break; | ||
| 3111 | } | ||
| 3098 | } | 3112 | } |
| 3099 | else | 3113 | else |
| 3100 | { | 3114 | { |
| 3101 | /* Invalid escape sequence. Just ignore it. */ | 3115 | /* Invalid escape sequence. Just ignore it. */ |
| 3116 | if (c >= 0x80) | ||
| 3117 | rejected |= (CATEGORY_MASK_ISO_7BIT | ||
| 3118 | | CATEGORY_MASK_ISO_7_ELSE); | ||
| 3102 | break; | 3119 | break; |
| 3103 | } | 3120 | } |
| 3104 | 3121 | ||
| @@ -3149,7 +3166,7 @@ detect_coding_iso_2022 (struct coding_system *coding, | |||
| 3149 | if (inhibit_iso_escape_detection) | 3166 | if (inhibit_iso_escape_detection) |
| 3150 | break; | 3167 | break; |
| 3151 | single_shifting = 0; | 3168 | single_shifting = 0; |
| 3152 | rejected |= CATEGORY_MASK_ISO_7BIT; | 3169 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
| 3153 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | 3170 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) |
| 3154 | & CODING_ISO_FLAG_SINGLE_SHIFT) | 3171 | & CODING_ISO_FLAG_SINGLE_SHIFT) |
| 3155 | { | 3172 | { |
| @@ -3176,9 +3193,9 @@ detect_coding_iso_2022 (struct coding_system *coding, | |||
| 3176 | single_shifting = 0; | 3193 | single_shifting = 0; |
| 3177 | break; | 3194 | break; |
| 3178 | } | 3195 | } |
| 3196 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; | ||
| 3179 | if (c >= 0xA0) | 3197 | if (c >= 0xA0) |
| 3180 | { | 3198 | { |
| 3181 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; | ||
| 3182 | found |= CATEGORY_MASK_ISO_8_1; | 3199 | found |= CATEGORY_MASK_ISO_8_1; |
| 3183 | /* Check the length of succeeding codes of the range | 3200 | /* Check the length of succeeding codes of the range |
| 3184 | 0xA0..0FF. If the byte length is even, we include | 3201 | 0xA0..0FF. If the byte length is even, we include |
| @@ -6867,6 +6884,11 @@ decode_eol (struct coding_system *coding) | |||
| 6867 | } | 6884 | } |
| 6868 | 6885 | ||
| 6869 | 6886 | ||
| 6887 | /* MAX_LOOKUP's maximum value. MAX_LOOKUP is an int and so cannot | ||
| 6888 | exceed INT_MAX. Also, MAX_LOOKUP is multiplied by sizeof (int) for | ||
| 6889 | alloca, so it cannot exceed MAX_ALLOCA / sizeof (int). */ | ||
| 6890 | enum { MAX_LOOKUP_MAX = min (INT_MAX, MAX_ALLOCA / sizeof (int)) }; | ||
| 6891 | |||
| 6870 | /* Return a translation table (or list of them) from coding system | 6892 | /* Return a translation table (or list of them) from coding system |
| 6871 | attribute vector ATTRS for encoding (if ENCODEP) or decoding (if | 6893 | attribute vector ATTRS for encoding (if ENCODEP) or decoding (if |
| 6872 | not ENCODEP). */ | 6894 | not ENCODEP). */ |
| @@ -6919,7 +6941,7 @@ get_translation_table (Lisp_Object attrs, bool encodep, int *max_lookup) | |||
| 6919 | { | 6941 | { |
| 6920 | val = XCHAR_TABLE (translation_table)->extras[1]; | 6942 | val = XCHAR_TABLE (translation_table)->extras[1]; |
| 6921 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | 6943 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) |
| 6922 | *max_lookup = XFASTINT (val); | 6944 | *max_lookup = min (XFASTINT (val), MAX_LOOKUP_MAX); |
| 6923 | } | 6945 | } |
| 6924 | else if (CONSP (translation_table)) | 6946 | else if (CONSP (translation_table)) |
| 6925 | { | 6947 | { |
| @@ -6931,7 +6953,7 @@ get_translation_table (Lisp_Object attrs, bool encodep, int *max_lookup) | |||
| 6931 | { | 6953 | { |
| 6932 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; | 6954 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; |
| 6933 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) | 6955 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) |
| 6934 | *max_lookup = XFASTINT (tailval); | 6956 | *max_lookup = min (XFASTINT (tailval), MAX_LOOKUP_MAX); |
| 6935 | } | 6957 | } |
| 6936 | } | 6958 | } |
| 6937 | } | 6959 | } |
| @@ -7014,8 +7036,10 @@ produce_chars (struct coding_system *coding, Lisp_Object translation_table, | |||
| 7014 | int *buf = coding->charbuf; | 7036 | int *buf = coding->charbuf; |
| 7015 | int *buf_end = buf + coding->charbuf_used; | 7037 | int *buf_end = buf + coding->charbuf_used; |
| 7016 | 7038 | ||
| 7017 | if (EQ (coding->src_object, coding->dst_object)) | 7039 | if (EQ (coding->src_object, coding->dst_object) |
| 7040 | && ! NILP (coding->dst_object)) | ||
| 7018 | { | 7041 | { |
| 7042 | eassert (growable_destination (coding)); | ||
| 7019 | coding_set_source (coding); | 7043 | coding_set_source (coding); |
| 7020 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | 7044 | dst_end = ((unsigned char *) coding->source) + coding->consumed; |
| 7021 | } | 7045 | } |
| @@ -7054,6 +7078,7 @@ produce_chars (struct coding_system *coding, Lisp_Object translation_table, | |||
| 7054 | 7078 | ||
| 7055 | if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars) | 7079 | if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars) |
| 7056 | { | 7080 | { |
| 7081 | eassert (growable_destination (coding)); | ||
| 7057 | if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf)) | 7082 | if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf)) |
| 7058 | / MAX_MULTIBYTE_LENGTH) | 7083 | / MAX_MULTIBYTE_LENGTH) |
| 7059 | < to_nchars) | 7084 | < to_nchars) |
| @@ -7098,7 +7123,10 @@ produce_chars (struct coding_system *coding, Lisp_Object translation_table, | |||
| 7098 | const unsigned char *src_end = src + coding->consumed; | 7123 | const unsigned char *src_end = src + coding->consumed; |
| 7099 | 7124 | ||
| 7100 | if (EQ (coding->dst_object, coding->src_object)) | 7125 | if (EQ (coding->dst_object, coding->src_object)) |
| 7101 | dst_end = (unsigned char *) src; | 7126 | { |
| 7127 | eassert (growable_destination (coding)); | ||
| 7128 | dst_end = (unsigned char *) src; | ||
| 7129 | } | ||
| 7102 | if (coding->src_multibyte != coding->dst_multibyte) | 7130 | if (coding->src_multibyte != coding->dst_multibyte) |
| 7103 | { | 7131 | { |
| 7104 | if (coding->src_multibyte) | 7132 | if (coding->src_multibyte) |
| @@ -7114,6 +7142,7 @@ produce_chars (struct coding_system *coding, Lisp_Object translation_table, | |||
| 7114 | ONE_MORE_BYTE (c); | 7142 | ONE_MORE_BYTE (c); |
| 7115 | if (dst == dst_end) | 7143 | if (dst == dst_end) |
| 7116 | { | 7144 | { |
| 7145 | eassert (growable_destination (coding)); | ||
| 7117 | if (EQ (coding->src_object, coding->dst_object)) | 7146 | if (EQ (coding->src_object, coding->dst_object)) |
| 7118 | dst_end = (unsigned char *) src; | 7147 | dst_end = (unsigned char *) src; |
| 7119 | if (dst == dst_end) | 7148 | if (dst == dst_end) |
| @@ -7144,6 +7173,7 @@ produce_chars (struct coding_system *coding, Lisp_Object translation_table, | |||
| 7144 | 7173 | ||
| 7145 | if (dst >= dst_end - 1) | 7174 | if (dst >= dst_end - 1) |
| 7146 | { | 7175 | { |
| 7176 | eassert (growable_destination (coding)); | ||
| 7147 | if (EQ (coding->src_object, coding->dst_object)) | 7177 | if (EQ (coding->src_object, coding->dst_object)) |
| 7148 | dst_end = (unsigned char *) src; | 7178 | dst_end = (unsigned char *) src; |
| 7149 | if (dst >= dst_end - 1) | 7179 | if (dst >= dst_end - 1) |
| @@ -9729,7 +9759,7 @@ DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_intern | |||
| 9729 | doc: /* Internal use only. */) | 9759 | doc: /* Internal use only. */) |
| 9730 | (Lisp_Object coding_system, Lisp_Object terminal) | 9760 | (Lisp_Object coding_system, Lisp_Object terminal) |
| 9731 | { | 9761 | { |
| 9732 | struct terminal *term = get_terminal (terminal, 1); | 9762 | struct terminal *term = decode_live_terminal (terminal); |
| 9733 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); | 9763 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); |
| 9734 | CHECK_SYMBOL (coding_system); | 9764 | CHECK_SYMBOL (coding_system); |
| 9735 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); | 9765 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
| @@ -9770,7 +9800,7 @@ frame's terminal device. */) | |||
| 9770 | (Lisp_Object terminal) | 9800 | (Lisp_Object terminal) |
| 9771 | { | 9801 | { |
| 9772 | struct coding_system *terminal_coding | 9802 | struct coding_system *terminal_coding |
| 9773 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | 9803 | = TERMINAL_TERMINAL_CODING (decode_live_terminal (terminal)); |
| 9774 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | 9804 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); |
| 9775 | 9805 | ||
| 9776 | /* For backward compatibility, return nil if it is `undecided'. */ | 9806 | /* For backward compatibility, return nil if it is `undecided'. */ |
| @@ -9782,7 +9812,7 @@ DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_intern | |||
| 9782 | doc: /* Internal use only. */) | 9812 | doc: /* Internal use only. */) |
| 9783 | (Lisp_Object coding_system, Lisp_Object terminal) | 9813 | (Lisp_Object coding_system, Lisp_Object terminal) |
| 9784 | { | 9814 | { |
| 9785 | struct terminal *t = get_terminal (terminal, 1); | 9815 | struct terminal *t = decode_live_terminal (terminal); |
| 9786 | CHECK_SYMBOL (coding_system); | 9816 | CHECK_SYMBOL (coding_system); |
| 9787 | if (NILP (coding_system)) | 9817 | if (NILP (coding_system)) |
| 9788 | coding_system = Qno_conversion; | 9818 | coding_system = Qno_conversion; |
| @@ -9801,7 +9831,7 @@ DEFUN ("keyboard-coding-system", | |||
| 9801 | (Lisp_Object terminal) | 9831 | (Lisp_Object terminal) |
| 9802 | { | 9832 | { |
| 9803 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING | 9833 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
| 9804 | (get_terminal (terminal, 1))->id); | 9834 | (decode_live_terminal (terminal))->id); |
| 9805 | } | 9835 | } |
| 9806 | 9836 | ||
| 9807 | 9837 | ||
| @@ -10011,7 +10041,8 @@ make_subsidiaries (Lisp_Object base) | |||
| 10011 | { | 10041 | { |
| 10012 | Lisp_Object subsidiaries; | 10042 | Lisp_Object subsidiaries; |
| 10013 | ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base)); | 10043 | ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base)); |
| 10014 | char *buf = alloca (base_name_len + 6); | 10044 | USE_SAFE_ALLOCA; |
| 10045 | char *buf = SAFE_ALLOCA (base_name_len + 6); | ||
| 10015 | int i; | 10046 | int i; |
| 10016 | 10047 | ||
| 10017 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); | 10048 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); |
| @@ -10021,6 +10052,7 @@ make_subsidiaries (Lisp_Object base) | |||
| 10021 | strcpy (buf + base_name_len, suffixes[i]); | 10052 | strcpy (buf + base_name_len, suffixes[i]); |
| 10022 | ASET (subsidiaries, i, intern (buf)); | 10053 | ASET (subsidiaries, i, intern (buf)); |
| 10023 | } | 10054 | } |
| 10055 | SAFE_FREE (); | ||
| 10024 | return subsidiaries; | 10056 | return subsidiaries; |
| 10025 | } | 10057 | } |
| 10026 | 10058 | ||