void *read_thread(void *data) { struct rtinfo *rip = data; int len; char buffer[65536]; if (debug) printf("read_thread(), fd=%d\n", rip->fd); do { len = s_read(rip->fd, buffer, sizeof(buffer)); if (len < 0) { perror("read"); return NULL; } rip->rbytes += len; } while (len > 0); if (debug) printf("read_thread(), rbytes=%d\n", rip->rbytes); return rip; }
char *fd_getline(CLI *c, SOCKET fd) { char *line; size_t ptr=0, allocated=32; line=str_alloc(allocated); for(;;) { if(ptr>65536) { /* >64KB --> DoS protection */ s_log(LOG_ERR, "fd_getline: Line too long"); str_free(line); longjmp(c->err, 1); } if(allocated<ptr+1) { allocated*=2; line=str_realloc(line, allocated); } s_read(c, fd, line+ptr, 1); if(line[ptr]=='\r') continue; if(line[ptr]=='\n') break; if(line[ptr]=='\0') break; ++ptr; } line[ptr]='\0'; s_log(LOG_DEBUG, " <- %s", line); return line; }
/* ** Load more data into the buffer */ static inline int _fd_fill(FDBUF *fdp) { int maxlen, len, avail, err; if (fdp->in_start == fdp->in_end) fdp->in_start = fdp->in_end = 0; /* Free space in the input buffer */ maxlen = fdp->inbufsize - fdp->in_end; if (maxlen == 0) return 0; avail = 0; err = ioctl(fdp->fd, FIONREAD, &avail); if (err < 0 || avail == 0) avail = 1; if (avail > maxlen) avail = maxlen; len = s_read(fdp->fd, fdp->inbuf+fdp->in_start, avail); if (len == 0) return -1; else if (len < 0) { return errno; } fdp->in_end += len; return 0; }
static inline ssize_t tls_pull(gnutls_transport_ptr ptr, void *buf, size_t size) { struct gnutella_socket *s = ptr; ssize_t ret; int saved_errno; socket_check(s); g_assert(is_valid_fd(s->file_desc)); ret = s_read(s->file_desc, buf, size); saved_errno = errno; tls_signal_pending(s); if ((ssize_t) -1 == ret) { tls_set_errno(s, saved_errno); if (!is_temporary_error(saved_errno)) { socket_connection_reset(s); } } else if (0 == ret) { socket_eof(s); } tls_transport_debug("tls_pull", s, size, ret); errno = saved_errno; return ret; }
void shell_loop(int sock, int pty, int crypt) { DEBUG("shell_loop called.\n"); fd_set fds; char buf[MAX_LEN]; int res, maxfd; ssize_t (*s_read)(); ssize_t (*s_write)(); if (crypt) { s_read = crypt_read; s_write = crypt_write; } else { char *sys_write = strdup(SYS_WRITE); char *sys_read = strdup(SYS_READ); x(sys_write); x(sys_read); s_read = dlsym(RTLD_NEXT, sys_read); s_write = dlsym(RTLD_NEXT, sys_write); cleanup(sys_write,strlen(sys_write)); cleanup(sys_read,strlen(sys_read)); } maxfd = pty; if (sock > maxfd) maxfd = sock; while(1) { FD_ZERO(&fds); FD_SET(sock, &fds); FD_SET(pty, &fds); if((res = select(maxfd+1, &fds, NULL, NULL, NULL)) == -1) DEBUG("Select failed.\n"); if(FD_ISSET(sock, &fds)) { memset(&buf, 0x00, MAX_LEN); if((res = s_read(sock, buf, MAX_LEN)) <= 0) { DEBUG("Error reading from client\n"); exit(1); } else { write(pty, buf, res); } } if(FD_ISSET(pty, &fds)) { memset(&buf, 0x00, MAX_LEN); if((res = read(pty, buf, MAX_LEN-31)) <= 0) { DEBUG("Error reading from pty\n"); exit(1); } else { s_write(sock, buf, res); } } } }
NOEXPORT void pgsql_client(CLI *c) { u8 buffer[1]; s_write(c, c->remote_fd.fd, ssl_request, sizeof ssl_request); s_read(c, c->remote_fd.fd, buffer, 1); /* S - accepted, N - rejected, non-SSL preferred */ if(buffer[0]!='S') { s_log(LOG_ERR, "PostgreSQL server rejected SSL"); longjmp(c->err, 1); } }
void int_80r(FileDesc fd, void * buff, int size) { int i = 0; switch(fd) { case KEYBOARD: k_read((char *)buff,size); break; case SCREENNL: s_read((char *)buff,size); break; case PCI : l_in(0xCFC, buff,size); break; } }
/** * @brief s_getc - get a character from serial port * * @return character read or error */ signed int s_getc(void) { unsigned char x = 0; int ret = 0; ret = s_read(&x, 1); if (ret < 0) { S_ERROR("getc failed-%d\n", ret); return ret; } S_INFO("[%c]%x", x, x); return x; }
NOEXPORT void pgsql_server(CLI *c) { u8 buffer[8], ssl_ok[1]={'S'}; memset(buffer, 0, sizeof buffer); s_read(c, c->local_rfd.fd, buffer, sizeof buffer); if(memcmp(buffer, ssl_request, sizeof ssl_request)) { s_log(LOG_ERR, "PostgreSQL client did not request SSL, rejecting"); /* no way to send error on startup, so just drop the client */ longjmp(c->err, 1); } s_write(c, c->local_wfd.fd, ssl_ok, sizeof ssl_ok); }
NOEXPORT void cifs_server(CLI *c) { u8 buffer[128]; u8 response_access_denied[5] = {0x83, 0, 0, 1, 0x81}; u8 response_use_ssl[5] = {0x83, 0, 0, 1, 0x8e}; u16 len; s_read(c, c->local_rfd.fd, buffer, 4) ;/* NetBIOS header */ len=buffer[3]; len|=(u16)(buffer[2]) << 8; if(len>sizeof buffer-4) { s_log(LOG_ERR, "Received block too long"); longjmp(c->err, 1); } s_read(c, c->local_rfd.fd, buffer+4, len); if(buffer[0]!=0x81) { /* NB_SSN_REQUEST */ s_log(LOG_ERR, "Client did not send session setup"); s_write(c, c->local_wfd.fd, response_access_denied, 5); longjmp(c->err, 1); } s_write(c, c->local_wfd.fd, response_use_ssl, 5); }
void read_type_form(FILE *fp) { CELL *cell1, *cell2; int i, j; LineNo = 1; i = 1; while (! s_feof(fp)) { LineNoForError = LineNo; cell1 = s_read(fp); Type[i].name = (U_CHAR *) my_alloc((sizeof(U_CHAR)*strlen(_Atom(car(cell1)))) + 1); strcpy(Type[i].name, _Atom(car(cell1))); cell1 = car(cdr(cell1)); j = 1; while (!Null(cell2 = car(cell1))) { Form[i][j].name = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cell2)))) + 1); strcpy(Form[i][j].name, _Atom(car(cell2))); Form[i][j].gobi = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cdr(cell2))))) + 1); if (strcmp(_Atom(car(cdr(cell2))), "*") == 0) strcpy(Form[i][j].gobi, ""); else strcpy(Form[i][j].gobi, _Atom(car(cdr(cell2)))); if (!Null(car(cdr(cdr(cell2))))) { /* 語尾の表記に漢字が混ざっている場合 */ Form[i][j].gobi_yomi = (U_CHAR *) my_alloc((sizeof(U_CHAR) * strlen(_Atom(car(cdr(cdr(cell2)))))) + 1); if (strcmp(_Atom(car(cdr(cdr(cell2)))), "*") == 0) strcpy(Form[i][j].gobi_yomi, ""); else strcpy(Form[i][j].gobi_yomi, _Atom(car(cdr(cdr(cell2))))); } else { Form[i][j].gobi_yomi = (U_CHAR *) my_alloc(sizeof(U_CHAR) * strlen(Form[i][j].gobi) + 1); strcpy(Form[i][j].gobi_yomi, Form[i][j].gobi); } j++; cell1 = cdr(cell1); } i++; } }
static String * httplogin(void) { String *s=s_new(); Biobuf *b; if((b = wBopen(".httplogin", OREAD)) == nil) goto Return; while(s_read(b, s, Bsize) > 0) ; Bterm(b); Return: return s; }
static String* Brdstring(Biobuf *b) { int32_t len; String *s; Dir *d; d = dirfstat(Bfildes(b)); if (d == nil) /* shouldn't happen, we just opened it */ len = 0; else len = d->length; free(d); s = s_newalloc(len); s_read(b, s, len); return s; }
NOEXPORT void cifs_client(CLI *c) { u8 buffer[5]; u8 request_dummy[4] = {0x81, 0, 0, 0}; /* a zero-length request */ s_write(c, c->remote_fd.fd, request_dummy, 4); s_read(c, c->remote_fd.fd, buffer, 5); if(buffer[0]!=0x83) { /* NB_SSN_NEGRESP */ s_log(LOG_ERR, "Negative response expected"); longjmp(c->err, 1); } if(buffer[2]!=0 || buffer[3]!=1) { /* length != 1 */ s_log(LOG_ERR, "Unexpected NetBIOS response size"); longjmp(c->err, 1); } if(buffer[4]!=0x8e) { /* use SSL */ s_log(LOG_ERR, "Remote server does not require SSL"); longjmp(c->err, 1); } }
static inline ssize_t unix_read(int fd, void *buf, size_t size) { /* * On Windows, we have to call s_read() for sockets, not read(), * or it does not work since winsock descriptors are distinct * from other file objects and the Windows kernel is too stupid * to do the dirty work for us. * --RAM, 2011-01-05 */ if (is_running_on_mingw()) { ssize_t ret = s_read(fd, buf, size); if (ret >= 0 || ENOTSOCK != errno) return ret; /* FALL THROUGH -- fd is a plain file, not a socket */ } return read(fd, buf, size); }
bool load_cached_os_font_list(void) { char *file_name = obs_module_config_path("font_data.bin"); uint32_t old_checksum; uint32_t new_checksum; struct serializer s; uint32_t ver; bool success; success = file_input_serializer_init(&s, file_name); bfree(file_name); if (!success) return false; success = read_data(&s, &ver, sizeof(ver)); if (!success || ver != font_cache_ver) { success = false; goto finish; } success = s_read(&s, &old_checksum, sizeof(old_checksum)); new_checksum = get_font_checksum(); if (!success || old_checksum != new_checksum) { success = false; goto finish; } success = load_cached_font_list(&s); finish: file_input_serializer_free(&s); return success; }
static rc_t old_KBZipFileReadInt ( KBZipFile * self, void * buffer, size_t bsize, size_t * num_read ) { rc_t rc = 0; size_t numRead = 0; do { int ret; size_t have = 0; if (self->BZ2_bzDecompressResult == BZ_STREAM_END) break; ret = s_read(self, (char*)buffer + numRead, bsize - numRead, &have, &rc); if ( ret == -70 ) /* rc hack - known to not collide with bzlib errors */ return rc; self->BZ2_bzDecompressResult = ret; if (!have) break; numRead += have; } while (numRead != bsize); *num_read = numRead; return rc; }
static int do_ssh_init(void) { char c; char version[10]; char vstring[40]; int i; #ifdef FWHACK i = 0; while (s_read(&c, 1) == 1) { if (c == 'S' && i < 2) i++; else if (c == 'S' && i == 2) i = 2; else if (c == 'H' && i == 2) break; else i = 0; } #else if (s_read(&c,1) != 1 || c != 'S') return 0; if (s_read(&c,1) != 1 || c != 'S') return 0; if (s_read(&c,1) != 1 || c != 'H') return 0; #endif if (s_read(&c,1) != 1 || c != '-') return 0; i = 0; while (1) { if (s_read(&c,1) != 1) return 0; if (i >= 0) { if (c == '-') { version[i] = '\0'; i = -1; } else if (i < sizeof(version)-1) version[i++] = c; } else if (c == '\n') break; } sprintf(vstring, "SSH-%s-7.7.7\n", (strcmp(version, "1.5") <= 0 ? version : "1.5")); s_write(vstring, strlen(vstring)); return 1; }
static inline bool read_data(struct serializer *s, void *data, size_t size) { return s_read(s, data, size) == size; }
NOEXPORT void ntlm(CLI *c) { char *line, buf[BUFSIZ], *ntlm1_txt, *ntlm2_txt, *ntlm3_txt, *tmpstr; long content_length=0; /* no HTTP content */ /* send Proxy-Authorization (phase 1) */ fd_printf(c, c->remote_fd.fd, "Proxy-Connection: keep-alive"); ntlm1_txt=ntlm1(); if(!ntlm1_txt) { s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM request"); longjmp(c->err, 1); } fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm1_txt); str_free(ntlm1_txt); fd_putline(c, c->remote_fd.fd, ""); /* empty line */ line=fd_getline(c, c->remote_fd.fd); /* receive Proxy-Authenticate (phase 2) */ if(!is_prefix(line, "HTTP/1.0 407") && !is_prefix(line, "HTTP/1.1 407")) { s_log(LOG_ERR, "Proxy-Authenticate: NTLM authorization request rejected"); do { /* read all headers */ str_free(line); line=fd_getline(c, c->remote_fd.fd); } while(*line); str_free(line); longjmp(c->err, 1); } ntlm2_txt=NULL; do { /* read all headers */ str_free(line); line=fd_getline(c, c->remote_fd.fd); if(is_prefix(line, "Proxy-Authenticate: NTLM ")) ntlm2_txt=str_dup(line+25); else if(is_prefix(line, "Content-Length: ")) { content_length=strtol(line+16, &tmpstr, 10); if(tmpstr==line+16 || *tmpstr || content_length<0) { s_log(LOG_ERR, "Proxy-Authenticate: Invalid Content-Length"); str_free(line); longjmp(c->err, 1); } } } while(*line); if(!ntlm2_txt) { /* no Proxy-Authenticate: NTLM header */ s_log(LOG_ERR, "Proxy-Authenticate: NTLM header not found"); str_free(line); longjmp(c->err, 1); } /* read and ignore HTTP content (if any) */ while(content_length>0) { s_read(c, c->remote_fd.fd, buf, s_min(content_length, BUFSIZ)); content_length-=s_min(content_length, BUFSIZ); } /* send Proxy-Authorization (phase 3) */ fd_printf(c, c->remote_fd.fd, "CONNECT %s HTTP/1.1", c->opt->protocol_host); fd_printf(c, c->remote_fd.fd, "Host: %s", c->opt->protocol_host); ntlm3_txt=ntlm3(c->opt->protocol_username, c->opt->protocol_password, ntlm2_txt); str_free(ntlm2_txt); if(!ntlm3_txt) { s_log(LOG_ERR, "Proxy-Authenticate: Failed to build NTLM response"); longjmp(c->err, 1); } fd_printf(c, c->remote_fd.fd, "Proxy-Authorization: NTLM %s", ntlm3_txt); str_free(ntlm3_txt); }
int exif_orientation(const fileinfo_t *file) { int fd; unsigned char data[EXIF_MAX_LEN]; byteorder_t order = BO_BIG_ENDIAN; unsigned int cnt, len, idx, val; if (file == NULL || file->path == NULL) return -1; fd = open(file->path, O_RDONLY); if (fd < 0) return -1; if (s_read(fd, file->name, data, 2) < 0) goto abort; if (btous(data, order) != JPEG_MARKER_SOI) goto abort; if (s_read(fd, file->name, data, 4) < 0) goto abort; if (btous(data, order) == JPEG_MARKER_APP0) { len = btous(data + 2, order); if (lseek(fd, len - 2, SEEK_CUR) == (off_t) -1) goto abort; if (s_read(fd, file->name, data, 4) < 0) goto abort; } if (btous(data, order) != JPEG_MARKER_APP1) goto abort; len = btous(data + 2, order); if (len < 8) goto abort; if (s_read(fd, file->name, data, 6) < 0) goto abort; if (btoui(data, order) != EXIF_HEAD) goto abort; len -= 8; if (len < 12 || len > EXIF_MAX_LEN) goto abort; if (s_read(fd, file->name, data, len) < 0) goto abort; switch (btous(data, order)) { case EXIF_BO_BIG_ENDIAN: order = BO_BIG_ENDIAN; break; case EXIF_BO_LITTLE_ENDIAN: order = BO_LITTLE_ENDIAN; break; default: goto abort; break; } if (btous(data + 2, order) != EXIF_TAG_MARK) goto abort; idx = btoui(data + 4, order); if (idx > len - 2) goto abort; val = 0; cnt = btous(data + idx, order); for (idx += 2; cnt > 0 && idx < len - 12; cnt--, idx += 12) { if (btous(data + idx, order) == EXIF_TAG_ORIENTATION) { val = btous(data + idx + 8, order); break; } } close(fd); return val; abort: close(fd); return -1; }
void main(int argc, char **argv) { String *msg; String *firstline; char *listname, *alfile; Waitmsg *w; int fd; char *replytoname = nil; ARGBEGIN{ case 'r': replytoname = ARGF(); break; }ARGEND; rfork(RFENVG|RFREND); if(argc < 2) usage(); alfile = argv[0]; listname = argv[1]; if(replytoname == nil) replytoname = listname; readaddrs(alfile); if(Binit(&in, 0, OREAD) < 0) sysfatal("opening input: %r"); msg = s_new(); firstline = s_new(); /* discard the 'From ' line */ if(s_read_line(&in, firstline) == nil) sysfatal("reading input: %r"); /* read up to the first 128k of the message. more is ridiculous. Not if word documents are distributed. Upped it to 2MB (pb) */ if(s_read(&in, msg, 2*1024*1024) <= 0) sysfatal("reading input: %r"); /* parse the header */ yyinit(s_to_c(msg), s_len(msg)); yyparse(); /* get the sender */ getaddrs(); if(from == nil) from = sender; if(from == nil) sysfatal("message must contain From: or Sender:"); if(strcmp(listname, s_to_c(from)) == 0) sysfatal("can't remail messages from myself"); addaddr(s_to_c(from)); /* start the mailer up and return a pipe to it */ fd = startmailer(listname); /* send message adding our own reply-to and precedence */ printmsg(fd, msg, replytoname, listname); close(fd); /* wait for mailer to end */ while(w = wait()){ if(w->msg != nil && w->msg[0]) sysfatal("%s", w->msg); free(w); } /* if the mailbox exists, cat the mail to the end of it */ appendtoarchive(listname, firstline, msg); exits(0); }
char* s_reads(const char* path) { return (char*)s_read(path, NULL, 1); }
int load_game_scripts(lua_State* L, const char* zipfile) { size_t size = 0; unsigned char* filedata = s_read(zipfile, &size, 0); if (!filedata) { fprintf(stderr, "unable to read zipfile = %s\n", zipfile); return 1; } unzFile unzfile = unzOpenBuffer(filedata, size); if(!unzfile) { fprintf(stderr, "open zip from buffer failed.\n"); return 1; } lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); char filename[1024] = ""; int err = unzGoToFirstFile(unzfile); if (err) { fprintf(stderr, "go to first file failed"); return 1; } bool succeed = true; while (true) { unz_file_info info; err = unzGetCurrentFileInfo(unzfile, &info, filename, 1024, NULL, 0, NULL, 0); if (err) { fprintf(stderr, "get current file info failed, filename = %s\n", filename); succeed = false; break; } unz_file_pos file_pos; err = unzGetFilePos(unzfile, &file_pos); err = unzGoToFilePos(unzfile, &file_pos); err = unzOpenCurrentFile(unzfile); unsigned long size = info.uncompressed_size; unsigned char* buffer = s_malloc(size); unsigned int readed = unzReadCurrentFile(unzfile, buffer, (unsigned int)size); if(readed != size) { succeed = false; fprintf(stderr, "read zip file failed? error size, required = %ld, readed = %d, filename = %s\n", size, readed, filename); goto error; } err = luaL_loadbuffer(L, (const char*)buffer, size, filename); if(err) { fprintf(stderr, "error loadL_loadbuffer, filename = %s\n", filename); goto error; } lua_setfield(L, -2, filename); if(unzGoToNextFile(unzfile) == UNZ_END_OF_LIST_OF_FILE) { succeed = true; break; } unzCloseCurrentFile(unzfile); s_free(buffer); continue; error: succeed = false; unzCloseCurrentFile(unzfile); s_free(buffer); break; } lua_pop(L, -1); return succeed ? 1 : 0; }
/* read in a message, interpret the 'From' header */ extern message * m_read(Biobuf *fp, int rmail, int interactive) { message *mp; Resub subexp[10]; char *line; int first; int n; mp = m_new(); /* parse From lines if remote */ if (rmail) { /* get remote address */ String *sender=s_new(); if (rfprog == 0) rfprog = regcomp(REMFROMRE); first = 1; while(s_read_line(fp, s_restart(mp->body)) != 0) { memset(subexp, 0, sizeof(subexp)); if (regexec(rfprog, s_to_c(mp->body), subexp, 10) == 0){ if(first == 0) break; if (fprog == 0) fprog = regcomp(FROMRE); memset(subexp, 0, sizeof(subexp)); if(regexec(fprog, s_to_c(mp->body), subexp,10) == 0) break; s_restart(mp->body); append_match(subexp, s_restart(sender), SENDERMATCH); append_match(subexp, s_restart(mp->date), DATEMATCH); break; } append_match(subexp, s_restart(sender), REMSENDERMATCH); append_match(subexp, s_restart(mp->date), REMDATEMATCH); if(subexp[REMSYSMATCH].sp!=subexp[REMSYSMATCH].ep){ append_match(subexp, mp->sender, REMSYSMATCH); s_append(mp->sender, "!"); } first = 0; } s_append(mp->sender, s_to_c(sender)); s_free(sender); } if(*s_to_c(mp->sender)=='\0') default_from(mp); /* if sender address is unreturnable, treat message as bulk mail */ if(!returnable(s_to_c(mp->sender))) mp->bulk = 1; /* get body */ if(interactive && !rmail){ /* user typing on terminal: terminator == '.' or EOF */ for(;;) { line = s_read_line(fp, mp->body); if (line == 0) break; if (strcmp(".\n", line)==0) { mp->body->ptr -= 2; *mp->body->ptr = '\0'; break; } } mp->size = mp->body->ptr - mp->body->base; } else { /* * read up to VMLIMIT bytes (more or less) into main memory. * if message is longer put the rest in a tmp file. */ mp->size = mp->body->ptr - mp->body->base; n = s_read(fp, mp->body, VMLIMIT); if(n < 0){ perror("m_read"); exit(1); } mp->size += n; if(n == VMLIMIT){ if(m_read_to_file(fp, mp) < 0){ perror("m_read"); exit(1); } } } /* * ignore 0 length messages from a terminal */ if (!rmail && mp->size == 0) return 0; rfc822cruft(mp); return mp; }