Coverage Report

Created: 2025-07-23 06:30

/proc/self/cwd/fuzz/fuzz_decode.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**/
28
29
#include <fenv.h> 
30
#include <stddef.h>
31
#include <stdint.h>
32
#include <string.h>
33
#include <stdlib.h>
34
35
#include "neaacdec.h"
36
37
int feenableexcept(int excepts);
38
39
9.15k
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
40
9.15k
  size_t preamble = 2 + 2 + 1 + sizeof(NeAACDecConfiguration);
41
9.15k
  NeAACDecConfiguration config;
42
9.15k
  uint64_t sample_rate;
43
9.15k
  unsigned char num_channels;
44
9.15k
  NeAACDecConfigurationPtr config_ptr;
45
  /* feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW); */
46
47
9.15k
  if (size < preamble) return 0;
48
9.14k
  size_t len1 = data[0] | (data[1] << 8);
49
9.14k
  data += 2;
50
9.14k
  size_t len2 = data[0] | (data[1] << 8);
51
9.14k
  data += 2;
52
9.14k
  uint8_t flags = data[0];
53
9.14k
  data += 1;
54
9.14k
  memcpy(&config, data, sizeof(NeAACDecConfiguration));
55
9.14k
  data += sizeof(NeAACDecConfiguration);
56
9.14k
  size -= preamble;
57
58
9.14k
  if (len1 + len2 > size) return 0;
59
9.11k
  size_t len3 = size - len1 - len2;
60
9.11k
  int use_init2 = flags & 1;
61
9.11k
  int seek_before = flags & 2;
62
9.11k
  int seek_between = flags & 4;
63
9.11k
  size_t buffer_op = (flags >> 3) & 3;
64
9.11k
  int use_drm = flags & 32;
65
9.11k
  int drm_channels = (flags >> 5) & 7;
66
9.11k
  unsigned long drm_sample_rate = config.defSampleRate;
67
9.11k
  int res, ok;
68
9.11k
  const size_t kBufferSize[4] = {0, 0, 16, 16384};
69
9.11k
  size_t buffer_size = kBufferSize[buffer_op];
70
9.11k
  void* buffer = buffer_size > 0 ? (unsigned char *)malloc(buffer_size) : NULL;
71
72
9.11k
  unsigned char* part1 = (unsigned char *)malloc(len1);
73
9.11k
  unsigned char* part2 = (unsigned char *)malloc(len2);
74
9.11k
  unsigned char* part3 = (unsigned char *)malloc(len3);
75
9.11k
  memcpy(part1, data, len1);
76
9.11k
  data += len1;
77
9.11k
  memcpy(part2, data, len2);
78
9.11k
  data += len2;
79
9.11k
  memcpy(part3, data, len3);
80
9.11k
  data += len3;
81
82
9.11k
  NeAACDecHandle decoder = NeAACDecOpen();
83
9.11k
  ok = NeAACDecSetConfiguration(decoder, &config);
84
9.11k
  if (!ok) goto cleanup;
85
9.03k
  config_ptr = NeAACDecGetCurrentConfiguration(decoder);
86
9.03k
  if (!config_ptr) __builtin_trap();
87
9.03k
  if (use_init2) {
88
2.73k
    res = NeAACDecInit2(decoder, part1, len1, &sample_rate, &num_channels);
89
6.29k
  } else {
90
6.29k
    res = NeAACDecInit(decoder, part1, len1, &sample_rate, &num_channels);
91
6.29k
  }
92
9.03k
  if (use_drm) {
93
#ifdef DRM_SUPPORT
94
    NeAACDecInitDRM(&decoder, drm_channels, drm_sample_rate);
95
#else
96
2.97k
    (void)drm_channels;
97
2.97k
    (void)drm_sample_rate;
98
2.97k
#endif
99
2.97k
  }
100
101
9.03k
  if (res != 0) goto cleanup;
102
8.85k
  NeAACDecFrameInfo faad_info;
103
8.85k
  if (seek_before) {
104
3.34k
    NeAACDecPostSeekReset(decoder, 0x1234567);
105
3.34k
  }
106
8.85k
  NeAACDecDecode(decoder, &faad_info, part2, len2);
107
8.85k
  if (seek_between) {
108
3.23k
    NeAACDecPostSeekReset(decoder, -0x1234567);
109
3.23k
  }
110
8.85k
  if (buffer_op == 0) {
111
5.83k
    NeAACDecDecode(decoder, &faad_info, part3, len3);
112
5.83k
  } else {
113
3.01k
    NeAACDecDecode2(decoder, &faad_info, part3, len3, &buffer, buffer_size);
114
3.01k
  }
115
116
9.11k
cleanup:
117
9.11k
  NeAACDecClose(decoder);
118
9.11k
  free(part1);
119
9.11k
  free(part2);
120
9.11k
  free(part3);
121
9.11k
  if (buffer) free(buffer);
122
123
9.11k
  return 0;
124
8.85k
}