Blame


1 b2cfc4e2 2003-09-30 devnull #include <lib9.h>
2 b2cfc4e2 2003-09-30 devnull
3 b2cfc4e2 2003-09-30 devnull int
4 b2cfc4e2 2003-09-30 devnull encodefmt(Fmt *f)
5 b2cfc4e2 2003-09-30 devnull {
6 b2cfc4e2 2003-09-30 devnull char *out;
7 e686c2b3 2004-12-27 devnull char *buf, *p;
8 b2cfc4e2 2003-09-30 devnull int len;
9 b2cfc4e2 2003-09-30 devnull int ilen;
10 b2cfc4e2 2003-09-30 devnull int rv;
11 b2cfc4e2 2003-09-30 devnull uchar *b;
12 cbeb0b26 2006-04-01 devnull char obuf[64]; /* rsc optimization */
13 b2cfc4e2 2003-09-30 devnull
14 b2cfc4e2 2003-09-30 devnull b = va_arg(f->args, uchar*);
15 a3add39b 2004-12-26 devnull if(b == 0)
16 a3add39b 2004-12-26 devnull return fmtstrcpy(f, "<nil>");
17 b2cfc4e2 2003-09-30 devnull
18 b2cfc4e2 2003-09-30 devnull ilen = f->prec;
19 b2cfc4e2 2003-09-30 devnull f->prec = 0;
20 f92a2ceb 2005-05-19 devnull
21 f92a2ceb 2005-05-19 devnull if(!(f->flags&FmtPrec) || ilen < 0)
22 f92a2ceb 2005-05-19 devnull goto error;
23 f92a2ceb 2005-05-19 devnull
24 b2cfc4e2 2003-09-30 devnull f->flags &= ~FmtPrec;
25 f92a2ceb 2005-05-19 devnull
26 b2cfc4e2 2003-09-30 devnull switch(f->r){
27 b2cfc4e2 2003-09-30 devnull case '<':
28 b2cfc4e2 2003-09-30 devnull len = (8*ilen+4)/5 + 3;
29 b2cfc4e2 2003-09-30 devnull break;
30 b2cfc4e2 2003-09-30 devnull case '[':
31 b2cfc4e2 2003-09-30 devnull len = (8*ilen+5)/6 + 4;
32 b2cfc4e2 2003-09-30 devnull break;
33 b2cfc4e2 2003-09-30 devnull case 'H':
34 b2cfc4e2 2003-09-30 devnull len = 2*ilen + 1;
35 b2cfc4e2 2003-09-30 devnull break;
36 b2cfc4e2 2003-09-30 devnull default:
37 b2cfc4e2 2003-09-30 devnull goto error;
38 b2cfc4e2 2003-09-30 devnull }
39 b2cfc4e2 2003-09-30 devnull
40 b2cfc4e2 2003-09-30 devnull if(len > sizeof(obuf)){
41 b2cfc4e2 2003-09-30 devnull buf = malloc(len);
42 b2cfc4e2 2003-09-30 devnull if(buf == nil)
43 b2cfc4e2 2003-09-30 devnull goto error;
44 b2cfc4e2 2003-09-30 devnull } else
45 b2cfc4e2 2003-09-30 devnull buf = obuf;
46 b2cfc4e2 2003-09-30 devnull
47 cbeb0b26 2006-04-01 devnull /* convert */
48 b2cfc4e2 2003-09-30 devnull out = buf;
49 b2cfc4e2 2003-09-30 devnull switch(f->r){
50 b2cfc4e2 2003-09-30 devnull case '<':
51 b2cfc4e2 2003-09-30 devnull rv = enc32(out, len, b, ilen);
52 b2cfc4e2 2003-09-30 devnull break;
53 b2cfc4e2 2003-09-30 devnull case '[':
54 b2cfc4e2 2003-09-30 devnull rv = enc64(out, len, b, ilen);
55 b2cfc4e2 2003-09-30 devnull break;
56 b2cfc4e2 2003-09-30 devnull case 'H':
57 b2cfc4e2 2003-09-30 devnull rv = enc16(out, len, b, ilen);
58 a3add39b 2004-12-26 devnull if(rv >= 0 && (f->flags & FmtLong))
59 a3add39b 2004-12-26 devnull for(p = buf; *p; p++)
60 1c171e3a 2005-07-19 devnull *p = tolower((uchar)*p);
61 b2cfc4e2 2003-09-30 devnull break;
62 b2cfc4e2 2003-09-30 devnull default:
63 b2cfc4e2 2003-09-30 devnull rv = -1;
64 b2cfc4e2 2003-09-30 devnull break;
65 b2cfc4e2 2003-09-30 devnull }
66 b2cfc4e2 2003-09-30 devnull if(rv < 0)
67 b2cfc4e2 2003-09-30 devnull goto error;
68 b2cfc4e2 2003-09-30 devnull
69 b2cfc4e2 2003-09-30 devnull fmtstrcpy(f, buf);
70 b2cfc4e2 2003-09-30 devnull if(buf != obuf)
71 b2cfc4e2 2003-09-30 devnull free(buf);
72 b2cfc4e2 2003-09-30 devnull return 0;
73 b2cfc4e2 2003-09-30 devnull
74 b2cfc4e2 2003-09-30 devnull error:
75 b2cfc4e2 2003-09-30 devnull return fmtstrcpy(f, "<encodefmt>");
76 b2cfc4e2 2003-09-30 devnull }