File: common.transpositions.rectangular.c

package info (click to toggle)
crank 0.1.4-2
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 952 kB
  • ctags: 449
  • sloc: ansic: 3,052; makefile: 231
file content (302 lines) | stat: -rw-r--r-- 11,034 bytes parent folder | download | duplicates (3)
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
/* common.transpostions.rectangular.h - common rectangular tranpositions
 * 
 * This program is part of Crank, a cryptanalysis tool
 * Copyright (C) 2000 Matthew Russell
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License (LICENSE) for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

#include "crank.h"
#include "common.transpositions.rectangular.h"

const char *grid_path_names[MAX_GRIDPATH_ARRAY] = {
    "",
    "Straight Horizontals TL",              
    "Straight Horizontals BL",
    "Reverse Horizontals TR", 
    "Reverse Horizontals BR", 
    "Alternate Horizontals TL",
    "Alternate Horizontals TR",
    "Alternate Horizontals BR",
    "Alternate Horizontals BL", 
    
    "Descending Verticals TL",
    "Descending Verticals TR",
    "Ascending Verticals BL",
    "Ascending Verticals BR",
    "Alternate Verticals TL",
    "Alternate Verticals TR",
    "Alternate Verticals BR",
    "Alternate Verticals BL",

    "Clockwise Spiral in TL", 
    "Clockwise Spiral in TR", 
    "Clockwise Spiral in BR", 
    "Clockwise Spiral in BL", 

    "Anticlockwise Spiral in TL", 
    "Anticlockwise Spiral in TR", 
    "Anticlockwise Spiral in BR", 
    "Anticlockwise Spiral in BL", 

    "Clockwise Spiral out TL", 
    "Clockwise Spiral out TR", 
    "Clockwise Spiral out BR", 
    "Clockwise Spiral out BL", 

    "Anticlockwise Spiral out TL", 
    "Anticlockwise Spiral out TR", 
    "Anticlockwise Spiral out BR", 
    "Anticlockwise Spiral out BL"

};

#define DIRECTION_UP    0
#define DIRECTION_RIGHT 1
#define DIRECTION_DOWN  2
#define DIRECTION_LEFT  3
#define ROTATE_CLOCKWISE(d)       (((d) + 1) % 4)
#define ROTATE_ANTICLOCKWISE(d)   (((d) + 3) % 4)
#define DIR_TO_ROW_CHANGE(d) \
                                  (  ((d) == DIRECTION_UP)      ? -1 : \
                                  (  ((d) == DIRECTION_DOWN)    ? 1  : \
                                   0))
#define DIR_TO_COL_CHANGE(d) \
                                  (  ((d) == DIRECTION_LEFT)    ? -1 : \
                                  (  ((d) == DIRECTION_RIGHT)   ? 1  : \
				     0))

#define CASE_SPIRAL(NAME, R, C, DIRECTION, ROTATE, ACTION) \
    case NAME: \
        counter = 0; \
        r = R; \
        c = C; \
        direction = DIRECTION; \
        while (counter < length - 1) { \
           counter++; \
 	   ACTION; \
           (marker_grid + r * cols)[c] = TRUE; \
           if (  (r + DIR_TO_ROW_CHANGE(direction) < 0) || \
                 (c + DIR_TO_COL_CHANGE(direction) < 0) || \
                 (r + DIR_TO_ROW_CHANGE(direction) >= rows) || \
                 (c + DIR_TO_COL_CHANGE(direction) >= cols) || \
                 (marker_grid + (DIR_TO_ROW_CHANGE(direction) + r) * cols)[DIR_TO_COL_CHANGE(direction) + c]) \
              direction = ROTATE(direction); \
           r += DIR_TO_ROW_CHANGE(direction); \
           c += DIR_TO_COL_CHANGE(direction); \
       } \
       ACTION; \
       break

/* Macro to share code between make_new_grid and extract_from_grid, which perform different operations along
   the same routes */
