📄 xdr_rec.c
字号:
break; } return (u_int) pos;}static bool_txdrrec_setpos (XDR *xdrs, u_int pos){ 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: break; } return FALSE;}static int32_t *xdrrec_inline (XDR *xdrs, int len){ 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; default: break; } return buf;}static voidxdrrec_destroy (XDR *xdrs){ 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));}static bool_txdrrec_getint32 (XDR *xdrs, int32_t *ip){ RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; int32_t *bufip = (int32_t *) rstrm->in_finger; int32_t mylong; /* first try the inline, fast case */ if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT && rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT) { *ip = ntohl (*bufip); rstrm->fbtbc -= BYTES_PER_XDR_UNIT; rstrm->in_finger += BYTES_PER_XDR_UNIT; } else { if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong, BYTES_PER_XDR_UNIT)) return FALSE; *ip = ntohl (mylong); } return TRUE;}static bool_txdrrec_putint32 (XDR *xdrs, const int32_t *ip){ RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; int32_t *dest_ip = (int32_t *) rstrm->out_finger; if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry) { /* * this case should almost never happen so the code is * inefficient */ rstrm->out_finger -= BYTES_PER_XDR_UNIT; rstrm->frag_sent = TRUE; if (!flush_out (rstrm, FALSE)) return FALSE; dest_ip = (int32_t *) rstrm->out_finger; rstrm->out_finger += BYTES_PER_XDR_UNIT; } *dest_ip = htonl (*ip); return TRUE;}/* * 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 (XDR *xdrs){ 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;}/* * Lookahead function. * Returns TRUE iff there is no more input in the buffer * after consuming the rest of the current record. */bool_txdrrec_eof (XDR *xdrs){ 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 parameter tells whether the record should be flushed to the * (output) tcp stream. (This lets the package support batched or * pipelined procedure calls.) TRUE => immediate flush to tcp connection. */bool_txdrrec_endofrecord (XDR *xdrs, bool_t sendnow){ RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; u_long len; /* fragment length */ if (sendnow || rstrm->frag_sent || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry) { rstrm->frag_sent = FALSE; return flush_out (rstrm, TRUE); } len = (rstrm->out_finger - (char *) rstrm->frag_header - BYTES_PER_XDR_UNIT); *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG); rstrm->frag_header = (u_int32_t *) rstrm->out_finger; rstrm->out_finger += BYTES_PER_XDR_UNIT; return TRUE;}/* * Internal useful routines */static bool_tinternal_functionflush_out (RECSTREAM *rstrm, bool_t eor){ u_long eormask = (eor == TRUE) ? LAST_FRAG : 0; u_long len = (rstrm->out_finger - (char *) rstrm->frag_header - BYTES_PER_XDR_UNIT); *rstrm->frag_header = htonl (len | eormask); len = rstrm->out_finger - 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 + BYTES_PER_XDR_UNIT; return TRUE;}static bool_t /* knows nothing about records! Only about input buffers */fill_input_buf (RECSTREAM *rstrm){ caddr_t where; size_t i; int len; where = rstrm->in_base; i = (size_t) 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 */internal_functionget_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len){ int current; while (len > 0) { current = rstrm->in_boundry - 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 */internal_functionset_input_fragment (RECSTREAM *rstrm){ uint32_t header; if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT)) return FALSE; header = ntohl (header); rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; /* * Sanity check. Try not to accept wildly incorrect fragment * sizes. Unfortunately, only a size of zero can be identified as * 'wildely incorrect', and this only, if it is not the last * fragment of a message. Ridiculously large fragment 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. Many existing RPC * implementations may sent a fragment of size zero as the last * fragment of a message. */ if (header == 0) return FALSE; rstrm->fbtbc = header & ~LAST_FRAG; return TRUE;}static bool_t /* consumes input bytes; knows nothing about records! */internal_functionskip_input_bytes (RECSTREAM *rstrm, long cnt){ int current; while (cnt > 0) { current = rstrm->in_boundry - 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_intinternal_functionfix_buf_size (u_int s){ if (s < 100) s = 4000; return RNDUP (s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -