summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dix/dispatch.c4
-rw-r--r--include/misc.h14
-rw-r--r--os/io.c6
-rw-r--r--randr/rrscreen.c5
-rw-r--r--test/input.c34
5 files changed, 50 insertions, 13 deletions
diff --git a/dix/dispatch.c b/dix/dispatch.c
index d916f345e..3c6a591db 100644
--- a/dix/dispatch.c
+++ b/dix/dispatch.c
@@ -466,8 +466,6 @@ Dispatch(void)
static int VendorRelease = VENDOR_RELEASE;
static char *VendorString = VENDOR_NAME;
-static const int padlength[4] = { 0, 3, 2, 1 };
-
void
SetVendorRelease(int release)
{
@@ -528,7 +526,7 @@ CreateConnectionBlock(void)
memmove(pBuf, VendorString, (int) setup.nbytesVendor);
sizesofar += setup.nbytesVendor;
pBuf += setup.nbytesVendor;
- i = padlength[setup.nbytesVendor & 3];
+ i = padding_for_int32(setup.nbytesVendor);
sizesofar += i;
while (--i >= 0)
*pBuf++ = 0;
diff --git a/include/misc.h b/include/misc.h
index 6bea82f33..7f7f221a8 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -232,6 +232,20 @@ pad_to_int32(const int bytes)
return (((bytes) + 3) & ~3);
}
+/**
+ * Calculate padding needed to bring the number of bytes to an even
+ * multiple of 4.
+ * @param bytes The minimum number of bytes needed.
+ * @return The bytes of padding needed to arrive at the closest multiple of 4
+ * that is equal or higher than bytes.
+ */
+static inline int
+padding_for_int32(const int bytes)
+{
+ return ((-bytes) & 3);
+}
+
+
extern char **xstrtokenize(const char *str, const char *separators);
extern void FormatUInt64(uint64_t num, char *string);
extern void FormatUInt64Hex(uint64_t num, char *string);
diff --git a/os/io.c b/os/io.c
index 8d0e5cc6f..e44db3933 100644
--- a/os/io.c
+++ b/os/io.c
@@ -578,8 +578,6 @@ ResetCurrentRequest(ClientPtr client)
}
}
-static const int padlength[4] = { 0, 3, 2, 1 };
-
/********************
* FlushAllOutput()
* Flush all clients with output. However, if some client still
@@ -757,7 +755,7 @@ WriteToClient(ClientPtr who, int count, const void *__buf)
oc->output = oco;
}
- padBytes = padlength[count & 3];
+ padBytes = padding_for_int32(count);
if (ReplyCallback) {
ReplyInfoRec replyinfo;
@@ -850,7 +848,7 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount)
if (!oco)
return 0;
written = 0;
- padsize = padlength[extraCount & 3];
+ padsize = padding_for_int32(extraCount);
notWritten = oco->count + extraCount + padsize;
todo = notWritten;
while (notWritten) {
diff --git a/randr/rrscreen.c b/randr/rrscreen.c
index a42772512..6a7a08939 100644
--- a/randr/rrscreen.c
+++ b/randr/rrscreen.c
@@ -22,8 +22,6 @@
#include "randrstr.h"
-static const int padlength[4] = { 0, 3, 2, 1 };
-
static CARD16
RR10CurrentSizeID(ScreenPtr pScreen);
@@ -46,8 +44,7 @@ RREditConnectionInfo(ScreenPtr pScreen)
connSetup = (xConnSetup *) ConnectionInfo;
vendor = (char *) connSetup + sizeof(xConnSetup);
formats = (xPixmapFormat *) ((char *) vendor +
- connSetup->nbytesVendor +
- padlength[connSetup->nbytesVendor & 3]);
+ pad_to_int32(connSetup->nbytesVendor));
root = (xWindowRoot *) ((char *) formats +
sizeof(xPixmapFormat) *
screenInfo.numPixmapFormats);
diff --git a/test/input.c b/test/input.c
index 90ab9aea3..191c81789 100644
--- a/test/input.c
+++ b/test/input.c
@@ -965,6 +965,19 @@ test_pad_to_int32(int i)
}
static void
+test_padding_for_int32(int i)
+{
+ static const int padlength[4] = { 0, 3, 2, 1 };
+ int expected_bytes = (((i + 3) / 4) * 4) - i;
+
+ assert(padding_for_int32(i) >= 0);
+ assert(padding_for_int32(i) <= 3);
+ assert(padding_for_int32(i) == expected_bytes);
+ assert(padding_for_int32(i) == padlength[i & 3]);
+ assert((padding_for_int32(i) + i) == pad_to_int32(i));
+}
+
+static void
include_byte_padding_macros(void)
{
printf("Testing bits_to_bytes()\n");
@@ -996,12 +1009,12 @@ include_byte_padding_macros(void)
test_bytes_to_int32(INT_MAX - 4);
test_bytes_to_int32(INT_MAX - 3);
- printf("Testing pad_to_int32\n");
+ printf("Testing pad_to_int32()\n");
test_pad_to_int32(0);
- test_pad_to_int32(0);
test_pad_to_int32(1);
test_pad_to_int32(2);
+ test_pad_to_int32(3);
test_pad_to_int32(7);
test_pad_to_int32(8);
test_pad_to_int32(0xFF);
@@ -1012,6 +1025,23 @@ include_byte_padding_macros(void)
test_pad_to_int32(0x1000000);
test_pad_to_int32(INT_MAX - 4);
test_pad_to_int32(INT_MAX - 3);
+
+ printf("Testing padding_for_int32()\n");
+
+ test_padding_for_int32(0);
+ test_padding_for_int32(1);
+ test_padding_for_int32(2);
+ test_padding_for_int32(3);
+ test_padding_for_int32(7);
+ test_padding_for_int32(8);
+ test_padding_for_int32(0xFF);
+ test_padding_for_int32(0x100);
+ test_padding_for_int32(0xFFFF);
+ test_padding_for_int32(0x10000);
+ test_padding_for_int32(0xFFFFFF);
+ test_padding_for_int32(0x1000000);
+ test_padding_for_int32(INT_MAX - 4);
+ test_padding_for_int32(INT_MAX - 3);
}
static void