|
| 1 | +/* BCB_CRC.C V1.2 |
| 2 | + | Copyright (c) 1988,1989,1990 by |
| 3 | + | The Hebrew University of Jerusalem, Computation Center. |
| 4 | + | |
| 5 | + | This software is distributed under a license from the Hebrew University |
| 6 | + | of Jerusalem. It may be copied only under the terms listed in the license |
| 7 | + | agreement. This copyright message should never be changed or removed. |
| 8 | + | This software is gievn without any warranty, and the Hebrew University |
| 9 | + | of Jerusalem assumes no responsibility for any damage that might be caused |
| 10 | + | by use or misuse of this software. |
| 11 | + | |
| 12 | + | Add the BCB and FCS to sending messages. Also compute CRC for incoming |
| 13 | + | and outgoing messages. |
| 14 | + | 1. The CRC computing algorithm should be enhanced. |
| 15 | + | |
| 16 | + | The generating polynomial is X^16+X^15+X^2+1 (CRC-16). When computing the |
| 17 | + | CRC, DLE's are not computed (except from a second DLE in a sequence of |
| 18 | + | 2 DLE's). Furthermore, the first DLE+ETB which starts a text block is |
| 19 | + | not computed also. |
| 20 | + | When a BCB is sent, first it is checked whether we have to send a reset |
| 21 | + | BCB. If so, we send a reset BCB (instead of normal one), and this flag is |
| 22 | + | set, so the next BCB will be a normal one. This flag is set to zero when |
| 23 | + | a line is (re)started, so the first BCB will be a reset one. |
| 24 | + | When adding BCB and CRC to the buffer, DLE's are replicated. Upon receive, |
| 25 | + | the routine that computes the CRC removes the second DLE and rfeturns the |
| 26 | + | new size to the caller. |
| 27 | + | |
| 28 | + | V1.1 - Add the functions REMOVE_DLES (same as CHECK-CRC but doesn't do the |
| 29 | + | CRC checking) and the function ADD_BCB (line ADD_BCB_CRC but does |
| 30 | + | not add the CRC). |
| 31 | + | V1.2 - Modify ADD_BCB function. When we pass a frame to DMB, we must leave |
| 32 | + | enough space (for CRCs) in the length of buffer we pass to DMB. |
| 33 | + | However, enough should be = exactly, and we should not reserve place |
| 34 | + | for the last PAD. Hence, the size of buffer was decreased by one in |
| 35 | + | this change (to not give place for the PAD character). |
| 36 | + */ |
| 37 | +#include "consts.h" |
| 38 | +#include "ebcdic.h" |
| 39 | + |
| 40 | +EXTERNAL struct LINE IoLines[MAX_LINES]; |
| 41 | + |
| 42 | +/* A naive bit-shifts algoruthm (not used): |
| 43 | +#define ACUCRC(crc, c) {\ |
| 44 | + register unsigned t0, t1;\ |
| 45 | + for(t0 = 0; t0 < 8; t0++) {\ |
| 46 | + if(((crc & 0x1) ^ ((c >> t0) & 0x1)) == 0) \ |
| 47 | + crc = ((crc >> 1) & 0x7fff);\ |
| 48 | + else\ |
| 49 | + crc = ((((crc & 0xbffd) | ((crc & 0x4002) ^ 0x4002)) >> \ |
| 50 | + 1) & 0x7fff) | 0x8000;\ |
| 51 | + };\ |
| 52 | +} |
| 53 | +*/ |
| 54 | + |
| 55 | + |
| 56 | +unsigned char cones[256] = |
| 57 | +{ |
| 58 | + 0x0, 0xFF, 0xFE, 0x1, 0xFC, 0x3, 0x2, 0xFD, |
| 59 | + 0xF8, 0x7, 0x6, 0xF9, 0x4, 0xFB, 0xFA, 0x5, |
| 60 | + 0xF0, 0x0F, 0x0E, 0xF1, 0xC, 0xF3, 0xF2, 0xD, |
| 61 | + 0x8, 0xF7, 0xF6, 0x9, 0xF4, 0xB, 0xA, 0xF5, |
| 62 | + 0xE0, 0x1F, 0x1E, 0xE1, 0x1C, 0xE3, 0xE2, 0x1D, |
| 63 | + 0x18, 0xE7, 0xE6, 0x19, 0xE4, 0x1B, 0x1A, 0xE5, |
| 64 | + 0x10, 0xEF, 0xEE, 0x11, 0xEC, 0x13, 0x12, 0xED, |
| 65 | + 0xE8, 0x17, 0x16, 0xE9, 0x14, 0xEB, 0xEA, 0x15, |
| 66 | + 0xC0, 0x3F, 0x3E, 0xC1, 0x3C, 0xC3, 0xC2, 0x3D, |
| 67 | + 0x38, 0xC7, 0xC6, 0x39, 0xC4, 0x3B, 0x3A, 0xC5, |
| 68 | + 0x30, 0xCF, 0xCE, 0x31, 0xCC, 0x33, 0x32, 0xCD, |
| 69 | + 0xC8, 0x37, 0x36, 0xC9, 0x34, 0xCB, 0xCA, 0x35, |
| 70 | + 0x20, 0xDF, 0xDE, 0x21, 0xDC, 0x23, 0x22, 0xDD, |
| 71 | + 0xD8, 0x27, 0x26, 0xD9, 0x24, 0xDB, 0xDA, 0x25, |
| 72 | + 0xD0, 0x2F, 0x2E, 0xD1, 0x2C, 0xD3, 0xD2, 0x2D, |
| 73 | + 0x28, 0xD7, 0xD6, 0x29, 0xD4, 0x2B, 0x2A, 0xD5, |
| 74 | + 0x80, 0x7F, 0x7E, 0x81, 0x7C, 0x83, 0x82, 0x7D, |
| 75 | + 0x78, 0x87, 0x86, 0x79, 0x84, 0x7B, 0x7A, 0x85, |
| 76 | + 0x70, 0x8F, 0x8E, 0x71, 0x8C, 0x73, 0x72, 0x8D, |
| 77 | + 0x88, 0x77, 0x76, 0x89, 0x74, 0x8B, 0x8A, 0x75, |
| 78 | + 0x60, 0x9F, 0x9E, 0x61, 0x9C, 0x63, 0x62, 0x9D, |
| 79 | + 0x98, 0x67, 0x66, 0x99, 0x64, 0x9B, 0x9A, 0x65, |
| 80 | + 0x90, 0x6F, 0x6E, 0x91, 0x6C, 0x93, 0x92, 0x6D, |
| 81 | + 0x68, 0x97, 0x96, 0x69, 0x94, 0x6B, 0x6A, 0x95, |
| 82 | + 0x40, 0xBF, 0xBE, 0x41, 0xBC, 0x43, 0x42, 0xBD, |
| 83 | + 0xB8, 0x47, 0x46, 0xB9, 0x44, 0xBB, 0xBA, 0x45, |
| 84 | + 0xB0, 0x4F, 0x4E, 0xB1, 0x4C, 0xB3, 0xB2, 0x4D, |
| 85 | + 0x48, 0xB7, 0xB6, 0x49, 0xB4, 0x4B, 0x4A, 0xB5, |
| 86 | + 0xA0, 0x5F, 0x5E, 0xA1, 0x5C, 0xA3, 0xA2, 0x5D, |
| 87 | + 0x58, 0xA7, 0xA6, 0x59, 0xA4, 0x5B, 0x5A, 0xA5, |
| 88 | + 0x50, 0xAF, 0xAE, 0x51, 0xAC, 0x53, 0x52, 0xAD, |
| 89 | + 0xA8, 0x57, 0x56, 0xA9, 0x54, 0xAB, 0xAA, 0x55 |
| 90 | +}; |
| 91 | + |
| 92 | +/* |
| 93 | + | Macro to calculate CRC-16 |
| 94 | + */ |
| 95 | +#define ACUCRC(crc, c) {\ |
| 96 | + register unsigned t0, t1;\ |
| 97 | + t0 = cones[c] ^ cones[crc & 0xff];\ |
| 98 | + crc >>= 8;\ |
| 99 | + t1 = (crc >> 6) & 03;\ |
| 100 | + crc &= 0x3f;\ |
| 101 | + t1 |= t0 << 2;\ |
| 102 | + t1 ^= t0;\ |
| 103 | + crc |= t1 << 6;\ |
| 104 | + crc |= (t0 & 0xc0) << 8;\ |
| 105 | + crc ^= (crc >> 15) & 1;} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | + | calculate the CRC and compare it to the received one. return 1 if ok, 0 if |
| 111 | + | not. If there are two DLE's in a sequence, remove one (the first is for |
| 112 | + | transparency) and reduce the buffer size by one. |
| 113 | + */ |
| 114 | +int |
| 115 | +check_crc(buffer, size) |
| 116 | +unsigned char *buffer; |
| 117 | +int *size; |
| 118 | +{ |
| 119 | + unsigned char c, *p, *q; |
| 120 | + unsigned short crc, rcrc; |
| 121 | + register int DleFound, AccumulatedSize; |
| 122 | + |
| 123 | + crc = 0; q = p = &buffer[2]; /* Skip first DLE+STX */ |
| 124 | + DleFound = AccumulatedSize = 0; |
| 125 | + for(;;) { |
| 126 | + c = *p++; /* Use direct mode instead of indirect, and |
| 127 | + also save us incrementing problems later. */ |
| 128 | + if((c != DLE) || (DleFound != 0)) { |
| 129 | + /* Accumulate only the second DLE */ |
| 130 | + ACUCRC(crc, c); |
| 131 | + } |
| 132 | + if((c == DLE) && (DleFound != 0)) { |
| 133 | + /* Remove the second DLE - Reduce size and don't increment *q */ |
| 134 | + *size -= 1; |
| 135 | + AccumulatedSize--; |
| 136 | + } |
| 137 | + else /* Retain it */ |
| 138 | + *q++ = c; |
| 139 | + if((c == ETB) && (DleFound)) |
| 140 | + break; /* End of the block */ |
| 141 | +/* Mark that we have a DLE only if it is first in a sequence (and not Second in |
| 142 | + a serias of 4 DLE's which should be made to 2) */ |
| 143 | + if((c == DLE) && (DleFound == 0)) |
| 144 | + DleFound++; |
| 145 | + else |
| 146 | + DleFound = 0; |
| 147 | + if(++AccumulatedSize > *size) { /* Unterminated block */ |
| 148 | + logger(2, "BCB_CRC, Unterminated block received\n"); |
| 149 | + trace(buffer, AccumulatedSize, (int)(2)); |
| 150 | + return 0; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | +/* Compute now the received CRC and compare */ |
| 155 | + rcrc = (*p++ & 0xff); |
| 156 | + rcrc += ((*p & 0xff) << 8); |
| 157 | + if(crc == rcrc) /* Match */ |
| 158 | + return 1; |
| 159 | + |
| 160 | + logger(2, "BCB_CRC: Received CRC error, received=x^%x, computed=x^%x\n", |
| 161 | + (int)(rcrc), (int)(crc)); |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +/* |
| 166 | + | In case of DMB working in BISYNC mode, the DMB does the CRC checking. However, |
| 167 | + | it doesn't strip out double DLE's, so we have to do it. |
| 168 | + | If there are two DLE's in a sequence, remove one (the first is for |
| 169 | + | transparency) and reduce the buffer size by one. |
| 170 | + */ |
| 171 | +int |
| 172 | +remove_dles(buffer, size) |
| 173 | +unsigned char *buffer; |
| 174 | +int *size; |
| 175 | +{ |
| 176 | + unsigned char c, *p, *q; |
| 177 | + register int DleFound, AccumulatedSize; |
| 178 | + |
| 179 | + q = p = &buffer[2]; /* Skip first DLE+STX */ |
| 180 | + DleFound = AccumulatedSize = 0; |
| 181 | + for(;;) { |
| 182 | + c = *p++; /* Use direct mode instead of indirect, and |
| 183 | + also save us incrementing problems later. */ |
| 184 | + if((c == DLE) && (DleFound != 0)) { |
| 185 | + /* Remove the second DLE - Reduce size and don't increment *q */ |
| 186 | + *size -= 1; |
| 187 | + AccumulatedSize--; |
| 188 | + } |
| 189 | + else /* Retain it */ |
| 190 | + *q++ = c; |
| 191 | + if((c == ETB) && (DleFound)) |
| 192 | + break; /* End of the block */ |
| 193 | +/* Mark that we have a DLE only if it is first in a sequence (and not Second in |
| 194 | + a serias of 4 DLE's which should be made to 2) */ |
| 195 | + if((c == DLE) && (DleFound == 0)) |
| 196 | + DleFound++; |
| 197 | + else |
| 198 | + DleFound = 0; |
| 199 | + if(++AccumulatedSize > *size) { /* Unterminated block */ |
| 200 | + logger(2, "BCB_CRC, Unterminated block received\n"); |
| 201 | + trace(buffer, AccumulatedSize, (int)(2)); |
| 202 | + return 0; |
| 203 | + } |
| 204 | + } |
| 205 | + return 1; |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +/* |
| 210 | + | Add DLE+STX+BCB+FCS in front of the transmission block, compute the CRC, |
| 211 | + | and append it to the end of the trasmission block. The complete output is |
| 212 | + | placed in NewLine (buffer is not changed). The size of the resultant buffer |
| 213 | + | is returned as the function value. |
| 214 | + | This function does not check whether there is enough place in the output |
| 215 | + | buffer. |
| 216 | + */ |
| 217 | +int |
| 218 | +add_bcb_crc(Index, buffer, size, NewLine, BCBcount) |
| 219 | +unsigned char *buffer, *NewLine; |
| 220 | +int Index, size, BCBcount; |
| 221 | +{ |
| 222 | + unsigned char *p, *q, c; |
| 223 | + int i, NewSize; |
| 224 | + unsigned short crc; |
| 225 | + |
| 226 | + NewSize = 0; p = NewLine; crc = 0; |
| 227 | + |
| 228 | +/* Put the DLE, STX, BCB and FCS. |
| 229 | + If BCB is zero, send a "reset" BCB. |
| 230 | + */ |
| 231 | + *p++ = DLE; *p++ = STX; |
| 232 | + if((BCBcount == 0) && ((IoLines[Index].flags & F_RESET_BCB) == 0)) { |
| 233 | + IoLines[Index].flags |= F_RESET_BCB; |
| 234 | + *p++ = 0xa0; /* Reset BCB count to zero */ |
| 235 | + } |
| 236 | + else |
| 237 | + *p++ = 0x80 + (BCBcount & 0xf); /* Normal block */ |
| 238 | + |
| 239 | + *p++ = 0x8f; *p++ = 0xcf; /* FCS - all streams are enabled */ |
| 240 | + NewSize = 5; /* DLE+STX+BCB+FCS1+FCS2 */ |
| 241 | + p = &NewLine[2]; /* Compute the CRC of the above */ |
| 242 | + for(i = 2; i < NewSize; i++) { /* Skip the DLE STX */ |
| 243 | + c = *p++; /* Use direct mode instead of indirect, and |
| 244 | + also save us incrementing problems later. */ |
| 245 | + ACUCRC(crc, c); |
| 246 | + } |
| 247 | + |
| 248 | +/* Copy the data, compute the CRC during the copy, and replicate DLE if needed */ |
| 249 | + q = buffer; |
| 250 | + for(i = 0; i < size; i++) { |
| 251 | + c = *q++; |
| 252 | + ACUCRC(crc, c); |
| 253 | + *p++ = c; |
| 254 | + if(c == DLE) { |
| 255 | + *p++ = DLE; |
| 256 | + NewSize++; |
| 257 | + } |
| 258 | + } |
| 259 | + NewSize += size; |
| 260 | + |
| 261 | +/* Add DLE+ETB and the CRC, and finaly the PAD character */ |
| 262 | + *p++ = DLE; *p++ = ETB; |
| 263 | + ACUCRC(crc, ETB); |
| 264 | + *p++ = (crc & 0xff); |
| 265 | + *p++ = ((crc & 0xff00) >> 8); |
| 266 | + *p++ = PAD; |
| 267 | + NewSize += 5; |
| 268 | + return NewSize; |
| 269 | +} |
| 270 | + |
| 271 | + |
| 272 | +/* |
| 273 | + | Add DLE+STX+BCB+FCS in front of the transmission block, and leave space for |
| 274 | + | CRCs at the end of the block. The complete output is placed in NewLine |
| 275 | + | (buffer is not changed). The size of the resultant buffer is returned as |
| 276 | + | the function value. |
| 277 | + | This function does not check whether there is enough place in the output |
| 278 | + | buffer. |
| 279 | + */ |
| 280 | +int |
| 281 | +add_bcb(Index, buffer, size, NewLine, BCBcount) |
| 282 | +unsigned char *buffer, *NewLine; |
| 283 | +int Index, size, BCBcount; |
| 284 | +{ |
| 285 | + unsigned char *p, *q, c; |
| 286 | + int i, NewSize; |
| 287 | + |
| 288 | + NewSize = 0; p = NewLine; |
| 289 | + |
| 290 | +/* Put the DLE, STX, BCB and FCS. |
| 291 | + If BCB is zero, send a "reset" BCB. |
| 292 | + */ |
| 293 | + *p++ = DLE; *p++ = STX; |
| 294 | + if((BCBcount == 0) && ((IoLines[Index].flags & F_RESET_BCB) == 0)) { |
| 295 | + IoLines[Index].flags |= F_RESET_BCB; |
| 296 | + *p++ = 0xa0; /* Reset BCB count to zero */ |
| 297 | + } |
| 298 | + else |
| 299 | + *p++ = 0x80 + (BCBcount & 0xf); /* Normal block */ |
| 300 | + |
| 301 | + *p++ = 0x8f; *p++ = 0xcf; /* FCS - all streams are enabled */ |
| 302 | + NewSize = 5; /* DLE+STX+BCB+FCS1+FCS2 */ |
| 303 | + |
| 304 | +/* Copy the data, replicate DLE if needed */ |
| 305 | + q = buffer; |
| 306 | + for(i = 0; i < size; i++) { |
| 307 | + c = *q++; |
| 308 | + *p++ = c; |
| 309 | + if(c == DLE) { |
| 310 | + *p++ = DLE; |
| 311 | + NewSize++; |
| 312 | + } |
| 313 | + } |
| 314 | + NewSize += size; |
| 315 | + |
| 316 | +/* Add DLE+ETB and leave space for CRC's but not for PAD (if we leave place for |
| 317 | + the PAD also, the DMB gets confused and adds garbage after the frame) */ |
| 318 | + *p++ = DLE; *p++ = ETB; |
| 319 | + NewSize += 4; /* DEL+ETB, 2 for CRCs */ |
| 320 | + return NewSize; |
| 321 | +} |
0 commit comments