Re: [PATCHv2] vsprintf: Add %pMR for Bluetooth MAC address

From: Andy Shevchenko
Date: Mon May 28 2012 - 06:01:05 EST


On Mon, May 28, 2012 at 12:00 PM, Andrei Emeltchenko
<Andrei.Emeltchenko.news@xxxxxxxxx> wrote:
> From: Andrei Emeltchenko <andrei.emeltchenko@xxxxxxxxx>
>
> Bluetooth uses mostly LE byte order which is reversed for visual
> interpretation. Currently in Bluetooth in use unsafe batostr function.
>
> This is slightly modified version of Joe Perches <joe@xxxxxxxxxxx>
> patch (sent Sat, Dec 4, 2010).
>
> Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@xxxxxxxxx>
> ---
> Â Â Â Âv2: changed bluetooth to reversed, syntax fixes
>
> Âlib/vsprintf.c | Â 22 +++++++++++++++++-----
> Â1 file changed, 17 insertions(+), 5 deletions(-)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index abbabec..d98b12d 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -557,17 +557,27 @@ char *mac_address_string(char *buf, char *end, u8 *addr,
> Â{
> Â Â Â Âchar mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
> Â Â Â Âchar *p = mac_addr;
> - Â Â Â int i;
> + Â Â Â int i, index;
> Â Â Â Âchar separator;
> + Â Â Â bool reversed = false;
>
> - Â Â Â if (fmt[1] == 'F') { Â Â Â Â Â Â/* FDDI canonical format */
> + Â Â Â switch (fmt[1]) {
> + Â Â Â case 'F':
> Â Â Â Â Â Â Â Âseparator = '-';
> - Â Â Â } else {
> + Â Â Â Â Â Â Â break;
> +
> + Â Â Â case 'R':
> + Â Â Â Â Â Â Â reversed = true;
> + Â Â Â Â Â Â Â /* fall through */
This solution looks a bit limited. On one hand it makes difficult to add another
case where format specifies colon separator with something else. On
the other hand
I don't see any troubles if you allow reverse as a modifier for both
cases %pMF & %pM

> +
> + Â Â Â default:
> Â Â Â Â Â Â Â Âseparator = ':';
> + Â Â Â Â Â Â Â break;
> Â Â Â Â}
>
> Â Â Â Âfor (i = 0; i < 6; i++) {
> - Â Â Â Â Â Â Â p = hex_byte_pack(p, addr[i]);
> + Â Â Â Â Â Â Â index = !reversed ? i : 5 - i;
> + Â Â Â Â Â Â Â p = hex_byte_pack(p, addr[index]);
I guess instead of using additional variable (index), you could use
just normal if () {} else {} sentence
here.

> Â Â Â Â Â Â Â Âif (fmt[0] == 'M' && i != 5)
> Â Â Â Â Â Â Â Â Â Â Â Â*p++ = separator;
> Â Â Â Â}
> @@ -830,6 +840,7 @@ int kptr_restrict __read_mostly;
> Â* - 'm' For a 6-byte MAC address, it prints the hex address without colons
> Â* - 'MF' For a 6-byte MAC FDDI address, it prints the address
> Â* Â Â Â with a dash-separated hex notation
> + * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
> Â* - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
> Â* Â Â Â IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
> Â* Â Â Â IPv6 uses colon separated network-order 16 bit hex with leading 0's
> @@ -890,7 +901,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
> Â Â Â Â Â Â Â Âreturn resource_string(buf, end, ptr, spec, fmt);
> Â Â Â Âcase 'M': Â Â Â Â Â Â Â Â Â Â Â /* Colon separated: 00:01:02:03:04:05 */
> Â Â Â Âcase 'm': Â Â Â Â Â Â Â Â Â Â Â /* Contiguous: 000102030405 */
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* [mM]F (FDDI, bit reversed) */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* [mM]F (FDDI) */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* [mM]R (Reverse order; Bluetooth) */
> Â Â Â Â Â Â Â Âreturn mac_address_string(buf, end, ptr, spec, fmt);
> Â Â Â Âcase 'I': Â Â Â Â Â Â Â Â Â Â Â /* Formatted IP supported
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â * 4: Â 1.2.3.4



--
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/