• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 <stddef.h>
17 #include <stdint.h>
18 #include <vector>
19 
20 #include "archive.h"
21 
22 struct Buffer {
23   const uint8_t *buf;
24   size_t len;
25 };
26 
reader_callback(struct archive * a,void * client_data,const void ** block)27 ssize_t reader_callback(struct archive *a, void *client_data,
28                         const void **block) {
29   Buffer *buffer = reinterpret_cast<Buffer *>(client_data);
30   *block = buffer->buf;
31   ssize_t len = buffer->len;
32   buffer->len = 0;
33   return len;
34 }
35 
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)36 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
37   int ret;
38   ssize_t r;
39   struct archive *a = archive_read_new();
40 
41   archive_read_support_filter_all(a);
42   archive_read_support_format_all(a);
43 
44   Buffer buffer = {buf, len};
45   archive_read_open(a, &buffer, NULL, reader_callback, NULL);
46 
47   std::vector<uint8_t> data_buffer(getpagesize(), 0);
48   struct archive_entry *entry;
49   while(1) {
50     ret = archive_read_next_header(a, &entry);
51     if (ret == ARCHIVE_EOF || ret == ARCHIVE_FATAL)
52       break;
53     if (ret == ARCHIVE_RETRY)
54       continue;
55     while ((r = archive_read_data(a, data_buffer.data(),
56             data_buffer.size())) > 0)
57       ;
58     if (r == ARCHIVE_FATAL)
59       break;
60   }
61 
62   archive_read_free(a);
63   return 0;
64 }
65