📄 xdr_rec.c
字号:
register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; register long pos; pos = lseek((int)(long)rstrm->tcp_handle, (off_t) 0, 1); if (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 = -1; break; } return ((u_int) pos);}static bool_txdrrec_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; case XDR_FREE: /* to avoid warning */ break; } return (FALSE);}static int32_t *xdrrec_inline(xdrs, len) register XDR *xdrs; int len;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; int32_t * buf = NULL; switch (xdrs->x_op) { case XDR_ENCODE: if ((rstrm->out_finger + len) <= rstrm->out_boundry) { buf = (int32_t *) rstrm->out_finger; rstrm->out_finger += len; } break; case XDR_DECODE: if ((len <= rstrm->fbtbc) && ((rstrm->in_finger + len) <= rstrm->in_boundry)) { buf = (int32_t *) rstrm->in_finger; rstrm->fbtbc -= len; rstrm->in_finger += len; } break; case XDR_FREE: /* to avoid warning */ break; } return (buf);}static voidxdrrec_destroy(xdrs) register XDR *xdrs;{ register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; mem_free(rstrm->the_buffer, 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, 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, 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_int32_t) >= (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_int32_t); *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG); rstrm->frag_header = (u_int32_t *)rstrm->out_finger; rstrm->out_finger += sizeof(u_int32_t); return (TRUE);}/* * Internal useful routines */static bool_tflush_out(rstrm, eor) register RECSTREAM *rstrm; bool_t eor;{ register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0; register u_int32_t len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - sizeof(u_int32_t); *(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_int32_t *)rstrm->out_base; rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t); return (TRUE);}static bool_t /* knows nothing about records! Only about input buffers */fill_input_buf(rstrm) register RECSTREAM *rstrm;{ register caddr_t where; u_long i; register long len; where = rstrm->in_base; i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT; where += i; len = rstrm->in_size - i; if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1) return (FALSE); rstrm->in_finger = where; where += len; rstrm->in_boundry = where; return (TRUE);}static 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 long current; while (len > 0) { current = (long)rstrm->in_boundry - (long)rstrm->in_finger; if (current == 0) { if (! fill_input_buf(rstrm)) return (FALSE); continue; } current = (len < current) ? len : current; memcpy(addr, rstrm->in_finger, current); rstrm->in_finger += current; addr += current; len -= current; } return (TRUE);}static bool_t /* next two bytes of the input stream are treated as a header */set_input_fragment(rstrm) register RECSTREAM *rstrm;{ u_int32_t header; if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header))) return (FALSE); header = (long)ntohl(header); rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; /* * Sanity check. Try not to accept wildly incorrect * record sizes. Unfortunately, the only record size * we can positively identify as being 'wildly incorrect' * is zero. Ridiculously large record sizes may look wrong, * but we don't have any way to be certain that they aren't * what the client actually intended to send us. */ if (header == 0) return(FALSE); rstrm->fbtbc = header & (~LAST_FRAG); return (TRUE);}static bool_t /* consumes input bytes; knows nothing about records! */skip_input_bytes(rstrm, cnt) register RECSTREAM *rstrm; long cnt;{ register long current; while (cnt > 0) { current = (long)rstrm->in_boundry - (long)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);}static u_intfix_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 + -