[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
This commit is contained in:
akallabeth 2024-09-03 22:03:32 +02:00
parent c5f346488f
commit ebbfe598dc
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
3 changed files with 53 additions and 13 deletions

View File

@ -24,6 +24,7 @@
#include <winpr/user.h>
#include <winpr/image.h>
#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);

View File

@ -49,6 +49,7 @@
#endif
#include <winpr/stream.h>
#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)

View File

@ -0,0 +1,30 @@
/*
* WinPR: Windows Portable Runtime
* Image Utils
*
* Copyright 2024 Armin Novak <anovak@thincast.com>
* 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 <winpr/wtypes.h>
#include <winpr/stream.h>
#include <winpr/image.h>
BOOL readBitmapFileHeader(wStream* s, WINPR_BITMAP_FILE_HEADER* bf);
#endif /* LIBWINPR_UTILS_IMAGE_H */