Coverage Report

Created: 2025-07-11 06:40

/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
24.6k
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
40
24.6k
  size_t preamble = 2 + 2 + 1 + sizeof(NeAACDecConfiguration);
41
24.6k
  NeAACDecConfiguration config;
42
24.6k
  uint64_t sample_rate;
43
24.6k
  unsigned char num_channels;
44
24.6k
  NeAACDecConfigurationPtr config_ptr;
45
  /* feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW); */
46
47
24.6k
  if (size < preamble) return 0;
48
24.5k
  size_t len1 = data[0] | (data[1] << 8);
49
24.5k
  data += 2;
50
24.5k
  size_t len2 = data[0] | (data[1] << 8);
51
24.5k
  data += 2;
52
24.5k
  uint8_t flags = data[0];
53
24.5k
  data += 1;
54
24.5k
  memcpy(&config, data, sizeof(NeAACDecConfiguration));
55
24.5k
  data += sizeof(NeAACDecConfiguration);
56
24.5k
  size -= preamble;
57
58
24.5k
  if (len1 + len2 > size) return 0;
59
24.4k
  size_t len3 = size - len1 - len2;
60
24.4k
  int use_init2 = flags & 1;
61
24.4k
  int seek_before = flags & 2;
62
24.4k
  int seek_between = flags & 4;
63
24.4k
  size_t buffer_op = (flags >> 3) & 3;
64
24.4k
  int use_drm = flags & 32;
65
24.4k
  int drm_channels = (flags >> 5) & 7;
66
24.4k
  unsigned long drm_sample_rate = config.defSampleRate;
67
24.4k
  int res, ok;
68
24.4k
  const size_t kBufferSize[4] = {0, 0, 16, 16384};
69
24.4k
  size_t buffer_size = kBufferSize[buffer_op];
70
24.4k
  void* buffer = buffer_size > 0 ? (unsigned char *)malloc(buffer_size) : NULL;
71
72
24.4k
  unsigned char* part1 = (unsigned char *)malloc(len1);
73
24.4k
  unsigned char* part2 = (unsigned char *)malloc(len2);
74
24.4k
  unsigned char* part3 = (unsigned char *)malloc(len3);
75
24.4k
  memcpy(part1, data, len1);
76
24.4k
  data += len1;
77
24.4k
  memcpy(part2, data, len2);
78
24.4k
  data += len2;
79
24.4k
  memcpy(part3, data, len3);
80
24.4k
  data += len3;
81
82
24.4k
  NeAACDecHandle decoder = NeAACDecOpen();
83
24.4k
  ok = NeAACDecSetConfiguration(decoder, &config);
84
24.4k
  if (!ok) goto cleanup;
85
24.2k
  config_ptr = NeAACDecGetCurrentConfiguration(decoder);
86
24.2k
  if (!config_ptr) __builtin_trap();
87
24.2k
  if (use_init2) {
88
7.38k
    res = NeAACDecInit2(decoder, part1, len1, &sample_rate, &num_channels);
89
16.8k
  } else {
90
16.8k
    res = NeAACDecInit(decoder, part1, len1, &sample_rate, &num_channels);
91
16.8k
  }
92
24.2k
  if (use_drm) {
93
6.26k
#ifdef DRM_SUPPORT
94
6.26k
    NeAACDecInitDRM(&decoder, drm_channels, drm_sample_rate);
95
#else
96
    (void)drm_channels;
97
    (void)drm_sample_rate;
98
#endif
99
6.26k
  }
100
101
24.2k
  if (res != 0) goto cleanup;
102
23.6k
  NeAACDecFrameInfo faad_info;
103
23.6k
  if (seek_before) {
104
9.24k
    NeAACDecPostSeekReset(decoder, 0x1234567);
105
9.24k
  }
106
23.6k
  NeAACDecDecode(decoder, &faad_info, part2, len2);
107
23.6k
  if (seek_between) {
108
7.18k
    NeAACDecPostSeekReset(decoder, -0x1234567);
109
7.18k
  }
110
23.6k
  if (buffer_op == 0) {
111
12.6k
    NeAACDecDecode(decoder, &faad_info, part3, len3);
112
12.6k
  } else {
113
10.9k
    NeAACDecDecode2(decoder, &faad_info, part3, len3, &buffer, buffer_size);
114
10.9k
  }
115
116
24.4k
cleanup:
117
24.4k
  NeAACDecClose(decoder);
118
24.4k
  free(part1);
119
24.4k
  free(part2);
120
24.4k
  free(part3);
121
24.4k
  if (buffer) free(buffer);
122
123
24.4k
  return 0;
124
23.6k
}