libragephoto: impl. support for NULL/nullptr to clear data values
This commit is contained in:
parent
32b8258af1
commit
4518ae5aa3
2 changed files with 112 additions and 95 deletions
|
|
@ -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)
|
static inline bool writeDataChar(const char *input, char **output)
|
||||||
{
|
{
|
||||||
const size_t src_s = strlen(input) + 1;
|
if (input) {
|
||||||
if (*output) {
|
const size_t src_s = strlen(input) + 1;
|
||||||
const size_t dst_s = strlen(*output) + 1;
|
if (*output) {
|
||||||
if (dst_s > src_s) {
|
const size_t dst_s = strlen(*output) + 1;
|
||||||
char *t_output = (char*)realloc(*output, src_s);
|
if (dst_s > src_s) {
|
||||||
if (!t_output) {
|
char *t_output = (char*)realloc(*output, src_s);
|
||||||
return false;
|
if (!t_output)
|
||||||
|
return false;
|
||||||
|
*output = t_output;
|
||||||
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
*output = t_output;
|
else if (dst_s < src_s) {
|
||||||
memcpy(*output, input, src_s);
|
char *t_output = (char*)malloc(src_s);
|
||||||
}
|
if (!t_output)
|
||||||
else if (dst_s < src_s) {
|
return false;
|
||||||
char *t_output = (char*)malloc(src_s);
|
free(*output);
|
||||||
if (!t_output) {
|
*output = t_output;
|
||||||
return false;
|
memcpy(*output, input, src_s);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
free(*output);
|
|
||||||
*output = t_output;
|
|
||||||
memcpy(*output, input, src_s);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
char *t_output = (char*)malloc(src_s);
|
||||||
|
if (!t_output)
|
||||||
|
return false;
|
||||||
|
*output = t_output;
|
||||||
memcpy(*output, input, src_s);
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (*output) {
|
||||||
char *t_output = (char*)malloc(src_s);
|
free(*output);
|
||||||
if (!t_output) {
|
*output = NULL;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
*output = t_output;
|
|
||||||
memcpy(*output, input, src_s);
|
|
||||||
}
|
}
|
||||||
return true;
|
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);
|
const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
free(data);
|
free(data);
|
||||||
return (fileSize == writeSize);
|
return fileSize == writeSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ragephoto_savefile(ragephoto_t instance, const char *filename)
|
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)
|
bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize)
|
||||||
{
|
{
|
||||||
if (instance->data->jpeg) {
|
if (data && size) {
|
||||||
if (instance->data->jpegSize > size) {
|
if (instance->data->jpeg) {
|
||||||
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
|
if (instance->data->jpegSize > size) {
|
||||||
if (!t_photoData) {
|
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
|
||||||
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
|
if (!t_photoData) {
|
||||||
return false;
|
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) {
|
else {
|
||||||
free(instance->data->jpeg);
|
|
||||||
instance->data->jpeg = (char*)malloc(size);
|
instance->data->jpeg = (char*)malloc(size);
|
||||||
if (!instance->data->jpeg) {
|
if (!instance->data->jpeg) {
|
||||||
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
|
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);
|
memcpy(instance->data->jpeg, data, size);
|
||||||
instance->data->jpegSize = size;
|
instance->data->jpegSize = size;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
memcpy(instance->data->jpeg, data, size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else if (instance->data->jpeg) {
|
||||||
instance->data->jpeg = (char*)malloc(size);
|
free(instance->data->jpeg);
|
||||||
if (!instance->data->jpeg) {
|
instance->data->jpeg = NULL;
|
||||||
instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memcpy(instance->data->jpeg, data, size);
|
|
||||||
instance->data->jpegSize = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bufferSize != 0) {
|
if (bufferSize != 0) {
|
||||||
|
|
|
||||||
|
|
@ -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)
|
inline bool writeDataChar(const char *input, char **output)
|
||||||
{
|
{
|
||||||
const size_t src_s = strlen(input) + 1;
|
if (input) {
|
||||||
if (*output) {
|
const size_t src_s = strlen(input) + 1;
|
||||||
const size_t dst_s = strlen(*output) + 1;
|
if (*output) {
|
||||||
if (dst_s > src_s) {
|
const size_t dst_s = strlen(*output) + 1;
|
||||||
char *t_output = static_cast<char*>(realloc(*output, src_s));
|
if (dst_s > src_s) {
|
||||||
if (!t_output) {
|
char *t_output = static_cast<char*>(realloc(*output, src_s));
|
||||||
return false;
|
if (!t_output)
|
||||||
|
return false;
|
||||||
|
*output = t_output;
|
||||||
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
*output = t_output;
|
else if (dst_s < src_s) {
|
||||||
memcpy(*output, input, src_s);
|
char *t_output = static_cast<char*>(malloc(src_s));
|
||||||
}
|
if (!t_output)
|
||||||
else if (dst_s < src_s) {
|
return false;
|
||||||
char *t_output = static_cast<char*>(malloc(src_s));
|
free(*output);
|
||||||
if (!t_output) {
|
*output = t_output;
|
||||||
return false;
|
memcpy(*output, input, src_s);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
free(*output);
|
|
||||||
*output = t_output;
|
|
||||||
memcpy(*output, input, src_s);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
char *t_output = static_cast<char*>(malloc(src_s));
|
||||||
|
if (!t_output)
|
||||||
|
return false;
|
||||||
|
*output = t_output;
|
||||||
memcpy(*output, input, src_s);
|
memcpy(*output, input, src_s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (*output) {
|
||||||
char *t_output = static_cast<char*>(malloc(src_s));
|
free(*output);
|
||||||
if (!t_output) {
|
*output = nullptr;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
*output = t_output;
|
|
||||||
memcpy(*output, input, src_s);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -1018,8 +1021,7 @@ bool RagePhoto::saveFile(const char *filename, uint32_t photoFormat)
|
||||||
ofs.close();
|
ofs.close();
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
else
|
return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RagePhoto::saveFile(const char *filename)
|
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)
|
bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
|
||||||
{
|
{
|
||||||
if (m_data->jpeg) {
|
if (data && size) {
|
||||||
if (m_data->jpegSize > size) {
|
if (m_data->jpeg) {
|
||||||
char *t_photoData = static_cast<char*>(realloc(m_data->jpeg, size));
|
if (m_data->jpegSize > size) {
|
||||||
if (!t_photoData) {
|
char *t_photoData = static_cast<char*>(realloc(m_data->jpeg, size));
|
||||||
m_data->error = Error::PhotoMallocError; // 16
|
if (!t_photoData) {
|
||||||
return false;
|
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) {
|
else {
|
||||||
free(m_data->jpeg);
|
|
||||||
m_data->jpeg = static_cast<char*>(malloc(size));
|
m_data->jpeg = static_cast<char*>(malloc(size));
|
||||||
if (!m_data->jpeg) {
|
if (!m_data->jpeg) {
|
||||||
m_data->error = Error::PhotoMallocError; // 16
|
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);
|
memcpy(m_data->jpeg, data, size);
|
||||||
m_data->jpegSize = size;
|
m_data->jpegSize = size;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
memcpy(m_data->jpeg, data, size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else if (m_data->jpeg) {
|
||||||
m_data->jpeg = static_cast<char*>(malloc(size));
|
free(m_data->jpeg);
|
||||||
if (!m_data->jpeg) {
|
m_data->jpeg = nullptr;
|
||||||
m_data->error = Error::PhotoMallocError; // 16
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memcpy(m_data->jpeg, data, size);
|
|
||||||
m_data->jpegSize = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bufferSize != 0) {
|
if (bufferSize != 0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue