[misc] add arbitrary buffer allocation to GetResource()

* If duplicate is TRUE and len is non-zero, then a buffer of len size,
  padded with zeroes, is allocated for the resource.
This commit is contained in:
Pete Batard 2020-02-19 12:41:13 +00:00
parent 841b79f45d
commit b8579c04da
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
6 changed files with 27 additions and 12 deletions

View file

@ -549,10 +549,17 @@ out:
return ret;
}
/*
* Get a resource from the RC. If needed that resource can be duplicated.
* If duplicate is true and len is non-zero, the a zeroed buffer of 'len'
* size is allocated for the resource. Else the buffer is allocate for
* the resource size.
*/
unsigned char* GetResource(HMODULE module, char* name, char* type, const char* desc, DWORD* len, BOOL duplicate)
{
HGLOBAL res_handle;
HRSRC res;
DWORD res_len;
unsigned char* p = NULL;
res = FindResourceA(module, name, type);
@ -565,18 +572,23 @@ unsigned char* GetResource(HMODULE module, char* name, char* type, const char* d
uprintf("Could not load resource '%s': %s\n", desc, WindowsErrorString());
goto out;
}
*len = SizeofResource(module, res);
res_len = SizeofResource(module, res);
if (duplicate) {
p = (unsigned char*)malloc(*len);
if (*len == 0)
*len = res_len;
p = (unsigned char*)calloc(*len, 1);
if (p == NULL) {
uprintf("Coult not allocate resource '%s'\n", desc);
uprintf("Could not allocate resource '%s'\n", desc);
goto out;
}
memcpy(p, LockResource(res_handle), *len);
memcpy(p, LockResource(res_handle), res_len);
if (res_len < *len)
uprintf("Warning: Resource '%s' was truncated by %d bytes!\n", desc, *len - res_len);
} else {
p = (unsigned char*)LockResource(res_handle);
}
*len = res_len;
out:
return p;