| OLD | NEW |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 3 // Use of this source code is governed by a BSD-style license |
| 4 // that can be found in the COPYING file in the root of the source | 4 // that can be found in the COPYING file in the root of the source |
| 5 // tree. An additional intellectual property rights grant can be found | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 // | 9 // |
| 10 // Everything about WebPDecBuffer | 10 // Everything about WebPDecBuffer |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 4, 4, 4, 2, // pre-multiplied modes | 26 4, 4, 4, 2, // pre-multiplied modes |
| 27 1, 1 }; | 27 1, 1 }; |
| 28 | 28 |
| 29 // Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE. | 29 // Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE. |
| 30 // Convert to an integer to handle both the unsigned/signed enum cases | 30 // Convert to an integer to handle both the unsigned/signed enum cases |
| 31 // without the need for casting to remove type limit warnings. | 31 // without the need for casting to remove type limit warnings. |
| 32 static int IsValidColorspace(int webp_csp_mode) { | 32 static int IsValidColorspace(int webp_csp_mode) { |
| 33 return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST); | 33 return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // strictly speaking, the very last (or first, if flipped) row |
| 37 // doesn't require padding. |
| 38 #define MIN_BUFFER_SIZE(WIDTH, HEIGHT, STRIDE) \ |
| 39 (uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH) |
| 40 |
| 36 static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) { | 41 static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) { |
| 37 int ok = 1; | 42 int ok = 1; |
| 38 const WEBP_CSP_MODE mode = buffer->colorspace; | 43 const WEBP_CSP_MODE mode = buffer->colorspace; |
| 39 const int width = buffer->width; | 44 const int width = buffer->width; |
| 40 const int height = buffer->height; | 45 const int height = buffer->height; |
| 41 if (!IsValidColorspace(mode)) { | 46 if (!IsValidColorspace(mode)) { |
| 42 ok = 0; | 47 ok = 0; |
| 43 } else if (!WebPIsRGBMode(mode)) { // YUV checks | 48 } else if (!WebPIsRGBMode(mode)) { // YUV checks |
| 44 const WebPYUVABuffer* const buf = &buffer->u.YUVA; | 49 const WebPYUVABuffer* const buf = &buffer->u.YUVA; |
| 50 const int uv_width = (width + 1) / 2; |
| 51 const int uv_height = (height + 1) / 2; |
| 45 const int y_stride = abs(buf->y_stride); | 52 const int y_stride = abs(buf->y_stride); |
| 46 const int u_stride = abs(buf->u_stride); | 53 const int u_stride = abs(buf->u_stride); |
| 47 const int v_stride = abs(buf->v_stride); | 54 const int v_stride = abs(buf->v_stride); |
| 48 const int a_stride = abs(buf->a_stride); | 55 const int a_stride = abs(buf->a_stride); |
| 49 const uint64_t y_size = (uint64_t)y_stride * height; | 56 const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride); |
| 50 const uint64_t u_size = (uint64_t)u_stride * ((height + 1) / 2); | 57 const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride); |
| 51 const uint64_t v_size = (uint64_t)v_stride * ((height + 1) / 2); | 58 const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride); |
| 52 const uint64_t a_size = (uint64_t)a_stride * height; | 59 const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride); |
| 53 ok &= (y_size <= buf->y_size); | 60 ok &= (y_size <= buf->y_size); |
| 54 ok &= (u_size <= buf->u_size); | 61 ok &= (u_size <= buf->u_size); |
| 55 ok &= (v_size <= buf->v_size); | 62 ok &= (v_size <= buf->v_size); |
| 56 ok &= (y_stride >= width); | 63 ok &= (y_stride >= width); |
| 57 ok &= (u_stride >= (width + 1) / 2); | 64 ok &= (u_stride >= uv_width); |
| 58 ok &= (v_stride >= (width + 1) / 2); | 65 ok &= (v_stride >= uv_width); |
| 59 ok &= (buf->y != NULL); | 66 ok &= (buf->y != NULL); |
| 60 ok &= (buf->u != NULL); | 67 ok &= (buf->u != NULL); |
| 61 ok &= (buf->v != NULL); | 68 ok &= (buf->v != NULL); |
| 62 if (mode == MODE_YUVA) { | 69 if (mode == MODE_YUVA) { |
| 63 ok &= (a_stride >= width); | 70 ok &= (a_stride >= width); |
| 64 ok &= (a_size <= buf->a_size); | 71 ok &= (a_size <= buf->a_size); |
| 65 ok &= (buf->a != NULL); | 72 ok &= (buf->a != NULL); |
| 66 } | 73 } |
| 67 } else { // RGB checks | 74 } else { // RGB checks |
| 68 const WebPRGBABuffer* const buf = &buffer->u.RGBA; | 75 const WebPRGBABuffer* const buf = &buffer->u.RGBA; |
| 69 const int stride = abs(buf->stride); | 76 const int stride = abs(buf->stride); |
| 70 const uint64_t size = (uint64_t)stride * height; | 77 const uint64_t size = MIN_BUFFER_SIZE(width, height, stride); |
| 71 ok &= (size <= buf->size); | 78 ok &= (size <= buf->size); |
| 72 ok &= (stride >= width * kModeBpp[mode]); | 79 ok &= (stride >= width * kModeBpp[mode]); |
| 73 ok &= (buf->rgba != NULL); | 80 ok &= (buf->rgba != NULL); |
| 74 } | 81 } |
| 75 return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM; | 82 return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM; |
| 76 } | 83 } |
| 84 #undef MIN_BUFFER_SIZE |
| 77 | 85 |
| 78 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { | 86 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) { |
| 79 const int w = buffer->width; | 87 const int w = buffer->width; |
| 80 const int h = buffer->height; | 88 const int h = buffer->height; |
| 81 const WEBP_CSP_MODE mode = buffer->colorspace; | 89 const WEBP_CSP_MODE mode = buffer->colorspace; |
| 82 | 90 |
| 83 if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) { | 91 if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) { |
| 84 return VP8_STATUS_INVALID_PARAM; | 92 return VP8_STATUS_INVALID_PARAM; |
| 85 } | 93 } |
| 86 | 94 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 *dst = *src; | 250 *dst = *src; |
| 243 if (src->private_memory != NULL) { | 251 if (src->private_memory != NULL) { |
| 244 src->is_external_memory = 1; // src relinquishes ownership | 252 src->is_external_memory = 1; // src relinquishes ownership |
| 245 src->private_memory = NULL; | 253 src->private_memory = NULL; |
| 246 } | 254 } |
| 247 } | 255 } |
| 248 } | 256 } |
| 249 | 257 |
| 250 //------------------------------------------------------------------------------ | 258 //------------------------------------------------------------------------------ |
| 251 | 259 |
| OLD | NEW |