1288
1288
#ifdef FEAT_MBYTE
1289
1289
else if (conv_restlen > 0)
1291
/* Reached end-of-file but some trailing bytes could
1292
* not be converted. Truncated file? */
1293
if (conv_error == 0)
1294
conv_error = linecnt;
1295
if (bad_char_behavior != BAD_DROP)
1292
* Reached end-of-file but some trailing bytes could
1293
* not be converted. Truncated file?
1296
/* When we did a conversion report an error. */
1299
|| iconv_fd != (iconv_t)-1
1303
if (conv_error == 0)
1304
conv_error = curbuf->b_ml.ml_line_count
1307
/* Remember the first linenr with an illegal byte */
1308
else if (illegal_byte == 0)
1309
illegal_byte = curbuf->b_ml.ml_line_count
1311
if (bad_char_behavior == BAD_DROP)
1313
*(ptr - conv_restlen) = NUL;
1318
/* Replace the trailing bytes with the replacement
1319
* character if we were converting; if we weren't,
1320
* leave the UTF8 checking code to do it, as it
1321
* works slightly differently. */
1322
if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
1324
|| iconv_fd != (iconv_t)-1
1328
while (conv_restlen > 0)
1330
*(--ptr) = bad_char_behavior;
1297
1334
fio_flags = 0; /* don't convert this */
1298
1335
# ifdef USE_ICONV
1299
1336
if (iconv_fd != (iconv_t)-1)
1872
1895
size = (long)((ptr + real_size) - dest);
1875
else if (enc_utf8 && conv_error == 0 && !curbuf->b_p_bin)
1898
else if (enc_utf8 && !curbuf->b_p_bin)
1877
/* Reading UTF-8: Check if the bytes are valid UTF-8.
1878
* Need to start before "ptr" when part of the character was
1879
* read in the previous read() call. */
1880
for (p = ptr - utf_head_off(buffer, ptr); ; ++p)
1900
int incomplete_tail = FALSE;
1902
/* Reading UTF-8: Check if the bytes are valid UTF-8. */
1903
for (p = ptr; ; ++p)
1882
1905
int todo = (int)((ptr + size) - p);
1891
1914
* read() will get the next bytes, we'll check it
1893
1916
l = utf_ptr2len_len(p, todo);
1917
if (l > todo && !incomplete_tail)
1896
/* Incomplete byte sequence, the next read()
1897
* should get them and check the bytes. */
1919
/* Avoid retrying with a different encoding when
1920
* a truncated file is more likely, or attempting
1921
* to read the rest of an incomplete sequence when
1922
* we have already done so. */
1923
if (p > ptr || filesize > 0)
1924
incomplete_tail = TRUE;
1925
/* Incomplete byte sequence, move it to conv_rest[]
1926
* and try to read the rest of it, unless we've
1927
* already done so. */
1930
conv_restlen = todo;
1931
mch_memmove(conv_rest, p, conv_restlen);
1932
size -= conv_restlen;
1936
if (l == 1 || l > todo)
1903
1938
/* Illegal byte. If we can try another encoding
1939
* do that, unless at EOF where a truncated
1940
* file is more likely than a conversion error. */
1941
if (can_retry && !incomplete_tail)
1944
/* When we did a conversion report an error. */
1945
if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1946
conv_error = readfile_linenr(linecnt, ptr, p);
1908
1948
/* Remember the first linenr with an illegal byte */
1909
if (illegal_byte == 0)
1949
if (conv_error == 0 && illegal_byte == 0)
1910
1950
illegal_byte = readfile_linenr(linecnt, ptr, p);
1912
/* When we did a conversion report an error. */
1913
if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1914
conv_error = readfile_linenr(linecnt, ptr, p);
1917
1952
/* Drop, keep or replace the bad byte. */
1918
1953
if (bad_char_behavior == BAD_DROP)
1920
mch_memmove(p, p+1, todo - 1);
1955
mch_memmove(p, p + 1, todo - 1);
1924
1959
else if (bad_char_behavior != BAD_KEEP)
1925
1960
*p = bad_char_behavior;
1966
if (p < ptr + size && !incomplete_tail)
1932
1968
/* Detected a UTF-8 error. */