1 b2cfc4e2 2003-09-30 devnull #include <lib9.h>
4 b2cfc4e2 2003-09-30 devnull encodefmt(Fmt *f)
6 b2cfc4e2 2003-09-30 devnull char *out;
7 e686c2b3 2004-12-27 devnull char *buf, *p;
11 b2cfc4e2 2003-09-30 devnull uchar *b;
12 cbeb0b26 2006-04-01 devnull char obuf[64]; /* rsc optimization */
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>");
18 b2cfc4e2 2003-09-30 devnull ilen = f->prec;
19 b2cfc4e2 2003-09-30 devnull f->prec = 0;
21 f92a2ceb 2005-05-19 devnull if(!(f->flags&FmtPrec) || ilen < 0)
22 f92a2ceb 2005-05-19 devnull goto error;
24 b2cfc4e2 2003-09-30 devnull f->flags &= ~FmtPrec;
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;
30 b2cfc4e2 2003-09-30 devnull case '[':
31 b2cfc4e2 2003-09-30 devnull len = (8*ilen+5)/6 + 4;
33 b2cfc4e2 2003-09-30 devnull case 'H':
34 b2cfc4e2 2003-09-30 devnull len = 2*ilen + 1;
37 b2cfc4e2 2003-09-30 devnull goto error;
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;
45 b2cfc4e2 2003-09-30 devnull buf = obuf;
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);
53 b2cfc4e2 2003-09-30 devnull case '[':
54 b2cfc4e2 2003-09-30 devnull rv = enc64(out, len, b, ilen);
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);
66 b2cfc4e2 2003-09-30 devnull if(rv < 0)
67 b2cfc4e2 2003-09-30 devnull goto error;
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;
75 b2cfc4e2 2003-09-30 devnull return fmtstrcpy(f, "<encodefmt>");