| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodec_libbmp.h" | 8 #include "SkCodec_libbmp.h" |
| 9 #include "SkCodec_libico.h" | 9 #include "SkCodec_libico.h" |
| 10 #include "SkCodec_libpng.h" | 10 #include "SkCodec_libpng.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 (!memcmp(buffer, icoSig, sizeof(icoSig)) || | 26 (!memcmp(buffer, icoSig, sizeof(icoSig)) || |
| 27 !memcmp(buffer, curSig, sizeof(curSig))); | 27 !memcmp(buffer, curSig, sizeof(curSig))); |
| 28 } | 28 } |
| 29 | 29 |
| 30 /* | 30 /* |
| 31 * Assumes IsIco was called and returned true | 31 * Assumes IsIco was called and returned true |
| 32 * Creates an Ico decoder | 32 * Creates an Ico decoder |
| 33 * Reads enough of the stream to determine the image format | 33 * Reads enough of the stream to determine the image format |
| 34 */ | 34 */ |
| 35 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) { | 35 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) { |
| 36 // Ensure that we do not leak the input stream |
| 37 SkAutoTDelete<SkStream> inputStream(stream); |
| 38 |
| 36 // Header size constants | 39 // Header size constants |
| 37 static const uint32_t kIcoDirectoryBytes = 6; | 40 static const uint32_t kIcoDirectoryBytes = 6; |
| 38 static const uint32_t kIcoDirEntryBytes = 16; | 41 static const uint32_t kIcoDirEntryBytes = 16; |
| 39 | 42 |
| 40 // Read the directory header | 43 // Read the directory header |
| 41 SkAutoTDeleteArray<uint8_t> dirBuffer( | 44 SkAutoTDeleteArray<uint8_t> dirBuffer( |
| 42 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes)); | 45 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes)); |
| 43 if (stream->read(dirBuffer.get(), kIcoDirectoryBytes) != | 46 if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) != |
| 44 kIcoDirectoryBytes) { | 47 kIcoDirectoryBytes) { |
| 45 SkDebugf("Error: unable to read ico directory header.\n"); | 48 SkDebugf("Error: unable to read ico directory header.\n"); |
| 46 return NULL; | 49 return NULL; |
| 47 } | 50 } |
| 48 | 51 |
| 49 // Process the directory header | 52 // Process the directory header |
| 50 const uint16_t numImages = get_short(dirBuffer.get(), 4); | 53 const uint16_t numImages = get_short(dirBuffer.get(), 4); |
| 51 if (0 == numImages) { | 54 if (0 == numImages) { |
| 52 SkDebugf("Error: No images embedded in ico.\n"); | 55 SkDebugf("Error: No images embedded in ico.\n"); |
| 53 return NULL; | 56 return NULL; |
| 54 } | 57 } |
| 55 | 58 |
| 56 // Ensure that we can read all of indicated directory entries | 59 // Ensure that we can read all of indicated directory entries |
| 57 SkAutoTDeleteArray<uint8_t> entryBuffer( | 60 SkAutoTDeleteArray<uint8_t> entryBuffer( |
| 58 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes)); | 61 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes)); |
| 59 if (stream->read(entryBuffer.get(), numImages*kIcoDirEntryBytes) != | 62 if (inputStream.get()->read(entryBuffer.get(), numImages*kIcoDirEntryBytes)
!= |
| 60 numImages*kIcoDirEntryBytes) { | 63 numImages*kIcoDirEntryBytes) { |
| 61 SkDebugf("Error: unable to read ico directory entries.\n"); | 64 SkDebugf("Error: unable to read ico directory entries.\n"); |
| 62 return NULL; | 65 return NULL; |
| 63 } | 66 } |
| 64 | 67 |
| 65 // This structure is used to represent the vital information about entries | 68 // This structure is used to represent the vital information about entries |
| 66 // in the directory header. We will obtain this information for each | 69 // in the directory header. We will obtain this information for each |
| 67 // directory entry. | 70 // directory entry. |
| 68 struct Entry { | 71 struct Entry { |
| 69 uint32_t offset; | 72 uint32_t offset; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 uint32_t size = directoryEntries.get()[i].size; | 117 uint32_t size = directoryEntries.get()[i].size; |
| 115 | 118 |
| 116 // Ensure that the offset is valid | 119 // Ensure that the offset is valid |
| 117 if (offset < bytesRead) { | 120 if (offset < bytesRead) { |
| 118 SkDebugf("Warning: invalid ico offset.\n"); | 121 SkDebugf("Warning: invalid ico offset.\n"); |
| 119 continue; | 122 continue; |
| 120 } | 123 } |
| 121 | 124 |
| 122 // If we cannot skip, assume we have reached the end of the stream and | 125 // If we cannot skip, assume we have reached the end of the stream and |
| 123 // stop trying to make codecs | 126 // stop trying to make codecs |
| 124 if (stream->skip(offset - bytesRead) != offset - bytesRead) { | 127 if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) { |
| 125 SkDebugf("Warning: could not skip to ico offset.\n"); | 128 SkDebugf("Warning: could not skip to ico offset.\n"); |
| 126 break; | 129 break; |
| 127 } | 130 } |
| 128 bytesRead = offset; | 131 bytesRead = offset; |
| 129 | 132 |
| 130 // Create a new stream for the embedded codec | 133 // Create a new stream for the embedded codec |
| 131 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, size)); | 134 SkAutoTUnref<SkData> data( |
| 135 SkData::NewFromStream(inputStream.get(), size)); |
| 132 if (NULL == data.get()) { | 136 if (NULL == data.get()) { |
| 133 SkDebugf("Warning: could not create embedded stream.\n"); | 137 SkDebugf("Warning: could not create embedded stream.\n"); |
| 134 break; | 138 break; |
| 135 } | 139 } |
| 136 SkAutoTDelete<SkMemoryStream> | 140 SkAutoTDelete<SkMemoryStream> |
| 137 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get()))); | 141 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get()))); |
| 138 bytesRead += size; | 142 bytesRead += size; |
| 139 | 143 |
| 140 // Check if the embedded codec is bmp or png and create the codec | 144 // Check if the embedded codec is bmp or png and create the codec |
| 141 const bool isPng = SkPngCodec::IsPng(embeddedStream); | 145 const bool isPng = SkPngCodec::IsPng(embeddedStream); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 249 } |
| 246 | 250 |
| 247 // On success or partial success, return the result | 251 // On success or partial success, return the result |
| 248 return result; | 252 return result; |
| 249 } | 253 } |
| 250 } | 254 } |
| 251 | 255 |
| 252 SkDebugf("Error: No matching candidate image in ico.\n"); | 256 SkDebugf("Error: No matching candidate image in ico.\n"); |
| 253 return result; | 257 return result; |
| 254 } | 258 } |
| OLD | NEW |