From ebbfe598dc7b844c3b304e996f89fd24b73ef258 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 3 Sep 2024 22:03:32 +0200 Subject: [PATCH] [winpr,utils] unify bitmap reading * clipboard synthesizer needs to read the bitmap header, use the function from utils/image * check bitmap header in utils/image and abort if wrong --- winpr/libwinpr/clipboard/synthetic.c | 13 +++++------- winpr/libwinpr/utils/image.c | 23 ++++++++++++++++----- winpr/libwinpr/utils/image.h | 30 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 winpr/libwinpr/utils/image.h diff --git a/winpr/libwinpr/clipboard/synthetic.c b/winpr/libwinpr/clipboard/synthetic.c index a928731cc..0104f5b48 100644 --- a/winpr/libwinpr/clipboard/synthetic.c +++ b/winpr/libwinpr/clipboard/synthetic.c @@ -24,6 +24,7 @@ #include #include +#include "../utils/image.h" #include "clipboard.h" static const char* mime_bitmap[] = { "image/bmp", "image/x-bmp", "image/x-MS-bmp", @@ -234,14 +235,10 @@ static void* clipboard_synthesize_cf_dib(wClipboard* clipboard, UINT32 formatId, } else if (is_format_bitmap(clipboard, formatId)) { - const BITMAPFILEHEADER* pFileHeader = NULL; - - if (SrcSize < (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER))) - return NULL; - - pFileHeader = (const BITMAPFILEHEADER*)data; - - if (pFileHeader->bfType != 0x4D42) + WINPR_BITMAP_FILE_HEADER pFileHeader = { 0 }; + wStream sbuffer = { 0 }; + wStream* s = Stream_StaticConstInit(&sbuffer, data, SrcSize); + if (!readBitmapFileHeader(s, &pFileHeader)) return NULL; DstSize = SrcSize - sizeof(BITMAPFILEHEADER); diff --git a/winpr/libwinpr/utils/image.c b/winpr/libwinpr/utils/image.c index db1f3d326..93aa7a436 100644 --- a/winpr/libwinpr/utils/image.c +++ b/winpr/libwinpr/utils/image.c @@ -49,6 +49,7 @@ #endif #include +#include "image.h" #include "../log.h" #define TAG WINPR_TAG("utils.image") @@ -73,9 +74,14 @@ static BOOL writeBitmapFileHeader(wStream* s, const WINPR_BITMAP_FILE_HEADER* bf return TRUE; } -static BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf) +BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf) { - if (!s || !bf || (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(WINPR_BITMAP_FILE_HEADER)))) + static wLog* log = NULL; + if (!log) + log = WLog_Get(TAG); + + if (!s || !bf || + (!Stream_CheckAndLogRequiredLengthWLog(log, s, sizeof(WINPR_BITMAP_FILE_HEADER)))) return FALSE; Stream_Read_UINT8(s, bf->bfType[0]); @@ -87,12 +93,19 @@ static BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf) if (bf->bfSize < sizeof(WINPR_BITMAP_FILE_HEADER)) { - WLog_ERR(TAG, ""); + WLog_Print(log, WLOG_ERROR, "Invalid bitmap::bfSize=%" PRIu32 ", require at least %" PRIuz, + bf->bfSize, sizeof(WINPR_BITMAP_FILE_HEADER)); return FALSE; } - return Stream_CheckAndLogRequiredCapacity(TAG, s, - bf->bfSize - sizeof(WINPR_BITMAP_FILE_HEADER)); + if ((bf->bfType[0] != 'B') || (bf->bfType[1] != 'M')) + { + WLog_Print(log, WLOG_ERROR, "Invalid bitmap header [%c%c], expected [BM]", bf->bfType[0], + bf->bfType[1]); + return FALSE; + } + return Stream_CheckAndLogRequiredCapacityWLog(log, s, + bf->bfSize - sizeof(WINPR_BITMAP_FILE_HEADER)); } static BOOL writeBitmapInfoHeader(wStream* s, const WINPR_BITMAP_INFO_HEADER* bi) diff --git a/winpr/libwinpr/utils/image.h b/winpr/libwinpr/utils/image.h new file mode 100644 index 000000000..e73156803 --- /dev/null +++ b/winpr/libwinpr/utils/image.h @@ -0,0 +1,30 @@ +/* + * WinPR: Windows Portable Runtime + * Image Utils + * + * Copyright 2024 Armin Novak + * Copyright 2024 Thincast Technologies GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBWINPR_UTILS_IMAGE_H +#define LIBWINPR_UTILS_IMAGE_H + +#include +#include +#include + +BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf); + +#endif /* LIBWINPR_UTILS_IMAGE_H */