mux_demux_api_fuzzer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #include "./fuzz_utils.h"
  17. #include "src/webp/demux.h"
  18. #include "src/webp/mux.h"
  19. int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
  20. WebPData webp_data;
  21. WebPDataInit(&webp_data);
  22. webp_data.size = size;
  23. webp_data.bytes = data;
  24. // Extracted chunks and frames are not processed or decoded,
  25. // which is already covered extensively by the other fuzz targets.
  26. if (size & 1) {
  27. // Mux API
  28. WebPMux* mux = WebPMuxCreate(&webp_data, size & 2);
  29. if (!mux) return 0;
  30. WebPData chunk;
  31. (void)WebPMuxGetChunk(mux, "EXIF", &chunk);
  32. (void)WebPMuxGetChunk(mux, "ICCP", &chunk);
  33. (void)WebPMuxGetChunk(mux, "FUZZ", &chunk); // unknown
  34. uint32_t flags;
  35. (void)WebPMuxGetFeatures(mux, &flags);
  36. WebPMuxAnimParams params;
  37. (void)WebPMuxGetAnimationParams(mux, &params);
  38. WebPMuxError status;
  39. WebPMuxFrameInfo info;
  40. for (int i = 0; i < kFuzzFrameLimit; i++) {
  41. status = WebPMuxGetFrame(mux, i + 1, &info);
  42. if (status == WEBP_MUX_NOT_FOUND) {
  43. break;
  44. } else if (status == WEBP_MUX_OK) {
  45. WebPDataClear(&info.bitstream);
  46. }
  47. }
  48. WebPMuxDelete(mux);
  49. } else {
  50. // Demux API
  51. WebPDemuxer* demux;
  52. if (size & 2) {
  53. WebPDemuxState state;
  54. demux = WebPDemuxPartial(&webp_data, &state);
  55. if (state < WEBP_DEMUX_PARSED_HEADER) {
  56. WebPDemuxDelete(demux);
  57. return 0;
  58. }
  59. } else {
  60. demux = WebPDemux(&webp_data);
  61. if (!demux) return 0;
  62. }
  63. WebPChunkIterator chunk_iter;
  64. if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter)) {
  65. (void)WebPDemuxNextChunk(&chunk_iter);
  66. }
  67. WebPDemuxReleaseChunkIterator(&chunk_iter);
  68. if (WebPDemuxGetChunk(demux, "ICCP", 0, &chunk_iter)) { // 0 == last
  69. (void)WebPDemuxPrevChunk(&chunk_iter);
  70. }
  71. WebPDemuxReleaseChunkIterator(&chunk_iter);
  72. // Skips FUZZ because the Demux API has no concept of (un)known chunks.
  73. WebPIterator iter;
  74. if (WebPDemuxGetFrame(demux, 1, &iter)) {
  75. for (int i = 1; i < kFuzzFrameLimit; i++) {
  76. if (!WebPDemuxNextFrame(&iter)) break;
  77. }
  78. }
  79. WebPDemuxReleaseIterator(&iter);
  80. WebPDemuxDelete(demux);
  81. }
  82. return 0;
  83. }