Source code: com/synchrona/jred/irlap/Crc.java
1 /*
2 **************************************************************************
3 ** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/irlap/Crc.java,v 1.1.1.1 2000/07/05 04:41:52 mpatters Exp $
4 **
5 ** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
6 **
7 ** This file is part of JRed, a 100% Java implementation of the IrDA
8 ** infrared communications protocols.
9 **
10 ** This file may be distributed under the terms of the Synchrona Public
11 ** License as defined by Synchrona, Inc. and appearing in the file
12 ** LICENSE included in the packaging of this file. The Synchrona Public
13 ** License is based on the Q Public License as defined by Troll Tech AS
14 ** of Norway; it differs only in its use of the courts of Florida, USA
15 ** rather than those of Oslo, Norway.
16 **************************************************************************
17 */
18 package com.synchrona.jred.irlap;
19
20 import java.io.FileOutputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24
25 /**
26 * Generate a 16-bit CRC-CCITT
27 */
28 class Crc {
29 public static final int HDLC_POLYNOMIAL = 0x8408;
30
31 public static void main(String [] astrArgs) {
32 try {
33 PrintWriter p = new PrintWriter(new FileOutputStream("CrcTable.java"));
34 Crc.writeTable(p);
35 p.close();
36 } catch ( Exception e ) {
37 System.err.println(e);
38 e.printStackTrace(System.err);
39 }
40 }
41
42 public static void writeTable(PrintWriter out) throws IOException {
43 out.println("public class CrcTable {");
44 out.print("\tpublic static short ayFcsTable[] = {");
45
46 int b;
47 int i;
48 int v;
49 for ( b = 0; ; ) {
50 if ( 0 == (b % 8) )
51 out.print("\n\t\t");
52
53 v = b;
54 for ( i = 8; i-- > 0; ) {
55
56 if ( 0 != (v & 1) ) {
57 v = (v >> 1) ^ HDLC_POLYNOMIAL;
58 } else {
59 v = v >> 1;
60 }
61 }
62 out.print("(short) " + (v & 0xFFFF));
63 if ( ++b == 256 )
64 break;
65 out.print(",\t");
66 }
67
68 out.println("\n\t};\n}");
69 }
70 }