Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1 | // Copyright 2019 Joe Drago. All rights reserved. |
| 2 | // SPDX-License-Identifier: BSD-2-Clause |
| 3 | |
| 4 | #include "avif/avif.h" |
| 5 | |
| 6 | #include <string.h> |
| 7 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 8 | void avifRWDataRealloc(avifRWData * raw, size_t newSize) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 9 | { |
| 10 | if (raw->size != newSize) { |
| 11 | uint8_t * old = raw->data; |
| 12 | size_t oldSize = raw->size; |
| 13 | raw->data = avifAlloc(newSize); |
| 14 | raw->size = newSize; |
| 15 | if (oldSize) { |
| 16 | size_t bytesToCopy = (oldSize < raw->size) ? oldSize : raw->size; |
| 17 | memcpy(raw->data, old, bytesToCopy); |
| 18 | avifFree(old); |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 23 | void avifRWDataSet(avifRWData * raw, const uint8_t * data, size_t len) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 24 | { |
| 25 | if (len) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 26 | avifRWDataRealloc(raw, len); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 27 | memcpy(raw->data, data, len); |
| 28 | } else { |
Joe Drago | 5932a05 | 2019-11-22 07:37:05 -0800 | [diff] [blame] | 29 | avifRWDataFree(raw); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 33 | void avifRWDataFree(avifRWData * raw) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 34 | { |
| 35 | avifFree(raw->data); |
| 36 | raw->data = NULL; |
| 37 | raw->size = 0; |
| 38 | } |