On 2015-04-14 18:24, Luka Perkov wrote: > Hi, > > On Tue, Apr 14, 2015 at 06:00:32PM +0200, Felix Fietkau wrote: >> >> + if (data[len - 1] == '=') >> >> + ret--; >> >> + >> >> + if (data[len - 2] == '=') >> >> + ret--; >> > >> > the 2 if clauses look redundant and i guess you could solve it with a loop > > Ok. I'll fix it. > >> I'd prefer not passing in the input buffer here at all - a tiny >> overestimation of the decode size doesn't hurt. > > I don't think that is a good idea since we do not have NULL terminating > output buffer in b64decode(). If you really want a function without > input buffer we can make another one called: > > static inline int b64_decode_size_approx(size_t len); > > The overestimation is likely going to cause problems in cases like this: > > buf = malloc(b64_decode_size(data, data_len)); > if (!buf) > return -1 > > buf_len = b64decode(rbuf, data, data_len); > if (!buf_len) > return -1 > > write(fd, buf, buf_len); > > In this case end of buf could be foobared and because of that extra > bytes could be written. As long as buf_len only returns the actual number of output bytes, I don't see how overestimation is going to cause problems here. Also, I think it would be nice to have a 0-terminated output. The extra 0-byte should be counted only in b64_decode_size and not in the result of b64decode. I think that will make the API harder to misuse, as calling C-string functions on the result will be safe by default.
- Felix _______________________________________________ openwrt-devel mailing list [email protected] https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
