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

/// Initializes the array with -1 in each field and returns it.
/// Array is created on the heap.
///
/// The array represents the indices of the symbols in the current
/// iteration trying to crack the password.
///
/// Example: If our alphabet is Σ={a,b,c}, our maximum password
/// length is 5 and our current attempt is [,,a,c,b] then the
/// indices will be [-1,-1,0,2,1]. Values will never ever
/// go back to "-1" once been at 0 because we can't have empty
/// slots inside a word (they shall be marked with a space in
/// the alphabet).
pub fn indices_create(length: usize) -> Box<[isize]> {
    vec![-1; length].into_boxed_slice()
}

/// Transforms the indices array into a string using the alphabet.
/// Empty slots will be skipped.
pub fn indices_to_string(alphabet: &Box<[char]>, indices: &Box<[isize]>) -> String {
    let mut word = String::new();
    for i in 0..indices.len() {
        let index = indices[i];
        if index != -1 {
            // otherwise our string isn't so far that long
            let symbol = alphabet[index as usize];
            if symbol != '\0' {
                word.push(symbol)
            }
        }
    }
    word
}

/*/// Calculates how many fields are not "-1" aka how long the word that is represented is.
pub fn indices_word_length(indices: &Box<[isize]>) -> usize {
    let mut n = 0;
    let mut i = (indices.len() - 1) as isize;
    while i >= 0 {
        if indices[i as usize] != -1 {
            n += 1;
            i -= 1;
        } else {
            break;
        }
    }
    n
}*/

/// Increments the indices array by a given number.
pub fn indices_increment_by(
    alphabet: &Box<[char]>,
    indices: &mut Box<[isize]>,
    add_value: isize) -> Result<(), &'static str> {
    if add_value < 0 {
        return Err("add_value must be > 0");
    } else if (add_value) == 0 {
        // Nothing to do
        return Ok(());
    }

    // The carry from the last iteration; in the first iteration the carry
    // is the add_value; in each further iteration its the actual carry
    let mut carry = add_value;
    for i in 0..indices.len() {
        // we go from left to right
        let position = indices.len() - 1 - i;
        if carry == 0 {
            // done, no more carry to bring to the next position
            break;
        }

        // the current index at this position in the indices array
        let current_value = indices[position];
        let mut new_value = current_value + carry;

        // out of bounds? modulo!
        if new_value >= alphabet.len() as isize {
            // carry for next position/next iteration
            carry = new_value / (alphabet.len()) as isize;
            new_value = new_value % (alphabet.len()) as isize;
        } else {
            carry = 0;
        }

        indices[position] = new_value;
    }

    if carry == 0 {
        Ok(())
    } else {
        // at the end its not the original state or the maximum value but some
        // invalid value
        Err("Overflow detected! Data/state is now invalid and no longer reliable!")
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use crate::symbols::combinations_count;

    #[test]
    fn test_create_indices_arr() {
        let arr = indices_create(3);
        assert_eq!(arr[0], -1);
        assert_eq!(arr[1], -1);
        assert_eq!(arr[2], -1);
    }

    #[test]
    fn test_get_word_as_string_1() {
        let alphabet: Box<[char]> = Box::from(['a', 'b', 'c']);
        let mut arr = indices_create(5);
        arr[2] = 1;
        arr[3] = 2;
        arr[4] = 0;
        let str = indices_to_string(&alphabet, &arr);
        assert_eq!(str, "bca", "Strings should equal")
    }

    #[test]
    fn test_get_word_as_string_2() {
        let alphabet: Box<[char]> = Box::from(['a', 'b', 'c']);
        let mut arr = indices_create(5);
        arr[0] = 1;
        arr[1] = 1;
        arr[2] = 1;
        arr[3] = 2;
        arr[4] = 0;
        let str = indices_to_string(&alphabet, &arr);
        assert_eq!(str, "bbbca", "Strings should equal")
    }

    #[test]
    fn test_increment_indices_array_add1_overflow() {
        let alphabet: Box<[char]> = Box::from(['0', '1']);
        let mut arr = indices_create(5);
        arr[3] = 1;
        arr[4] = 1;
        indices_increment_by(&alphabet, &mut arr, 1);
        assert_eq!(arr[0], -1, "after '11' comes '000'");
        assert_eq!(arr[1], -1, "after '11' comes '000'");
        assert_eq!(arr[2], 0, "after '11' comes '000'");
        assert_eq!(arr[3], 0, "after '11' comes '000'");
        assert_eq!(arr[4], 0, "after '11' comes '000'");
    }

    #[test]
    fn test_increment_indices_array_add1() {
        let alphabet: Box<[char]> = Box::from(['a', 'b', 'c', 'd', 'e', 'f']);
        let mut arr = indices_create(5);
        arr[2] = 3;
        arr[3] = 5;
        arr[4] = 5;
        indices_increment_by(&alphabet, &mut arr, 1);
        assert_eq!(arr[2], 4, "after 'ffd' comes 'ffe'");
        assert_eq!(arr[3], 0, "after 'ffd' comes 'ffe'");
        assert_eq!(arr[4], 0, "after 'ffd' comes 'ffe'");
    }

    #[test]
    fn test_increment_indices_array_add1_initial() {
        let alphabet: Box<[char]> = Box::from(['a', 'b']);
        let mut arr = indices_create(5);
        indices_increment_by(&alphabet, &mut arr, 1);
        assert_eq!(arr[4], 0, "after () comes 'a'");
    }

    #[test]
    fn test_increment_indices_array_total_overflow() {
        let alphabet: Box<[char]> = Box::from(['a', 'b', 'c', 'd', 'e', 'f']);
        let mut arr = indices_create(3);
        arr[0] = 5;
        arr[1] = 5;
        arr[2] = 5;
        match indices_increment_by(&alphabet, &mut arr, 1) {
            Ok(_) => {
                assert!(false, "fff with length 3 should not be incrementable!")
            }
            _ => ()
        }
    }

    #[test]
    fn test_increment_indices_to_upper_bound() {
        const LEN: usize = 3;
        let alphabet: Box<[char]> = Box::from(['a', 'b', 'c']);
        let mut indices = indices_create(LEN);
        // should make -1 -1 -1 to 2 2 2
        // minus one because we are already at the first element (-1, -1, -1)
        let steps = combinations_count(&alphabet, LEN as u32, ) as isize - 1;
        indices_increment_by(&alphabet, &mut indices, steps);
        for i in 0..LEN {
            assert_eq!(indices[i], (alphabet.len() - 1) as isize)
        }
    }

    #[test]
    fn test_length_of_indices_array() {
        let alphabet: Box<[char]> = Box::from(['a']);
        const LENGTH: usize = 5;
        let mut indices = indices_create(LENGTH);
        assert_eq!(0, indices_word_length(&indices));
        indices_increment_by(&alphabet, &mut indices, 1);
        assert_eq!(1, indices_word_length(&indices));
        indices_increment_by(&alphabet, &mut indices, 4);
        assert_eq!(LENGTH, indices_word_length(&indices));
    }
}