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 b2cfc4e2 2003-09-30 devnull char obuf[64]; // rsc optimization
14 a3add39b 2004-12-26 devnull if(!(f->flags&FmtPrec) || f->prec < 1)
15 b2cfc4e2 2003-09-30 devnull goto error;
17 b2cfc4e2 2003-09-30 devnull b = va_arg(f->args, uchar*);
18 a3add39b 2004-12-26 devnull if(b == 0)
19 a3add39b 2004-12-26 devnull return fmtstrcpy(f, "<nil>");
21 b2cfc4e2 2003-09-30 devnull ilen = f->prec;
22 b2cfc4e2 2003-09-30 devnull f->prec = 0;
23 b2cfc4e2 2003-09-30 devnull f->flags &= ~FmtPrec;
24 b2cfc4e2 2003-09-30 devnull switch(f->r){
25 b2cfc4e2 2003-09-30 devnull case '<':
26 b2cfc4e2 2003-09-30 devnull len = (8*ilen+4)/5 + 3;
28 b2cfc4e2 2003-09-30 devnull case '[':
29 b2cfc4e2 2003-09-30 devnull len = (8*ilen+5)/6 + 4;
31 b2cfc4e2 2003-09-30 devnull case 'H':
32 b2cfc4e2 2003-09-30 devnull len = 2*ilen + 1;
35 b2cfc4e2 2003-09-30 devnull goto error;
38 b2cfc4e2 2003-09-30 devnull if(len > sizeof(obuf)){
39 b2cfc4e2 2003-09-30 devnull buf = malloc(len);
40 b2cfc4e2 2003-09-30 devnull if(buf == nil)
41 b2cfc4e2 2003-09-30 devnull goto error;
43 b2cfc4e2 2003-09-30 devnull buf = obuf;
45 b2cfc4e2 2003-09-30 devnull // convert
46 b2cfc4e2 2003-09-30 devnull out = buf;
47 b2cfc4e2 2003-09-30 devnull switch(f->r){
48 b2cfc4e2 2003-09-30 devnull case '<':
49 b2cfc4e2 2003-09-30 devnull rv = enc32(out, len, b, ilen);
51 b2cfc4e2 2003-09-30 devnull case '[':
52 b2cfc4e2 2003-09-30 devnull rv = enc64(out, len, b, ilen);
54 b2cfc4e2 2003-09-30 devnull case 'H':
55 b2cfc4e2 2003-09-30 devnull rv = enc16(out, len, b, ilen);
56 a3add39b 2004-12-26 devnull if(rv >= 0 && (f->flags & FmtLong))
57 a3add39b 2004-12-26 devnull for(p = buf; *p; p++)
58 a3add39b 2004-12-26 devnull *p = tolower(*p);
64 b2cfc4e2 2003-09-30 devnull if(rv < 0)
65 b2cfc4e2 2003-09-30 devnull goto error;
67 b2cfc4e2 2003-09-30 devnull fmtstrcpy(f, buf);
68 b2cfc4e2 2003-09-30 devnull if(buf != obuf)
69 b2cfc4e2 2003-09-30 devnull free(buf);
70 b2cfc4e2 2003-09-30 devnull return 0;
73 b2cfc4e2 2003-09-30 devnull return fmtstrcpy(f, "<encodefmt>");