Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcodex/code-rot13.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Phong Vo <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
/*
25
* rot13 decoder/encoder
26
*/
27
28
#include <codex.h>
29
30
typedef struct State_s
31
{
32
unsigned char rot[256];
33
34
unsigned char* buf;
35
size_t bufsiz;
36
} State_t;
37
38
static const char rot[] = "abcdefghijklmnopqrstuvwxyz";
39
static const char ROT[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40
41
static int
42
rot13_open(Codex_t* p, char* const args[], Codexnum_t flags)
43
{
44
register State_t* state;
45
register int i;
46
47
if (!(state = newof(0, State_t, 1, 0)))
48
{
49
if (p->disc->errorf)
50
(*p->disc->errorf)(NiL, p->disc, 2, "out of space");
51
return -1;
52
}
53
for (i = 0; i < sizeof(state->rot); i++)
54
state->rot[i] = i;
55
for (i = 0; i < sizeof(rot); i++)
56
state->rot[rot[i]] = rot[(i + 13) % 26];
57
for (i = 0; i < sizeof(ROT); i++)
58
state->rot[ROT[i]] = ROT[(i + 13) % 26];
59
p->data = state;
60
return 0;
61
}
62
63
static int
64
rot13_close(Codex_t* p)
65
{
66
State_t* state = (State_t*)p->data;
67
68
if (!state)
69
return -1;
70
if (state->buf)
71
free(state->buf);
72
free(state);
73
return 0;
74
}
75
76
static ssize_t
77
rot13_read(Sfio_t* sp, void* buf, size_t n, Sfdisc_t* disc)
78
{
79
register State_t* state = (State_t*)CODEX(disc)->data;
80
register unsigned char* s;
81
register unsigned char* e;
82
ssize_t r;
83
84
if ((r = sfrd(sp, buf, n, disc)) > 0)
85
for (e = (s = (unsigned char*)buf) + r; s < e; s++)
86
*s = state->rot[*s];
87
return r;
88
}
89
90
static ssize_t
91
rot13_write(Sfio_t* sp, const void* buf, size_t n, Sfdisc_t* disc)
92
{
93
register State_t* state = (State_t*)CODEX(disc)->data;
94
register unsigned char* s;
95
register unsigned char* e;
96
register unsigned char* b;
97
98
if (n > state->bufsiz)
99
{
100
state->bufsiz = roundof(n, 1024);
101
if (!(state->buf = newof(state->buf, unsigned char, state->bufsiz, 0)))
102
{
103
if (CODEX(disc)->disc->errorf)
104
(*CODEX(disc)->disc->errorf)(NiL, CODEX(disc)->disc, 2, "out of space");
105
return -1;
106
}
107
}
108
for (b = state->buf, e = (s = (unsigned char*)buf) + n; s < e; s++)
109
*b++ = state->rot[*s];
110
return sfwr(sp, state->buf, n, disc);
111
}
112
113
Codexmeth_t codex_rot13 =
114
{
115
"rot13",
116
"rot13 self-inverting encoding.",
117
"[+(version)?codex-rot13 (AT&T Research) 2003-12-11]"
118
"[+(author)?Glenn Fowler <[email protected]>]",
119
CODEX_DECODE|CODEX_ENCODE|CODEX_UU,
120
0,
121
0,
122
rot13_open,
123
rot13_close,
124
0,
125
0,
126
rot13_read,
127
rot13_write,
128
0,
129
0,
130
0,
131
0,
132
0,
133
CODEXNEXT(rot13)
134
};
135
136
CODEXLIB(rot13)
137
138