#define DO_PATH(ACTION, ACTION_REVERSE) \
    switch (path) { \
    case HORIZONTALS_STRAIGHT_TL: \
	for (r = 0; r < rows; r++)  \
	    for (c = 0; c < cols; c++) \
		ACTION; \
	break; \
    case HORIZONTALS_STRAIGHT_BL: \
	for (r = rows - 1; r >= 0; r--)  \
	    for (c = 0; c < cols; c++) \
		ACTION; \
	break; \
    case HORIZONTALS_REVERSE_TR: \
	for (r = 0; r < rows; r++)  \
	    for (c = cols - 1; c >= 0; c--) \
		ACTION; \
	break; \
    case HORIZONTALS_REVERSE_BR: \
	for (r = rows - 1; r >= 0; r--)  \
	    for (c = cols - 1; c >= 0; c--) \
		ACTION; \
	break; \
    case HORIZONTALS_ALTERNATE_TL: \
        for (r = 0; r < rows; r++) \
           if ((r + 1) % 2) \
              for (c = 0; c < cols; c++) \
                ACTION; \
           else \
              for (c = cols - 1; c >= 0; c--) \
                ACTION; \
        break; \
    case HORIZONTALS_ALTERNATE_TR: \
        for (r = 0; r < rows; r++) \
           if (r % 2) \
              for (c = 0; c < cols; c++) \
                ACTION; \
           else \
              for (c = cols - 1; c >= 0; c--) \
                ACTION; \
        break; \
    case HORIZONTALS_ALTERNATE_BL: \
        for (r = rows - 1; r >= 0; r--) \
           if ((rows - r) % 2) \
              for (c = 0; c < cols; c++) \
                ACTION; \
           else \
              for (c = cols - 1; c >= 0; c--) \
                ACTION; \
        break; \
    case HORIZONTALS_ALTERNATE_BR: \
        for (r = rows - 1; r >= 0; r--) \
           if ((rows - r + 1) % 2) \
              for (c = 0; c < cols; c++) \
                ACTION; \
           else \
              for (c = cols - 1; c >= 0; c--) \
                ACTION; \
        break; \
    case VERTICALS_DESCENDING_TL: \
	for (c = 0; c < cols; c++) \
	    for (r = 0; r < rows; r++)  \
		ACTION; \
	break; \
    case VERTICALS_DESCENDING_TR: \
	for (c = cols - 1; c >= 0; c--) \
	    for (r = 0; r < rows; r++)  \
		ACTION; \
	break; \
    case VERTICALS_ASCENDING_BL: \
	for (c = 0; c < cols; c++) \
	    for (r = rows - 1; r >= 0; r--)  \
		ACTION; \
	break; \
    case VERTICALS_ASCENDING_BR: \
	for (c = cols - 1; c >= 0; c--) \
	    for (r = rows - 1; r >= 0; r--)  \
		ACTION; \
	break; \
    case VERTICALS_ALTERNATE_TL: \
        for (c = 0; c < cols; c++) \
           if ((c + 1) % 2) \
              for (r = 0; r < rows; r++) \
                ACTION; \
           else \
              for (r = rows - 1; r >= 0; r--) \
                ACTION; \
        break; \
    case VERTICALS_ALTERNATE_BL: \
        for (c = 0; c < cols; c++) \
           if (c % 2) \
              for (r = 0; r < rows; r++) \
                ACTION; \
           else \
              for (r = rows - 1; r >= 0; r--) \
                ACTION; \
        break; \
    case VERTICALS_ALTERNATE_TR: \
        for (c = cols - 1; c >= 0; c--) \
           if ((cols - c) % 2) \
              for (r = 0; r < rows; r++) \
                ACTION; \
           else \
              for (r = rows - 1; r >= 0; r--) \
                ACTION; \
        break; \
    case VERTICALS_ALTERNATE_BR: \
        for (c = cols - 1; c >= 0; c--) \
           if ((cols - c + 1) % 2) \
              for (r = 0; r < rows; r++) \
                ACTION; \
           else \
              for (r = rows - 1; r >= 0; r--) \
                ACTION; \
        break; \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_IN_TL,      0,        0,        DIRECTION_RIGHT, ROTATE_CLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_IN_TR,      0,        cols - 1, DIRECTION_DOWN,  ROTATE_CLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_IN_BR,      rows - 1, cols - 1, DIRECTION_LEFT,  ROTATE_CLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_IN_BL,      rows - 1, 0,        DIRECTION_UP,    ROTATE_CLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_IN_TL,  0,        0,        DIRECTION_DOWN,  ROTATE_ANTICLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_IN_TR,  0,        cols - 1, DIRECTION_LEFT,  ROTATE_ANTICLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_IN_BR,  rows - 1, cols - 1, DIRECTION_UP,    ROTATE_ANTICLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_IN_BL,  rows - 1, 0,        DIRECTION_RIGHT, ROTATE_ANTICLOCKWISE, ACTION); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_OUT_TL,      0,        0,        DIRECTION_RIGHT, ROTATE_CLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_OUT_TR,      0,        cols - 1, DIRECTION_DOWN,  ROTATE_CLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_OUT_BR,      rows - 1, cols - 1, DIRECTION_LEFT,  ROTATE_CLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_ANTICLOCKWISE_OUT_BL,      rows - 1, 0,        DIRECTION_UP,    ROTATE_CLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_OUT_TL,  0,        0,        DIRECTION_DOWN,  ROTATE_ANTICLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_OUT_TR,  0,        cols - 1, DIRECTION_LEFT,  ROTATE_ANTICLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_OUT_BR,  rows - 1, cols - 1, DIRECTION_UP,    ROTATE_ANTICLOCKWISE, ACTION_REVERSE); \
    CASE_SPIRAL(SPIRAL_CLOCKWISE_OUT_BL,  rows - 1, 0,        DIRECTION_RIGHT, ROTATE_ANTICLOCKWISE, ACTION_REVERSE); \
    default: \
	g_warning("Unknown gridpath type."); \
	break; \
}


