lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANiq72=YAU1iNyUpXd-1VgQQDxy+wCRLkqp==a4QH2ir_pe5Jg@mail.gmail.com>
Date:   Mon, 26 Apr 2021 20:01:21 +0200
From:   Miguel Ojeda <miguel.ojeda.sandonis@...il.com>
To:     Linus Walleij <linus.walleij@...aro.org>
Cc:     Wedson Almeida Filho <wedsonaf@...gle.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Miguel Ojeda <ojeda@...nel.org>,
        Linus Torvalds <torvalds@...ux-foundation.org>,
        Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
        rust-for-linux <rust-for-linux@...r.kernel.org>,
        linux-kbuild <linux-kbuild@...r.kernel.org>,
        Linux Doc Mailing List <linux-doc@...r.kernel.org>,
        linux-kernel <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 00/13] [RFC] Rust support

On Mon, Apr 26, 2021 at 2:18 AM Linus Walleij <linus.walleij@...aro.org> wrote:
>
> static void yas53x_extract_calibration(u8 *data, struct yas5xx_calibration *c)
> {
>         u64 val = get_unaligned_be64(data);
>
>         c->a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32;
>         c->a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8;
>         c->a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32;
>         c->a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38;
>         c->a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32;
>         c->a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64;
>         c->a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32;
>         c->a9 = FIELD_GET(GENMASK_ULL(22, 15), val);
>         c->k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10;
>         c->dck = FIELD_GET(GENMASK_ULL(9, 7), val);
> }

By the way, to give a more concrete example, this function could look like this:

    fn yas53x_extract_calibration(data: [u8; 8], c: &mut yas5xx_calibration)
    {
        let val = u64::from_be_bytes(data);

        c.a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32;
        c.a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8;
        c.a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32;
        c.a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38;
        c.a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32;
        c.a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64;
        c.a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32;
        c.a9 = FIELD_GET(GENMASK_ULL(22, 15), val);
        c.k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10;
        c.dck = FIELD_GET(GENMASK_ULL(9, 7), val) as u8;
    }

assuming `FIELD_GET()` returns `i32`. In particular, `GENMASK_ULL` and
`FIELD_GET` can be written as normal functions, no need for macros
(and can be `const fn` too -- i.e. can be evaluated at compile-time if
needed).

As you see, it looks remarkably similar, and there is no `unsafe`
because we pass the array of bytes instead of a raw pointer.

The caller needs to get the array from somewhere, of course -- if you
only have a raw pointer to start with, then the caller will need an
`unsafe` line to dereference it, as usual.

Cheers,
Miguel

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