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