Grid make_new_grid(int rows, int cols, char *text, gridpath path) {
    Grid grid, marker_grid;
    char *pos = text, *end_pos;
    int r, c, length = rows * cols, counter = 0, direction;
    end_pos = text + length;
    grid = malloc(sizeof(char) * length);
    marker_grid = malloc(sizeof(char) * length);
    for (r = 0; r < rows; r++)
	for (c = 0; c < cols; c++)
	    (marker_grid + r * cols)[c] = FALSE;

    DO_PATH((grid + r * cols)[c] = *(pos++), (grid + r * cols)[c] = *(--end_pos));
    free(marker_grid);
    return grid;
}
char *extract_from_grid(Grid grid, int rows, int cols, gridpath path) {
    char *pos, *end_pos, *text;
    int r, c, length = rows * cols, counter = 0, direction;
    Grid marker_grid;
    marker_grid = malloc(sizeof(char) * length);
    for (r = 0; r < rows; r++)
	for (c = 0; c < cols; c++)
	    (marker_grid + r * cols)[c] = FALSE;
    text = malloc(sizeof(char) * (length + 1));
    pos = text;
    end_pos = text + length;
    DO_PATH(*(pos++) = (grid + r * cols)[c], *(--end_pos) = (grid + r * cols)[c]);
    
    *(text + length) = '\0';
    free(marker_grid);
    return text;
}
void free_grid(Grid grid) {
    free(grid);
}

char *transform_with_grid(int rows, int cols, char *text, gridpath writepath, gridpath readpath) {
    Grid tempgrid;
    char *newtext, *gridful, *returned_gridful;
    int i, pos = 0, grid_length = cols * rows, length = strlen(text);
    newtext = malloc(sizeof(char) * (length + grid_length + 1));
    while (pos < length) {
	gridful = malloc(sizeof(char) * (grid_length + 1));
	for (i = 0; i < grid_length; i++) 
	    if (pos + i < length) 
		gridful[i] = text[pos + i];
	    else
		gridful[i] = NULL_CHARACTER;
	gridful[i] = '\0';
	tempgrid = make_new_grid(rows, cols, gridful, writepath);
	returned_gridful = extract_from_grid(tempgrid, rows, cols, readpath);
	for (i = 0; i < grid_length; i++) 
	    newtext[pos + i] = returned_gridful[i];
	free(tempgrid);
	free(gridful);
	free(returned_gridful);
	pos += grid_length;
    }
    newtext[pos] = '\0';
    return newtext;
}