Add macro to check remaining length of STREAM

Avoids poking around in the internals, and also makes the code
easier to read.
This commit is contained in:
Pierre Ossman 2019-04-09 13:13:35 +02:00
parent 3e340f2f20
commit 6268b44f06
16 changed files with 25 additions and 23 deletions

View File

@ -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;

2
iso.c
View File

@ -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 */

View File

@ -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);

2
mcs.c
View File

@ -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));

2
rdp.c
View File

@ -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));

View File

@ -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 */

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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)

View File

@ -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))