1 ## Copyright (C) 2003 David Bateman
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {} rsencof (@var{in},@var{out})
18 ## @deftypefnx {Function File} {} rsencof (@var{in},@var{out},@var{t})
19 ## @deftypefnx {Function File} {} rsencof (@var{...},@var{pad})
21 ## Encodes an ascii file using a Reed-Solomon coder. The input file is
22 ## defined by @var{in} and the result is written to the output file @var{out}.
23 ## The type of coding to use is determined by whether the input file is 7-
24 ## or 8-bit. If the input file is 7-bit, the default coding is [127,117].
25 ## while the default coding for an 8-bit file is a [255, 235]. This allows
26 ## for 5 or 10 error characters in 127 or 255 symbols to be corrected
27 ## respectively. The number of errors that can be corrected can be overridden
28 ## by the variable @var{t}.
30 ## If the file is not an integer multiple of the message size (127 or 255)
31 ## in length, then the file is padded with the EOT (ascii character 4)
32 ## characters before coding. Whether these characters are written to the
33 ## output is defined by the @var{pad} variable. Valid values for @var{pad}
34 ## are "pad" (the default) and "nopad", which write or not the padding
40 function rsencof(in, out, varargin)
42 if ((nargin < 2) || (nargin > 4))
43 usage("rsencof (in, out [, t [, pad]])");
46 if (!ischar(in) || !ischar(out))
47 error ("rsencof: input and output filenames must be strings");
52 for i=1:length(varargin)
55 if (strcmp(arg,"pad"))
57 elseif (strcmp(arg,"nopad"))
60 error ("rsencof: unrecognized string argument");
63 if (!isscalar(t) || (t != floor(d)) || (t < 1))
64 error ("rsencof: t must be a postive, non-zero integer");
69 try fid = fopen(in, "r");
71 error ("rsencof: can not open input file");
73 [msg, count] = fread(fid, Inf, "char");
76 is8bit = (max(msg) > 127);
93 ncodewords = ceil(count / k);
94 npad = k * ncodewords - count;
95 msg = reshape([msg ; 4 * ones(npad,1)],k,ncodewords)';
97 code = rsenc(gf(msg,m), n, k,"beginning")';
100 try fid = fopen(out, "w");
102 error ("rsencof: can not open output file");
105 fwrite(fid, code, "char");
107 fwrite(fid, code(1:(ncodewords*n-npad)), "char");