/* Receive a binary style header (type and position) */ static int zrbhdr(char *hdr) { register int c, n; register unsigned short crc; if ((c = zdlread()) & ~0377) return c; Rxtype = c; crc = updcrc(c, 0); for (n = 4; --n >= 0; ++hdr) { if ((c = zdlread()) & ~0377) return c; crc = updcrc(c, crc); *hdr = c; } if ((c = zdlread()) & ~0377) return c; crc = updcrc(c, crc); if ((c = zdlread()) & ~0377) return c; crc = updcrc(c, crc); if (crc & 0xFFFF) { return ERROR; } protocol = ZM_ZMODEM; zmodem_requested = TRUE; return Rxtype; }
/* Send ZMODEM binary header hdr of type type */ void zsbhdr(int type, char *hdr) { register int n; register unsigned short crc; if (type == ZDATA) for (n = Znulls; --n >= 0;) xsendline(0); xsendline(ZPAD); xsendline(ZDLE); Crc32t = Txfcs32; if (Crc32t) zsbh32(hdr, type); else { xsendline(ZBIN); zsendline(type); crc = updcrc(type, 0); for (n = 4; --n >= 0; ++hdr) { zsendline(*hdr); crc = updcrc((0377 & *hdr), crc); } crc = updcrc(0, updcrc(0, crc)); zsendline(crc >> 8); zsendline(crc); } if (type != ZDATA) flushmo(); }
unsigned int calc_crc_checksum(unsigned char *buffer, int length) { unsigned int checksum = 0; while (length>0) { checksum = updcrc(*buffer++, checksum); length--; } checksum = updcrc(0,checksum); checksum = updcrc(0,checksum); return (checksum); }
/* * Receive array buf of max length with ending ZDLE sequence * and CRC. Returns the ending character or error code. * NB: On errors may store length+1 bytes! */ int zrdata(char *buf, int length, size_t * bytes_received) { register int c; register unsigned short crc; register char *end; register int d; *bytes_received = 0; if (Rxframeind == ZBIN32) return zrdat32(buf, length, bytes_received); crc = 0; end = buf + length; while (buf <= end) { if ((c = zdlread()) & ~0377) { crcfoo: switch (c) { case GOTCRCE: case GOTCRCG: case GOTCRCQ: case GOTCRCW: { d = c; c &= 0377; crc = updcrc(c, crc); if ((c = zdlread()) & ~0377) goto crcfoo; crc = updcrc(c, crc); if ((c = zdlread()) & ~0377) goto crcfoo; crc = updcrc(c, crc); if (crc & 0xFFFF) { return ERROR; } *bytes_received = length - (end - buf); COUNT_BLK(*bytes_received); return d; } case GOTCAN: return ZCAN; case TIMEOUT: return c; default: return c; } } *buf++ = c; crc = updcrc(c, crc); } return ERROR; }
int ndl_check_crc(char *filename) { unsigned short filecrc = 0, mycrc = 0; FILE *fp; int rc = 0, firstline = TRUE; char *buf, *p; if ((fp = fopen(filename, "r")) == NULL) { return -1; } buf = calloc(1024, sizeof(char)); while (Fgets(buf, 1023, fp)) { /* * Stop if EOF detected */ if (buf[0] == '\032') break; /* * The first line contains the crc and is not used to calculate * the crc. */ if (firstline) { firstline = FALSE; if ((p = strrchr(buf, ':'))) { filecrc = atoi(p+1); } } else { for (p = buf; *p; p++) mycrc = updcrc(*p, mycrc); /* * We have clean lines, but the crc includes cr-lf */ mycrc = updcrc('\r', mycrc); mycrc = updcrc('\n', mycrc); } } if (filecrc != mycrc) rc = 1; fclose(fp); free(buf); return rc; }
static int state_makecrc(volatile unsigned char *buffer, int length) { /* Calculate CRC for an entire block */ int crc=0xffff; while(--length>=0) { crc=updcrc(*buffer,crc); buffer++; } return(crc&0xffff); }
static void send_hex_header(const __xdata uint8_t *header) { uint8_t i; uint16_t crc = 0; zmodem_send(ZPAD); zmodem_send(ZPAD); zmodem_send(ZDLE); zmodem_send(ZHEX); for (i = 0; i < 5; i++) { uint8_t d = header[i]; send_hex(d); crc = updcrc(d, crc); } crc = updcrc(0, crc); crc = updcrc(0, crc); send_hex(crc>>8); send_hex(crc); zmodem_send('\r'); zmodem_send(0x8a); }
/* * Send binary array buf of length length, with ending ZDLE sequence frameend */ void zsdata(const char *buf, size_t length, int frameend) { register unsigned short crc; crc = 0; do { zsendline(*buf); crc = updcrc((0377 & *buf), crc); buf++; } while (--length > 0); xsendline(ZDLE); xsendline(frameend); crc = updcrc(frameend, crc); crc = updcrc(0, updcrc(0, crc)); zsendline(crc >> 8); zsendline(crc); if (frameend == ZCRCW) { xsendline(XON); flushmo(); } }
/* Receive a hex style header (type and position) */ static int zrhhdr(char *hdr) { register int c; register unsigned short crc; register int n; if ((c = zgethex()) < 0) return c; Rxtype = c; crc = updcrc(c, 0); for (n = 4; --n >= 0; ++hdr) { if ((c = zgethex()) < 0) return c; crc = updcrc(c, crc); *hdr = c; } if ((c = zgethex()) < 0) return c; crc = updcrc(c, crc); if ((c = zgethex()) < 0) return c; crc = updcrc(c, crc); if (crc & 0xFFFF) { return ERROR; } switch (c = READLINE_PF(1)) { case 0215: /* **** FALL THRU TO **** */ case 015: /* Throw away possible cr/lf */ READLINE_PF(1); break; } protocol = ZM_ZMODEM; zmodem_requested = TRUE; return Rxtype; }
/* Send ZMODEM HEX header hdr of type type */ void zshhdr(int type, char *hdr) { register int n; register unsigned short crc; char s[30]; size_t len; s[0] = ZPAD; s[1] = ZPAD; s[2] = ZDLE; s[3] = ZHEX; zputhex(type & 0x7f, s + 4); len = 6; Crc32t = 0; crc = updcrc((type & 0x7f), 0); for (n = 4; --n >= 0; ++hdr) { zputhex(*hdr, s + len); len += 2; crc = updcrc((0377 & *hdr), crc); } crc = updcrc(0, updcrc(0, crc)); zputhex(crc >> 8, s + len); zputhex(crc, s + len + 2); len += 4; /* Make it printable on remote machine */ s[len++] = 015; s[len++] = 0212; /* * Uncork the remote in case a fake XOFF has stopped data flow */ if (type != ZFIN && type != ZACK) { s[len++] = 021; } flushmo(); raw_write(0, s, len); }
static int wcgetsec(char *rxbuf, int maxtime) { int checksum, wcj, firstch; unsigned short oldcrc; char *p; int sectcurr; for (Lastrx = errors = 0; errors < RETRYMAX; errors++) { if ((firstch = readline(maxtime)) == STX) { Blklen = 1024; goto get2; } if (firstch == SOH) { Blklen = 128; get2: sectcurr = readline(1); if ((sectcurr + (oldcrc = readline(1))) == 0377) { oldcrc = checksum = 0; for (p = rxbuf, wcj = Blklen; --wcj >= 0;) { if ((firstch = readline(1)) < 0) goto bilge; oldcrc = updcrc(firstch, oldcrc); checksum += (*p++ = firstch); } if ((firstch = readline(1)) < 0) goto bilge; if (Crcflg) { oldcrc = updcrc(firstch, oldcrc); if ((firstch = readline(1)) < 0) goto bilge; oldcrc = updcrc(firstch, oldcrc); if (oldcrc & 0xFFFF) zperr("CRC"); else { Firstsec = FALSE; return sectcurr; } } else if (((checksum - firstch) & 0377) == 0) { Firstsec = FALSE; return sectcurr; } else zperr("Checksum"); } else zperr("Sector number garbled"); } /* make sure eot really is eot and not just mixmash */ else if (firstch == EOT && readline(1) == TIMEOUT) return WCEOT; else if (firstch == CAN) { if (Lastrx == CAN) { zperr("Sender CANcelled"); return ERROR; } else { Lastrx = CAN; continue; } } else if (firstch == TIMEOUT) { if (Firstsec) goto humbug; bilge: zperr("TIMEOUT"); } else zperr("Got 0%o sector header", firstch); humbug: Lastrx = 0; while (readline(1) != TIMEOUT); if (Firstsec) { xsendline(Crcflg ? WANTCRC : NAK); Lleft = 0; /* Do read next time ... */ } else { maxtime = 40; xsendline(NAK); Lleft = 0; /* Do read next time ... */ } } /* try to stop the bubble machine. */ canit(); return ERROR; }
static int wcputsec(char *buf, int sectnum, size_t cseclen) { int checksum, wcj; char *cp; unsigned oldcrc; int firstch; int attempts; firstch = 0; /* part of logic to detect CAN CAN */ for (attempts = 0; attempts <= RETRYMAX; attempts++) { Lastrx = firstch; sendline(cseclen == 1024 ? STX : SOH); sendline(sectnum); sendline(-sectnum - 1); oldcrc = checksum = 0; for (wcj = cseclen, cp = buf; --wcj >= 0;) { sendline(*cp); oldcrc = updcrc((0377 & *cp), oldcrc); checksum += *cp++; } if (Crcflg) { oldcrc = updcrc(0, updcrc(0, oldcrc)); sendline((int) oldcrc >> 8); sendline((int) oldcrc); } else sendline(checksum); flushmo(); if (Optiong) { firstsec = FALSE; return OK; } firstch = READLINE_PF(Rxtimeout); gotnak: switch (firstch) { case CAN: if (Lastrx == CAN) { cancan: return ERROR; } break; case TIMEOUT: continue; case WANTCRC: if (firstsec) Crcflg = TRUE; case NAK: continue; case ACK: firstsec = FALSE; Totsecs += (cseclen >> 7); return OK; case ERROR: break; default: break; } for (;;) { Lastrx = firstch; if ((firstch = READLINE_PF(Rxtimeout)) == TIMEOUT) break; if (firstch == NAK || firstch == WANTCRC) goto gotnak; if (firstch == CAN && Lastrx == CAN) goto cancan; } }
int wcputsec(char *buf,int sectnum,int cseclen) { int checksum, wcj; char *cp; unsigned oldcrc; int firstch; uint8_t attempts; firstch=0; /* part of logic to detect CAN CAN */ if (Verbose>2) fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 ); else if (Verbose>1) fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 ); for (attempts=0; attempts <= Tx_RETRYMAX; attempts++) { Lastrx= firstch; sendline(cseclen==1024?STX:SOH); sendline(sectnum); sendline(-sectnum -1); oldcrc=checksum=0; for (wcj=cseclen,cp=buf; --wcj>=0; ) { sendline(*cp); oldcrc=updcrc((0377& *cp), oldcrc); checksum += *cp++; } if (Crcflg) { oldcrc=updcrc(0,updcrc(0,oldcrc)); sendline((int)oldcrc>>8); sendline((int)oldcrc); } else sendline(checksum); if (Optiong) { firstsec = FALSE; return OK; } firstch = readline(Rxtimeout); gotnak: switch (firstch) { case CAN: if(Lastrx == CAN) { cancan: zperr("Cancelled"); return ERROR; } break; case TIMEOUT: zperr("Timeout on sector ACK"); continue; case WANTCRC: if (firstsec) Crcflg = TRUE; case NAK: zperr("NAK on sector"); continue; case ACK: firstsec=FALSE; Totsecs += (cseclen>>7); return OK; case ERROR: zperr("Got burst for sector ACK"); break; default: zperr("Got %02x for sector ACK", firstch); break; } for (;;) { Lastrx = firstch; if ((firstch = readline(Rxtimeout)) == TIMEOUT) break; if (firstch == NAK || firstch == WANTCRC) goto gotnak; if (firstch == CAN && Lastrx == CAN) goto cancan; } }
static inline int state_store(void) { extern void hijack_save_settings (unsigned char *buf); extern int hijack_volumelock_enabled; /* Store the contents of read_buffer to flash, at savebase */ int a,status,crc=0xffff,data; volatile unsigned short *from=(volatile unsigned short*)state_devices[0].read_buffer; /* Store current unixtime */ *((unsigned int*)from)=xtime.tv_sec; /* Store current power-on time */ *((unsigned int*)(from+2))=(xtime.tv_sec-unixtime)+powerontime; // Store desired start-up volume level, if enabled. if (hijack_volumelock_enabled) { player_savearea_fields_t *buf = (player_savearea_fields_t *)from; if (empeg_on_dc_power) buf->dc_volume = hijack_saved_volume; else buf->ac_volume = hijack_saved_volume; } /* Store hijack_savearea */ hijack_save_settings((unsigned char *)from); /* Enable writes to flash chip */ state_enablewrite(); /* Unlock if necessary */ if (flash_product==FLASH_C3) { *STATE_BASE=FLASH_UNLOCK1; *STATE_BASE=FLASH_UNLOCK2; } /* For each halfword in the state block... */ for(a=0;a<STATE_BLOCK_SIZE;a+=sizeof(short)) { /* Program word */ *savebase=FLASH_PROGRAM; data=(a==(STATE_BLOCK_SIZE-2)?crc:(*from++)); *savebase++=data; /* Update CRC (flash will be busy here) */ crc=updcrc(data&0xff,crc); crc=updcrc(data>>8,crc); /* Wait for completion */ while(!((status=*savebase)&0x80)); /* Programmed OK? */ if (status&0x1a) { printk("F%x\n",((int)savebase)&0xffff); } } /* Lock if necessary */ if (flash_product==FLASH_C3) { *STATE_BASE=FLASH_LOCK1; *STATE_BASE=FLASH_LOCK2; } /* Back to read array mode */ *STATE_BASE=FLASH_READ; state_disablewrite(); /* Cleansed */ dirty=0; return 0; }
/* * This routine reads the superblock and tests if it can be an ext2 file system. * It does NOT use buffers from the cache. */ int check_ext2fs_magic(struct vpfsi32 *pvpfsi, unsigned short hVPB) { int nb_sec; int rc; int rc2; char *buf; struct ext2_super_block *es; // // Allocates a temporary buffer // if ((rc = DevHlp32_VMAlloc(BLOCK_SIZE, VMDHA_NOPHYSADDR, VMDHA_FIXED | VMDHA_CONTIG, __StackToFlat(&buf))) == NO_ERROR) { // // Reads disk block 1 (with blocksize = 1024) // nb_sec = BLOCK_SIZE / pvpfsi->vpi_bsize; if ((rc = fsh32_dovolio( DVIO_OPREAD | DVIO_OPRESMEM | DVIO_OPBYPASS | DVIO_OPNCACHE, DVIO_ALLABORT | DVIO_ALLRETRY | DVIO_ALLFAIL, hVPB, buf, __StackToFlat(&nb_sec), nb_sec )) == NO_ERROR) { es = (struct ext2_super_block *)buf; if (es->s_magic == EXT2_SUPER_MAGIC) { kernel_printf("ext2 signature found in superblock (hVPB = 0x%04X)", hVPB); /* * The volume serial number is a 32 bits CRC checksum of the UUID field */ pvpfsi->vpi_vid = updcrc(es->s_uuid, sizeof(es->s_uuid)); /* * The volume name is truncated to the valid OS/2 volume label length (11 chars) */ strncpy(pvpfsi->vpi_text, es->s_volume_name, sizeof(pvpfsi->vpi_text)); pvpfsi->vpi_text[sizeof(pvpfsi->vpi_text) - 1] = '\0'; rc = NO_ERROR; } else { kernel_printf("ext2 signature NOT found in superblock (hVPB = 0x%04X)", hVPB); rc = ERROR_VOLUME_NOT_MOUNTED; } } else { kernel_printf("check_ext2fs_magic - fsh32_dovolio returned %d", rc); } if ((rc2 = DevHlp32_VMFree((void *)buf)) == NO_ERROR) { /* * Nothing else */ } else { kernel_printf("check_ext2fs_magic - VMFree returned %d", rc); rc = rc2; } } else { kernel_printf("check_ext2fs_magic - VMAlloc returned %d", rc); } return rc; }
int gunzip_read (char *buf, int len) { int ret = 0; int check_crc; ulg crc_value = 0xffffffffUL; compressed_file = 0; gunzip_swap_values (); /* * Now "gzip_*" values refer to the uncompressed data. */ /* do we reset decompression to the beginning of the file? */ if (saved_filepos > gzip_filepos + WSIZE) initialize_tables (); /* perform CRC check only if reading the entire file */ check_crc = (saved_filepos == 0 && len == MAXINT); /* * This loop operates upon uncompressed data only. The only * special thing it does is to make sure the decompression * window is within the range of data it needs. */ while (len > 0 && !errnum) { register int size; register char *srcaddr; while (gzip_filepos >= saved_filepos && !errnum) inflate_window (); if (errnum) break; /* We could have started with an unknown gzip_filemax (MAXINT) * which has been updated in get_byte(). If so, update len * to avoid reading beyond the end. */ if (len > (gzip_filemax - gzip_filepos)) { len = gzip_filemax - gzip_filepos; if (len < 0) { errnum = ERR_BAD_GZIP_DATA; break; } } srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide); size = saved_filepos - gzip_filepos; if (size > len) size = len; memmove (buf, srcaddr, size); /* do CRC calculation here! */ crc_value = updcrc(buf, (unsigned)size); buf += size; len -= size; gzip_filepos += size; ret += size; } /* check for CRC error if reading entire file */ if (!errnum && check_crc && gzip_crc != crc_value) { #if 0 printf ("gunzip: crc value 0x%x, expected 0x%x\n", crc_value, gzip_crc); #endif errnum = ERR_BAD_GZIP_CRC; } compressed_file = 1; gunzip_swap_values (); /* * Now "gzip_*" values refer to the compressed data. */ if (errnum) ret = 0; return ret; }