Mercurial > hg > xemacs-beta
comparison src/file-coding.c @ 665:fdefd0186b75
[xemacs-hg @ 2001-09-20 06:28:42 by ben]
The great integral types renaming.
The purpose of this is to rationalize the names used for various
integral types, so that they match their intended uses and follow
consist conventions, and eliminate types that were not semantically
different from each other.
The conventions are:
-- All integral types that measure quantities of anything are
signed. Some people disagree vociferously with this, but their
arguments are mostly theoretical, and are vastly outweighed by
the practical headaches of mixing signed and unsigned values,
and more importantly by the far increased likelihood of
inadvertent bugs: Because of the broken "viral" nature of
unsigned quantities in C (operations involving mixed
signed/unsigned are done unsigned, when exactly the opposite is
nearly always wanted), even a single error in declaring a
quantity unsigned that should be signed, or even the even more
subtle error of comparing signed and unsigned values and
forgetting the necessary cast, can be catastrophic, as
comparisons will yield wrong results. -Wsign-compare is turned
on specifically to catch this, but this tends to result in a
great number of warnings when mixing signed and unsigned, and
the casts are annoying. More has been written on this
elsewhere.
-- All such quantity types just mentioned boil down to EMACS_INT,
which is 32 bits on 32-bit machines and 64 bits on 64-bit
machines. This is guaranteed to be the same size as Lisp
objects of type `int', and (as far as I can tell) of size_t
(unsigned!) and ssize_t. The only type below that is not an
EMACS_INT is Hashcode, which is an unsigned value of the same
size as EMACS_INT.
-- Type names should be relatively short (no more than 10
characters or so), with the first letter capitalized and no
underscores if they can at all be avoided.
-- "count" == a zero-based measurement of some quantity. Includes
sizes, offsets, and indexes.
-- "bpos" == a one-based measurement of a position in a buffer.
"Charbpos" and "Bytebpos" count text in the buffer, rather than
bytes in memory; thus Bytebpos does not directly correspond to
the memory representation. Use "Membpos" for this.
-- "Char" refers to internal-format characters, not to the C type
"char", which is really a byte.
-- For the actual name changes, see the script below.
I ran the following script to do the conversion. (NOTE: This script
is idempotent. You can safely run it multiple times and it will
not screw up previous results -- in fact, it will do nothing if
nothing has changed. Thus, it can be run repeatedly as necessary
to handle patches coming in from old workspaces, or old branches.)
There are two tags, just before and just after the change:
`pre-integral-type-rename' and `post-integral-type-rename'. When
merging code from the main trunk into a branch, the best thing to
do is first merge up to `pre-integral-type-rename', then apply the
script and associated changes, then merge from
`post-integral-type-change' to the present. (Alternatively, just do
the merging in one operation; but you may then have a lot of
conflicts needing to be resolved by hand.)
Script `fixtypes.sh' follows:
----------------------------------- cut ------------------------------------
files="*.[ch] s/*.h m/*.h config.h.in ../configure.in Makefile.in.in ../lib-src/*.[ch] ../lwlib/*.[ch]"
gr Memory_Count Bytecount $files
gr Lstream_Data_Count Bytecount $files
gr Element_Count Elemcount $files
gr Hash_Code Hashcode $files
gr extcount bytecount $files
gr bufpos charbpos $files
gr bytind bytebpos $files
gr memind membpos $files
gr bufbyte intbyte $files
gr Extcount Bytecount $files
gr Bufpos Charbpos $files
gr Bytind Bytebpos $files
gr Memind Membpos $files
gr Bufbyte Intbyte $files
gr EXTCOUNT BYTECOUNT $files
gr BUFPOS CHARBPOS $files
gr BYTIND BYTEBPOS $files
gr MEMIND MEMBPOS $files
gr BUFBYTE INTBYTE $files
gr MEMORY_COUNT BYTECOUNT $files
gr LSTREAM_DATA_COUNT BYTECOUNT $files
gr ELEMENT_COUNT ELEMCOUNT $files
gr HASH_CODE HASHCODE $files
----------------------------------- cut ------------------------------------
`fixtypes.sh' is a Bourne-shell script; it uses 'gr':
----------------------------------- cut ------------------------------------
#!/bin/sh
# Usage is like this:
# gr FROM TO FILES ...
# globally replace FROM with TO in FILES. FROM and TO are regular expressions.
# backup files are stored in the `backup' directory.
from="$1"
to="$2"
shift 2
echo ${1+"$@"} | xargs global-replace "s/$from/$to/g"
----------------------------------- cut ------------------------------------
`gr' in turn uses a Perl script to do its real work,
`global-replace', which follows:
----------------------------------- cut ------------------------------------
: #-*- Perl -*-
### global-modify --- modify the contents of a file by a Perl expression
## Copyright (C) 1999 Martin Buchholz.
## Copyright (C) 2001 Ben Wing.
## Authors: Martin Buchholz <martin@xemacs.org>, Ben Wing <ben@xemacs.org>
## Maintainer: Ben Wing <ben@xemacs.org>
## Current Version: 1.0, May 5, 2001
# This program 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 2, or (at your option)
# any later version.
#
# This program 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 XEmacs; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
eval 'exec perl -w -S $0 ${1+"$@"}'
if 0;
use strict;
use FileHandle;
use Carp;
use Getopt::Long;
use File::Basename;
(my $myName = $0) =~ s@.*/@@; my $usage="
Usage: $myName [--help] [--backup-dir=DIR] [--line-mode] [--hunk-mode]
PERLEXPR FILE ...
Globally modify a file, either line by line or in one big hunk.
Typical usage is like this:
[with GNU print, GNU xargs: guaranteed to handle spaces, quotes, etc.
in file names]
find . -name '*.[ch]' -print0 | xargs -0 $0 's/\bCONST\b/const/g'\n
[with non-GNU print, xargs]
find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n
The file is read in, either line by line (with --line-mode specified)
or in one big hunk (with --hunk-mode specified; it's the default), and
the Perl expression is then evalled with \$_ set to the line or hunk of
text, including the terminating newline if there is one. It should
destructively modify the value there, storing the changed result in \$_.
Files in which any modifications are made are backed up to the directory
specified using --backup-dir, or to `backup' by default. To disable this,
use --backup-dir= with no argument.
Hunk mode is the default because it is MUCH MUCH faster than line-by-line.
Use line-by-line only when it matters, e.g. you want to do a replacement
only once per line (the default without the `g' argument). Conversely,
when using hunk mode, *ALWAYS* use `g'; otherwise, you will only make one
replacement in the entire file!
";
my %options = ();
$Getopt::Long::ignorecase = 0;
&GetOptions (
\%options,
'help', 'backup-dir=s', 'line-mode', 'hunk-mode',
);
die $usage if $options{"help"} or @ARGV <= 1;
my $code = shift;
die $usage if grep (-d || ! -w, @ARGV);
sub SafeOpen {
open ((my $fh = new FileHandle), $_[0]);
confess "Can't open $_[0]: $!" if ! defined $fh;
return $fh;
}
sub SafeClose {
close $_[0] or confess "Can't close $_[0]: $!";
}
sub FileContents {
my $fh = SafeOpen ("< $_[0]");
my $olddollarslash = $/;
local $/ = undef;
my $contents = <$fh>;
$/ = $olddollarslash;
return $contents;
}
sub WriteStringToFile {
my $fh = SafeOpen ("> $_[0]");
binmode $fh;
print $fh $_[1] or confess "$_[0]: $!\n";
SafeClose $fh;
}
foreach my $file (@ARGV) {
my $changed_p = 0;
my $new_contents = "";
if ($options{"line-mode"}) {
my $fh = SafeOpen $file;
while (<$fh>) {
my $save_line = $_;
eval $code;
$changed_p = 1 if $save_line ne $_;
$new_contents .= $_;
}
} else {
my $orig_contents = $_ = FileContents $file;
eval $code;
if ($_ ne $orig_contents) {
$changed_p = 1;
$new_contents = $_;
}
}
if ($changed_p) {
my $backdir = $options{"backup-dir"};
$backdir = "backup" if !defined ($backdir);
if ($backdir) {
my ($name, $path, $suffix) = fileparse ($file, "");
my $backfulldir = $path . $backdir;
my $backfile = "$backfulldir/$name";
mkdir $backfulldir, 0755 unless -d $backfulldir;
print "modifying $file (original saved in $backfile)\n";
rename $file, $backfile;
}
WriteStringToFile ($file, $new_contents);
}
}
----------------------------------- cut ------------------------------------
In addition to those programs, I needed to fix up a few other
things, particularly relating to the duplicate definitions of
types, now that some types merged with others. Specifically:
1. in lisp.h, removed duplicate declarations of Bytecount. The
changed code should now look like this: (In each code snippet
below, the first and last lines are the same as the original, as
are all lines outside of those lines. That allows you to locate
the section to be replaced, and replace the stuff in that
section, verifying that there isn't anything new added that
would need to be kept.)
--------------------------------- snip -------------------------------------
/* Counts of bytes or chars */
typedef EMACS_INT Bytecount;
typedef EMACS_INT Charcount;
/* Counts of elements */
typedef EMACS_INT Elemcount;
/* Hash codes */
typedef unsigned long Hashcode;
/* ------------------------ dynamic arrays ------------------- */
--------------------------------- snip -------------------------------------
2. in lstream.h, removed duplicate declaration of Bytecount.
Rewrote the comment about this type. The changed code should
now look like this:
--------------------------------- snip -------------------------------------
#endif
/* The have been some arguments over the what the type should be that
specifies a count of bytes in a data block to be written out or read in,
using Lstream_read(), Lstream_write(), and related functions.
Originally it was long, which worked fine; Martin "corrected" these to
size_t and ssize_t on the grounds that this is theoretically cleaner and
is in keeping with the C standards. Unfortunately, this practice is
horribly error-prone due to design flaws in the way that mixed
signed/unsigned arithmetic happens. In fact, by doing this change,
Martin introduced a subtle but fatal error that caused the operation of
sending large mail messages to the SMTP server under Windows to fail.
By putting all values back to be signed, avoiding any signed/unsigned
mixing, the bug immediately went away. The type then in use was
Lstream_Data_Count, so that it be reverted cleanly if a vote came to
that. Now it is Bytecount.
Some earlier comments about why the type must be signed: This MUST BE
SIGNED, since it also is used in functions that return the number of
bytes actually read to or written from in an operation, and these
functions can return -1 to signal error.
Note that the standard Unix read() and write() functions define the
count going in as a size_t, which is UNSIGNED, and the count going
out as an ssize_t, which is SIGNED. This is a horrible design
flaw. Not only is it highly likely to lead to logic errors when a
-1 gets interpreted as a large positive number, but operations are
bound to fail in all sorts of horrible ways when a number in the
upper-half of the size_t range is passed in -- this number is
unrepresentable as an ssize_t, so code that checks to see how many
bytes are actually written (which is mandatory if you are dealing
with certain types of devices) will get completely screwed up.
--ben
*/
typedef enum lstream_buffering
--------------------------------- snip -------------------------------------
3. in dumper.c, there are four places, all inside of switch()
statements, where XD_BYTECOUNT appears twice as a case tag. In
each case, the two case blocks contain identical code, and you
should *REMOVE THE SECOND* and leave the first.
author | ben |
---|---|
date | Thu, 20 Sep 2001 06:31:11 +0000 |
parents | b39c14581166 |
children | 943eaba38521 |
comparison
equal
deleted
inserted
replaced
664:6e99cc8c6ca5 | 665:fdefd0186b75 |
---|---|
172 #endif /* MULE */ | 172 #endif /* MULE */ |
173 EXFUN (Fcopy_coding_system, 2); | 173 EXFUN (Fcopy_coding_system, 2); |
174 #ifdef MULE | 174 #ifdef MULE |
175 struct detection_state; | 175 struct detection_state; |
176 static int detect_coding_sjis (struct detection_state *st, | 176 static int detect_coding_sjis (struct detection_state *st, |
177 const Extbyte *src, Lstream_Data_Count n); | 177 const Extbyte *src, Bytecount n); |
178 static void decode_coding_sjis (Lstream *decoding, const Extbyte *src, | 178 static void decode_coding_sjis (Lstream *decoding, const Extbyte *src, |
179 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 179 unsigned_char_dynarr *dst, Bytecount n); |
180 static void encode_coding_sjis (Lstream *encoding, const Bufbyte *src, | 180 static void encode_coding_sjis (Lstream *encoding, const Intbyte *src, |
181 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 181 unsigned_char_dynarr *dst, Bytecount n); |
182 static int detect_coding_big5 (struct detection_state *st, | 182 static int detect_coding_big5 (struct detection_state *st, |
183 const Extbyte *src, Lstream_Data_Count n); | 183 const Extbyte *src, Bytecount n); |
184 static void decode_coding_big5 (Lstream *decoding, const Extbyte *src, | 184 static void decode_coding_big5 (Lstream *decoding, const Extbyte *src, |
185 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 185 unsigned_char_dynarr *dst, Bytecount n); |
186 static void encode_coding_big5 (Lstream *encoding, const Bufbyte *src, | 186 static void encode_coding_big5 (Lstream *encoding, const Intbyte *src, |
187 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 187 unsigned_char_dynarr *dst, Bytecount n); |
188 static int detect_coding_ucs4 (struct detection_state *st, | 188 static int detect_coding_ucs4 (struct detection_state *st, |
189 const Extbyte *src, Lstream_Data_Count n); | 189 const Extbyte *src, Bytecount n); |
190 static void decode_coding_ucs4 (Lstream *decoding, const Extbyte *src, | 190 static void decode_coding_ucs4 (Lstream *decoding, const Extbyte *src, |
191 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 191 unsigned_char_dynarr *dst, Bytecount n); |
192 static void encode_coding_ucs4 (Lstream *encoding, const Bufbyte *src, | 192 static void encode_coding_ucs4 (Lstream *encoding, const Intbyte *src, |
193 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 193 unsigned_char_dynarr *dst, Bytecount n); |
194 static int detect_coding_utf8 (struct detection_state *st, | 194 static int detect_coding_utf8 (struct detection_state *st, |
195 const Extbyte *src, Lstream_Data_Count n); | 195 const Extbyte *src, Bytecount n); |
196 static void decode_coding_utf8 (Lstream *decoding, const Extbyte *src, | 196 static void decode_coding_utf8 (Lstream *decoding, const Extbyte *src, |
197 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 197 unsigned_char_dynarr *dst, Bytecount n); |
198 static void encode_coding_utf8 (Lstream *encoding, const Bufbyte *src, | 198 static void encode_coding_utf8 (Lstream *encoding, const Intbyte *src, |
199 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 199 unsigned_char_dynarr *dst, Bytecount n); |
200 static int postprocess_iso2022_mask (int mask); | 200 static int postprocess_iso2022_mask (int mask); |
201 static void reset_iso2022 (Lisp_Object coding_system, | 201 static void reset_iso2022 (Lisp_Object coding_system, |
202 struct iso2022_decoder *iso); | 202 struct iso2022_decoder *iso); |
203 static int detect_coding_iso2022 (struct detection_state *st, | 203 static int detect_coding_iso2022 (struct detection_state *st, |
204 const Extbyte *src, Lstream_Data_Count n); | 204 const Extbyte *src, Bytecount n); |
205 static void decode_coding_iso2022 (Lstream *decoding, const Extbyte *src, | 205 static void decode_coding_iso2022 (Lstream *decoding, const Extbyte *src, |
206 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 206 unsigned_char_dynarr *dst, Bytecount n); |
207 static void encode_coding_iso2022 (Lstream *encoding, const Bufbyte *src, | 207 static void encode_coding_iso2022 (Lstream *encoding, const Intbyte *src, |
208 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 208 unsigned_char_dynarr *dst, Bytecount n); |
209 #endif /* MULE */ | 209 #endif /* MULE */ |
210 static void decode_coding_no_conversion (Lstream *decoding, const Extbyte *src, | 210 static void decode_coding_no_conversion (Lstream *decoding, const Extbyte *src, |
211 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 211 unsigned_char_dynarr *dst, Bytecount n); |
212 static void encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src, | 212 static void encode_coding_no_conversion (Lstream *encoding, const Intbyte *src, |
213 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 213 unsigned_char_dynarr *dst, Bytecount n); |
214 static void mule_decode (Lstream *decoding, const Extbyte *src, | 214 static void mule_decode (Lstream *decoding, const Extbyte *src, |
215 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 215 unsigned_char_dynarr *dst, Bytecount n); |
216 static void mule_encode (Lstream *encoding, const Bufbyte *src, | 216 static void mule_encode (Lstream *encoding, const Intbyte *src, |
217 unsigned_char_dynarr *dst, Lstream_Data_Count n); | 217 unsigned_char_dynarr *dst, Bytecount n); |
218 | 218 |
219 typedef struct codesys_prop codesys_prop; | 219 typedef struct codesys_prop codesys_prop; |
220 struct codesys_prop | 220 struct codesys_prop |
221 { | 221 { |
222 Lisp_Object sym; | 222 Lisp_Object sym; |
1615 return (mask & (mask - 1)) == 0; | 1615 return (mask & (mask - 1)) == 0; |
1616 } | 1616 } |
1617 | 1617 |
1618 static eol_type_t | 1618 static eol_type_t |
1619 detect_eol_type (struct detection_state *st, const Extbyte *src, | 1619 detect_eol_type (struct detection_state *st, const Extbyte *src, |
1620 Lstream_Data_Count n) | 1620 Bytecount n) |
1621 { | 1621 { |
1622 while (n--) | 1622 while (n--) |
1623 { | 1623 { |
1624 unsigned char c = *(unsigned char *)src++; | 1624 unsigned char c = *(unsigned char *)src++; |
1625 if (c == '\n') | 1625 if (c == '\n') |
1658 1 == definitive answers are here for both st->eol_type and st->mask | 1658 1 == definitive answers are here for both st->eol_type and st->mask |
1659 */ | 1659 */ |
1660 | 1660 |
1661 static int | 1661 static int |
1662 detect_coding_type (struct detection_state *st, const Extbyte *src, | 1662 detect_coding_type (struct detection_state *st, const Extbyte *src, |
1663 Lstream_Data_Count n, int just_do_eol) | 1663 Bytecount n, int just_do_eol) |
1664 { | 1664 { |
1665 if (st->eol_type == EOL_AUTODETECT) | 1665 if (st->eol_type == EOL_AUTODETECT) |
1666 st->eol_type = detect_eol_type (st, src, n); | 1666 st->eol_type = detect_eol_type (st, src, n); |
1667 | 1667 |
1668 if (just_do_eol) | 1668 if (just_do_eol) |
1789 || *eol_type_in_out == EOL_AUTODETECT) | 1789 || *eol_type_in_out == EOL_AUTODETECT) |
1790 { | 1790 { |
1791 Extbyte buf[4096]; | 1791 Extbyte buf[4096]; |
1792 Lisp_Object coding_system = Qnil; | 1792 Lisp_Object coding_system = Qnil; |
1793 Extbyte *p; | 1793 Extbyte *p; |
1794 Lstream_Data_Count nread = Lstream_read (stream, buf, sizeof (buf)); | 1794 Bytecount nread = Lstream_read (stream, buf, sizeof (buf)); |
1795 Extbyte *scan_end; | 1795 Extbyte *scan_end; |
1796 | 1796 |
1797 /* Look for initial "-*-"; mode line prefix */ | 1797 /* Look for initial "-*-"; mode line prefix */ |
1798 for (p = buf, | 1798 for (p = buf, |
1799 scan_end = buf + nread - LENGTH ("-*-coding:?-*-"); | 1799 scan_end = buf + nread - LENGTH ("-*-coding:?-*-"); |
1905 */ | 1905 */ |
1906 (start, end, buffer)) | 1906 (start, end, buffer)) |
1907 { | 1907 { |
1908 Lisp_Object val = Qnil; | 1908 Lisp_Object val = Qnil; |
1909 struct buffer *buf = decode_buffer (buffer, 0); | 1909 struct buffer *buf = decode_buffer (buffer, 0); |
1910 Bufpos b, e; | 1910 Charbpos b, e; |
1911 Lisp_Object instream, lb_instream; | 1911 Lisp_Object instream, lb_instream; |
1912 Lstream *istr, *lb_istr; | 1912 Lstream *istr, *lb_istr; |
1913 struct detection_state decst; | 1913 struct detection_state decst; |
1914 struct gcpro gcpro1, gcpro2; | 1914 struct gcpro gcpro1, gcpro2; |
1915 | 1915 |
1923 decst.eol_type = EOL_AUTODETECT; | 1923 decst.eol_type = EOL_AUTODETECT; |
1924 decst.mask = ~0; | 1924 decst.mask = ~0; |
1925 while (1) | 1925 while (1) |
1926 { | 1926 { |
1927 Extbyte random_buffer[4096]; | 1927 Extbyte random_buffer[4096]; |
1928 Lstream_Data_Count nread = Lstream_read (istr, random_buffer, sizeof (random_buffer)); | 1928 Bytecount nread = Lstream_read (istr, random_buffer, sizeof (random_buffer)); |
1929 | 1929 |
1930 if (!nread) | 1930 if (!nread) |
1931 break; | 1931 break; |
1932 if (detect_coding_type (&decst, random_buffer, nread, 0)) | 1932 if (detect_coding_type (&decst, random_buffer, nread, 0)) |
1933 break; | 1933 break; |
2090 unsigned char counter; | 2090 unsigned char counter; |
2091 #endif | 2091 #endif |
2092 struct detection_state decst; | 2092 struct detection_state decst; |
2093 }; | 2093 }; |
2094 | 2094 |
2095 static Lstream_Data_Count decoding_reader (Lstream *stream, | 2095 static Bytecount decoding_reader (Lstream *stream, |
2096 unsigned char *data, Lstream_Data_Count size); | 2096 unsigned char *data, Bytecount size); |
2097 static Lstream_Data_Count decoding_writer (Lstream *stream, | 2097 static Bytecount decoding_writer (Lstream *stream, |
2098 const unsigned char *data, Lstream_Data_Count size); | 2098 const unsigned char *data, Bytecount size); |
2099 static int decoding_rewinder (Lstream *stream); | 2099 static int decoding_rewinder (Lstream *stream); |
2100 static int decoding_seekable_p (Lstream *stream); | 2100 static int decoding_seekable_p (Lstream *stream); |
2101 static int decoding_flusher (Lstream *stream); | 2101 static int decoding_flusher (Lstream *stream); |
2102 static int decoding_closer (Lstream *stream); | 2102 static int decoding_closer (Lstream *stream); |
2103 | 2103 |
2125 } | 2125 } |
2126 | 2126 |
2127 /* Read SIZE bytes of data and store it into DATA. We are a decoding stream | 2127 /* Read SIZE bytes of data and store it into DATA. We are a decoding stream |
2128 so we read data from the other end, decode it, and store it into DATA. */ | 2128 so we read data from the other end, decode it, and store it into DATA. */ |
2129 | 2129 |
2130 static Lstream_Data_Count | 2130 static Bytecount |
2131 decoding_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) | 2131 decoding_reader (Lstream *stream, unsigned char *data, Bytecount size) |
2132 { | 2132 { |
2133 struct decoding_stream *str = DECODING_STREAM_DATA (stream); | 2133 struct decoding_stream *str = DECODING_STREAM_DATA (stream); |
2134 unsigned char *orig_data = data; | 2134 unsigned char *orig_data = data; |
2135 Lstream_Data_Count read_size; | 2135 Bytecount read_size; |
2136 int error_occurred = 0; | 2136 int error_occurred = 0; |
2137 | 2137 |
2138 /* We need to interface to mule_decode(), which expects to take some | 2138 /* We need to interface to mule_decode(), which expects to take some |
2139 amount of data and store the result into a Dynarr. We have | 2139 amount of data and store the result into a Dynarr. We have |
2140 mule_decode() store into str->runoff, and take data from there | 2140 mule_decode() store into str->runoff, and take data from there |
2146 { | 2146 { |
2147 /* Take data from the runoff if we can. Make sure to take at | 2147 /* Take data from the runoff if we can. Make sure to take at |
2148 most SIZE bytes, and delete the data from the runoff. */ | 2148 most SIZE bytes, and delete the data from the runoff. */ |
2149 if (Dynarr_length (str->runoff) > 0) | 2149 if (Dynarr_length (str->runoff) > 0) |
2150 { | 2150 { |
2151 Lstream_Data_Count chunk = min (size, (Lstream_Data_Count) Dynarr_length (str->runoff)); | 2151 Bytecount chunk = min (size, (Bytecount) Dynarr_length (str->runoff)); |
2152 memcpy (data, Dynarr_atp (str->runoff, 0), chunk); | 2152 memcpy (data, Dynarr_atp (str->runoff, 0), chunk); |
2153 Dynarr_delete_many (str->runoff, 0, chunk); | 2153 Dynarr_delete_many (str->runoff, 0, chunk); |
2154 data += chunk; | 2154 data += chunk; |
2155 size -= chunk; | 2155 size -= chunk; |
2156 } | 2156 } |
2187 return error_occurred ? -1 : 0; | 2187 return error_occurred ? -1 : 0; |
2188 else | 2188 else |
2189 return data - orig_data; | 2189 return data - orig_data; |
2190 } | 2190 } |
2191 | 2191 |
2192 static Lstream_Data_Count | 2192 static Bytecount |
2193 decoding_writer (Lstream *stream, const unsigned char *data, Lstream_Data_Count size) | 2193 decoding_writer (Lstream *stream, const unsigned char *data, Bytecount size) |
2194 { | 2194 { |
2195 struct decoding_stream *str = DECODING_STREAM_DATA (stream); | 2195 struct decoding_stream *str = DECODING_STREAM_DATA (stream); |
2196 Lstream_Data_Count retval; | 2196 Bytecount retval; |
2197 | 2197 |
2198 /* Decode all our data into the runoff, and then attempt to write | 2198 /* Decode all our data into the runoff, and then attempt to write |
2199 it all out to the other end. Remove whatever chunk we succeeded | 2199 it all out to the other end. Remove whatever chunk we succeeded |
2200 in writing. */ | 2200 in writing. */ |
2201 mule_decode (stream, (Extbyte *) data, str->runoff, size); | 2201 mule_decode (stream, (Extbyte *) data, str->runoff, size); |
2349 or decoding_writer(). This allows the same functions to | 2349 or decoding_writer(). This allows the same functions to |
2350 be used for both reading and writing. */ | 2350 be used for both reading and writing. */ |
2351 | 2351 |
2352 static void | 2352 static void |
2353 mule_decode (Lstream *decoding, const Extbyte *src, | 2353 mule_decode (Lstream *decoding, const Extbyte *src, |
2354 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 2354 unsigned_char_dynarr *dst, Bytecount n) |
2355 { | 2355 { |
2356 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 2356 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
2357 | 2357 |
2358 /* If necessary, do encoding-detection now. We do this when | 2358 /* If necessary, do encoding-detection now. We do this when |
2359 we're a writing stream or a non-seekable reading stream, | 2359 we're a writing stream or a non-seekable reading stream, |
2437 Return length of decoded text. | 2437 Return length of decoded text. |
2438 BUFFER defaults to the current buffer if unspecified. | 2438 BUFFER defaults to the current buffer if unspecified. |
2439 */ | 2439 */ |
2440 (start, end, coding_system, buffer)) | 2440 (start, end, coding_system, buffer)) |
2441 { | 2441 { |
2442 Bufpos b, e; | 2442 Charbpos b, e; |
2443 struct buffer *buf = decode_buffer (buffer, 0); | 2443 struct buffer *buf = decode_buffer (buffer, 0); |
2444 Lisp_Object instream, lb_outstream, de_outstream, outstream; | 2444 Lisp_Object instream, lb_outstream, de_outstream, outstream; |
2445 Lstream *istr, *ostr; | 2445 Lstream *istr, *ostr; |
2446 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | 2446 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
2447 | 2447 |
2469 */ | 2469 */ |
2470 | 2470 |
2471 while (1) | 2471 while (1) |
2472 { | 2472 { |
2473 char tempbuf[1024]; /* some random amount */ | 2473 char tempbuf[1024]; /* some random amount */ |
2474 Bufpos newpos, even_newer_pos; | 2474 Charbpos newpos, even_newer_pos; |
2475 Bufpos oldpos = lisp_buffer_stream_startpos (istr); | 2475 Charbpos oldpos = lisp_buffer_stream_startpos (istr); |
2476 Lstream_Data_Count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf)); | 2476 Bytecount size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf)); |
2477 | 2477 |
2478 if (!size_in_bytes) | 2478 if (!size_in_bytes) |
2479 break; | 2479 break; |
2480 newpos = lisp_buffer_stream_startpos (istr); | 2480 newpos = lisp_buffer_stream_startpos (istr); |
2481 Lstream_write (ostr, tempbuf, size_in_bytes); | 2481 Lstream_write (ostr, tempbuf, size_in_bytes); |
2556 used by the CCL encoder. */ | 2556 used by the CCL encoder. */ |
2557 struct ccl_program ccl; | 2557 struct ccl_program ccl; |
2558 #endif /* MULE */ | 2558 #endif /* MULE */ |
2559 }; | 2559 }; |
2560 | 2560 |
2561 static Lstream_Data_Count encoding_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size); | 2561 static Bytecount encoding_reader (Lstream *stream, unsigned char *data, Bytecount size); |
2562 static Lstream_Data_Count encoding_writer (Lstream *stream, const unsigned char *data, | 2562 static Bytecount encoding_writer (Lstream *stream, const unsigned char *data, |
2563 Lstream_Data_Count size); | 2563 Bytecount size); |
2564 static int encoding_rewinder (Lstream *stream); | 2564 static int encoding_rewinder (Lstream *stream); |
2565 static int encoding_seekable_p (Lstream *stream); | 2565 static int encoding_seekable_p (Lstream *stream); |
2566 static int encoding_flusher (Lstream *stream); | 2566 static int encoding_flusher (Lstream *stream); |
2567 static int encoding_closer (Lstream *stream); | 2567 static int encoding_closer (Lstream *stream); |
2568 | 2568 |
2590 } | 2590 } |
2591 | 2591 |
2592 /* Read SIZE bytes of data and store it into DATA. We are a encoding stream | 2592 /* Read SIZE bytes of data and store it into DATA. We are a encoding stream |
2593 so we read data from the other end, encode it, and store it into DATA. */ | 2593 so we read data from the other end, encode it, and store it into DATA. */ |
2594 | 2594 |
2595 static Lstream_Data_Count | 2595 static Bytecount |
2596 encoding_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) | 2596 encoding_reader (Lstream *stream, unsigned char *data, Bytecount size) |
2597 { | 2597 { |
2598 struct encoding_stream *str = ENCODING_STREAM_DATA (stream); | 2598 struct encoding_stream *str = ENCODING_STREAM_DATA (stream); |
2599 unsigned char *orig_data = data; | 2599 unsigned char *orig_data = data; |
2600 Lstream_Data_Count read_size; | 2600 Bytecount read_size; |
2601 int error_occurred = 0; | 2601 int error_occurred = 0; |
2602 | 2602 |
2603 /* We need to interface to mule_encode(), which expects to take some | 2603 /* We need to interface to mule_encode(), which expects to take some |
2604 amount of data and store the result into a Dynarr. We have | 2604 amount of data and store the result into a Dynarr. We have |
2605 mule_encode() store into str->runoff, and take data from there | 2605 mule_encode() store into str->runoff, and take data from there |
2652 return error_occurred ? -1 : 0; | 2652 return error_occurred ? -1 : 0; |
2653 else | 2653 else |
2654 return data - orig_data; | 2654 return data - orig_data; |
2655 } | 2655 } |
2656 | 2656 |
2657 static Lstream_Data_Count | 2657 static Bytecount |
2658 encoding_writer (Lstream *stream, const unsigned char *data, Lstream_Data_Count size) | 2658 encoding_writer (Lstream *stream, const unsigned char *data, Bytecount size) |
2659 { | 2659 { |
2660 struct encoding_stream *str = ENCODING_STREAM_DATA (stream); | 2660 struct encoding_stream *str = ENCODING_STREAM_DATA (stream); |
2661 Lstream_Data_Count retval; | 2661 Bytecount retval; |
2662 | 2662 |
2663 /* Encode all our data into the runoff, and then attempt to write | 2663 /* Encode all our data into the runoff, and then attempt to write |
2664 it all out to the other end. Remove whatever chunk we succeeded | 2664 it all out to the other end. Remove whatever chunk we succeeded |
2665 in writing. */ | 2665 in writing. */ |
2666 mule_encode (stream, data, str->runoff, size); | 2666 mule_encode (stream, data, str->runoff, size); |
2795 /* Convert N bytes of internally-formatted data stored in SRC to an | 2795 /* Convert N bytes of internally-formatted data stored in SRC to an |
2796 external format, according to the encoding stream ENCODING. | 2796 external format, according to the encoding stream ENCODING. |
2797 Store the encoded data into DST. */ | 2797 Store the encoded data into DST. */ |
2798 | 2798 |
2799 static void | 2799 static void |
2800 mule_encode (Lstream *encoding, const Bufbyte *src, | 2800 mule_encode (Lstream *encoding, const Intbyte *src, |
2801 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 2801 unsigned_char_dynarr *dst, Bytecount n) |
2802 { | 2802 { |
2803 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 2803 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
2804 | 2804 |
2805 switch (CODING_SYSTEM_TYPE (str->codesys)) | 2805 switch (CODING_SYSTEM_TYPE (str->codesys)) |
2806 { | 2806 { |
2850 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded | 2850 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded |
2851 text. BUFFER defaults to the current buffer if unspecified. | 2851 text. BUFFER defaults to the current buffer if unspecified. |
2852 */ | 2852 */ |
2853 (start, end, coding_system, buffer)) | 2853 (start, end, coding_system, buffer)) |
2854 { | 2854 { |
2855 Bufpos b, e; | 2855 Charbpos b, e; |
2856 struct buffer *buf = decode_buffer (buffer, 0); | 2856 struct buffer *buf = decode_buffer (buffer, 0); |
2857 Lisp_Object instream, lb_outstream, de_outstream, outstream; | 2857 Lisp_Object instream, lb_outstream, de_outstream, outstream; |
2858 Lstream *istr, *ostr; | 2858 Lstream *istr, *ostr; |
2859 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | 2859 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
2860 | 2860 |
2880 ------> [BUFFER] | 2880 ------> [BUFFER] |
2881 */ | 2881 */ |
2882 while (1) | 2882 while (1) |
2883 { | 2883 { |
2884 char tempbuf[1024]; /* some random amount */ | 2884 char tempbuf[1024]; /* some random amount */ |
2885 Bufpos newpos, even_newer_pos; | 2885 Charbpos newpos, even_newer_pos; |
2886 Bufpos oldpos = lisp_buffer_stream_startpos (istr); | 2886 Charbpos oldpos = lisp_buffer_stream_startpos (istr); |
2887 Lstream_Data_Count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf)); | 2887 Bytecount size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf)); |
2888 | 2888 |
2889 if (!size_in_bytes) | 2889 if (!size_in_bytes) |
2890 break; | 2890 break; |
2891 newpos = lisp_buffer_stream_startpos (istr); | 2891 newpos = lisp_buffer_stream_startpos (istr); |
2892 Lstream_write (ostr, tempbuf, size_in_bytes); | 2892 Lstream_write (ostr, tempbuf, size_in_bytes); |
2945 | 2945 |
2946 #define BYTE_SJIS_KATAKANA_P(c) \ | 2946 #define BYTE_SJIS_KATAKANA_P(c) \ |
2947 ((c) >= 0xA1 && (c) <= 0xDF) | 2947 ((c) >= 0xA1 && (c) <= 0xDF) |
2948 | 2948 |
2949 static int | 2949 static int |
2950 detect_coding_sjis (struct detection_state *st, const Extbyte *src, Lstream_Data_Count n) | 2950 detect_coding_sjis (struct detection_state *st, const Extbyte *src, Bytecount n) |
2951 { | 2951 { |
2952 while (n--) | 2952 while (n--) |
2953 { | 2953 { |
2954 unsigned char c = *(unsigned char *)src++; | 2954 unsigned char c = *(unsigned char *)src++; |
2955 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | 2955 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) |
2968 | 2968 |
2969 /* Convert Shift-JIS data to internal format. */ | 2969 /* Convert Shift-JIS data to internal format. */ |
2970 | 2970 |
2971 static void | 2971 static void |
2972 decode_coding_sjis (Lstream *decoding, const Extbyte *src, | 2972 decode_coding_sjis (Lstream *decoding, const Extbyte *src, |
2973 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 2973 unsigned_char_dynarr *dst, Bytecount n) |
2974 { | 2974 { |
2975 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 2975 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
2976 unsigned int flags = str->flags; | 2976 unsigned int flags = str->flags; |
2977 unsigned int ch = str->ch; | 2977 unsigned int ch = str->ch; |
2978 eol_type_t eol_type = str->eol_type; | 2978 eol_type_t eol_type = str->eol_type; |
3023 } | 3023 } |
3024 | 3024 |
3025 /* Convert internally-formatted data to Shift-JIS. */ | 3025 /* Convert internally-formatted data to Shift-JIS. */ |
3026 | 3026 |
3027 static void | 3027 static void |
3028 encode_coding_sjis (Lstream *encoding, const Bufbyte *src, | 3028 encode_coding_sjis (Lstream *encoding, const Intbyte *src, |
3029 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3029 unsigned_char_dynarr *dst, Bytecount n) |
3030 { | 3030 { |
3031 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 3031 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
3032 unsigned int flags = str->flags; | 3032 unsigned int flags = str->flags; |
3033 unsigned int ch = str->ch; | 3033 unsigned int ch = str->ch; |
3034 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys); | 3034 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys); |
3035 | 3035 |
3036 while (n--) | 3036 while (n--) |
3037 { | 3037 { |
3038 Bufbyte c = *src++; | 3038 Intbyte c = *src++; |
3039 if (c == '\n') | 3039 if (c == '\n') |
3040 { | 3040 { |
3041 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT) | 3041 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT) |
3042 Dynarr_add (dst, '\r'); | 3042 Dynarr_add (dst, '\r'); |
3043 if (eol_type != EOL_CR) | 3043 if (eol_type != EOL_CR) |
3047 else if (BYTE_ASCII_P (c)) | 3047 else if (BYTE_ASCII_P (c)) |
3048 { | 3048 { |
3049 Dynarr_add (dst, c); | 3049 Dynarr_add (dst, c); |
3050 ch = 0; | 3050 ch = 0; |
3051 } | 3051 } |
3052 else if (BUFBYTE_LEADING_BYTE_P (c)) | 3052 else if (INTBYTE_LEADING_BYTE_P (c)) |
3053 ch = (c == LEADING_BYTE_KATAKANA_JISX0201 || | 3053 ch = (c == LEADING_BYTE_KATAKANA_JISX0201 || |
3054 c == LEADING_BYTE_JAPANESE_JISX0208_1978 || | 3054 c == LEADING_BYTE_JAPANESE_JISX0208_1978 || |
3055 c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0; | 3055 c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0; |
3056 else if (ch) | 3056 else if (ch) |
3057 { | 3057 { |
3226 b2 = I % BIG5_SAME_ROW; \ | 3226 b2 = I % BIG5_SAME_ROW; \ |
3227 b2 += b2 < 0x3F ? 0x40 : 0x62; \ | 3227 b2 += b2 < 0x3F ? 0x40 : 0x62; \ |
3228 } while (0) | 3228 } while (0) |
3229 | 3229 |
3230 static int | 3230 static int |
3231 detect_coding_big5 (struct detection_state *st, const Extbyte *src, Lstream_Data_Count n) | 3231 detect_coding_big5 (struct detection_state *st, const Extbyte *src, Bytecount n) |
3232 { | 3232 { |
3233 while (n--) | 3233 while (n--) |
3234 { | 3234 { |
3235 unsigned char c = *(unsigned char *)src++; | 3235 unsigned char c = *(unsigned char *)src++; |
3236 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO || | 3236 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO || |
3250 | 3250 |
3251 /* Convert Big5 data to internal format. */ | 3251 /* Convert Big5 data to internal format. */ |
3252 | 3252 |
3253 static void | 3253 static void |
3254 decode_coding_big5 (Lstream *decoding, const Extbyte *src, | 3254 decode_coding_big5 (Lstream *decoding, const Extbyte *src, |
3255 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3255 unsigned_char_dynarr *dst, Bytecount n) |
3256 { | 3256 { |
3257 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 3257 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
3258 unsigned int flags = str->flags; | 3258 unsigned int flags = str->flags; |
3259 unsigned int ch = str->ch; | 3259 unsigned int ch = str->ch; |
3260 eol_type_t eol_type = str->eol_type; | 3260 eol_type_t eol_type = str->eol_type; |
3298 } | 3298 } |
3299 | 3299 |
3300 /* Convert internally-formatted data to Big5. */ | 3300 /* Convert internally-formatted data to Big5. */ |
3301 | 3301 |
3302 static void | 3302 static void |
3303 encode_coding_big5 (Lstream *encoding, const Bufbyte *src, | 3303 encode_coding_big5 (Lstream *encoding, const Intbyte *src, |
3304 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3304 unsigned_char_dynarr *dst, Bytecount n) |
3305 { | 3305 { |
3306 unsigned char c; | 3306 unsigned char c; |
3307 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 3307 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
3308 unsigned int flags = str->flags; | 3308 unsigned int flags = str->flags; |
3309 unsigned int ch = str->ch; | 3309 unsigned int ch = str->ch; |
3322 else if (BYTE_ASCII_P (c)) | 3322 else if (BYTE_ASCII_P (c)) |
3323 { | 3323 { |
3324 /* ASCII. */ | 3324 /* ASCII. */ |
3325 Dynarr_add (dst, c); | 3325 Dynarr_add (dst, c); |
3326 } | 3326 } |
3327 else if (BUFBYTE_LEADING_BYTE_P (c)) | 3327 else if (INTBYTE_LEADING_BYTE_P (c)) |
3328 { | 3328 { |
3329 if (c == LEADING_BYTE_CHINESE_BIG5_1 || | 3329 if (c == LEADING_BYTE_CHINESE_BIG5_1 || |
3330 c == LEADING_BYTE_CHINESE_BIG5_2) | 3330 c == LEADING_BYTE_CHINESE_BIG5_2) |
3331 { | 3331 { |
3332 /* A recognized leading byte. */ | 3332 /* A recognized leading byte. */ |
3503 { | 3503 { |
3504 Lisp_Object chr = ucs_to_char (ch); | 3504 Lisp_Object chr = ucs_to_char (ch); |
3505 | 3505 |
3506 if (! NILP (chr)) | 3506 if (! NILP (chr)) |
3507 { | 3507 { |
3508 Bufbyte work[MAX_EMCHAR_LEN]; | 3508 Intbyte work[MAX_EMCHAR_LEN]; |
3509 int len; | 3509 int len; |
3510 | 3510 |
3511 ch = XCHAR (chr); | 3511 ch = XCHAR (chr); |
3512 len = (ch < 128) ? | 3512 len = (ch < 128) ? |
3513 simple_set_charptr_emchar (work, ch) : | 3513 simple_set_charptr_emchar (work, ch) : |
3565 Dynarr_add (dst, (code >> 8) & 255); | 3565 Dynarr_add (dst, (code >> 8) & 255); |
3566 Dynarr_add (dst, code & 255); | 3566 Dynarr_add (dst, code & 255); |
3567 } | 3567 } |
3568 | 3568 |
3569 static int | 3569 static int |
3570 detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, Lstream_Data_Count n) | 3570 detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, Bytecount n) |
3571 { | 3571 { |
3572 while (n--) | 3572 while (n--) |
3573 { | 3573 { |
3574 unsigned char c = *(unsigned char *)src++; | 3574 unsigned char c = *(unsigned char *)src++; |
3575 switch (st->ucs4.in_byte) | 3575 switch (st->ucs4.in_byte) |
3590 return CODING_CATEGORY_UCS4_MASK; | 3590 return CODING_CATEGORY_UCS4_MASK; |
3591 } | 3591 } |
3592 | 3592 |
3593 static void | 3593 static void |
3594 decode_coding_ucs4 (Lstream *decoding, const Extbyte *src, | 3594 decode_coding_ucs4 (Lstream *decoding, const Extbyte *src, |
3595 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3595 unsigned_char_dynarr *dst, Bytecount n) |
3596 { | 3596 { |
3597 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 3597 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
3598 unsigned int flags = str->flags; | 3598 unsigned int flags = str->flags; |
3599 unsigned int ch = str->ch; | 3599 unsigned int ch = str->ch; |
3600 unsigned char counter = str->counter; | 3600 unsigned char counter = str->counter; |
3625 str->ch = ch; | 3625 str->ch = ch; |
3626 str->counter = counter; | 3626 str->counter = counter; |
3627 } | 3627 } |
3628 | 3628 |
3629 static void | 3629 static void |
3630 encode_coding_ucs4 (Lstream *encoding, const Bufbyte *src, | 3630 encode_coding_ucs4 (Lstream *encoding, const Intbyte *src, |
3631 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3631 unsigned_char_dynarr *dst, Bytecount n) |
3632 { | 3632 { |
3633 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 3633 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
3634 unsigned int flags = str->flags; | 3634 unsigned int flags = str->flags; |
3635 unsigned int ch = str->ch; | 3635 unsigned int ch = str->ch; |
3636 unsigned char char_boundary = str->iso2022.current_char_boundary; | 3636 unsigned char char_boundary = str->iso2022.current_char_boundary; |
3637 Lisp_Object charset = str->iso2022.current_charset; | 3637 Lisp_Object charset = str->iso2022.current_charset; |
3638 | 3638 |
3639 #ifdef ENABLE_COMPOSITE_CHARS | 3639 #ifdef ENABLE_COMPOSITE_CHARS |
3640 /* flags for handling composite chars. We do a little switcharoo | 3640 /* flags for handling composite chars. We do a little switcharoo |
3641 on the source while we're outputting the composite char. */ | 3641 on the source while we're outputting the composite char. */ |
3642 Lstream_Data_Count saved_n = 0; | 3642 Bytecount saved_n = 0; |
3643 const unsigned char *saved_src = NULL; | 3643 const unsigned char *saved_src = NULL; |
3644 int in_composite = 0; | 3644 int in_composite = 0; |
3645 | 3645 |
3646 back_to_square_n: | 3646 back_to_square_n: |
3647 #endif | 3647 #endif |
3654 { /* Processing ASCII character */ | 3654 { /* Processing ASCII character */ |
3655 ch = 0; | 3655 ch = 0; |
3656 encode_ucs4 (Vcharset_ascii, c, 0, dst); | 3656 encode_ucs4 (Vcharset_ascii, c, 0, dst); |
3657 char_boundary = 1; | 3657 char_boundary = 1; |
3658 } | 3658 } |
3659 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch)) | 3659 else if (INTBYTE_LEADING_BYTE_P (c) || INTBYTE_LEADING_BYTE_P (ch)) |
3660 { /* Processing Leading Byte */ | 3660 { /* Processing Leading Byte */ |
3661 ch = 0; | 3661 ch = 0; |
3662 charset = CHARSET_BY_LEADING_BYTE (c); | 3662 charset = CHARSET_BY_LEADING_BYTE (c); |
3663 if (LEADING_BYTE_PREFIX_P(c)) | 3663 if (LEADING_BYTE_PREFIX_P(c)) |
3664 ch = c; | 3664 ch = c; |
3764 /************************************************************************/ | 3764 /************************************************************************/ |
3765 /* UTF-8 methods */ | 3765 /* UTF-8 methods */ |
3766 /************************************************************************/ | 3766 /************************************************************************/ |
3767 | 3767 |
3768 static int | 3768 static int |
3769 detect_coding_utf8 (struct detection_state *st, const Extbyte *src, Lstream_Data_Count n) | 3769 detect_coding_utf8 (struct detection_state *st, const Extbyte *src, Bytecount n) |
3770 { | 3770 { |
3771 while (n--) | 3771 while (n--) |
3772 { | 3772 { |
3773 unsigned char c = *(unsigned char *)src++; | 3773 unsigned char c = *(unsigned char *)src++; |
3774 switch (st->utf8.in_byte) | 3774 switch (st->utf8.in_byte) |
3799 return CODING_CATEGORY_UTF8_MASK; | 3799 return CODING_CATEGORY_UTF8_MASK; |
3800 } | 3800 } |
3801 | 3801 |
3802 static void | 3802 static void |
3803 decode_coding_utf8 (Lstream *decoding, const Extbyte *src, | 3803 decode_coding_utf8 (Lstream *decoding, const Extbyte *src, |
3804 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3804 unsigned_char_dynarr *dst, Bytecount n) |
3805 { | 3805 { |
3806 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 3806 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
3807 unsigned int flags = str->flags; | 3807 unsigned int flags = str->flags; |
3808 unsigned int ch = str->ch; | 3808 unsigned int ch = str->ch; |
3809 eol_type_t eol_type = str->eol_type; | 3809 eol_type_t eol_type = str->eol_type; |
3912 Dynarr_add (dst, (code & 0x3f) | 0x80); | 3912 Dynarr_add (dst, (code & 0x3f) | 0x80); |
3913 } | 3913 } |
3914 } | 3914 } |
3915 | 3915 |
3916 static void | 3916 static void |
3917 encode_coding_utf8 (Lstream *encoding, const Bufbyte *src, | 3917 encode_coding_utf8 (Lstream *encoding, const Intbyte *src, |
3918 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 3918 unsigned_char_dynarr *dst, Bytecount n) |
3919 { | 3919 { |
3920 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 3920 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
3921 unsigned int flags = str->flags; | 3921 unsigned int flags = str->flags; |
3922 unsigned int ch = str->ch; | 3922 unsigned int ch = str->ch; |
3923 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys); | 3923 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys); |
3925 Lisp_Object charset = str->iso2022.current_charset; | 3925 Lisp_Object charset = str->iso2022.current_charset; |
3926 | 3926 |
3927 #ifdef ENABLE_COMPOSITE_CHARS | 3927 #ifdef ENABLE_COMPOSITE_CHARS |
3928 /* flags for handling composite chars. We do a little switcharoo | 3928 /* flags for handling composite chars. We do a little switcharoo |
3929 on the source while we're outputting the composite char. */ | 3929 on the source while we're outputting the composite char. */ |
3930 Lstream_Data_Count saved_n = 0; | 3930 Bytecount saved_n = 0; |
3931 const unsigned char *saved_src = NULL; | 3931 const unsigned char *saved_src = NULL; |
3932 int in_composite = 0; | 3932 int in_composite = 0; |
3933 | 3933 |
3934 back_to_square_n: | 3934 back_to_square_n: |
3935 #endif /* ENABLE_COMPOSITE_CHARS */ | 3935 #endif /* ENABLE_COMPOSITE_CHARS */ |
3950 } | 3950 } |
3951 else | 3951 else |
3952 encode_utf8 (Vcharset_ascii, c, 0, dst); | 3952 encode_utf8 (Vcharset_ascii, c, 0, dst); |
3953 char_boundary = 1; | 3953 char_boundary = 1; |
3954 } | 3954 } |
3955 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch)) | 3955 else if (INTBYTE_LEADING_BYTE_P (c) || INTBYTE_LEADING_BYTE_P (ch)) |
3956 { /* Processing Leading Byte */ | 3956 { /* Processing Leading Byte */ |
3957 ch = 0; | 3957 ch = 0; |
3958 charset = CHARSET_BY_LEADING_BYTE (c); | 3958 charset = CHARSET_BY_LEADING_BYTE (c); |
3959 if (LEADING_BYTE_PREFIX_P(c)) | 3959 if (LEADING_BYTE_PREFIX_P(c)) |
3960 ch = c; | 3960 ch = c; |
4672 iso->switched_dir_and_no_valid_charset_yet = 0; | 4672 iso->switched_dir_and_no_valid_charset_yet = 0; |
4673 return 1; | 4673 return 1; |
4674 } | 4674 } |
4675 | 4675 |
4676 static int | 4676 static int |
4677 detect_coding_iso2022 (struct detection_state *st, const Extbyte *src, Lstream_Data_Count n) | 4677 detect_coding_iso2022 (struct detection_state *st, const Extbyte *src, Bytecount n) |
4678 { | 4678 { |
4679 int mask; | 4679 int mask; |
4680 | 4680 |
4681 /* #### There are serious deficiencies in the recognition mechanism | 4681 /* #### There are serious deficiencies in the recognition mechanism |
4682 here. This needs to be much smarter if it's going to cut it. | 4682 here. This needs to be much smarter if it's going to cut it. |
4863 | 4863 |
4864 /* Convert ISO2022-format data to internal format. */ | 4864 /* Convert ISO2022-format data to internal format. */ |
4865 | 4865 |
4866 static void | 4866 static void |
4867 decode_coding_iso2022 (Lstream *decoding, const Extbyte *src, | 4867 decode_coding_iso2022 (Lstream *decoding, const Extbyte *src, |
4868 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 4868 unsigned_char_dynarr *dst, Bytecount n) |
4869 { | 4869 { |
4870 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 4870 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
4871 unsigned int flags = str->flags; | 4871 unsigned int flags = str->flags; |
4872 unsigned int ch = str->ch; | 4872 unsigned int ch = str->ch; |
4873 eol_type_t eol_type = str->eol_type; | 4873 eol_type_t eol_type = str->eol_type; |
4903 str->iso2022.composite_chars = Dynarr_new (unsigned_char); | 4903 str->iso2022.composite_chars = Dynarr_new (unsigned_char); |
4904 dst = str->iso2022.composite_chars; | 4904 dst = str->iso2022.composite_chars; |
4905 break; | 4905 break; |
4906 case ISO_ESC_END_COMPOSITE: | 4906 case ISO_ESC_END_COMPOSITE: |
4907 { | 4907 { |
4908 Bufbyte comstr[MAX_EMCHAR_LEN]; | 4908 Intbyte comstr[MAX_EMCHAR_LEN]; |
4909 Bytecount len; | 4909 Bytecount len; |
4910 Emchar emch = lookup_composite_char (Dynarr_atp (dst, 0), | 4910 Emchar emch = lookup_composite_char (Dynarr_atp (dst, 0), |
4911 Dynarr_length (dst)); | 4911 Dynarr_length (dst)); |
4912 dst = real_dst; | 4912 dst = real_dst; |
4913 len = set_charptr_emchar (comstr, emch); | 4913 len = set_charptr_emchar (comstr, emch); |
5188 } | 5188 } |
5189 | 5189 |
5190 /* Convert internally-formatted data to ISO2022 format. */ | 5190 /* Convert internally-formatted data to ISO2022 format. */ |
5191 | 5191 |
5192 static void | 5192 static void |
5193 encode_coding_iso2022 (Lstream *encoding, const Bufbyte *src, | 5193 encode_coding_iso2022 (Lstream *encoding, const Intbyte *src, |
5194 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 5194 unsigned_char_dynarr *dst, Bytecount n) |
5195 { | 5195 { |
5196 unsigned char charmask, c; | 5196 unsigned char charmask, c; |
5197 unsigned char char_boundary; | 5197 unsigned char char_boundary; |
5198 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 5198 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
5199 unsigned int flags = str->flags; | 5199 unsigned int flags = str->flags; |
5205 int half; | 5205 int half; |
5206 | 5206 |
5207 #ifdef ENABLE_COMPOSITE_CHARS | 5207 #ifdef ENABLE_COMPOSITE_CHARS |
5208 /* flags for handling composite chars. We do a little switcharoo | 5208 /* flags for handling composite chars. We do a little switcharoo |
5209 on the source while we're outputting the composite char. */ | 5209 on the source while we're outputting the composite char. */ |
5210 Lstream_Data_Count saved_n = 0; | 5210 Bytecount saved_n = 0; |
5211 const unsigned char *saved_src = NULL; | 5211 const unsigned char *saved_src = NULL; |
5212 int in_composite = 0; | 5212 int in_composite = 0; |
5213 #endif /* ENABLE_COMPOSITE_CHARS */ | 5213 #endif /* ENABLE_COMPOSITE_CHARS */ |
5214 | 5214 |
5215 char_boundary = str->iso2022.current_char_boundary; | 5215 char_boundary = str->iso2022.current_char_boundary; |
5268 Dynarr_add (dst, c); | 5268 Dynarr_add (dst, c); |
5269 } | 5269 } |
5270 char_boundary = 1; | 5270 char_boundary = 1; |
5271 } | 5271 } |
5272 | 5272 |
5273 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch)) | 5273 else if (INTBYTE_LEADING_BYTE_P (c) || INTBYTE_LEADING_BYTE_P (ch)) |
5274 { /* Processing Leading Byte */ | 5274 { /* Processing Leading Byte */ |
5275 ch = 0; | 5275 ch = 0; |
5276 charset = CHARSET_BY_LEADING_BYTE (c); | 5276 charset = CHARSET_BY_LEADING_BYTE (c); |
5277 if (LEADING_BYTE_PREFIX_P(c)) | 5277 if (LEADING_BYTE_PREFIX_P(c)) |
5278 ch = c; | 5278 ch = c; |
5498 /* This is used when reading in "binary" files -- i.e. files that may | 5498 /* This is used when reading in "binary" files -- i.e. files that may |
5499 contain all 256 possible byte values and that are not to be | 5499 contain all 256 possible byte values and that are not to be |
5500 interpreted as being in any particular decoding. */ | 5500 interpreted as being in any particular decoding. */ |
5501 static void | 5501 static void |
5502 decode_coding_no_conversion (Lstream *decoding, const Extbyte *src, | 5502 decode_coding_no_conversion (Lstream *decoding, const Extbyte *src, |
5503 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 5503 unsigned_char_dynarr *dst, Bytecount n) |
5504 { | 5504 { |
5505 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); | 5505 struct decoding_stream *str = DECODING_STREAM_DATA (decoding); |
5506 unsigned int flags = str->flags; | 5506 unsigned int flags = str->flags; |
5507 unsigned int ch = str->ch; | 5507 unsigned int ch = str->ch; |
5508 eol_type_t eol_type = str->eol_type; | 5508 eol_type_t eol_type = str->eol_type; |
5521 str->flags = flags; | 5521 str->flags = flags; |
5522 str->ch = ch; | 5522 str->ch = ch; |
5523 } | 5523 } |
5524 | 5524 |
5525 static void | 5525 static void |
5526 encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src, | 5526 encode_coding_no_conversion (Lstream *encoding, const Intbyte *src, |
5527 unsigned_char_dynarr *dst, Lstream_Data_Count n) | 5527 unsigned_char_dynarr *dst, Bytecount n) |
5528 { | 5528 { |
5529 unsigned char c; | 5529 unsigned char c; |
5530 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); | 5530 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding); |
5531 unsigned int flags = str->flags; | 5531 unsigned int flags = str->flags; |
5532 unsigned int ch = str->ch; | 5532 unsigned int ch = str->ch; |
5546 else if (BYTE_ASCII_P (c)) | 5546 else if (BYTE_ASCII_P (c)) |
5547 { | 5547 { |
5548 assert (ch == 0); | 5548 assert (ch == 0); |
5549 Dynarr_add (dst, c); | 5549 Dynarr_add (dst, c); |
5550 } | 5550 } |
5551 else if (BUFBYTE_LEADING_BYTE_P (c)) | 5551 else if (INTBYTE_LEADING_BYTE_P (c)) |
5552 { | 5552 { |
5553 assert (ch == 0); | 5553 assert (ch == 0); |
5554 if (c == LEADING_BYTE_LATIN_ISO8859_1 || | 5554 if (c == LEADING_BYTE_LATIN_ISO8859_1 || |
5555 c == LEADING_BYTE_CONTROL_1) | 5555 c == LEADING_BYTE_CONTROL_1) |
5556 ch = c; | 5556 ch = c; |