libragephoto: impl. support for NULL/nullptr to clear data values

This commit is contained in:
Syping 2025-11-05 11:58:45 +01:00
parent 32b8258af1
commit 4518ae5aa3
2 changed files with 112 additions and 95 deletions

View file

@ -124,37 +124,40 @@ static inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, siz
static inline bool writeDataChar(const char *input, char **output)
{
const size_t src_s = strlen(input) + 1;
if (*output) {
const size_t dst_s = strlen(*output) + 1;
if (dst_s > src_s) {
char *t_output = (char*)realloc(*output, src_s);
if (!t_output) {
return false;
if (input) {
const size_t src_s = strlen(input) + 1;
if (*output) {
const size_t dst_s = strlen(*output) + 1;
if (dst_s > src_s) {
char *t_output = (char*)realloc(*output, src_s);
if (!t_output)
return false;
*output = t_output;
memcpy(*output, input, src_s);
}
*output = t_output;
memcpy(*output, input, src_s);
}
else if (dst_s < src_s) {
char *t_output = (char*)malloc(src_s);
if (!t_output) {
return false;
else if (dst_s < src_s) {
char *t_output = (char*)malloc(src_s);
if (!t_output)
return false;
free(*output);
*output = t_output;
memcpy(*output, input, src_s);
}
else {
memcpy(*output, input, src_s);
}
free(*output);
*output = t_output;
memcpy(*output, input, src_s);
}
else {
char *t_output = (char*)malloc(src_s);
if (!t_output)
return false;
*output = t_output;
memcpy(*output, input, src_s);
}
}
else {
char *t_output = (char*)malloc(src_s);
if (!t_output) {
return false;
}
*output = t_output;
memcpy(*output, input, src_s);
else if (*output) {
free(*output);
*output = NULL;
}
return true;
}
@ -1024,7 +1027,7 @@ bool ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t ph
const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
fclose(file);
free(data);
return (fileSize == writeSize);
return fileSize == writeSize;
}
bool ragephoto_savefile(ragephoto_t instance, const char *filename)
@ -1192,19 +1195,33 @@ void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat)
bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize)
{
if (instance->data->jpeg) {
if (instance->data->jpegSize > size) {
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
if (!t_photoData) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
if (data && size) {
if (instance->data->jpeg) {
if (instance->data->jpegSize > size) {
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
if (!t_photoData) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
}
instance->data->jpeg = t_photoData;
memcpy(instance->data->jpeg, data, size);
instance->data->jpegSize = size;
}
else if (instance->data->jpegSize < size) {
free(instance->data->jpeg);
instance->data->jpeg = (char*)malloc(size);
if (!instance->data->jpeg) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
}
memcpy(instance->data->jpeg, data, size);
instance->data->jpegSize = size;
}
else {
memcpy(instance->data->jpeg, data, size);
}
instance->data->jpeg = t_photoData;
memcpy(instance->data->jpeg, data, size);
instance->data->jpegSize = size;
}
else if (instance->data->jpegSize < size) {
free(instance->data->jpeg);
else {
instance->data->jpeg = (char*)malloc(size);
if (!instance->data->jpeg) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
@ -1213,18 +1230,10 @@ bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t siz
memcpy(instance->data->jpeg, data, size);
instance->data->jpegSize = size;
}
else {
memcpy(instance->data->jpeg, data, size);
}
}
else {
instance->data->jpeg = (char*)malloc(size);
if (!instance->data->jpeg) {
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
return false;
}
memcpy(instance->data->jpeg, data, size);
instance->data->jpegSize = size;
else if (instance->data->jpeg) {
free(instance->data->jpeg);
instance->data->jpeg = NULL;
}
if (bufferSize != 0) {

View file

@ -118,37 +118,40 @@ inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, size_t inp
inline bool writeDataChar(const char *input, char **output)
{
const size_t src_s = strlen(input) + 1;
if (*output) {
const size_t dst_s = strlen(*output) + 1;
if (dst_s > src_s) {
char *t_output = static_cast<char*>(realloc(*output, src_s));
if (!t_output) {
return false;
if (input) {
const size_t src_s = strlen(input) + 1;
if (*output) {
const size_t dst_s = strlen(*output) + 1;
if (dst_s > src_s) {
char *t_output = static_cast<char*>(realloc(*output, src_s));
if (!t_output)
return false;
*output = t_output;
memcpy(*output, input, src_s);
}
*output = t_output;
memcpy(*output, input, src_s);
}
else if (dst_s < src_s) {
char *t_output = static_cast<char*>(malloc(src_s));
if (!t_output) {
return false;
else if (dst_s < src_s) {
char *t_output = static_cast<char*>(malloc(src_s));
if (!t_output)
return false;
free(*output);
*output = t_output;
memcpy(*output, input, src_s);
}
else {
memcpy(*output, input, src_s);
}
free(*output);
*output = t_output;
memcpy(*output, input, src_s);
}
else {
char *t_output = static_cast<char*>(malloc(src_s));
if (!t_output)
return false;
*output = t_output;
memcpy(*output, input, src_s);
}
}
else {
char *t_output = static_cast<char*>(malloc(src_s));
if (!t_output) {
return false;
}
*output = t_output;
memcpy(*output, input, src_s);
else if (*output) {
free(*output);
*output = nullptr;
}
return true;
}
@ -1018,8 +1021,7 @@ bool RagePhoto::saveFile(const char *filename, uint32_t photoFormat)
ofs.close();
return ok;
}
else
return false;
return false;
}
bool RagePhoto::saveFile(const char *filename)
@ -1189,19 +1191,33 @@ void RagePhoto::setHeader(const char *header, uint32_t headerSum, uint32_t heade
bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
{
if (m_data->jpeg) {
if (m_data->jpegSize > size) {
char *t_photoData = static_cast<char*>(realloc(m_data->jpeg, size));
if (!t_photoData) {
m_data->error = Error::PhotoMallocError; // 16
return false;
if (data && size) {
if (m_data->jpeg) {
if (m_data->jpegSize > size) {
char *t_photoData = static_cast<char*>(realloc(m_data->jpeg, size));
if (!t_photoData) {
m_data->error = Error::PhotoMallocError; // 16
return false;
}
m_data->jpeg = t_photoData;
memcpy(m_data->jpeg, data, size);
m_data->jpegSize = size;
}
else if (m_data->jpegSize < size) {
free(m_data->jpeg);
m_data->jpeg = static_cast<char*>(malloc(size));
if (!m_data->jpeg) {
m_data->error = Error::PhotoMallocError; // 16
return false;
}
memcpy(m_data->jpeg, data, size);
m_data->jpegSize = size;
}
else {
memcpy(m_data->jpeg, data, size);
}
m_data->jpeg = t_photoData;
memcpy(m_data->jpeg, data, size);
m_data->jpegSize = size;
}
else if (m_data->jpegSize < size) {
free(m_data->jpeg);
else {
m_data->jpeg = static_cast<char*>(malloc(size));
if (!m_data->jpeg) {
m_data->error = Error::PhotoMallocError; // 16
@ -1210,18 +1226,10 @@ bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
memcpy(m_data->jpeg, data, size);
m_data->jpegSize = size;
}
else {
memcpy(m_data->jpeg, data, size);
}
}
else {
m_data->jpeg = static_cast<char*>(malloc(size));
if (!m_data->jpeg) {
m_data->error = Error::PhotoMallocError; // 16
return false;
}
memcpy(m_data->jpeg, data, size);
m_data->jpegSize = size;
else if (m_data->jpeg) {
free(m_data->jpeg);
m_data->jpeg = nullptr;
}
if (bufferSize != 0) {