-rw-r--r-- | NEW-ENVIRON.cpp | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/NEW-ENVIRON.cpp b/NEW-ENVIRON.cpp new file mode 100644 index 0000000..370ff73 --- a/dev/null +++ b/NEW-ENVIRON.cpp | |||
@@ -0,0 +1,272 @@ | |||
1 | |||
2 | enum_NESB { | ||
3 | nesbSend = 1, | ||
4 | nesbIs = 0, | ||
5 | nesbInfo = 2, | ||
6 | |||
7 | nesbVar = 0, | ||
8 | nesbValue = 1, | ||
9 | nesbESC = 2, | ||
10 | nesbUserVar = 3, | ||
11 | |||
12 | nesbNothing = 256 | ||
13 | }; | ||
14 | |||
15 | struct _envariable{ | ||
16 | BYTEtype; | ||
17 | char*name; | ||
18 | char*value; | ||
19 | _envariable() : type(0), name(NULL), value(NULL) {} | ||
20 | ~_envariable() { ItIsNot(); } | ||
21 | void ItIsNot() { | ||
22 | if(name) delete name; | ||
23 | if(value) delete value; | ||
24 | } | ||
25 | BOOL ItIs(BYTE t,LPCTSTR n,LPCTSTR v) { | ||
26 | ItIsNot(); | ||
27 | name = new CHAR[strlen(n)+1]; strcpy(name,n); | ||
28 | value = new CHAR[strlen(v)+1]; strcpy(value,v); | ||
29 | type=t; | ||
30 | return TRUE; | ||
31 | } | ||
32 | intTransmitSize() { | ||
33 | char* v = value; | ||
34 | for(int rv=0;*v;v++,rv++){ | ||
35 | switch(*v){ | ||
36 | case nesbVar: | ||
37 | case nesbValue: | ||
38 | case nesbUserVar: | ||
39 | case nesbESC: | ||
40 | rv++; | ||
41 | break; | ||
42 | } | ||
43 | } | ||
44 | for(v=name;*v;v++,rv++){ | ||
45 | switch(*v){ | ||
46 | case nesbVar: | ||
47 | case nesbValue: | ||
48 | case nesbUserVar: | ||
49 | case nesbESC: | ||
50 | rv++; | ||
51 | break; | ||
52 | } | ||
53 | } | ||
54 | return rv+2;// VAR/VAL | ||
55 | } | ||
56 | int Paste(CHAR* data) { | ||
57 | int rv=0; | ||
58 | ASSERT(type==nesbVar || type==nesbUserVar); | ||
59 | data[rv++]=type; | ||
60 | for(CHAR* v=name;*v;v++){ | ||
61 | switch(*v){ | ||
62 | case nesbVar: | ||
63 | case nesbValue: | ||
64 | case nesbUserVar: | ||
65 | case nesbESC: | ||
66 | data[rv++]=nesbESC; | ||
67 | default: | ||
68 | data[rv++]=*v; | ||
69 | } | ||
70 | } | ||
71 | data[rv++]=nesbValue; | ||
72 | for(v=value;*v;v++){ | ||
73 | switch(*v){ | ||
74 | case nesbVar: | ||
75 | case nesbValue: | ||
76 | case nesbUserVar: | ||
77 | case nesbESC: | ||
78 | data[rv++]=nesbESC; | ||
79 | default: | ||
80 | data[rv++]=*v; | ||
81 | } | ||
82 | } | ||
83 | return rv; | ||
84 | } | ||
85 | BOOL IsIt(CHAR* n,int l) { | ||
86 | BOOL bESC = FALSE; | ||
87 | CHAR* nPtr = name; | ||
88 | for(int tmp=0;tmp<l;tmp++){ | ||
89 | if(!*nPtr) | ||
90 | return FALSE; | ||
91 | if(bESC) | ||
92 | bESC=FALSE; | ||
93 | else if(n[tmp]==nesbESC) | ||
94 | bESC=TRUE; | ||
95 | else if(n[tmp]!=*(nPtr++)) | ||
96 | return FALSE; | ||
97 | } | ||
98 | return (*nPtr)?FALSE:TRUE; | ||
99 | } | ||
100 | }*Envars = NULL; | ||
101 | intnEnvars = 0; | ||
102 | int nEnvarsAllocated = 0; | ||
103 | |||
104 | _envariable* FindEnvar(BYTE type,CHAR* n,int l) | ||
105 | { | ||
106 | ASSERT(Envars && nEnvars); | ||
107 | for(int tmp=0;tmp<nEnvars;tmp++){ | ||
108 | if(Envars[tmp].type!=type) | ||
109 | continue; | ||
110 | if(Envars[tmp].IsIt(n,l)) | ||
111 | return &Envars[tmp]; | ||
112 | } | ||
113 | return NULL; | ||
114 | } | ||
115 | |||
116 | #define ENVGROW8 | ||
117 | |||
118 | BOOL AddEnvar(BYTE type,LPTSTR name,LPTSTR value) | ||
119 | { | ||
120 | if(Envars && nEnvars){ | ||
121 | if(FindEnvar(type,name,strlen(name)))// ** Maybe replace | ||
122 | return FALSE; | ||
123 | } | ||
124 | if(nEnvars>=nEnvarsAllocated){ | ||
125 | ASSERT(nEnvars==nEnvarsAllocated); | ||
126 | _envariable* ne = new _envariable[nEnvarsAllocated+ENVGROW]; | ||
127 | ASSERT(ne); | ||
128 | if(nEnvarsAllocated){ | ||
129 | memmove(ne,Envars,sizeof(_envariable)*nEnvarsAllocated); | ||
130 | delete Envars; | ||
131 | } | ||
132 | Envars=ne; | ||
133 | nEnvarsAllocated+=ENVGROW; | ||
134 | } | ||
135 | return Envars[nEnvars++].ItIs(type,name,value); | ||
136 | } | ||
137 | BOOL CleanEnvars() | ||
138 | { | ||
139 | if(Envars){ | ||
140 | ASSERT(nEnvarsAllocated); | ||
141 | delete[] Envars; | ||
142 | } | ||
143 | return TRUE; | ||
144 | } | ||
145 | |||
146 | BOOL newenvironOnDont() | ||
147 | { | ||
148 | TRACE0("DONT NEW-ENVIRON\n"); | ||
149 | return TRUE; | ||
150 | } | ||
151 | BOOL newenvironOnDo() | ||
152 | { | ||
153 | TRACE0("DO NEW-ENVIRON\n"); | ||
154 | return (Envars && nEnvars)?TRUE:FALSE; | ||
155 | } | ||
156 | BOOL newenvironOnSB(LPBYTE data,UINT size) | ||
157 | { | ||
158 | if(!size) | ||
159 | return FALSE; | ||
160 | if(data[0]!=nesbSend) | ||
161 | return FALSE; | ||
162 | if(!(Envars && nEnvars)){ | ||
163 | BYTE no = nesbIs; | ||
164 | return SubNegotiate(toNewEnviron,&no,1); | ||
165 | } | ||
166 | UINT totalSize = 1;// IS byte | ||
167 | if(size>1 ){ | ||
168 | int type = 0; | ||
169 | int name = 0; | ||
170 | BOOL bESC = FALSE; | ||
171 | for(UINT tmp=1;tmp<=size;tmp++){ | ||
172 | switch((tmp==size)?nesbNothing:(UINT)data[tmp]){ | ||
173 | case nesbESC: | ||
174 | bESC=TRUE; | ||
175 | break; | ||
176 | case nesbVar: | ||
177 | case nesbUserVar: | ||
178 | case nesbNothing: | ||
179 | if(!bESC){ | ||
180 | if(type){ | ||
181 | if(name){ | ||
182 | _envariable* ev = FindEnvar(data[type],(CHAR*)&data[name],tmp-name); | ||
183 | if(ev){ | ||
184 | totalSize+=ev->TransmitSize(); | ||
185 | } | ||
186 | }else{ | ||
187 | for(int tmp=0;tmp<nEnvars;tmp++){ | ||
188 | if(Envars[tmp].type==data[type]){ | ||
189 | totalSize+=Envars[tmp].TransmitSize(); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | type=tmp; | ||
194 | name=0; | ||
195 | }else{ | ||
196 | type=tmp; | ||
197 | ASSERT(!name); | ||
198 | } | ||
199 | break; | ||
200 | } | ||
201 | // Fall through if ESCaped | ||
202 | default: | ||
203 | bESC=FALSE; | ||
204 | if(!name) | ||
205 | name=tmp; | ||
206 | break; | ||
207 | } | ||
208 | } | ||
209 | TRACE1("Total Size: %u\n",totalSize); | ||
210 | CHAR* d = new CHAR[totalSize]; | ||
211 | UINT ptr = 0; | ||
212 | d[ptr++]=nesbIs; | ||
213 | for(tmp=1;tmp<=size;tmp++){ | ||
214 | switch((tmp==size)?nesbNothing:(UINT)data[tmp]){ | ||
215 | case nesbESC: | ||
216 | bESC=TRUE; | ||
217 | break; | ||
218 | case nesbVar: | ||
219 | case nesbUserVar: | ||
220 | case nesbNothing: | ||
221 | if(!bESC){ | ||
222 | if(type){ | ||
223 | if(name){ | ||
224 | _envariable* ev = FindEnvar(data[type],(CHAR*)&data[name],tmp-name); | ||
225 | if(ev){ | ||
226 | ptr+=ev->Paste(&d[ptr]); | ||
227 | } | ||
228 | }else{ | ||
229 | for(int tmp=0;tmp<nEnvars;tmp++){ | ||
230 | if(Envars[tmp].type==data[type]){ | ||
231 | ptr+=Envars[tmp].Paste(&d[ptr]); | ||
232 | } | ||
233 | } | ||
234 | } | ||
235 | ASSERT(ptr<=totalSize); | ||
236 | type=tmp; | ||
237 | name=0; | ||
238 | }else{ | ||
239 | type=tmp; | ||
240 | ASSERT(!name); | ||
241 | } | ||
242 | break; | ||
243 | } | ||
244 | // Fall through if ESCaped | ||
245 | default: | ||
246 | bESC=FALSE; | ||
247 | if(!name) | ||
248 | name=tmp; | ||
249 | break; | ||
250 | } | ||
251 | } | ||
252 | ASSERT(ptr==totalSize); | ||
253 | VERIFY(SubNegotiate(toNewEnviron,(BYTE*)d,totalSize)); | ||
254 | delete d; | ||
255 | }else{ | ||
256 | for(int tmp=0;tmp<nEnvars;tmp++){ | ||
257 | totalSize+=Envars[tmp].TransmitSize(); | ||
258 | } | ||
259 | TRACE1("Total Size: %u\n",totalSize); | ||
260 | CHAR* d = new CHAR[totalSize]; | ||
261 | UINT ptr = 0; | ||
262 | d[ptr++]=nesbIs; | ||
263 | for(tmp=0;tmp<nEnvars;tmp++){ | ||
264 | ptr+=Envars[tmp].Paste(&d[ptr]); | ||
265 | ASSERT(ptr<=totalSize); | ||
266 | } | ||
267 | ASSERT(ptr==totalSize); | ||
268 | VERIFY(SubNegotiate(toNewEnviron,(BYTE*)d,totalSize)); | ||
269 | delete d; | ||
270 | } | ||
271 | return TRUE; | ||
272 | } \ No newline at end of file | ||