void mpcdec_bitstream_fill(struct mpc_decoder_data *mpci,unsigned long (*readfunc)(void *fbds,void *buf,unsigned long len),void *fbds,unsigned int num) { long bytes=0,i; i=MPC_BITSTREAM_BYTESIZE-mpci->bs_putbyteptr; if(i<=num){ bytes=readfunc(fbds,((char *)(mpci->bitstream))+mpci->bs_putbyteptr,i); mpci->bs_putbyteptr+=bytes; mpci->bs_forwardbits+=bytes*8; if(mpci->bs_rewindbits>bytes*8) mpci->bs_rewindbits-=bytes*8; else mpci->bs_rewindbits=0; if(bytes!=i) return; num-=i; mpci->bs_putbyteptr=0; } if(num){ bytes=readfunc(fbds,((char *)(mpci->bitstream))+mpci->bs_putbyteptr,num); mpci->bs_putbyteptr+=bytes; mpci->bs_forwardbits+=bytes*8; if(mpci->bs_rewindbits>bytes*8) mpci->bs_rewindbits-=bytes*8; else mpci->bs_rewindbits=0; } mpci->bs_dword = mpci->bitstream[mpci->bs_elementptr]; }
void InitializePPMdRangeCoder(PPMdRangeCoder *self, PPMdReadFunction *readfunc,void *inputcontext, bool uselow,int bottom) { self->readfunc=readfunc; self->inputcontext=inputcontext; self->low=0; self->code=0; self->range=0xffffffff; self->uselow=uselow; self->bottom=bottom; for(int i=0;i<4;i++) self->code=(self->code<<8)|readfunc(inputcontext); }
void hexdump2(char *desc, uint8_t (*readfunc)(uint32_t), int pos, int len) { int i; unsigned char buff[17]; // unsigned char *pc = (unsigned char *)addr; unsigned char data; // Output description if given. if (desc != NULL) printf("%s:\r\n", desc); // Process every byte in the data. for (i = 0; i < len; i++) { // Multiple of 16 means new line (with line offset). if ((i % 16) == 0) { // Just don't print ASCII for the zeroth line. if (i != 0) printf(" %s\r\n", buff); // Output the offset. printf(" %04x ", i); } // Now the hex code for the specific character. data = readfunc(pos + i); printf(" %02x", data); // And store a printable ASCII character for later. if ((data < 0x20) || (data > 0x7e)) buff[i % 16] = '.'; else buff[i % 16] = data; buff[(i % 16) + 1] = '\0'; } // Pad out last line if not exactly 16 characters. while ((i % 16) != 0) { printf(" "); i++; } // And print the final ASCII bit. printf(" %s\r\n", buff); }
static void* read_header_file(const char* fn, int ext, anbool only, void* dest, void* (*readfunc)(const qfits_header*, void*)) { qfits_header* hdr; void* result; if (only) { hdr = anqfits_get_header_only(fn, ext); } else { hdr = anqfits_get_header2(fn, ext); } if (!hdr) { ERROR("Failed to read FITS header from file \"%s\" extension %i", fn, ext); return NULL; } result = readfunc(hdr, dest); if (!result) { ERROR("Failed to parse WCS header from file \"%s\" extension %i", fn, ext); } qfits_header_destroy(hdr); return result; }
int main(void) { char* read = malloc(255); puts("Enter Directory: "); gets(read); int check = readfunc(read); if (check == OK) puts("\nSuccess"); else puts("\nFailure"); free(read); printf("\nPress Any Key to Continue \n"); getch(); return 0; }
int pd_getline(char *buf, unsigned int bufsize, int *bufcur, int *buflen, int(*readfunc)(int, void *, unsigned int, char *, int), int(*closefunc)(int, char *, int), int readfd, char **line, char *errdesc, int errlen) { char *eol; unsigned int srcsize; int linelen; int linesize = 1024; linelen = 0; if (!(*line = malloc(linesize))) { /* no memory */ if (closefunc) (void) closefunc(readfd, errdesc, errlen); return 0; } **line = 0; top: /* this loops until buf is empty, or we hit the end of a line */ while (*bufcur < *buflen) { /* looking for eol - \n or \0 * eol points to the line ending \0, or NULL if line continues. * srcsize set to number of chars to copy into line. */ if (!(eol = strchr(buf + *bufcur, '\n'))) { if (!(eol = memchr(buf + *bufcur, '\0', *buflen - *bufcur))) { /* couldn't find '\n' or '\0' */ srcsize = *buflen - *bufcur; } else { /* found '\0' */ srcsize = eol - buf - *bufcur + 1; } } else { /* found '\n' */ srcsize = eol - buf - *bufcur + 1; /* change \n into \0 */ *eol = 0; } /* double size of *line until it's big enough for srcsize characters */ while ((int)srcsize > (linesize - linelen)) { char *newline; linesize *= 2; if (!(newline = realloc(*line, linesize))) { /* not enough memory */ free(*line); *line = NULL; if (closefunc) (void) closefunc(readfd, errdesc, errlen); return 0; } *line = newline; } /* copy buf into line and modify pointers */ memcpy(*line + linelen, buf + *bufcur, srcsize); linelen += srcsize; *bufcur += srcsize; /* Are we at the end of the string? */ if (eol) { /* change the first \r into a \0 * This handles when they send \r\n but not \n\r */ if ((eol = strchr(*line, '\r')) != NULL) *eol = 0; /* This is the (only) happy return from this function */ return 1; } } //while (*bufcur < *buflen) /* we get here when there wasn't the end of a string in the buffer */ *bufcur = 0; /* read in some data */ if ((*buflen = readfunc(readfd, buf, bufsize - 1, errdesc, errlen)) <= 0) { /* read either errored, or the socket is closed */ free(*line); *line = NULL; if (closefunc) (void) closefunc(readfd, errdesc, errlen); return 0; } /* null terminate - so strchr knows when to stop */ buf[*buflen] = 0; /* go process it */ goto top; }
/** * @brief * This function provides the port forwarding feature for forwarding the * X data from mom to qsub and from qsub to the X server. * * @param socks[in] - Input structure which tracks the sockets that are active * and data read/written by peers. * @param connfunc[in] - Function pointer pointing to a function used for * either connecting the X server (if running in qsub) or * connecting qsub (if running in mom). * @param phost[in] - peer host that needs to be connected. * @param pport[in] - peer port number. * @param inter_read_sock[in] - socket descriptor from where mom and qsub * readers read data. * @param readfunc[in] - function pointer pointing to the mom and qsub readers. * @param logfunc[in] - Function pointer for log function * * @return void */ void port_forwarder( struct pfwdsock *socks, int (*connfunc)(char *, long), char *phost, int pport, int inter_read_sock, int (*readfunc)(int), void (*logfunc) (char *)) { fd_set rfdset, wfdset, efdset; int rc, maxsock = 0; struct sockaddr_in from; pbs_socklen_t fromlen; int n, n2, sock; fromlen = sizeof(from); char err_msg[LOG_BUF_SIZE]; int readfunc_ret; /* * Make the sockets in the socks structure non blocking */ for (n = 0; n < NUM_SOCKS; n++) { if (!(socks + n)->active || ((socks + n)->sock < 0)) continue; if (set_nonblocking((socks + n)->sock) == -1) { close((socks + n)->sock); (socks + n)->active = 0; snprintf(err_msg, sizeof(err_msg), "set_nonblocking failed for socket=%d, errno=%d", (socks + n)->sock, errno); PF_LOGGER(logfunc, err_msg); continue; } if (set_nodelay((socks + n)->sock) == -1) { snprintf(err_msg, sizeof(err_msg), "set_nodelay failed for socket=%d, errno=%d", (socks + n)->sock, errno); PF_LOGGER(logfunc, err_msg); } } while (x11_reader_go) { FD_ZERO(&rfdset); FD_ZERO(&wfdset); FD_ZERO(&efdset); maxsock = inter_read_sock + 1; /*setting the sock fd in rfdset for qsub and mom readers to read data*/ FD_SET(inter_read_sock, &rfdset); FD_SET(inter_read_sock, &efdset); for (n = 0; n < NUM_SOCKS; n++) { if (!(socks + n)->active || ((socks + n)->sock < 0)) continue; if ((socks + n)->listening) { FD_SET((socks + n)->sock, &rfdset); maxsock = (socks + n)->sock > maxsock ?(socks + n)->sock : maxsock; } else{ if ((socks + n)->bufavail < PF_BUF_SIZE) { FD_SET((socks + n)->sock, &rfdset); maxsock = (socks + n)->sock > maxsock ?(socks + n)->sock : maxsock; } if ((socks + ((socks + n)->peer))->bufavail - (socks + ((socks + n)->peer))->bufwritten > 0) { FD_SET((socks + n)->sock, &wfdset); maxsock = (socks + n)->sock > maxsock ?(socks + n)->sock : maxsock; } } } maxsock++; rc = select(maxsock, &rfdset, &wfdset, &efdset, NULL); if ((rc == -1) && (errno == EINTR)) continue; if (rc < 0) { snprintf(err_msg, sizeof(err_msg), "port forwarding select() error"); PF_LOGGER(logfunc, err_msg); return; } if (FD_ISSET(inter_read_sock, &efdset)) { snprintf(err_msg, sizeof(err_msg), "exception for socket=%d, errno=%d", inter_read_sock, errno); PF_LOGGER(logfunc, err_msg); close(inter_read_sock); return; } if (FD_ISSET(inter_read_sock, &rfdset)) { /*calling mom/qsub readers*/ readfunc_ret = readfunc(inter_read_sock); if (readfunc_ret == -1) { snprintf(err_msg, sizeof(err_msg), "readfunc failed for socket:%d", inter_read_sock); PF_LOGGER(logfunc, err_msg); } if (readfunc_ret < 0) { return; } } for (n = 0; n < NUM_SOCKS; n++) { if (!(socks + n)->active || ((socks + n)->sock < 0)) continue; if (FD_ISSET((socks + n)->sock, &rfdset)) { if ((socks + n)->listening && (socks + n)->active) { int newsock = 0, peersock = 0; if ((sock = accept((socks + n)->sock, (struct sockaddr *) & from, &fromlen)) < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR) || (errno == ECONNABORTED)) continue; snprintf(err_msg, sizeof(err_msg), "closing the socket %d after accept call failure, errno=%d", (socks + n)->sock, errno); PF_LOGGER(logfunc, err_msg); close((socks + n)->sock); (socks + n)->active = 0; continue; } /* * Make the sock non blocking */ if (set_nonblocking(sock) == -1) { snprintf(err_msg, sizeof(err_msg), "set_nonblocking failed for socket=%d, errno=%d", sock, errno); PF_LOGGER(logfunc, err_msg); close(sock); continue; } if (set_nodelay(sock) == -1) { snprintf(err_msg, sizeof(err_msg), "set_nodelay failed for socket=%d, errno=%d", sock, errno); PF_LOGGER(logfunc, err_msg); } newsock = peersock = 0; for (n2 = 0; n2 < NUM_SOCKS; n2++) { if ((socks + n2)->active || (((socks + n2)->peer != 0) && (socks + ((socks + n2)->peer))->active)) continue; if (newsock == 0) newsock = n2; else if (peersock == 0) peersock = n2; else break; } (socks + newsock)->sock = (socks + peersock)->remotesock = sock; (socks + newsock)->listening = (socks + peersock)->listening = 0; (socks + newsock)->active = (socks + peersock)->active = 1; (socks + peersock)->sock = connfunc(phost, pport); /* * Make sockets non-blocking */ if (set_nonblocking((socks + peersock)->sock) == -1) { snprintf(err_msg, sizeof(err_msg), "set_nonblocking failed for socket=%d, errno=%d", (socks + peersock)->sock, errno); PF_LOGGER(logfunc, err_msg); close((socks + peersock)->sock); (socks + peersock)->active = 0; continue; } if (set_nodelay((socks + peersock)->sock) == -1) { snprintf(err_msg, sizeof(err_msg), "set_nodelay failed for socket=%d, errno=%d", (socks + peersock)->sock, errno); PF_LOGGER(logfunc, err_msg); } (socks + newsock)->bufwritten = (socks + peersock)->bufwritten = 0; (socks + newsock)->bufavail = (socks + peersock)->bufavail = 0; (socks + newsock)->buff[0] = (socks + peersock)->buff[0] = '\0'; (socks + newsock)->peer = peersock; (socks + peersock)->peer = newsock; } else{ /* non-listening socket to be read */ rc = read( (socks + n)->sock, (socks + n)->buff + (socks + n)->bufavail, PF_BUF_SIZE - (socks + n)->bufavail); if (rc == -1) { if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR) || (errno == EINPROGRESS)) { continue; } shutdown((socks + n)->sock, SHUT_RDWR); close((socks + n)->sock); (socks + n)->active = 0; snprintf(err_msg, sizeof(err_msg), "closing the socket %d after read failure, errno=%d", (socks + n)->sock, errno); PF_LOGGER(logfunc, err_msg); } else if (rc == 0) { shutdown((socks + n)->sock, SHUT_RDWR); close((socks + n)->sock); (socks + n)->active = 0; } else{ (socks + n)->bufavail += rc; } } } /* END if rfdset */ if (FD_ISSET((socks + n)->sock, &wfdset)) { int peer = (socks + n)->peer; rc = write( (socks + n)->sock, (socks + peer)->buff + (socks + peer)->bufwritten, (socks + peer)->bufavail - (socks + peer)->bufwritten); if (rc == -1) { if ((errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR) || (errno == EINPROGRESS)) { continue; } shutdown((socks + n)->sock, SHUT_RDWR); close((socks + n)->sock); (socks + n)->active = 0; snprintf(err_msg, sizeof(err_msg), "closing the socket %d after write failure, errno=%d", (socks + n)->sock, errno); PF_LOGGER(logfunc, err_msg); } else if (rc == 0) { shutdown((socks + n)->sock, SHUT_RDWR); close((socks + n)->sock); (socks + n)->active = 0; } else{ (socks + peer)->bufwritten += rc; } } /* END if wfdset */ if (!(socks + n)->listening) { int peer = (socks + n)->peer; if ((socks + peer)->bufavail == (socks + peer)->bufwritten) { (socks + peer)->bufavail = (socks + peer)->bufwritten = 0; } if (!(socks + peer)->active && ((socks + peer)->bufwritten == (socks + peer)->bufavail)) { shutdown((socks + n)->sock, SHUT_RDWR); close((socks + n)->sock); (socks + n)->active = 0; } } } /* END foreach fd */ } /* END while(x11_reader_go) */ } /* END port_forwarder() */