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} {} rsdecof (@var{in},@var{out})
18 ## @deftypefnx {Function File} {} rsdecof (@var{in},@var{out},@var{t})
20 ## Decodes an ascii file using a Reed-Solomon coder. The input file is
21 ## defined by @var{in} and the result is written to the output file @var{out}.
22 ## The type of coding to use is determined by whether the input file is 7-
23 ## or 8-bit. If the input file is 7-bit, the default coding is [127,117].
24 ## while the default coding for an 8-bit file is a [255, 235]. This allows
25 ## for 5 or 10 error characters in 127 or 255 symbols to be corrected
26 ## respectively. The number of errors that can be corrected can be overridden
27 ## by the variable @var{t}.
29 ## If the file is not an integer multiple of the message size (127 or 255)
30 ## in length, then the file is padded with the EOT (ascii character 4)
31 ## character before decoding.
36 function rsdecof(in, out, t)
38 if ((nargin < 2) || (nargin > 3))
39 usage("rsdecof (in, out [, t])");
42 if (!ischar(in) || !ischar(out))
43 error ("rsdecof: input and output filenames must be strings");
49 if (!isscalar(t) || (t != floor(d)) || (t < 1))
50 error ("rsdecof: t must be a postive, non-zero integer");
54 try fid = fopen(in, "rt");
56 error ("rsdecof: can not open input file");
58 [code, count] = fread(fid, Inf, "char");
61 is8bit = (max(code) > 127);
78 ncodewords = ceil(count / n);
79 npad = n * ncodewords - count;
80 code = reshape([code ; 4 * ones(npad,1)],n,ncodewords)';
82 msg = rsdec(gf(code,m), n, k, "beginning")';
85 try fid = fopen(out, "w+t");
87 error ("rsdecof; can not open output file");
89 fwrite(fid, msg(1:(ncodewords*k-npad)), "char");