📄 encoder.c
字号:
#include "io.h"/* Test data */char data[] = "\nFrom \"The Cathedral and the Bazaar\"\n\n" \ "Linux is subversive. Who would have thought even five years ago (1991)\n" \ "that a world-class operating system could coalesce as if by magic out\n" \ "of part-time hacking by several thousand developers scattered all over\n" \ "the planet, connected only by the tenuous strands of the Internet?\n\n"\ "\t\t -- ESR\n";/* The encodeH takes an in-block of words (a row of bits for a bitstream * in row major order) and produces a corresponding out-block with N * Hamming control words inserted for correction of single bit errors. * A total of N control words are inserted, so the input block must be * 2**N -1 - N words long. */void encodeH(unsigned int in [], unsigned int out[],int N){ unsigned int j=0, /* The C index of the next code word to be inserted */ i, /* The C index of the output word to be filled */ k, /* The value of the last data word inserted in out */ q, /* The C index of a code word */ p=0; /* Count of code words used */ for (i=0; p<=N; i++) if (i==j) { out[i] = 0; j += j+1 ; p++; } /* Add new code word */ else { /* Find contributions to previous code words */ q = 0; k = out[i] = in[i-p]; /* A data word */ while (q<=j) { if ((q+1) & (i+1)) out[q] ^= k; /* Renew control word */ q += q+1; } }}int getdata(void *buf, int count) { static char* dataptr = data; int i; for (i=0; i<count; i++) { if ((unsigned int) (dataptr-data) >= sizeof(data)-1) break; *((unsigned char*) buf + i) = *dataptr++; } return i;}void encodeAndCopy(int codeblock, int datablock, int N){ int inbuffer[datablock], // data to be read and encoded outbuffer[codeblock], // encoded data to be written wanted, // count of bytes to be read count; // count of bytes actually read char *inptr; // pointer to next free in inbuffer do { // read a block of test data, encode it, and write it to stdout inptr = (char*)inbuffer; wanted = datablock*sizeof(int); while (wanted && 0 < (count = getdata(inptr, wanted))) { wanted -= count; inptr += count; } if (count > 0) { // the ordinary case: a whole block has been read encodeH(inbuffer,outbuffer,N); write(IO_FD3, outbuffer, codeblock*sizeof(int)); // write(IO_FD3, inbuffer, datablock*sizeof(int)); continue; } if (count == 0) { // end-of file has occurred // the last word written should tell how many // trailing bytes have been added if (wanted > 3) { count = wanted-sizeof(int); while (count--) *(inptr++) = 0; inbuffer[datablock-1] = wanted; encodeH(inbuffer,outbuffer,N); write(IO_FD3,outbuffer,codeblock*sizeof(int)); // write(IO_FD3,inbuffer,datablock*sizeof(int)); break; } // not enough room for the count // so a new, otherwise empty block must be generated count = wanted; while (count--) *(inptr++) = 0; encodeH(inbuffer,outbuffer,N); write(IO_FD3,outbuffer,codeblock*sizeof(int)); // write(IO_FD3,inbuffer,datablock*sizeof(int)); inbuffer[datablock-1] = wanted+datablock*sizeof(int); count = datablock-2; while (count) inbuffer[count--] = 0; encodeH(inbuffer, outbuffer, N); write(IO_FD3, outbuffer, codeblock*sizeof(int)); // write(IO_FD3, inbuffer, datablock*sizeof(int)); break; } } while (1); // All done}int main(void) { int N, // The number of auxiliary bit in a Hamming code. codeblock, // The length of an encoded block datablock; // The length of a block of data N = 5; codeblock = (1<<N) - 1; datablock = codeblock - N; encodeAndCopy(codeblock, datablock, N); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -