aboutsummaryrefslogtreecommitdiffstats
path: root/src/character.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/character.c')
-rw-r--r--src/character.c58
1 files changed, 35 insertions, 23 deletions
diff --git a/src/character.c b/src/character.c
index c4a3a008bc..90f6be1006 100644
--- a/src/character.c
+++ b/src/character.c
@@ -592,10 +592,11 @@ multibyte_chars_in_text (const unsigned char *ptr, EMACS_INT nbytes)
represented by 2-byte in a multibyte text. */
void
-parse_str_as_multibyte (const unsigned char *str, int len, int *nchars, int *nbytes)
+parse_str_as_multibyte (const unsigned char *str, EMACS_INT len,
+ EMACS_INT *nchars, EMACS_INT *nbytes)
{
const unsigned char *endp = str + len;
- int n, chars = 0, bytes = 0;
+ EMACS_INT n, chars = 0, bytes = 0;
if (len >= MAX_MULTIBYTE_LENGTH)
{
@@ -633,12 +634,13 @@ parse_str_as_multibyte (const unsigned char *str, int len, int *nchars, int *nby
area and that is enough. Return the number of bytes of the
resulting text. */
-int
-str_as_multibyte (unsigned char *str, int len, int nbytes, int *nchars)
+EMACS_INT
+str_as_multibyte (unsigned char *str, EMACS_INT len, EMACS_INT nbytes,
+ EMACS_INT *nchars)
{
unsigned char *p = str, *endp = str + nbytes;
unsigned char *to;
- int chars = 0;
+ EMACS_INT chars = 0;
int n;
if (nbytes >= MAX_MULTIBYTE_LENGTH)
@@ -709,11 +711,11 @@ str_as_multibyte (unsigned char *str, int len, int nbytes, int *nchars)
bytes it may ocupy when converted to multibyte string by
`str_to_multibyte'. */
-int
-parse_str_to_multibyte (const unsigned char *str, int len)
+EMACS_INT
+parse_str_to_multibyte (const unsigned char *str, EMACS_INT len)
{
const unsigned char *endp = str + len;
- int bytes;
+ EMACS_INT bytes;
for (bytes = 0; str < endp; str++)
bytes += (*str < 0x80) ? 1 : 2;
@@ -727,8 +729,8 @@ parse_str_to_multibyte (const unsigned char *str, int len)
that we can use LEN bytes at STR as a work area and that is
enough. */
-int
-str_to_multibyte (unsigned char *str, int len, int bytes)
+EMACS_INT
+str_to_multibyte (unsigned char *str, EMACS_INT len, EMACS_INT bytes)
{
unsigned char *p = str, *endp = str + bytes;
unsigned char *to;
@@ -756,8 +758,8 @@ str_to_multibyte (unsigned char *str, int len, int bytes)
actually converts characters in the range 0x80..0xFF to
unibyte. */
-int
-str_as_unibyte (unsigned char *str, int bytes)
+EMACS_INT
+str_as_unibyte (unsigned char *str, EMACS_INT bytes)
{
const unsigned char *p = str, *endp = str + bytes;
unsigned char *to;
@@ -818,14 +820,14 @@ str_to_unibyte (const unsigned char *src, unsigned char *dst, EMACS_INT chars, i
}
-int
+EMACS_INT
string_count_byte8 (Lisp_Object string)
{
int multibyte = STRING_MULTIBYTE (string);
- int nbytes = SBYTES (string);
+ EMACS_INT nbytes = SBYTES (string);
unsigned char *p = SDATA (string);
unsigned char *pend = p + nbytes;
- int count = 0;
+ EMACS_INT count = 0;
int c, len;
if (multibyte)
@@ -851,10 +853,10 @@ string_count_byte8 (Lisp_Object string)
Lisp_Object
string_escape_byte8 (Lisp_Object string)
{
- int nchars = SCHARS (string);
- int nbytes = SBYTES (string);
+ EMACS_INT nchars = SCHARS (string);
+ EMACS_INT nbytes = SBYTES (string);
int multibyte = STRING_MULTIBYTE (string);
- int byte8_count;
+ EMACS_INT byte8_count;
const unsigned char *src, *src_end;
unsigned char *dst;
Lisp_Object val;
@@ -869,12 +871,22 @@ string_escape_byte8 (Lisp_Object string)
return string;
if (multibyte)
- /* Convert 2-byte sequence of byte8 chars to 4-byte octal. */
- val = make_uninit_multibyte_string (nchars + byte8_count * 3,
- nbytes + byte8_count * 2);
+ {
+ if ((MOST_POSITIVE_FIXNUM - nchars) / 3 < byte8_count
+ || (MOST_POSITIVE_FIXNUM - nbytes) / 2 < byte8_count)
+ error ("Maximum string size exceeded");
+
+ /* Convert 2-byte sequence of byte8 chars to 4-byte octal. */
+ val = make_uninit_multibyte_string (nchars + byte8_count * 3,
+ nbytes + byte8_count * 2);
+ }
else
- /* Convert 1-byte sequence of byte8 chars to 4-byte octal. */
- val = make_uninit_string (nbytes + byte8_count * 3);
+ {
+ if ((MOST_POSITIVE_FIXNUM - nchars) / 3 < byte8_count)
+ error ("Maximum string size exceeded");
+ /* Convert 1-byte sequence of byte8 chars to 4-byte octal. */
+ val = make_uninit_string (nbytes + byte8_count * 3);
+ }
src = SDATA (string);
src_end = src + nbytes;