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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to <http://unlicense.org/>
 */

pub mod mutator {
    extern crate rand;
    use self::rand::Rng;
    /* These implementations of rand_usize() and rand_u8() are
     * contained in these functions, so if you want to use
     * another seed you don't have to edit the whole code.
     */
    #[inline]
    fn rand_usize(min: usize, max: usize) -> usize {
        rand::thread_rng().gen_range(min, max)
    }
    #[inline]
    fn rand_u8() -> u8 {
        rand::thread_rng().gen_range(0usize, u8::max_value() as usize) as u8
    }
    /*
     * This function flips bits randomly in data:Vec.
     * The extent of the mutation in data can be
     * customized by changing the value at [x].
     */
    #[inline]
    pub fn bitflipping(data: &mut Vec<u8>) {
        let mut count = ((data.len() as f64 * 8.0)* /*[x]*/ 0.01 /*[x]*/ ) as i64;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            let bit = rand_usize(
                0,
                if (if data.len() * 8 == 0 {
                        append_data(data);
                        continue;
                    } else {
                        data.len() * 8 - 1
                    }) <= 0
                {
                    append_data(data);
                    continue;
                } else {
                    data.len() * 8 - 1
                },
            );
            let idx_bit = bit % 8;
            let idx_byte = bit / 8;
            if idx_byte < data.len() {
                data[idx_byte] ^= 1 << idx_bit;
            }
        }
    }
    /*
     * This function flips bytes in data:Vec.
     * The extent of the mutation in data can be
     * customized by changing the value at [x].
     */
    #[inline]
    pub fn byteflipping(data: &mut Vec<u8>) {
        let mut count = (data.len() as f64 * /*[x]*/ 0.01 /*[x]*/) as usize;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            let index = rand_usize(
                0,
                if data.len() <= 1 {
                    append_data(data);
                    continue;
                } else {
                    data.len() - 1
                },
            );
            if index < data.len() {
                data[index] = rand_u8();
            }
        }
    }
    /*
     * In this function some special integers will be
     * placed in data:Vec<u8>.
     */
    #[inline]
    pub fn special_ints(data: &mut Vec<u8>) {
        let byte: [u8; 3] = [0xff, 0x7f, 0];
        let short: [[u8; 2]; 2] = [[0xff, 0xff], [0x0, 0x0]];
        let int32: [[u8; 4]; 5] = [
            [0xff, 0xff, 0xff, 0xff],
            [0, 0, 0, 0],
            [0x80, 0, 0, 0],
            [0x40, 0, 0, 0],
            [0x7f, 0xff, 0xff, 0xff],
        ];
        let mut count = (data.len() as f64 * 0.01) as usize;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            match rand_usize(0, 2) {
                0 => {
                    /* byte */
                    let index = rand_usize(
                        0,
                        if data.len() == 0 {
                            append_data(data);
                            continue;
                        } else {
                            data.len()
                        },
                    );
                    data[index] = byte[rand_usize(0, 2)];
                }
                1 => {
                    /* short */
                    let n_size = 2;
                    let sz: i64 = data.len() as i64 - n_size as i64;
                    if sz <= 0 {
                        continue;
                    }
                    let mut index = rand_usize(0, sz as usize);
                    for i in short[rand_usize(0, 1)].iter() {
                        if index < data.len() {
                            data[index] = *i;
                            index += 1;
                        }
                    }
                }
                2 => {
                    /* int */
                    let n_size = 4;
                    let sz: i64 = data.len() as i64 - n_size as i64;
                    if sz <= 0 {
                        continue;
                    }
                    let mut index = rand_usize(0, sz as usize);
                    for i in int32[rand_usize(0, 1)].iter() {
                        if index < data.len() {
                            data[index] = *i;
                            index += 1;
                        }
                    }
                }
                _ => unreachable!(),
            }
        }
    }
    /*
     * In this function values in data:Vec will be decremented
     * or incremented a random number of times.
     */
    #[inline]
    pub fn add_substract_binary(data: &mut Vec<u8>) {
        let mut count = (data.len() as f64 * 0.01) as usize;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            let substract = rand_usize(0, 1);
            let index = rand_usize(
                0,
                if data.len() == 0 {
                    append_data(data);
                    continue;
                } else {
                    data.len()
                },
            );
            let un8 = rand_u8();
            if substract == 1 {
                if data[index] >= un8 {
                    data[index] = data[index] - un8;
                }
            } else {
                if (data[index] as u32 + un8 as u32) < (u8::max_value() as u32) {
                    data[index] = data[index] + un8;
                }
            }
        }

    }
    /*
     * This function will replace a value in data:Vec with
     * another one in data:Vec.
     * Here is an example:
     * [1,2,3,4,5,6,7,8,9,0]
     *           <- - - - -
     * [1,2,3,4,0,6,7,8,9,0]
     * In the above example 0 has been copied where 5 has
     * previously been.
     */
    #[inline]
    pub fn chunk_spew(data: &mut Vec<u8>) {
        let mut count = (data.len() as f64 * 0.01) as usize;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            let offset_sz_src = rand_usize(
                0,
                if data.len() / 2 == 0 {
                    append_data(data);
                    continue;
                } else {
                    data.len() / 2
                },
            );
            let mut offsetsrc = rand_usize(
                0,
                if (data.len() as i64 - offset_sz_src as i64) < 0 {
                    append_data(data);
                    continue;
                } else {
                    data.len() - offset_sz_src
                },
            );
            let mut offsetdest = rand_usize(
                0,
                if (data.len() as i64 - offset_sz_src as i64) < 0 {
                    append_data(data);
                    continue;
                } else {
                    data.len() - offset_sz_src
                },
            );
            for _ in 0..offset_sz_src {
                if offsetsrc < data.len() && offsetdest < data.len() {
                    /* Using the offsets like raw pointers */
                    data[offsetdest] = data[offsetsrc];
                    offsetdest += 1;
                    offsetsrc += 1;
                } else {
                    /* Errors do not matter, we will just save time and break to continue */
                    break;
                }
            }
        }
    }
    /*
     * Appends random bytes to data:Vec.
     * NOTE: Do not use this function too often,
     * if data:Vec becomes too long it will take too long to
     * process it with the other functions since it takes
     * exponentially longer to process.
     * This function will be called automagically if a function
     * detects that data:Vec is too short.
     */
    #[inline]
    pub fn append_data(data: &mut Vec<u8>) {
        let mut count = (data.len() as f64 * 0.01) as usize;
        if count == 0 {
            count = 1;
        }
        for _ in 0..count {
            data.push(rand_u8());
        }
    }
    /*
     * Shrinks data:Vec to a smaller size.
     * NOTE: Use this function not too often, in a loop it
     * will keep the length of data:Vec at 0.
     */
    #[allow(dead_code)]
    /*
     * Since this function might not
     * be called at all
     */
    #[inline]
    pub fn truncate(data: &mut Vec<u8>) {
        if data.len() == 0 {
            return;
        }
        let mut count = (data.len() as f64 * 0.999) as usize;
        if count == 0 {
            count = 1;
        }
        data.truncate(count);
    }
    /*
     * If you don't need specialized mutation you can use
     * this function to choose the kind of
     * mutation at random.
     */
    #[inline]
    pub fn mutate(v: &mut Vec<u8>) {
        match rand_usize(0, 5) {
            0 => special_ints(v),
            1 => bitflipping(v),
            2 => byteflipping(v),
            3 => add_substract_binary(v),
            4 => chunk_spew(v),
            _ => unreachable!(),
        }
    }
}