Sie sind auf Seite 1von 3

OPS435C Assignment 2

2014 Summer
Requirement:
Preamble:
When sending information across the Internet, blocks of data are 'framed' by
the transmitter, so that they can be recognized by the receiver. Each 'frame' must
begin and end with a special pattern. The problem is that any pattern that is used
to mark the start and end of a frame might appear as legitimate data within the
block, thus confusing the receiver. One way to overcome this problem is to use a
technique known as 'bit stuffing'. Using this method, each data block is 'framed' at
each end with the 8 bit (1 byte) pattern, 01111110, and the data is modified to
ensure that 6 one bits never appear in sequence. This is accomplished by
inserting a zero bit after any sequence of 5 ones. The receiver will then remove
the extra zero bits after it receives the frame.
Amble:
Bit stuffing works on bits within a stream of bytes. Here are several
examples of frames:
Example 1: Empty Frame (only 2 framing bytes, no data) - in ASCII this is
the string "~~".
0111111001111110
Example 2: Four byte frame (2 framing bytes plus 2 data bytes) NOTE:
This frame is unstuffed. In ASCII this is the string "~AB~"
01111110010000010100001001111110
Example 3: Five byte frame (2 framing bytes plus 3 data bytes) NOTE:
This frame is unstuffed. In ASCII this is the string "~AB?~"
0111111001000001010000100011111101111110
Example 4: Five byte frame (2 framing bytes plus 4 data bytes) NOTE:
This frame is stuffed with one zero bit (yellow). In ASCII this is the string
"~AB>~". Also NOTE the Euro currency symbol which is part of the
extended ASCII table, 128 in decimal or 80 in hex.
011111100100000101000010001111101000000001111110
For this assignment you are to code a BASH script named "frame".
Your frame script should accept one required parameter, -s for stuff or -u for
unstuff and two required parameters which are the names of the input and output
files.
For example, suppose you had a file containing an unstuffed frame. Suppose
its name was unstuffed.txt. You could then run this command to bit stuff the frame:
frame -s unstuffed.txt stuffed.txt
In which case the script would read unstuffed.txt and write to stuffed.txt
Here is an example for unstuffing a frame:
frame -u stuffed.txt unstuffed.txt
In which case the script would read stuffed.txt and write to unstuffed.txt
Of course, if incorrect parameters are given you might get something like this:
frame -p stuffed.txt
Usage: frame -s|-u infile outfile
Postamble:
NOTE 0: You should ignore the framing bytes (01111110 or '~') for this
assignment. In other words do not add them onto your stuffed file.
Similarly you should not need to remove them when un-stuffing a file
since they would not be there in the first place.
NOTE 1: Your script should output a usage statement or error message if
any invalid parameters are used on the command line.
NOTE 2: This assignment works on bits so it's important to be
comfortable with BASH bitwise operators before beginning this
assignment.
NOTE 3: Be sure you follow your teacher's published assignment
expectations when preparing and submitting your work.



















Solution:
# !/bin/bash

# Check number of parameter, and output prompt
if [ $# -ne 3 ]; then
echo -e "Usage: frame -s|-u infile outfile" >&2
exit 1
fi
# Check the status of source file
if [ ! -f $2 ];then
echo -e "File does not exist or is not readable: $2" >&2
exit 2
fi
# Execute stuffing feature
if [ $1 = "-s" ]; then
xxd -b -g0 $2 | cut -c10-57 | tr -d '\n'| tr -d ' '| sed 's/11111/111110/g'> $3
exit 0
# Execute stuffing feature
elif [ $1 = "-u" ]; then
# Check the content of source file, it should have only 0 and 1
if [[ ! $(cat $2) =~ ^[01]+$ ]]; then
echo -e "$2: Content is wrong. Exit" >&2
exit 3
fi
String=$(cat $2 | sed 's/111110/11111/g' | tr -d '\n')
# Check the length of string, it should be a multiple of 8
if [ $[$(($(echo $String | wc -c) -1)) % 8] -ne 0 ]; then
echo -e "$2: Size is wrong. Exit" >&2
exit 4
# Execute unstuffing feature
else
echo $String | tr -d '\n' | perl -e 'print pack "B*", <>' > $3
exit 0
fi
# Output prompt for wrong parameter
else
echo -e "Usage: frame -s|-u infile outfile" >&2
exit 1
fi

Das könnte Ihnen auch gefallen