File: fuzz_backtranslate.c

package info (click to toggle)
liblouis 3.36.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 86,248 kB
  • sloc: ansic: 37,162; makefile: 1,298; python: 772; lisp: 390; sh: 339; perl: 221; cpp: 21
file content (127 lines) | stat: -rw-r--r-- 3,162 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// liblouis Braille Translation and Back-Translation Library
//
// Copyright (C) 2023 Anna Stan, Nicolas Morel, Kalilou Mamadou Dramé
//
// This file is part of liblouis.
//
// liblouis is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// liblouis is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with liblouis. If not, see <http://www.gnu.org/licenses/>.
//

#include <config.h>

#include <assert.h>
#include <fcntl.h>
#include <internal.h>
#include <liblouis.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define LANGUAGE "en"

static int initialized = 0;

#define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m"

static const char *table_default;

static void __attribute__((destructor)) free_ressources(void) { lou_free(); }

void avoid_log(logLevels level, const char *msg) {
  (void)level;
  (void)msg;
}

extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  int inputLen = 0;
  int outputLen = 0;
  static int counter = 0;
  if (!initialized) {
    lou_registerLogCallback(avoid_log);
    initialized = 1;
  }

  if (size < 512) {
    return 0;
  }

  // Write first half of fuzz data to a table file.
  char new_file[256];
  sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter);
  counter++;

  FILE *fp = fopen(new_file, "wb");
  if (!fp) {
    return 0;
  }
  fwrite(data, 512, 1, fp);
  fclose(fp);

  // Adjust data pointer to after the table file data.
  data += 512;
  size -= 512;

  /* check if this works, otherwise bail */
  if (lou_checkTable(new_file) == 0) {
    lou_free();
    unlink(new_file);
    return 0;
  }
  char *mutable_data = NULL;
  table_default = new_file;

  mutable_data = strndup((char *)data, size);
  if (!mutable_data) {
    perror("malloc");
    exit(1);
  }

  widechar *inputText = malloc((size * 16 + 1) * sizeof(widechar));
  int len = (int)_lou_extParseChars(mutable_data, inputText);
  free(mutable_data);
  if (len <= 0) {
    free(inputText);
    unlink(new_file);
    lou_free();
    return -1;
  }

  assert(len <= (size * 16));
  inputLen = len;
  outputLen = len * 16;
  widechar *outputText = malloc((outputLen + 1) * sizeof(widechar));

  lou_backTranslateString(table_default, inputText, &inputLen, outputText,
                      &outputLen, NULL, NULL, ucBrl);

  free(inputText);
  free(outputText);

  lou_free();
  unlink(new_file);

  return 0;
}

extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
                              int (*UserCb)(const uint8_t *Data, size_t Size));

int main(int argc, char *argv[]) {
       LLVMFuzzerRunDriver(&argc, &argv, LLVMFuzzerTestOneInput);
}