void getstr(char *cmdstr, size_t cmdstrsize) { char *s; int c; s = cmdstr; for (;;) { switch (c = xgetc(0)) { case 0: break; case '\177': case '\b': if (s > cmdstr) { s--; printf("\b \b"); } break; case '\n': case '\r': *s = 0; return; default: if (s - cmdstr < cmdstrsize - 1) *s++ = c; putchar(c); break; } } }
void write_jpg(integer img) { long unsigned l; FILE *f; pdf_puts("/Type /XObject\n/Subtype /Image\n"); pdf_printf("/Width %i\n/Height %i\n/BitsPerComponent %i\n/Length %i\n", (int) img_width(img), (int) img_height(img), (int) jpg_ptr(img)->bits_per_component, (int) jpg_ptr(img)->length); pdf_puts("/ColorSpace "); if (img_colorspace_ref(img) != 0) { pdf_printf("%i 0 R\n", (int) img_colorspace_ref(img)); } else { switch (jpg_ptr(img)->color_space) { case JPG_GRAY: pdf_puts("/DeviceGray\n"); break; case JPG_RGB: pdf_puts("/DeviceRGB\n"); break; case JPG_CMYK: pdf_puts("/DeviceCMYK\n/Decode [1 0 1 0 1 0 1 0]\n"); break; default: pdftex_fail("Unsupported color space %i", (int) jpg_ptr(img)->color_space); } } pdf_puts("/Filter /DCTDecode\n>>\nstream\n"); for (l = jpg_ptr(img)->length, f = jpg_ptr(img)->file; l > 0; l--) pdfout(xgetc(f)); pdfendstream(); }
int xgettok(XFILE *xfp, char sepa, char *tok, int len) { int i = 0; int c = 0; int quoted=0; while (i < len && (c = xgetc(xfp)) != EOF && (quoted || (c != sepa && !isspace(c)))) { if (c == '"') { quoted = !quoted; continue; } tok[i++] = c; if (i >= 4 && tok[i-4] == '\\' && isoctal(tok[i-3]) && isoctal(tok[i-2]) && isoctal(tok[i-1]) && ((tok[i]=0), (c = strtol(tok+i-3,NULL, 8)) < 256)) { i -= 4; tok[i++] = c; } } if (c == '\n') xungetc(c, xfp); if (!i) return 0; if (i >= len || (sepa && c != sepa)) return -1; tok[i] = '\0'; return 1; }
static inline void getstr(void) { char *s; int c; s = cmd; for (;;) { switch (c = xgetc(0)) { case 0: break; case '\177': case '\b': if (s > cmd) { s--; printf("\b \b"); } break; case '\n': case '\r': *s = 0; return; default: if (s - cmd < sizeof(cmd) - 1) *s++ = c; putchar(c); } } }
static shalfword pkbyte(void) { register shalfword i; if ((i = xgetc(pkfile)) == EOF) pdftex_fail("unexpected eof in pk file"); return (i); }
static int fgetNUL(char *obuf, int rmdr, FILE *file) { int c; char *buf = obuf; while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) *buf++ = c; *buf++ = 0; return ((feof(file) || ferror(file)) ? 0 : 1); }
static INLINE int get_ccode() { int inp_byte; int cur_code; while (inp_len < inp_code_len) { if ((inp_byte = xgetc(infile)) < 0) return (EOF); inp_byte_cnt++; inp_code_remainder |= ((long) inp_byte) << inp_len; inp_len += 8; } inp_len -= inp_code_len; cur_code = inp_code_remainder & cdmsk[inp_code_len]; inp_code_remainder >>= inp_code_len; switch (cur_code) { case 0: /* Freeze dictionary */ FREEZE_flag = 1; break; case 1: /* reset dictionary */ inp_code_len = 9; inp_len = 0; inp_code_remainder = 0; break; case 2: /* increment codeword size */ inp_code_len++; if (inp_code_len > 12 ) { fprintf(stderr,"Error: codeword length > 12 at count %d\n", (int)inp_byte_cnt); ERROR_flag = 1; return 0; } break; case 3: /* next code is last one */ inp_len = 0; inp_code_remainder = 0; break; case 4: case 5: case 6: case 7: fprintf(stderr,"Error: undefined control code %d at count %d\n", cur_code,(int)inp_byte_cnt); ERROR_flag = 1; break; } return (cur_code); }
int var_read_qlz_block_header(FILE *fi, qlz_block_header *header) { // read the QLZ block marker // it's 4b (& 4a for uncompressed) with QLZ_COMPRESSION_LEVEL 2 header->type = xgetc(fi); // read qlz block uncompressed & compressed size header->in_len = xread32(fi); header->out_len = xread32(fi); return 1; }
static int fgetNUL(char *obuf, int rmdr, FILE *file) { int c; char *buf = obuf; c = 0; /* XXXGCC -Wuninitialized */ while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) *buf++ = c; *buf++ = 0; return (feof(file) || ferror(file)) ? 0 : 1; }
void xskip(XFILE *xfp, char *str) { int c; while ((c = xgetc(xfp)) != EOF) { if (c == '#') c = xskipcomment(xfp); if (strchr(str, c) == NULL) break; } xungetc(c, xfp); }
static int GetByte(void) /* get one byte */ { unsigned i; while (getlen <= 8) { if ((int)(i = xgetc(infile)) < 0) return(0); //i = 0; getbuf |= i << (8 - getlen); getlen += 8; } i = getbuf; getbuf <<= 8; getlen -= 8; return (int)((i & 0xff00) >> 8); }
static int GetBit(void) /* get one bit */ { unsigned i; while (getlen <= 8) { if ((int)(i = xgetc(infile)) < 0) return(0); //i = 0; getbuf |= i << (8 - getlen); getlen += 8; } i = getbuf; getbuf <<= 1; getlen--; return (int)((i & 0x8000) >> 15); }
static void check_type_by_header(image_dict * idict) { int i; FILE *file = NULL; char header[MAX_HEADER]; char prefix[LEN_PDF_MEMSTREAM+1]; if (idict == NULL) return; if (img_type(idict) != IMG_TYPE_NONE) return; /*tex Here we read the and also check for a memstream object. */ if (!img_filepath(idict) || !FOPEN_RBIN_MODE) { normal_error("pdf backend","reading image file failed"); } file = fopen(img_filepath(idict), FOPEN_RBIN_MODE); if (file == NULL) { /*tex We check the prefix of img_filepath(idict). */ for (i = 0; (unsigned) i < LEN_PDF_MEMSTREAM; i++) { prefix[i] = (char) (img_filepath(idict)[i]); } prefix[LEN_PDF_MEMSTREAM]='\0'; if (strncmp(prefix, HEADER_PDF_MEMSTREAM, LEN_PDF_MEMSTREAM) == 0) { img_type(idict) = IMG_TYPE_PDFMEMSTREAM; return; } else { formatted_error("pdf backend","reading image file '%s' failed",img_filepath(idict)); } } /*tex Do we have a valid file but perhaps unsupported? */ for (i = 0; (unsigned) i < MAX_HEADER; i++) { header[i] = (char) xgetc(file); if (feof(file)) { normal_error("pdf backend","reading image file failed"); } } xfclose(file, img_filepath(idict)); /*tex Further tests: */ if (strncmp(header, HEADER_JPG, sizeof(HEADER_JPG) - 1) == 0) img_type(idict) = IMG_TYPE_JPG; else if (strncmp(header + 4, HEADER_JP2, sizeof(HEADER_JP2) - 1) == 0) img_type(idict) = IMG_TYPE_JP2; else if (strncmp(header, HEADER_PNG, sizeof(HEADER_PNG) - 1) == 0) img_type(idict) = IMG_TYPE_PNG; else if (strncmp(header, HEADER_JBIG2, sizeof(HEADER_JBIG2) - 1) == 0) img_type(idict) = IMG_TYPE_JBIG2; else if (strncmp(header, HEADER_PDF, sizeof(HEADER_PDF) - 1) == 0) img_type(idict) = IMG_TYPE_PDF; }
char *xfgets(char *s, int size, xFILE *stream) { int c; char *buf; xfflush(NULL); if (size == 0) { return NULL; } buf = s; while (--size > 0 && (c = xgetc(stream)) != EOF) { if ((*buf++ = c) == '\n') break; } *buf = '\0'; return (c == EOF && buf == s) ? NULL : s; }
int keyhit(unsigned int secs) { uint32_t t0, t1; if (OPT_CHECK(RBX_NOINTR)) return (0); secs *= SECOND; t0 = 0; for (;;) { if (xgetc(1)) return (1); if (secs > 0) { t1 = *(uint32_t *)PTOV(0x46c); if (!t0) t0 = t1; if (t1 < t0 || t1 >= t0 + secs) return (0); } } /* NOTREACHED */ }
static int check_dial_pulses(int line) { int current_state = read_hook_state(line); int new_state; int dial_pulses = 0; xprintf("Counting dial pulses for line %d, press any key to exit...\n\r", line); while (UARTAvail() == 0) { if ((new_state = read_hook_state(line)) != current_state) { current_state = new_state; if (current_state == ON_HOOK) { dial_pulses++; xprintf("Current pulse count %d\n\r", dial_pulses); } } } xgetc(); // flush character that ended the while loop. xprintf("Final pulse count %d\n\r", dial_pulses); return dial_pulses; }
void read_jpg_info(integer img) { int i, units = 0; unsigned char jpg_id[] = "JFIF"; img_xres(img) = img_yres(img) = 0; jpg_ptr(img)->file = xfopen(img_name(img), FOPEN_RBIN_MODE); /* no LFS needed, as JPEG is limited to <4GiB */ xfseek(jpg_ptr(img)->file, 0, SEEK_END, cur_file_name); jpg_ptr(img)->length = xftell(jpg_ptr(img)->file, cur_file_name); xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); if (read2bytes(jpg_ptr(img)->file) != 0xFFD8) pdftex_fail("reading JPEG image failed (no JPEG header found)"); /* currently only true JFIF files allow extracting img_xres and img_yres */ if (read2bytes(jpg_ptr(img)->file) == 0xFFE0) { /* check for JFIF */ (void) read2bytes(jpg_ptr(img)->file); for (i = 0; i < 5; i++) { if (xgetc(jpg_ptr(img)->file) != jpg_id[i]) break; } if (i == 5) { /* it's JFIF */ read2bytes(jpg_ptr(img)->file); units = xgetc(jpg_ptr(img)->file); img_xres(img) = read2bytes(jpg_ptr(img)->file); img_yres(img) = read2bytes(jpg_ptr(img)->file); switch (units) { case 1: break; /* pixels per inch */ case 2: img_xres(img) *= 2.54; img_yres(img) *= 2.54; break; /* pixels per cm */ default: img_xres(img) = img_yres(img) = 0; break; } } } xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); while (1) { if (feof(jpg_ptr(img)->file)) pdftex_fail("reading JPEG image failed (premature file end)"); if (fgetc(jpg_ptr(img)->file) != 0xFF) pdftex_fail("reading JPEG image failed (no marker found)"); switch (xgetc(jpg_ptr(img)->file)) { case M_SOF5: case M_SOF6: case M_SOF7: case M_SOF9: case M_SOF10: case M_SOF11: case M_SOF13: case M_SOF14: case M_SOF15: pdftex_fail("unsupported type of compression"); case M_SOF2: if (fixedpdfminorversion <= 2) pdftex_fail("cannot use progressive DCT with PDF-1.2"); case M_SOF0: case M_SOF1: case M_SOF3: (void) read2bytes(jpg_ptr(img)->file); /* read segment length */ jpg_ptr(img)->bits_per_component = xgetc(jpg_ptr(img)->file); img_height(img) = read2bytes(jpg_ptr(img)->file); img_width(img) = read2bytes(jpg_ptr(img)->file); jpg_ptr(img)->color_space = xgetc(jpg_ptr(img)->file); xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name); switch (jpg_ptr(img)->color_space) { case JPG_GRAY: img_color(img) = IMAGE_COLOR_B; break; case JPG_RGB: img_color(img) = IMAGE_COLOR_C; break; case JPG_CMYK: img_color(img) = IMAGE_COLOR_C; break; default: pdftex_fail("Unsupported color space %i", (int) jpg_ptr(img)->color_space); } return; case M_SOI: /* ignore markers without parameters */ case M_EOI: case M_TEM: case M_RST0: case M_RST1: case M_RST2: case M_RST3: case M_RST4: case M_RST5: case M_RST6: case M_RST7: break; default: /* skip variable length markers */ xfseek(jpg_ptr(img)->file, read2bytes(jpg_ptr(img)->file) - 2, SEEK_CUR, cur_file_name); break; } } }
int main(void) { int line = 1; char c; /* Configure GPIO pins */ PORTC_PCR2 = PORT_PCR_MUX(0x1); // REFUND Relay is on PC2 (pin 23), config as GPIO (alt = 1) PORTC_PCR1 = PORT_PCR_MUX(0x1); // DISPOSITION Relay is on PC1 (pin 22), config as GPIO (alt = 1) PORTC_PCR5 = PORT_PCR_MUX(0x1); // LED is on PC5 (pin 13), config as GPIO (alt = 1) GPIOC_PDDR = (1 << 5 | 1 << 2 | 1 << 1); // make these output pins PORTD_PCR6 = PORT_PCR_MUX(0x1); // L1_HOLD_CO is on PD6 (pin 21), config as GPIO (alt = 1) PORTD_PCR5 = PORT_PCR_MUX(0x1); // L1_COIN_CTRL is on PD5 (pin 20), config as GPIO (alt = 1) GPIOD_PDDR = (1 << 6 | 1 << 5); // make these output pins PORTD_PCR3 = PORT_PCR_MUX(0x1); // L4_OFF_HOOK input is on PD3 (pin 8), config as GPIO (alt = 1) PORTC_PCR3 = PORT_PCR_MUX(0x1); // L3_OFF_HOOK input is on PC3 (pin 9), config as GPIO (alt = 1) PORTC_PCR4 = PORT_PCR_MUX(0x1); // L2_OFF_HOOK input is on PC4 (pin 10), config as GPIO (alt = 1) PORTC_PCR6 = PORT_PCR_MUX(0x1); // L1_OFF_HOOK input is on PC6 (pin 11), config as GPIO (alt = 1) PORTC_PCR7 = PORT_PCR_MUX(0x1); // TEST_STATUS input is on PC7 (pin 12), config as GPIO (alt = 1) LED_OFF; // start with LED off /* Make sure all relays are off initially. */ REFUND_OFF; DISPOSITION_OFF; for (line = 1; line <= 4; line++) { do_coin_ctrl(line, 0); set_line_hold(line, RELEASE); } line = 1; // Set default line UARTInit(0, 115200); // open UART0 for communication xprintf("\n\rWestern Electric 1D Coin Controller Firmware v0.1\n\r(c) 2015 Howard M. Harte, WZ2Q\n\r"); while (1) { xprintf("\n\rCurrent Line: %d\n\r", line); xprintf(menu); c = xgetc(); xprintf("%c\n\n\r", c); switch (c) { case '1': set_line_hold(line, HOLD); break; case '2': set_line_hold(line, RELEASE); break; case '3': collect_coin(line, COLLECT); break; case '4': collect_coin(line, REFUND); break; case '5': test_coin(line, INITIAL_RATE); break; case '6': test_coin(line, STUCK_COIN); break; case '7': xprintf("Line %d is %s hook.\n\r", line, read_hook_state(line) ? "OFF" : "on"); break; case '8': check_dial_pulses(line); break; case '0': xprintf("Which line (1-4?) "); c = xgetc(); line = c - '0'; if (line > 0 && line <= 4) { xprintf("%c - Line selected.\n\r", c); } else { xprintf("%c - Invalid line, defaulting to line 1.\n\r", c); line = 1; } break; default: xprintf("Invalid command '%c'", c); break; } } }
static JPG_UINT16 read2bytes(FILE * f) { int c = xgetc(f); return (c << 8) + xgetc(f); }
/* 復号 */ static int decode(void) { int osize = 0; /* バッファサイズの読込 */ bufsize = xgetc(infp); bufsize = (bufsize << 8) | xgetc(infp); if (bufsize < BUFMIN || BUFMAX < bufsize) return(-1); //printf("作業領域用メモリバッファ: %dByte\n", bufsize); while (TRUE) { int i, typ; int pts, ch, c2; int isize, srcsize; /* ブロックヘッダ読込 */ if (ob_count > 0) { if (xread(workbuf, 1, bufsize, infp) != (size_t)bufsize) return(-1); xwrite(workbuf, 1, bufsize, outfp); srcsize = bufsize; ob_count--; } else { int headcode = xgetc(infp); if (headcode == EOF) break; if (headcode == 0x7f) { ob_count = xgetc(infp); headcode = bufsize; } else { headcode = (headcode << 8) | xgetc(infp); } typ = headcode >> 15; isize = headcode & 0x7fff; /* ブロックデータの読込 */ if (xread(workbuf, 1, isize, infp) != (size_t)isize) return(-1); /* ペア表を初期化 */ for (i = 0; i < 256; i++) { pairtable1[i] = i; } if (typ) { /* ペア表の読込 */ if ((pts = xgetc(infp)) < 0) return(-1); for (i = 0; i < pts; i++) { if ((ch = xgetc(infp)) < 0) return(-1); if ((c2 = xgetc(infp)) < 0) return(-1); pairtable1[ch] = c2; if ((c2 = xgetc(infp)) < 0) return(-1); pairtable2[ch] = c2; } } srcsize = decode_buf(bufsize, isize); /* 復号処理 */ if(srcsize < 0) return(-1); xwrite(srcbuf, 1, srcsize, outfp); } /* 途中経過表示 */ osize += srcsize; //printf(" %d\r", osize); } return(0); }
int xfgetc(xFILE *fp) { return xgetc(fp); }
/* * Copy a word from the standard input into the line buffer, * and point to it w/ *wordp. Each copied word is represented * in line as an individual `\0'-terminated string. */ static void get_word(void) { char c, c1; *wordp++ = linep; loop: switch (c = xgetc(DOLSUB)) { case SPACE: case TAB: goto loop; case DQUOT:/* "..." == multi-char literal */ case SQUOT:/* '...' == ... */ c1 = c; while ((c = xgetc(!DOLSUB)) != c1) { if (c == EOL) { if (error_message == NULL) error_message = ERR_SYNTAX; peekc = c; *linep++ = EOS; return; } if (c == BQUOT) { if ((c = xgetc(!DOLSUB)) == EOL) c = SPACE;/* continue line */ else { peekc = c; c = BQUOT; } } *linep++ = c | QUOTE; } break; case BQUOT:/* \. == one-char literal */ if ((c = xgetc(!DOLSUB)) == EOL) goto loop; c |= QUOTE; peekc = c; break; case LPARENTHESIS: case RPARENTHESIS: case SEMICOLON: case AMPERSAND: case VERTICALBAR: case CARET: case LESSTHAN: case GREATERTHAN: case EOL: *linep++ = c; *linep++ = EOS; return; default: peekc = c; } for (;;) { if ((c = xgetc(DOLSUB)) == BQUOT) { if ((c = xgetc(!DOLSUB)) == EOL) c = SPACE;/* continue line */ else c |= QUOTE; } if (any(c, WORDPACK)) { peekc = c; if (any(c, QUOTPACK)) goto loop; *linep++ = EOS; return; } *linep++ = c; } /*NOTREACHED*/ }
/* * If dolsub is true, get either the next literal character from the * standard input or substitute the current $ dollar w/ the next * character of its value, which is pointed to by dolp. Otherwise, * get only the next literal character from the standard input. */ static char xgetc(bool dolsub) { int n; char c; if (peekc != EOS) { c = peekc; peekc = EOS; return c; } if (wordp >= &word[WORDMAX - 5]) { wordp -= 10; while (xgetc(!DOLSUB) != EOL) ; /* nothing */ wordp += 10; error_message = ERR_TMARGS; goto geterr; } if (linep >= &line[LINEMAX - 5]) { linep -= 10; while (xgetc(!DOLSUB) != EOL) ; /* nothing */ linep += 10; error_message = ERR_TMCHARS; goto geterr; } getd: if (dolp != NULL) { c = *dolp++ & ASCII; if (c != EOS) return c; dolp = NULL; } c = readc(); if (c == DOLLAR && dolsub) { c = readc(); if (c >= '0' && c <= '9') { n = c - '0'; if (IS_DIGIT(n, c) && n < dolc) dolp = (n > 0) ? dolv[n] : name; goto getd; } if (c == DOLLAR) { dolp = apid; goto getd; } } /* Ignore all EOS/NUL characters. */ if (c == EOS) do { if (++nul_count >= LINEMAX) { error_message = ERR_TMCHARS; goto geterr; } c = readc(); } while (c == EOS); return c; geterr: return EOL; }