]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cifs/pack.c
cmd: remove a bit of unused stuff
[plan9front.git] / sys / src / cmd / cifs / pack.c
1 /* packet packing and unpacking */
2 #include <u.h>
3 #include <libc.h>
4 #include <ctype.h>
5 #include "cifs.h"
6
7 void *
8 pmem(Pkt *p, void *v, int len)
9 {
10         uchar *str = v;
11         void *s = p->pos;
12
13         if(!len || !v)
14                 return s;
15         while(len--)
16                 *p->pos++ = *str++;
17         return s;
18 }
19
20 void *
21 ppath(Pkt *p, char *str)
22 {
23         char c;
24         Rune r;
25         void *s = p->pos;
26
27         if(!str)
28                 return s;
29
30         if(p->s->flags2 & FL2_UNICODE){
31                 if(((p->pos - p->buf) % 2) != 0)        /* pad to even offset */
32                         p8(p, 0);
33                 while(*str){
34                         str += chartorune(&r, str);
35                         if(r == L'/')
36                                 r = L'\\';
37                         pl16(p, r);
38                 }
39                 pl16(p, 0);
40         } else {
41                 while((c = *str++) != 0){
42                         if(c == '/')
43                                 c = '\\';
44                         *p->pos++ = c;
45                 }
46                 *p->pos++ = 0;
47         }
48         return s;
49 }
50
51 void *
52 pstr(Pkt *p, char *str)
53 {
54         void *s = p->pos;
55         Rune r;
56
57         if(!str)
58                 return s;
59
60         if(p->s->flags2 & FL2_UNICODE){
61                 if(((p->pos - p->buf) % 2) != 0)
62                         p8(p, 0);               /* pad to even offset */
63                 while(*str){
64                         str += chartorune(&r, str);
65                         pl16(p, r);
66                 }
67                 pl16(p, 0);
68         } else {
69                 while(*str)
70                         *p->pos++ = *str++;
71                 *p->pos++ = 0;
72         }
73         return s;
74 }
75
76 void *
77 pascii(Pkt *p, char *str)
78 {
79         void *s = p->pos;
80
81         while(*str)
82                 *p->pos++ = *str++;
83         *p->pos++ = 0;
84         return s;
85 }
86
87
88 void *
89 pl64(Pkt *p, uvlong n)
90 {
91         void *s = p->pos;
92
93         *p->pos++ = n;
94         *p->pos++ = n >> 8;
95         *p->pos++ = n >> 16;
96         *p->pos++ = n >> 24;
97         *p->pos++ = n >> 32;
98         *p->pos++ = n >> 40;
99         *p->pos++ = n >> 48;
100         *p->pos++ = n >> 56;
101         return s;
102 }
103
104 void *
105 pb32(Pkt *p, uint n)
106 {
107         void *s = p->pos;
108
109         *p->pos++ = n >> 24;
110         *p->pos++ = n >> 16;
111         *p->pos++ = n >> 8;
112         *p->pos++ = n;
113         return s;
114 }
115
116 void *
117 pl32(Pkt *p, uint n)
118 {
119         void *s = p->pos;
120
121         *p->pos++ = n;
122         *p->pos++ = n >> 8;
123         *p->pos++ = n >> 16;
124         *p->pos++ = n >> 24;
125         return s;
126 }
127
128 void *
129 pb16(Pkt *p, uint n)
130 {
131         void *s = p->pos;
132
133         *p->pos++ = n >> 8;
134         *p->pos++ = n;
135         return s;
136 }
137
138 void *
139 pl16(Pkt *p, uint n)
140 {
141         void *s = p->pos;
142
143         *p->pos++ = n;
144         *p->pos++ = n >> 8;
145         return s;
146 }
147
148 void *
149 p8(Pkt *p, uint n)
150 {
151         void *s = p->pos;
152
153         *p->pos++ = n;
154         return s;
155 }
156
157 /*
158  * Encode a Netbios name
159  */
160 void *
161 pname(Pkt *p, char *name, char pad)
162 {
163         int i, done = 0;
164         char c;
165         void *s = p->pos;
166
167         *p->pos++ = ' ';
168         for(i = 0; i < 16; i++) {
169                 c = pad;
170                 if(!done && name[i] == '\0')
171                         done = 1;
172                 if(!done)
173                         c = islower(name[i])? toupper(name[i]): name[i];
174                 *p->pos++ = ((uchar)c >> 4) + 'A';
175                 *p->pos++ = (c & 0xf) + 'A';
176         }
177         *p->pos++ = '\0';
178         return s;
179 }
180
181 void *
182 pvtime(Pkt *p, uvlong n)
183 {
184         void *s = p->pos;
185
186         n += 11644473600LL;
187         n *= 10000000LL;
188
189         pl32(p, n);
190         pl32(p, n >> 32);
191         return s;
192 }
193
194 void *
195 pdatetime(Pkt *p, long utc)
196 {
197         void *s = p->pos;
198         Tm *tm = localtime(utc);
199         int t = tm->hour << 11 | tm->min << 5 | (tm->sec / 2);
200         int d = (tm->year - 80) << 9 | (tm->mon + 1) << 5 | tm->mday;
201
202         /*
203          * bug in word swapping in Win95 requires this
204          */
205         if(p->s->caps & CAP_NT_SMBS){
206                 pl16(p, d);
207                 pl16(p, t);
208         } else{
209                 pl16(p, t);
210                 pl16(p, d);
211         }
212         return s;
213 }
214
215
216 void
217 gmem(Pkt *p, void *v, int n)
218 {
219         uchar *str = v;
220
221         if(!n || !v)
222                 return;
223         while(n-- && p->pos < p->eop)
224                 *str++ = *p->pos++;
225 }
226
227 /*
228  * note len is the length of the source string in
229  * in runes or bytes, in ASCII mode this is also the size
230  * of the output buffer but this is not so in Unicode mode!
231  */
232 void
233 gstr(Pkt *p, char *str, int n)
234 {
235         int i;
236         Rune r;
237
238         if(!n || !str)
239                 return;
240
241         if(p->flags2 & FL2_UNICODE){
242                 if(((p->pos - p->buf) % 2) != 0)
243                         g8(p);          /* strip padding to even offset */
244
245                 i = 0;
246                 while(*p->pos && n && p->pos < p->eop){
247                         r = gl16(p);
248                         i += runetochar(str +i, &r);
249                         n -= 2;
250                 }
251                 *(str + i) = 0;
252
253                 while(*p->pos && p->pos < p->eop)
254                         gl16(p);
255                 /*
256                  * some versions of windows terminate a rune string
257                  * with a single nul so we do a dangerous hack...
258                  */
259                 if(p->pos[1])
260                         g8(p);
261                 else
262                         gl16(p);
263         } else {
264                 while(*p->pos && n-- && p->pos < p->eop)
265                         *str++ = *p->pos++;
266                 *str = 0;
267                 while(*p->pos++ && p->pos < p->eop)
268                         continue;
269         }
270 }
271
272 void
273 gascii(Pkt *p, char *str, int n)
274 {
275         if(!n || !str)
276                 return;
277
278         while(*p->pos && n-- && p->pos < p->eop)
279                 *str++ = *p->pos++;
280         *str = 0;
281         while(*p->pos++ && p->pos < p->eop)
282                 continue;
283 }
284
285
286 uvlong
287 gl64(Pkt *p)
288 {
289         uvlong n;
290
291         if(p->pos + 8 > p->eop)
292                 return 0;
293
294         n  = (uvlong)*p->pos++;
295         n |= (uvlong)*p->pos++ << 8;
296         n |= (uvlong)*p->pos++ << 16;
297         n |= (uvlong)*p->pos++ << 24;
298         n |= (uvlong)*p->pos++ << 32;
299         n |= (uvlong)*p->pos++ << 40;
300         n |= (uvlong)*p->pos++ << 48;
301         n |= (uvlong)*p->pos++ << 56;
302         return n;
303 }
304
305 uvlong
306 gb48(Pkt *p)
307 {
308         uvlong n;
309
310         if(p->pos + 6 > p->eop)
311                 return 0;
312
313         n  = (uvlong)*p->pos++ << 40;
314         n |= (uvlong)*p->pos++ << 24;
315         n |= (uvlong)*p->pos++ << 32;
316         n |= (uvlong)*p->pos++ << 16;
317         n |= (uvlong)*p->pos++ << 8;
318         n |= (uvlong)*p->pos++;
319         return n;
320 }
321
322 uint
323 gb32(Pkt *p)
324 {
325         uint n;
326
327         if(p->pos + 4 > p->eop)
328                 return 0;
329
330         n  = (uint)*p->pos++ << 24;
331         n |= (uint)*p->pos++ << 16;
332         n |= (uint)*p->pos++ << 8;
333         n |= (uint)*p->pos++;
334         return n;
335 }
336
337 uint
338 gl32(Pkt *p)
339 {
340         uint n;
341
342         if(p->pos + 4 > p->eop)
343                 return 0;
344
345         n  = (uint)*p->pos++;
346         n |= (uint)*p->pos++ << 8;
347         n |= (uint)*p->pos++ << 16;
348         n |= (uint)*p->pos++ << 24;
349         return n;
350 }
351
352 uint
353 gb16(Pkt *p)
354 {
355         uint n;
356
357         if(p->pos + 2 > p->eop)
358                 return 0;
359         n  = (uint)*p->pos++ << 8;
360         n |= (uint)*p->pos++;
361         return n;
362 }
363
364 uint
365 gl16(Pkt *p)
366 {
367         uint n;
368
369         if(p->pos + 2 > p->eop)
370                 return 0;
371         n  = (uint)*p->pos++;
372         n |= (uint)*p->pos++ << 8;
373         return n;
374 }
375
376 uint
377 g8(Pkt *p)
378 {
379         if(p->pos + 1 > p->eop)
380                 return 0;
381         return (uint)*p->pos++;
382 }
383
384 long
385 gdatetime(Pkt *p)
386 {
387         Tm tm;
388         uint d, t;
389
390         if(p->pos + 4 > p->eop)
391                 return 0;
392
393         /*
394          * bug in word swapping in Win95 requires this
395          */
396         if(p->s->caps & CAP_NT_SMBS){
397                 d = gl16(p);
398                 t = gl16(p);
399         }else{
400                 t = gl16(p);
401                 d = gl16(p);
402         }
403
404         memset(&tm, 0, sizeof(tm));
405         tm.year = 80 + (d >> 9);
406         tm.mon = ((d >> 5) & 017) - 1;
407         tm.mday = d & 037;
408
409         tm.hour = t >> 11;
410         tm.min = (t >> 5) & 63;
411         tm.sec = (t & 31) << 1;
412         strcpy(tm.zone, "GMT");
413
414         return tm2sec(&tm) + p->s->tz;
415 }
416
417 long
418 gvtime(Pkt *p)
419 {
420         uvlong vl;
421
422         if(p->pos + 8 > p->eop)
423                 return 0;
424
425         vl  = (uvlong)gl32(p);
426         vl |= (uvlong)gl32(p) << 32;
427
428         vl /= 10000000LL;
429         vl -= 11644473600LL;
430         return vl;
431 }
432
433 void
434 gconv(Pkt *p, int conv, char *str, int n)
435 {
436         int off;
437         uchar *pos;
438
439         off = gl32(p) & 0xffff;
440         if(off == 0 || p->tdata - conv + off > p->eop){
441                 memset(str, 0, n);
442                 return;
443         }
444
445         pos = p->pos;
446         p->pos = p->tdata - conv + off;
447         gascii(p, str, n);
448         p->pos = pos;
449 }
450
451 void
452 goff(Pkt *p, uchar *base, char *str, int n)
453 {
454         int off;
455         uchar *pos;
456
457         off = gl16(p);
458         if(off == 0 || base + off > p->eop){
459                 memset(str, 0, n);
460                 return;
461         }
462         pos = p->pos;
463         p->pos = base + off;
464         gstr(p, str, n);
465         p->pos = pos;
466 }