📄 xdr_rec.c
字号:
register int current; while (len > 0) { current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger; current = (len < current) ? len : current; bcopy(addr, rstrm->out_finger, current); rstrm->out_finger += current; addr += current; len -= current; if (rstrm->out_finger == rstrm->out_boundry) { rstrm->frag_sent = TRUE; if (! flush_out(rstrm, FALSE)) return (FALSE); } } return (TRUE);}LOCAL u_int /* 4.0 */xdrrec_getpos(xdrs) register XDR *xdrs;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; register u_int pos; pos = lseek((int)rstrm->tcp_handle, (long) 0, 1); if ((int)pos != -1) switch (xdrs->x_op) { case XDR_ENCODE: pos += rstrm->out_finger - rstrm->out_base; break; case XDR_DECODE: pos -= rstrm->in_boundry - rstrm->in_finger; break; default: pos = (u_int) -1; break; } return ((u_int) pos); /* 4.0 */}LOCAL bool_t /* 4.0 */xdrrec_setpos(xdrs, pos) register XDR *xdrs; u_int pos;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; u_int currpos = xdrrec_getpos(xdrs); int delta = currpos - pos; caddr_t newpos; if ((int)currpos != -1) switch (xdrs->x_op) { case XDR_ENCODE: newpos = rstrm->out_finger - delta; if ((newpos > (caddr_t)(rstrm->frag_header)) && (newpos < rstrm->out_boundry)) { rstrm->out_finger = newpos; return (TRUE); } break; case XDR_DECODE: newpos = rstrm->in_finger - delta; if ((delta < (int)(rstrm->fbtbc)) && (newpos <= rstrm->in_boundry) && (newpos >= rstrm->in_base)) { rstrm->in_finger = newpos; rstrm->fbtbc -= delta; return (TRUE); } break; default: /* XDR_FREE */ } return (FALSE);}LOCAL long * /* 4.0 */xdrrec_inline(xdrs, len) register XDR *xdrs; int len;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; long * buf = NULL; switch (xdrs->x_op) { case XDR_ENCODE: if ((rstrm->out_finger + len) <= rstrm->out_boundry) { buf = (long *) rstrm->out_finger; rstrm->out_finger += len; } break; case XDR_DECODE: if ((len <= rstrm->fbtbc) && ((rstrm->in_finger + len) <= rstrm->in_boundry)) { buf = (long *) rstrm->in_finger; rstrm->fbtbc -= len; rstrm->in_finger += len; } break; default: /* XDR_FREE */ break; } return (buf);}LOCAL void /* 4.0 */xdrrec_destroy(xdrs) register XDR *xdrs;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; mem_free (rstrm->the_buffer, /* 4.0 */ rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT); mem_free((caddr_t)rstrm, sizeof(RECSTREAM));}/* * Exported routines to manage xdr records *//* * Before reading (deserializing from the stream, one should always call * this procedure to guarantee proper record alignment. */bool_txdrrec_skiprecord(xdrs) XDR *xdrs;{ register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { if (! skip_input_bytes(rstrm, (int) rstrm->fbtbc)) return (FALSE); rstrm->fbtbc = 0; if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) return (FALSE); } rstrm->last_frag = FALSE; return (TRUE);}/* * Look ahead fuction. * Returns TRUE iff there is no more input in the buffer * after consuming the rest of the current record. */bool_txdrrec_eof(xdrs) XDR *xdrs;{ register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { if (! skip_input_bytes(rstrm, (int) rstrm->fbtbc)) return (TRUE); rstrm->fbtbc = 0; if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) return (TRUE); } if (rstrm->in_finger == rstrm->in_boundry) return (TRUE); return (FALSE);}/* * The client must tell the package when an end-of-record has occurred. * The second paraemters tells whether the record should be flushed to the * (output) tcp stream. (This let's the package support batched or * pipelined procedure calls.) TRUE => immmediate flush to tcp connection. */bool_txdrrec_endofrecord(xdrs, sendnow) XDR *xdrs; bool_t sendnow;{ register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); register u_long len; /* fragment length */ if (sendnow || rstrm->frag_sent || ((u_long)rstrm->out_finger + sizeof(u_long) >= (u_long)rstrm->out_boundry)) { rstrm->frag_sent = FALSE; return (flush_out(rstrm, TRUE)); } len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - sizeof(u_long); *(rstrm->frag_header) = htonl(len | LAST_FRAG); rstrm->frag_header = (u_long *)rstrm->out_finger; rstrm->out_finger += sizeof(u_long); return (TRUE);}/* * Internal useful routines */LOCAL bool_t /* 4.0 */flush_out(rstrm, eor) register RECSTREAM *rstrm; bool_t eor;{ register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0; register u_long len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - sizeof(u_long); *(rstrm->frag_header) = htonl(len | eormask); len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base); if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len) != (int)len) return (FALSE); rstrm->frag_header = (u_long *)rstrm->out_base; rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long); return (TRUE);}LOCAL bool_t /* knows nothing about records! Only about input buffers */fill_input_buf(rstrm) register RECSTREAM *rstrm;{ register caddr_t where; /* 4.0 */ register int len = rstrm->in_size; u_int i; /* 4.0 */ where = rstrm->in_base; /* 4.0 */ i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT; /* 4.0 */ where += i; /* 4.0 */ len = rstrm->in_size - i; /* 4.0 */ if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1) return (FALSE); rstrm->in_finger = where; where += len; rstrm->in_boundry = where; return (TRUE);}LOCAL bool_t /* knows nothing about records! Only about input buffers */get_input_bytes(rstrm, addr, len) register RECSTREAM *rstrm; register caddr_t addr; register int len;{ register int current; while (len > 0) { current = (int)rstrm->in_boundry - (int)rstrm->in_finger; if (current == 0) { if (! fill_input_buf(rstrm)) return (FALSE); continue; } current = (len < current) ? len : current; bcopy(rstrm->in_finger, addr, current); rstrm->in_finger += current; addr += current; len -= current; } return (TRUE);}LOCAL bool_t /* next two bytes of the input stream are treated as a header */set_input_fragment(rstrm) register RECSTREAM *rstrm;{ u_long header; if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header))) return (FALSE); header = ntohl(header); rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; rstrm->fbtbc = header & (~LAST_FRAG); return (TRUE);}LOCAL bool_t /* consumes input bytes; knows nothing about records! */skip_input_bytes(rstrm, cnt) register RECSTREAM *rstrm; int cnt;{ register int current; while (cnt > 0) { current = (int)rstrm->in_boundry - (int)rstrm->in_finger; if (current == 0) { if (! fill_input_buf(rstrm)) return (FALSE); continue; } current = (cnt < current) ? cnt : current; rstrm->in_finger += current; cnt -= current; } return (TRUE);}LOCAL u_int /* 4.0 */fix_buf_size(s) register u_int s;{ if (s < 100) s = 4000; return (RNDUP(s));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -