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 b2cfc4e2 2003-09-30 devnull char *buf;
11 b2cfc4e2 2003-09-30 devnull uchar *b;
12 b2cfc4e2 2003-09-30 devnull char obuf[64]; // rsc optimization
14 a6ea03ae 2003-10-11 devnull if(!(f->flags&FmtPrec) || f->prec < 0)
15 b2cfc4e2 2003-09-30 devnull goto error;
17 b2cfc4e2 2003-09-30 devnull b = va_arg(f->args, uchar*);
19 b2cfc4e2 2003-09-30 devnull ilen = f->prec;
20 b2cfc4e2 2003-09-30 devnull f->prec = 0;
21 b2cfc4e2 2003-09-30 devnull f->flags &= ~FmtPrec;
22 b2cfc4e2 2003-09-30 devnull switch(f->r){
23 b2cfc4e2 2003-09-30 devnull case '<':
24 b2cfc4e2 2003-09-30 devnull len = (8*ilen+4)/5 + 3;
26 b2cfc4e2 2003-09-30 devnull case '[':
27 b2cfc4e2 2003-09-30 devnull len = (8*ilen+5)/6 + 4;
29 b2cfc4e2 2003-09-30 devnull case 'H':
30 b2cfc4e2 2003-09-30 devnull len = 2*ilen + 1;
33 b2cfc4e2 2003-09-30 devnull goto error;
36 b2cfc4e2 2003-09-30 devnull if(len > sizeof(obuf)){
37 b2cfc4e2 2003-09-30 devnull buf = malloc(len);
38 b2cfc4e2 2003-09-30 devnull if(buf == nil)
39 b2cfc4e2 2003-09-30 devnull goto error;
41 b2cfc4e2 2003-09-30 devnull buf = obuf;
43 b2cfc4e2 2003-09-30 devnull // convert
44 b2cfc4e2 2003-09-30 devnull out = buf;
45 b2cfc4e2 2003-09-30 devnull switch(f->r){
46 b2cfc4e2 2003-09-30 devnull case '<':
47 b2cfc4e2 2003-09-30 devnull rv = enc32(out, len, b, ilen);
49 b2cfc4e2 2003-09-30 devnull case '[':
50 b2cfc4e2 2003-09-30 devnull rv = enc64(out, len, b, ilen);
52 b2cfc4e2 2003-09-30 devnull case 'H':
53 b2cfc4e2 2003-09-30 devnull rv = enc16(out, len, b, ilen);
59 b2cfc4e2 2003-09-30 devnull if(rv < 0)
60 b2cfc4e2 2003-09-30 devnull goto error;
62 b2cfc4e2 2003-09-30 devnull fmtstrcpy(f, buf);
63 b2cfc4e2 2003-09-30 devnull if(buf != obuf)
64 b2cfc4e2 2003-09-30 devnull free(buf);
65 b2cfc4e2 2003-09-30 devnull return 0;
68 b2cfc4e2 2003-09-30 devnull return fmtstrcpy(f, "<encodefmt>");