/* Coding system handler (conversion, detection, etc).
- Copyright (C) 2001-2015 Free Software Foundation, Inc.
+ Copyright (C) 2001-2016 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)
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.
+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
#include "ccl.h"
#include "composite.h"
#include "coding.h"
-#include "window.h"
-#include "frame.h"
#include "termhooks.h"
Lisp_Object Vcoding_system_hash_table;
static void
coding_alloc_by_realloc (struct coding_system *coding, ptrdiff_t bytes)
{
- if (STRING_BYTES_BOUND - coding->dst_bytes < bytes)
+ ptrdiff_t newbytes;
+ if (INT_ADD_WRAPV (coding->dst_bytes, bytes, &newbytes)
+ || SIZE_MAX < newbytes)
string_overflow ();
- coding->destination = xrealloc (coding->destination,
- coding->dst_bytes + bytes);
- coding->dst_bytes += bytes;
+ coding->destination = xrealloc (coding->destination, newbytes);
+ coding->dst_bytes = newbytes;
}
static void
else
ENCODE_LOCKING_SHIFT_3;
break;
+
+ default:
+ break;
}
}
if (NILP (coding_system))
coding_system = Qraw_text;
+ else
+ CHECK_CODING_SYSTEM (coding_system);
spec = CODING_SYSTEM_SPEC (coding_system);
eol_type = AREF (spec, 2);
if (VECTORP (eol_type))
{
Lisp_Object parent_spec;
+ CHECK_CODING_SYSTEM (parent);
parent_spec = CODING_SYSTEM_SPEC (parent);
parent_eol_type = AREF (parent_spec, 2);
if (VECTORP (parent_eol_type))
else if (EQ (eol_type, Qdos))
{
ptrdiff_t n = 0;
+ ptrdiff_t pos = coding->dst_pos;
+ ptrdiff_t pos_byte = coding->dst_pos_byte;
+ ptrdiff_t pos_end = pos_byte + coding->produced - 1;
- if (NILP (coding->dst_object))
- {
- /* Start deleting '\r' from the tail to minimize the memory
- movement. */
- for (p = pend - 2; p >= pbeg; p--)
- if (*p == '\r')
- {
- memmove (p, p + 1, pend-- - p - 1);
- n++;
- }
- }
- else
- {
- ptrdiff_t pos_byte = coding->dst_pos_byte;
- ptrdiff_t pos = coding->dst_pos;
- ptrdiff_t pos_end = pos + coding->produced_char - 1;
+ /* This assertion is here instead of code, now deleted, that
+ handled the NILP case, which no longer happens with the
+ current codebase. */
+ eassert (!NILP (coding->dst_object));
- while (pos < pos_end)
+ while (pos_byte < pos_end)
+ {
+ p = BYTE_POS_ADDR (pos_byte);
+ if (*p == '\r' && p[1] == '\n')
{
- p = BYTE_POS_ADDR (pos_byte);
- if (*p == '\r' && p[1] == '\n')
- {
- del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
- n++;
- pos_end--;
- }
- pos++;
- if (coding->dst_multibyte)
- pos_byte += BYTES_BY_CHAR_HEAD (*p);
- else
- pos_byte++;
+ del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
+ n++;
+ pos_end--;
}
+ pos++;
+ if (coding->dst_multibyte)
+ pos_byte += BYTES_BY_CHAR_HEAD (*p);
+ else
+ pos_byte++;
}
coding->produced -= n;
coding->produced_char -= n;
if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars)
{
eassert (growable_destination (coding));
- if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf))
- / MAX_MULTIBYTE_LENGTH)
- < to_nchars)
+ ptrdiff_t dst_size;
+ if (INT_MULTIPLY_WRAPV (to_nchars, MAX_MULTIBYTE_LENGTH,
+ &dst_size)
+ || INT_ADD_WRAPV (buf_end - buf, dst_size, &dst_size))
memory_full (SIZE_MAX);
- dst = alloc_destination (coding,
- buf_end - buf
- + MAX_MULTIBYTE_LENGTH * to_nchars,
- dst);
+ dst = alloc_destination (coding, dst_size, dst);
if (EQ (coding->src_object, coding->dst_object))
{
coding_set_source (coding);
case CODING_ANNOTATE_CHARSET_MASK:
produce_charset (coding, charbuf, pos);
break;
+ default:
+ break;
}
charbuf += len;
}
code_conversion_restore (Lisp_Object arg)
{
Lisp_Object current, workbuf;
- struct gcpro gcpro1;
- GCPRO1 (arg);
current = XCAR (arg);
workbuf = XCDR (arg);
if (! NILP (workbuf))
Fkill_buffer (workbuf);
}
set_buffer_internal (XBUFFER (current));
- UNGCPRO;
}
Lisp_Object
Lisp_Object dst_object)
{
ptrdiff_t count = SPECPDL_INDEX ();
- unsigned char *destination IF_LINT (= NULL);
- ptrdiff_t dst_bytes IF_LINT (= 0);
+ unsigned char *destination;
+ ptrdiff_t dst_bytes;
ptrdiff_t chars = to - from;
ptrdiff_t bytes = to_byte - from_byte;
Lisp_Object attrs;
- ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
+ ptrdiff_t saved_pt = -1, saved_pt_byte;
bool need_marker_adjustment = 0;
Lisp_Object old_deactivate_mark;
if (! NILP (CODING_ATTR_POST_READ (attrs)))
{
- struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE;
Lisp_Object val;
TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
- GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
- old_deactivate_mark);
val = safe_call1 (CODING_ATTR_POST_READ (attrs),
make_number (coding->produced_char));
- UNGCPRO;
CHECK_NATNUM (val);
coding->produced_char += Z - prev_Z;
coding->produced += Z_BYTE - prev_Z_BYTE;
ptrdiff_t chars = to - from;
ptrdiff_t bytes = to_byte - from_byte;
Lisp_Object attrs;
- ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0);
+ ptrdiff_t saved_pt = -1, saved_pt_byte;
bool need_marker_adjustment = 0;
bool kill_src_buffer = 0;
Lisp_Object old_deactivate_mark;
set_buffer_internal (XBUFFER (coding->src_object));
}
- {
- struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
-
- GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object,
- old_deactivate_mark);
- safe_call2 (CODING_ATTR_PRE_WRITE (attrs),
- make_number (BEG), make_number (Z));
- UNGCPRO;
- }
+ safe_call2 (CODING_ATTR_PRE_WRITE (attrs),
+ make_number (BEG), make_number (Z));
if (XBUFFER (coding->src_object) != current_buffer)
kill_src_buffer = 1;
coding->src_object = Fcurrent_buffer ();
}
}
else
- code_conversion_save (0, 0);
+ {
+ code_conversion_save (0, 0);
+ coding->src_pos = from;
+ coding->src_pos_byte = from_byte;
+ }
if (BUFFERP (dst_object))
{
Lisp_Object
from_unicode_buffer (const wchar_t *wstr)
{
- return from_unicode (
- make_unibyte_string (
- (char *) wstr,
- /* we get one of the two final 0 bytes for free. */
- 1 + sizeof (wchar_t) * wcslen (wstr)));
+ /* We get one of the two final null bytes for free. */
+ ptrdiff_t len = 1 + sizeof (wchar_t) * wcslen (wstr);
+ AUTO_STRING_WITH_LEN (str, (char *) wstr, len);
+ return from_unicode (str);
}
wchar_t *
if (!(STRINGP (target)
|| (EQ (operation, Qinsert_file_contents) && CONSP (target)
&& STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
- || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
+ || (EQ (operation, Qopen_network_stream)
+ && (INTEGERP (target) || EQ (target, Qt)))))
error ("Invalid argument %"pI"d of operation `%s'",
XFASTINT (target_idx) + 1, SDATA (SYMBOL_NAME (operation)));
if (CONSP (target))
DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion,
doc: /*
-*Non-nil means always inhibit code conversion of end-of-line format.
+Non-nil means always inhibit code conversion of end-of-line format.
See info node `Coding Systems' and info node `Text and Binary' concerning
such conversion. */);
inhibit_eol_conversion = 0;
/* The eol mnemonics are reset in startup.el system-dependently. */
DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix,
doc: /*
-*String displayed in mode line for UNIX-like (LF) end-of-line format. */);
+String displayed in mode line for UNIX-like (LF) end-of-line format. */);
eol_mnemonic_unix = build_pure_c_string (":");
DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos,
doc: /*
-*String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
+String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
eol_mnemonic_dos = build_pure_c_string ("\\");
DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac,
doc: /*
-*String displayed in mode line for MAC-like (CR) end-of-line format. */);
+String displayed in mode line for MAC-like (CR) end-of-line format. */);
eol_mnemonic_mac = build_pure_c_string ("/");
DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided,
doc: /*
-*String displayed in mode line when end-of-line format is not yet determined. */);
+String displayed in mode line when end-of-line format is not yet determined. */);
eol_mnemonic_undecided = build_pure_c_string (":");
DEFVAR_LISP ("enable-character-translation", Venable_character_translation,
doc: /*
-*Non-nil enables character translation while encoding and decoding. */);
+Non-nil enables character translation while encoding and decoding. */);
Venable_character_translation = Qt;
DEFVAR_LISP ("standard-translation-table-for-decode",