diff --git a/channels.c b/channels.c index 952d676..440ece3 100644 --- a/channels.c +++ b/channels.c @@ -93,7 +93,7 @@ channel_send(STREAM s, VCHANNEL * channel) /* first fragment sent in-place */ s_pop_layer(s, channel_hdr); - length = s->end - s->p - 8; + length = s_remaining(s) - 8; logger(Protocol, Debug, "channel_send(), channel = %d, length = %d", channel->mcs_id, length); @@ -186,7 +186,7 @@ channel_process(STREAM s, uint16 mcs_channel) in->p = in->data; } - thislength = MIN(s->end - s->p, in->data + in->size - in->p); + thislength = MIN(s_remaining(s), in->data + in->size - in->p); memcpy(in->p, s->p, thislength); in->p += thislength; diff --git a/iso.c b/iso.c index a92c97b..bddfcd6 100644 --- a/iso.c +++ b/iso.c @@ -177,7 +177,7 @@ iso_send(STREAM s) uint16 length; s_pop_layer(s, iso_hdr); - length = s->end - s->p; + length = s_remaining(s); out_uint8(s, T123_HEADER_VERSION); /* version */ out_uint8(s, 0); /* reserved */ diff --git a/lspci.c b/lspci.c index 7690a1f..6b0f4d2 100644 --- a/lspci.c +++ b/lspci.c @@ -136,7 +136,7 @@ lspci_process(STREAM s) static char *rest = NULL; char *buf; - pkglen = s->end - s->p; + pkglen = s_remaining(s); /* str_handle_lines requires null terminated strings */ buf = xmalloc(pkglen + 1); STRNCPY(buf, (char *) s->p, pkglen + 1); diff --git a/mcs.c b/mcs.c index 67ec178..de6ab04 100644 --- a/mcs.c +++ b/mcs.c @@ -306,7 +306,7 @@ mcs_send_to_channel(STREAM s, uint16 channel) uint16 length; s_pop_layer(s, mcs_hdr); - length = s->end - s->p - 8; + length = s_remaining(s) - 8; length |= 0x8000; out_uint8(s, (MCS_SDRQ << 2)); diff --git a/rdp.c b/rdp.c index 2b12a01..ff8ccb3 100644 --- a/rdp.c +++ b/rdp.c @@ -194,7 +194,7 @@ rdp_send_data(STREAM s, uint8 data_pdu_type) uint16 length; s_pop_layer(s, rdp_hdr); - length = s->end - s->p; + length = s_remaining(s); out_uint16_le(s, length); out_uint16_le(s, (RDP_PDU_DATA | 0x10)); diff --git a/rdpdr.c b/rdpdr.c index ac6ecff..14b895e 100644 --- a/rdpdr.c +++ b/rdpdr.c @@ -879,7 +879,7 @@ rdpdr_process(STREAM s) uint16 pakid; logger(Protocol, Debug, "rdpdr_process()"); - /* hexdump(s->p, s->end - s->p); */ + /* hexdump(s->p, s_remaining(s)); */ in_uint16(s, component); /* RDPDR_HEADER.Component */ in_uint16(s, pakid); /* RDPDR_HEADER.PacketId */ diff --git a/rdpsnd.c b/rdpsnd.c index 9289380..dceaa08 100644 --- a/rdpsnd.c +++ b/rdpsnd.c @@ -343,7 +343,7 @@ rdpsnd_process_packet(uint8 opcode, STREAM s) } rdpsnd_queue_write(rdpsnd_dsp_process - (s->p, s->end - s->p, current_driver, + (s->p, s_remaining(s), current_driver, &formats[current_format]), tick, packet_index); return; break; @@ -386,7 +386,7 @@ rdpsnd_process(STREAM s) /* New packet */ if (packet.size == 0) { - if ((s->end - s->p) < 4) + if (!s_check_rem(s, 4)) { logger(Sound, Error, "rdpsnd_process(), split at packet header, things will go south from here..."); @@ -405,7 +405,7 @@ rdpsnd_process(STREAM s) } else { - len = MIN(s->end - s->p, packet.end - packet.p); + len = MIN(s_remaining(s), s_remaining(&packet)); /* Microsoft's server is so broken it's not even funny... */ if (packet_opcode == SNDC_WAVE) @@ -451,7 +451,7 @@ rdpsnddbg_process(STREAM s) static char *rest = NULL; char *buf; - pkglen = s->end - s->p; + pkglen = s_remaining(s); /* str_handle_lines requires null terminated strings */ buf = (char *) xmalloc(pkglen + 1); STRNCPY(buf, (char *) s->p, pkglen + 1); diff --git a/rdpsnd_alsa.c b/rdpsnd_alsa.c index f68562a..26e5cc8 100644 --- a/rdpsnd_alsa.c +++ b/rdpsnd_alsa.c @@ -380,7 +380,7 @@ alsa_play(void) next_tick = rdpsnd_queue_next_tick(); - len = (out->end - out->p) / (samplewidth_out * audiochannels_out); + len = s_remaining(out) / (samplewidth_out * audiochannels_out); if ((len = snd_pcm_writei(out_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0) { snd_pcm_prepare(out_handle); diff --git a/rdpsnd_libao.c b/rdpsnd_libao.c index ddf2eaf..6d1996f 100644 --- a/rdpsnd_libao.c +++ b/rdpsnd_libao.c @@ -171,7 +171,7 @@ libao_play(void) next_tick = rdpsnd_queue_next_tick(); - len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN; + len = MIN(WAVEOUTLEN, s_remaining(out)); ao_play(o_device, (char *) out->p, len); out->p += len; diff --git a/rdpsnd_oss.c b/rdpsnd_oss.c index 98e7e70..ae5df21 100644 --- a/rdpsnd_oss.c +++ b/rdpsnd_oss.c @@ -412,7 +412,7 @@ oss_play(void) packet = rdpsnd_queue_current_packet(); out = &packet->s; - len = out->end - out->p; + len = s_remaining(out); len = write(dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len); if (len == -1) diff --git a/rdpsnd_pulse.c b/rdpsnd_pulse.c index 413dba4..9bcee39 100644 --- a/rdpsnd_pulse.c +++ b/rdpsnd_pulse.c @@ -1192,7 +1192,7 @@ pulse_play(void) playback_seek = PA_SEEK_RELATIVE; avail_space = pa_stream_writable_size(playback_stream); - audio_size = out->end - out->p <= avail_space ? out->end - out->p : avail_space; + audio_size = MIN(s_remaining(out), avail_space); if (audio_size) { if (pa_stream_write diff --git a/rdpsnd_sgi.c b/rdpsnd_sgi.c index 07c07fd..d94f648 100644 --- a/rdpsnd_sgi.c +++ b/rdpsnd_sgi.c @@ -256,7 +256,7 @@ sgi_play(void) packet = rdpsnd_queue_current_packet(); out = (STREAM) (void *) &(packet->s); - len = out->end - out->p; + len = s_remaining(out); alWriteFrames(output_port, out->p, len / combinedFrameSize); diff --git a/rdpsnd_sun.c b/rdpsnd_sun.c index 3f78e24..50a8fb2 100644 --- a/rdpsnd_sun.c +++ b/rdpsnd_sun.c @@ -419,7 +419,7 @@ sun_play(void) packet = rdpsnd_queue_current_packet(); out = &packet->s; - len = out->end - out->p; + len = s_remaining(out); len = write(dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len); if (len == -1) diff --git a/seamless.c b/seamless.c index 0f294cc..d105a54 100644 --- a/seamless.c +++ b/seamless.c @@ -377,7 +377,7 @@ seamless_process(STREAM s) unsigned int pkglen; char *buf; - pkglen = s->end - s->p; + pkglen = s_remaining(s); /* str_handle_lines requires null terminated strings */ buf = xmalloc(pkglen + 1); STRNCPY(buf, (char *) s->p, pkglen + 1); diff --git a/secure.c b/secure.c index 980d5b2..59918a1 100644 --- a/secure.c +++ b/secure.c @@ -352,7 +352,7 @@ sec_send_to_channel(STREAM s, uint32 flags, uint16 channel) if (flags & SEC_ENCRYPT) { flags &= ~SEC_ENCRYPT; - datalen = s->end - s->p - 8; + datalen = s_remaining(s) - 8; sec_sign(s->p, 8, g_sec_sign_key, g_rc4_key_len, s->p + 8, datalen); sec_encrypt(s->p + 8, datalen); } @@ -885,7 +885,7 @@ sec_recv(RD_BOOL * is_fastpath) } in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); + sec_decrypt(s->p, s_remaining(s)); } return s; } @@ -905,7 +905,7 @@ sec_recv(RD_BOOL * is_fastpath) } in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); + sec_decrypt(s->p, s_remaining(s)); } if (sec_flags & SEC_LICENSE_PKT) @@ -923,7 +923,7 @@ sec_recv(RD_BOOL * is_fastpath) } in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); + sec_decrypt(s->p, s_remaining(s)); /* Check for a redirect packet, starts with 00 04 */ if (s->p[0] == 0 && s->p[1] == 4) diff --git a/stream.h b/stream.h index ec61f0e..3005ad3 100644 --- a/stream.h +++ b/stream.h @@ -60,7 +60,9 @@ size_t in_ansi_string(STREAM s, char *string, size_t len); #define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; } #define s_pop_layer(s,h) (s)->p = (s)->h; #define s_mark_end(s) (s)->end = (s)->p; -#define s_check_rem(s,n) (((s)->p <= (s)->end) && ((size_t)n <= (size_t)((s)->end - (s)->p))) +/* Returns number of bytes that can still be read from STREAM */ +#define s_remaining(s) (size_t)((s)->end - (s)->p) +#define s_check_rem(s,n) (((s)->p <= (s)->end) && ((size_t)n <= s_remaining(s))) #define s_check_end(s) ((s)->p == (s)->end) #define s_length(s) ((s)->end - (s)->data) #define s_left(s) ((s)->size - (size_t)((s)->p - (s)->data))