xref: /openbsd/usr.sbin/crunchgen/mangle.c (revision 264ca280)
1 /* $OpenBSD: mangle.c,v 1.2 2009/12/08 21:10:24 drahn Exp $ */
2 /*
3  * Copyright (c) 2009 Dale Rahn.
4  * All rights reserved.
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include "mangle.h"
35 
36 /*
37  * supports randomizing up to 32 characters, if an identifier is longer than
38  * 32 characters, we only modify the first 32 characters of it.
39  *
40  * However not all of the 32 characters will really be randomized as
41  * 2^32 (uint32_t  gives a maximum string of cmpPQeaa[a]*
42  * Dont thing this will really matter....
43  *
44  * HOWEVER this does not mean we want to change the MAX_KEY_STR_LEN to 6
45  * because then all strings len >= 6 would use from the same index pool.
46  */
47 #define MAX_KEY_STR_LEN 32
48 uint32_t key[MAX_KEY_STR_LEN];
49 
50 char *filename = "mangledata";
51 
52 int key_fd = -1;
53 void
54 init_mangle_state()
55 {
56 	int failed = 0;
57 	ssize_t len;
58 	int i, ret;
59 
60 	/* open file if it exists, else init to zeros */
61 	struct stat sb;
62 
63 	ret = stat(filename, &sb);
64 
65 	if (ret != -1 && sb.st_size == sizeof(key)) {
66 		key_fd = open(filename, O_RDWR);
67 	}
68 
69 	if (ret == -1 || key_fd == -1)
70 		failed = 1;
71 
72 	if (failed == 0) {
73 		len = read(key_fd, key, sizeof(key));
74 		if (len != sizeof(key)) {
75 			failed = 1;
76 		}
77 	}
78 
79 	if (failed == 1) {
80 		if (key_fd != -1) {
81 			close(key_fd);
82 			key_fd = -1;
83 		}
84 		for (i = 0; i < MAX_KEY_STR_LEN; i++) {
85 			key[i] = 0;
86 		}
87 	}
88 }
89 
90 void
91 fini_mangle_state()
92 {
93 	int len;
94 	if (key_fd == -1) {
95 		/* open file for create/truncate */
96 		key_fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0666);
97 	} else {
98 		/* seek to the beginning */
99 		lseek(key_fd, 0, SEEK_SET);
100 	}
101 	if (key_fd != -1) {
102 		/* write file */
103 		len = write(key_fd, key, sizeof(key));
104 		if (len != sizeof(key)) {
105 			printf("writing file failed\n");
106 		}
107 		close(key_fd);
108 		key_fd = -1;
109 	}
110 }
111 
112 void
113 dump_mangle_state()
114 {
115 	int i;
116 
117 	for (i = 0; i < MAX_KEY_STR_LEN; i++) {
118 		printf("key %d %d\n", i, key[i]);
119 	}
120 }
121 
122 char validchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
123 #define NUM_KEYS (sizeof(validchars)-1)
124 
125 void
126 mangle_str(char *str)
127 {
128 	int i;
129 	int len;
130 	int keyval;
131 
132 	len = strlen(str);
133 	if (len == 0)
134 		return; /* nothing to mangle */
135 
136 	if (len > MAX_KEY_STR_LEN)
137 		len = MAX_KEY_STR_LEN;
138 
139 	keyval = key[len-1]++;
140 
141 	for (i = 0; i < len; i++) {
142 		int idx = keyval % NUM_KEYS;
143 		keyval = keyval / NUM_KEYS;
144 		str[i] = validchars[idx];
145 
146 	}
147 }
148 
149