From e03b163b9ba7ed2ec628172bd2e8f862cfe45103 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Fri, 12 Jul 2024 16:43:55 +0200 Subject: [PATCH] Xext: xvmc: simplify reply struct initialization * use static initialization where possible * put the lists into one one block, so they can be written in one pass * simplify size computations Signed-off-by: Enrico Weigelt, metux IT consult --- Xext/xvmc.c | 159 +++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 76 deletions(-) diff --git a/Xext/xvmc.c b/Xext/xvmc.c index f8772d6bd..63802c4b7 100644 --- a/Xext/xvmc.c +++ b/Xext/xvmc.c @@ -112,7 +112,6 @@ ProcXvMCQueryVersion(ClientPtr client) xvmcQueryVersionReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, - .length = 0, .major = SERVER_XVMC_MAJOR_VERSION, .minor = SERVER_XVMC_MINOR_VERSION }; @@ -129,11 +128,7 @@ ProcXvMCListSurfaceTypes(ClientPtr client) { XvPortPtr pPort; XvMCScreenPtr pScreenPriv; - xvmcListSurfaceTypesReply rep; - xvmcSurfaceInfo info; XvMCAdaptorPtr adaptor = NULL; - XvMCSurfaceInfoPtr surface; - int num_surfaces; REQUEST(xvmcListSurfaceTypesReq); REQUEST_SIZE_MATCH(xvmcListSurfaceTypesReq); @@ -153,28 +148,33 @@ ProcXvMCListSurfaceTypes(ClientPtr client) } } - num_surfaces = (adaptor) ? adaptor->num_surfaces : 0; - rep = (xvmcListSurfaceTypesReply) { + int num_surfaces = (adaptor) ? adaptor->num_surfaces : 0; + xvmcSurfaceInfo *info = calloc(sizeof(xvmcSurfaceInfo), num_surfaces); + if (!info && num_surfaces) + return BadAlloc; + + for (int i = 0; i < num_surfaces; i++) { + XvMCSurfaceInfoPtr surface = adaptor->surfaces[i]; + info[i].surface_type_id = surface->surface_type_id; + info[i].chroma_format = surface->chroma_format; + info[i].max_width = surface->max_width; + info[i].max_height = surface->max_height; + info[i].subpicture_max_width = surface->subpicture_max_width; + info[i].subpicture_max_height = surface->subpicture_max_height; + info[i].mc_type = surface->mc_type; + info[i].flags = surface->flags; + } + + xvmcListSurfaceTypesReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, .num = num_surfaces, - .length = bytes_to_int32(num_surfaces * sizeof(xvmcSurfaceInfo)), + .length = bytes_to_int32(sizeof(xvmcSurfaceInfo) * num_surfaces), }; WriteToClient(client, sizeof(xvmcListSurfaceTypesReply), &rep); - - for (int i = 0; i < num_surfaces; i++) { - surface = adaptor->surfaces[i]; - info.surface_type_id = surface->surface_type_id; - info.chroma_format = surface->chroma_format; - info.max_width = surface->max_width; - info.max_height = surface->max_height; - info.subpicture_max_width = surface->subpicture_max_width; - info.subpicture_max_height = surface->subpicture_max_height; - info.mc_type = surface->mc_type; - info.flags = surface->flags; - WriteToClient(client, sizeof(xvmcSurfaceInfo), &info); - } + WriteToClient(client, sizeof(xvmcSurfaceInfo) * num_surfaces, info); + free(info); return Success; } @@ -191,7 +191,6 @@ ProcXvMCCreateContext(ClientPtr client) XvMCScreenPtr pScreenPriv; XvMCAdaptorPtr adaptor = NULL; XvMCSurfaceInfoPtr surface = NULL; - xvmcCreateContextReply rep; REQUEST(xvmcCreateContextReq); REQUEST_SIZE_MATCH(xvmcCreateContextReq); @@ -256,7 +255,7 @@ ProcXvMCCreateContext(ClientPtr client) return BadAlloc; } - rep = (xvmcCreateContextReply) { + xvmcCreateContextReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, .length = dwords, @@ -302,7 +301,6 @@ ProcXvMCCreateSurface(ClientPtr client) XvMCContextPtr pContext; XvMCSurfacePtr pSurface; XvMCScreenPtr pScreenPriv; - xvmcCreateSurfaceReply rep; REQUEST(xvmcCreateSurfaceReq); REQUEST_SIZE_MATCH(xvmcCreateSurfaceReq); @@ -335,7 +333,7 @@ ProcXvMCCreateSurface(ClientPtr client) return BadAlloc; } - rep = (xvmcCreateSurfaceReply) { + xvmcCreateSurfaceReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, .length = dwords @@ -380,7 +378,6 @@ ProcXvMCCreateSubpicture(ClientPtr client) XvMCContextPtr pContext; XvMCSubpicturePtr pSubpicture; XvMCScreenPtr pScreenPriv; - xvmcCreateSubpictureReply rep; XvMCAdaptorPtr adaptor; XvMCSurfaceInfoPtr surface = NULL; @@ -455,7 +452,7 @@ ProcXvMCCreateSubpicture(ClientPtr client) return BadAlloc; } - rep = (xvmcCreateSubpictureReply) { + xvmcCreateSubpictureReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, .length = dwords, @@ -503,12 +500,10 @@ static int ProcXvMCListSubpictureTypes(ClientPtr client) { XvPortPtr pPort; - xvmcListSubpictureTypesReply rep; XvMCScreenPtr pScreenPriv; ScreenPtr pScreen; XvMCAdaptorPtr adaptor = NULL; XvMCSurfaceInfoPtr surface = NULL; - xvImageFormatInfo info; XvImagePtr pImage; REQUEST(xvmcListSubpictureTypesReq); @@ -544,19 +539,14 @@ ProcXvMCListSubpictureTypes(ClientPtr client) if (!surface) return BadMatch; - rep = (xvmcListSubpictureTypesReply) { - .type = X_Reply, - .sequenceNumber = client->sequence, - .num = 0 - }; - if (surface->compatible_subpictures) - rep.num = surface->compatible_subpictures->num_xvimages; + int num = (surface->compatible_subpictures ? + surface->compatible_subpictures->num_xvimages : 0); - rep.length = bytes_to_int32(rep.num * sizeof(xvImageFormatInfo)); + xvImageFormatInfo *info = calloc(sizeof(xvImageFormatInfo), num); + if (!info && num) + return BadAlloc; - WriteToClient(client, sizeof(xvmcListSubpictureTypesReply), &rep); - - for (int i = 0; i < rep.num; i++) { + for (int i = 0; i < num; i++) { pImage = NULL; for (int j = 0; j < adaptor->num_subpictures; j++) { if (surface->compatible_subpictures->xvimage_ids[i] == @@ -565,41 +555,51 @@ ProcXvMCListSubpictureTypes(ClientPtr client) break; } } - if (!pImage) + if (!pImage) { + free(info); return BadImplementation; + } - info.id = pImage->id; - info.type = pImage->type; - info.byte_order = pImage->byte_order; - memcpy(&info.guid, pImage->guid, 16); - info.bpp = pImage->bits_per_pixel; - info.num_planes = pImage->num_planes; - info.depth = pImage->depth; - info.red_mask = pImage->red_mask; - info.green_mask = pImage->green_mask; - info.blue_mask = pImage->blue_mask; - info.format = pImage->format; - info.y_sample_bits = pImage->y_sample_bits; - info.u_sample_bits = pImage->u_sample_bits; - info.v_sample_bits = pImage->v_sample_bits; - info.horz_y_period = pImage->horz_y_period; - info.horz_u_period = pImage->horz_u_period; - info.horz_v_period = pImage->horz_v_period; - info.vert_y_period = pImage->vert_y_period; - info.vert_u_period = pImage->vert_u_period; - info.vert_v_period = pImage->vert_v_period; - memcpy(&info.comp_order, pImage->component_order, 32); - info.scanline_order = pImage->scanline_order; - WriteToClient(client, sizeof(xvImageFormatInfo), &info); + info[i].id = pImage->id; + info[i].type = pImage->type; + info[i].byte_order = pImage->byte_order; + memcpy(&info[i].guid, pImage->guid, 16); + info[i].bpp = pImage->bits_per_pixel; + info[i].num_planes = pImage->num_planes; + info[i].depth = pImage->depth; + info[i].red_mask = pImage->red_mask; + info[i].green_mask = pImage->green_mask; + info[i].blue_mask = pImage->blue_mask; + info[i].format = pImage->format; + info[i].y_sample_bits = pImage->y_sample_bits; + info[i].u_sample_bits = pImage->u_sample_bits; + info[i].v_sample_bits = pImage->v_sample_bits; + info[i].horz_y_period = pImage->horz_y_period; + info[i].horz_u_period = pImage->horz_u_period; + info[i].horz_v_period = pImage->horz_v_period; + info[i].vert_y_period = pImage->vert_y_period; + info[i].vert_u_period = pImage->vert_u_period; + info[i].vert_v_period = pImage->vert_v_period; + memcpy(&info[i].comp_order, pImage->component_order, 32); + info[i].scanline_order = pImage->scanline_order; } + xvmcListSubpictureTypesReply rep = { + .type = X_Reply, + .sequenceNumber = client->sequence, + .num = num, + .length = bytes_to_int32(sizeof(info)), + }; + + WriteToClient(client, sizeof(xvmcListSubpictureTypesReply), &rep); + WriteToClient(client, sizeof(xvImageFormatInfo) * num, info); + free(info); return Success; } static int ProcXvMCGetDRInfo(ClientPtr client) { - xvmcGetDRInfoReply rep; XvPortPtr pPort; ScreenPtr pScreen; XvMCScreenPtr pScreenPriv; @@ -616,21 +616,30 @@ ProcXvMCGetDRInfo(ClientPtr client) pScreen = pPort->pAdaptor->pScreen; pScreenPriv = XVMC_GET_PRIVATE(pScreen); - rep = (xvmcGetDRInfoReply) { + int nameLen = strlen(pScreenPriv->clientDriverName) + 1; + int busIDLen = strlen(pScreenPriv->busID) + 1; + + // buffer holds two zero-terminated strings, padded to 4-byte ints + const size_t buflen = pad_to_int32(nameLen+busIDLen); + char *buf = calloc(1, buflen); + if (!buf) + return BadAlloc; + + memcpy(buf, pScreenPriv->clientDriverName, nameLen); + memcpy(buf+nameLen, pScreenPriv->busID, busIDLen); + + xvmcGetDRInfoReply rep = { .type = X_Reply, .sequenceNumber = client->sequence, .major = pScreenPriv->major, .minor = pScreenPriv->minor, .patchLevel = pScreenPriv->patchLevel, - .nameLen = bytes_to_int32(strlen(pScreenPriv->clientDriverName) + 1), - .busIDLen = bytes_to_int32(strlen(pScreenPriv->busID) + 1), + .nameLen = nameLen, + .busIDLen = busIDLen, + .length = bytes_to_int32(sizeof(buf)), .isLocal = 1 }; - rep.length = rep.nameLen + rep.busIDLen; - rep.nameLen <<= 2; - rep.busIDLen <<= 2; - /* * Read back to the client what she has put in the shared memory * segment she prepared for us. @@ -658,10 +667,8 @@ ProcXvMCGetDRInfo(ClientPtr client) #endif /* HAS_XVMCSHM */ WriteToClient(client, sizeof(xvmcGetDRInfoReply), &rep); - if (rep.length) { - WriteToClient(client, rep.nameLen, pScreenPriv->clientDriverName); - WriteToClient(client, rep.busIDLen, pScreenPriv->busID); - } + WriteToClient(client, buflen, buf); + free(buf); return Success; }