-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmkov.c
More file actions
274 lines (223 loc) · 8.11 KB
/
mkov.c
File metadata and controls
274 lines (223 loc) · 8.11 KB
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
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/jiffies.h>
// Module Stuff
MODULE_LICENSE("Apache"); // Change this to "GPL" if you get annoyed about
// the kernal playing a crying fit about non GPL stuff
MODULE_DESCRIPTION("A Markov device driver.");
MODULE_AUTHOR("Ben Cartwright-Cox");
static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
// Adding callbacks into the gig
static struct file_operations fops =
{
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release = dev_rls,
};
static struct MKovEnt
{
int times;
char word[20];
int wordlen;
char lastword[20];
int lastwordlen;
};
static int rollingLimit = 0;
static struct MKovEnt words[1024] = {};
int init_module(void) {
int t = register_chrdev(89,"mkov",&fops);
if(t<0) {
printk(KERN_ALERT "MKOV MODULE COULD NOT INIT AAAAAAAAA");
} else {
printk(KERN_ALERT "mkov module init'd. Insanity loaded into kernel. Good job hero.");
}
return t;
}
void cleanup_module(void) {
unregister_chrdev(89,"mkov");
}
static int dev_open(struct inode *inod,struct file *fil) {
return 0; // Who actually cares?!
}
static char msg[20]={0};
static short readPos=0;
static char lastword[20]={0};
static int lastwordsize = 0;
static int wordsize = 0;
static char lastwordread[20]={0};
static ssize_t dev_read(struct file *foole,char *buff,size_t len,loff_t *off) {
int i = 0;
int j = 0;
int matches = 0;
int matchlist[1024]={0};
if(lastwordread[0] == 0x00) {
printk(KERN_ALERT "[Read] I've got nothing to read. Scanning for words in the table");
// Nothing to read?
// Lets pick the first thing in the mkov list.
for (j = 0; j < 1024; ++j) {
if(words[j].word[0] == 0x00) {
} else {
// Copy that into the lastwordread array.
printk(KERN_ALERT "[Read] I will use %s as my starting point.", words[j].word);
memcpy(lastwordread, words[j].word, 20);
break;
}
}
}
if(lastwordread[0] == 0x00) {
printk(KERN_ALERT "[Read] There is nothing to start from. Not going to give anything.");
// Okay so this means there has been literally nothing entered in yet.
// thus there is nothing to give to the user.
return 0;
}
// Right so at this point we can assume that we have somthing to base our prev knowlage off.
// So we are going to build a options table and then use get_jiffies_64() to pick one.
for (i = 0; i < 1024; ++i) {
// Now we are going to scan the words table to see how many
// and copy the matches into the table where we will pick the winner.
int ismatch = 1;
for (j = 0; j < 19; ++j) {
if (words[i].lastword[j] != lastwordread[j]) {
ismatch = 0;
}
}
if(ismatch) {
int isrepeat = 1;
for (j = 0; j < 19; ++j) {
if (words[i].word[j] != lastwordread[j]) {
isrepeat = 0;
}
}
if(isrepeat == 0) {
printk(KERN_ALERT "[Read] Adding %s -> %s as a candidate for the next word",lastwordread,words[i].word);
matchlist[matches] = i;
matches++;
} else {
printk(KERN_ALERT "[Read] wtf %s -> %s",words[i].word,lastwordread);
}
}
}
if(matches == 0) {
printk(KERN_ALERT "[Read] No matches to word found. abort.");
// So I am now going to copy a random work (provided there is one in there) to the
// lastwordread array. to spice things up a tad.
int pickR = get_jiffies_64();
memset(lastwordread,0x00,20);
int attempts = 0;
while(lastwordread[0] == 0x00) {
int pick = pickR % 1023;
printk(KERN_ALERT "[Read] Pick %d",pick);
if(words[pick].word[0] != 0x00) {
memset(lastwordread,0x00,20);
printk(KERN_ALERT "[Read] Why not use %s for the next word? Pick %d",words[pick].word,pick);
memcpy(lastwordread,words[pick].word,20);
}
pickR++;
attempts++;
if(attempts == 1024) {
printk(KERN_ALERT "[Read] There is literally nothing we can use");
break;
}
}
return 0;
}
int totalprobcount = 0;
for (i = 0; i < matches; ++i) {
totalprobcount += words[matchlist[i]].times;
}
int target = get_jiffies_64() % totalprobcount; // Good lord what have I done.
short count = 0;
for (i = 0; i < matches; ++i) {
target = target - words[matchlist[i]].times;
if(target < 0) {
// WE HAVE GOT IT LADIES AND GENTLEMEN.
memset(lastwordread, 0x00, 20);
memcpy(lastwordread, words[matchlist[i]].word, 20);
while (len && (words[matchlist[i]].word[readPos]!=0))
{
put_user(words[matchlist[i]].word[readPos],buff++); //copy byte from kernel space to user space
count++;
len--;
readPos++;
}
break;
}
}
if(count != 0) {
put_user(0x20,buff++); // " "
}
readPos = 0;
return count+1;
}
static ssize_t dev_write(struct file *foole,const char *buff,size_t len,loff_t *off) {
short bytesprocessed = 0;
int index;
for (index = 0; index < len; ++index) {
bytesprocessed++;
// what you want is in buff[index]
char letter = buff[index];
if(letter == 0x2E || letter == 0x20 || letter == 0x2C || letter == 0x0D || letter == 0x0A ) {
// Check how much is in the word buffer
if(wordsize == 0) {
// Then this is useless
continue;
} else {
int i; // C99 mode
int j; // C99 mode
// Then check if the word is in the system already
int foundit = 0;
for (i = 0; i < 1023; ++i) {
int correctwordmaybe = 1;
for (j = 0; j < 19; ++j) {
if (words[i].word[j] != msg[j]) {
correctwordmaybe = 0;
}
if (words[i].lastword[j] != lastword[j]) {
correctwordmaybe = 0;
}
}
if(correctwordmaybe) {
printk(KERN_ALERT "Found that word again... %s->%s",lastword, words[i].word);
// If it is then increment it.
words[i].times++;
foundit = 1;
break;
}
}
if(foundit == 0) {
rollingLimit++;
if(rollingLimit == 1024) {
rollingLimit = 0;
}
memcpy(words[rollingLimit].word, msg, 20);
words[rollingLimit].wordlen = wordsize;
memcpy(words[rollingLimit].lastword, lastword, 20);
words[rollingLimit].lastwordlen = lastwordsize;
printk(KERN_ALERT "Added a new word. %s->%s",lastword,msg);
words[rollingLimit].times = 1;
}
// If not add it
wordsize = 0;
}
// Then set the latest word var
memcpy(lastword, msg, 20);
lastwordsize = wordsize;
memset(msg,0x00,20);
} else {
if(wordsize != 20) {
msg[wordsize] = buff[index];
wordsize++;
}
}
}
return bytesprocessed;
}
static int dev_rls(struct inode *inod,struct file *fil) {
return 0;
}