Design and Implementation of a Documentation Tool for ... - MADOC
Design and Implementation of a Documentation Tool for ... - MADOC
Design and Implementation of a Documentation Tool for ... - MADOC
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
A.4 Magic Number<br />
Since a <strong>for</strong>script file has to start with a file version chunk followed by a begin<br />
<strong>of</strong> session chunk, there is a distinctive eight-byte signature at the beginning <strong>of</strong><br />
each file:<br />
0x0e 0x0e 0x01 0x?? 0x0f 0x0e 0x0e 0x02<br />
The first two bytes start a metadata chunk, the third one identifies it as a file<br />
version chunk. The fourth byte contains the version number, which is currently<br />
0x01 but may change in the future. Byte 5 closes the version chunk, 5 to 8 start<br />
a begin <strong>of</strong> session chunk.<br />
A.5 Writing Metadata Chunks to Disk<br />
The function chunkwd() takes a pointer <strong>and</strong> a byte count as arguments <strong>and</strong><br />
writes chunk data to the transcript file, applying required escapes on the fly. To<br />
improve per<strong>for</strong>mance, it does not write byte-by-byte, but instead scans the input<br />
data until it finds a special character. When it does, it writes everything up to,<br />
but not including, the special character to the file <strong>and</strong> then adds a DLE character.<br />
The search then goes on. If another special character is found, everything<br />
from the last special character (inclusive) to the current one (exclusive) plus a<br />
DLE is written. Eventually the whole input data will have been scanned <strong>and</strong><br />
the function terminates after writing everything from the last special character<br />
(inclusive) or the beginning <strong>of</strong> the data (if there were no special characters) to<br />
the end <strong>of</strong> the input data. This is the code:<br />
21 〈chunkw 21〉≡ (15c)<br />
int chunkwd(unsigned char *data, int count) {<br />
int escaped = 0;<br />
int pos = 0;<br />
int start = 0;<br />
while (pos < count) {<br />
if (data[pos] = ESCMIN) {<br />
if (pos > start) {<br />
if (!swrite(&data[start], size<strong>of</strong>(char),<br />
pos - start, OUTF))<br />
return -1;<br />
}<br />
if (!swrite(&DLE, size<strong>of</strong>(DLE), 1, OUTF))<br />
return -2;<br />
start = pos;<br />
escaped++;<br />
}<br />
pos++;<br />
}<br />
if (!swrite(&data[start], size<strong>of</strong>(char),<br />
pos - start, OUTF))<br />
return -3;<br />
return escaped;<br />
21