<feed xmlns='http://www.w3.org/2005/Atom'>
<title>kernel/linux.git/lib/base64.c, branch v6.19.11</title>
<subtitle>Linux kernel stable tree (mirror)</subtitle>
<id>https://git.radix-linux.su/kernel/linux.git/atom?h=v6.19.11</id>
<link rel='self' href='https://git.radix-linux.su/kernel/linux.git/atom?h=v6.19.11'/>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/'/>
<updated>2025-11-20T22:03:44+00:00</updated>
<entry>
<title>lib/base64: rework encode/decode for speed and stricter validation</title>
<updated>2025-11-20T22:03:44+00:00</updated>
<author>
<name>Guan-Chun Wu</name>
<email>409411716@gms.tku.edu.tw</email>
</author>
<published>2025-11-14T06:01:32+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=9c7d3cf94d33927b6e4e576e7090a929e7162d05'/>
<id>urn:sha1:9c7d3cf94d33927b6e4e576e7090a929e7162d05</id>
<content type='text'>
The old base64 implementation relied on a bit-accumulator loop, which was
slow for larger inputs and too permissive in validation.  It would accept
extra '=', missing '=', or even '=' appearing in the middle of the input,
allowing malformed strings to pass.  This patch reworks the internals to
improve performance and enforce stricter validation.

Changes:
 - Encoder:
   * Process input in 3-byte blocks, mapping 24 bits into four 6-bit
     symbols, avoiding bit-by-bit shifting and reducing loop iterations.
   * Handle the final 1-2 leftover bytes explicitly and emit '=' only when
     requested.
 - Decoder:
   * Based on the reverse lookup tables from the previous patch, decode
     input in 4-character groups.
   * Each group is looked up directly, converted into numeric values, and
     combined into 3 output bytes.
   * Explicitly handle padded and unpadded forms:
      - With padding: input length must be a multiple of 4, and '=' is
        allowed only in the last two positions. Reject stray or early '='.
      - Without padding: validate tail lengths (2 or 3 chars) and require
        unused low bits to be zero.
   * Removed the bit-accumulator style loop to reduce loop iterations.

Performance (x86_64, Intel Core i7-10700 @ 2.90GHz, avg over 1000 runs,
KUnit):

Encode:
  64B   ~90ns   -&gt; ~32ns   (~2.8x)
  1KB  ~1332ns  -&gt; ~510ns  (~2.6x)

Decode:
  64B  ~1530ns  -&gt; ~35ns   (~43.7x)
  1KB ~27726ns  -&gt; ~530ns  (~52.3x)

[akpm@linux-foundation.org: remove u32 casts, per David and Guan-Chun]
Link: https://lkml.kernel.org/r/20251114060132.89279-1-409411716@gms.tku.edu.tw
Co-developed-by: Kuan-Wei Chiu &lt;visitorckw@gmail.com&gt;
Signed-off-by: Kuan-Wei Chiu &lt;visitorckw@gmail.com&gt;
Co-developed-by: Yu-Sheng Huang &lt;home7438072@gmail.com&gt;
Signed-off-by: Yu-Sheng Huang &lt;home7438072@gmail.com&gt;
Signed-off-by: Guan-Chun Wu &lt;409411716@gms.tku.edu.tw&gt;
Reviewed-by: David Laight &lt;david.laight.linux@gmail.com&gt;
Cc: Christoph Hellwig &lt;hch@lst.de&gt;
Cc: Eric Biggers &lt;ebiggers@kernel.org&gt;
Cc: Ilya Dryomov &lt;idryomov@gmail.com&gt;
Cc: Jaegeuk Kim &lt;jaegeuk@kernel.org&gt;
Cc: Jens Axboe &lt;axboe@kernel.dk&gt;
Cc: Keith Busch &lt;kbusch@kernel.org&gt;
Cc: Sagi Grimberg &lt;sagi@grimberg.me&gt;
Cc: "Theodore Y. Ts'o" &lt;tytso@mit.edu&gt;
Cc: Viacheslav Dubeyko &lt;Slava.Dubeyko@ibm.com&gt;
Cc: Xiubo Li &lt;xiubli@redhat.com&gt;
Signed-off-by: Andrew Morton &lt;akpm@linux-foundation.org&gt;
</content>
</entry>
<entry>
<title>lib/base64: optimize base64_decode() with reverse lookup tables</title>
<updated>2025-11-20T22:03:44+00:00</updated>
<author>
<name>Kuan-Wei Chiu</name>
<email>visitorckw@gmail.com</email>
</author>
<published>2025-11-14T06:01:07+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=c4eb7ad32eab13ba64cc452c6f43d518b63f5e03'/>
<id>urn:sha1:c4eb7ad32eab13ba64cc452c6f43d518b63f5e03</id>
<content type='text'>
Replace the use of strchr() in base64_decode() with precomputed reverse
lookup tables for each variant. This avoids repeated string scans and
improves performance. Use -1 in the tables to mark invalid characters.

Decode:
  64B   ~1530ns  -&gt;  ~80ns    (~19.1x)
  1KB  ~27726ns  -&gt; ~1239ns   (~22.4x)

[akpm@linux-foundation.org: fix kernedoc]
Link: https://lkml.kernel.org/r/20251114060107.89026-1-409411716@gms.tku.edu.tw
Signed-off-by: Kuan-Wei Chiu &lt;visitorckw@gmail.com&gt;
Co-developed-by: Guan-Chun Wu &lt;409411716@gms.tku.edu.tw&gt;
Signed-off-by: Guan-Chun Wu &lt;409411716@gms.tku.edu.tw&gt;
Reviewed-by: David Laight &lt;david.laight.linux@gmail.com&gt;
Cc: Christoph Hellwig &lt;hch@lst.de&gt;
Cc: Eric Biggers &lt;ebiggers@kernel.org&gt;
Cc: Ilya Dryomov &lt;idryomov@gmail.com&gt;
Cc: Jaegeuk Kim &lt;jaegeuk@kernel.org&gt;
Cc: Jens Axboe &lt;axboe@kernel.dk&gt;
Cc: Keith Busch &lt;kbusch@kernel.org&gt;
Cc: Sagi Grimberg &lt;sagi@grimberg.me&gt;
Cc: "Theodore Y. Ts'o" &lt;tytso@mit.edu&gt;
Cc: Viacheslav Dubeyko &lt;Slava.Dubeyko@ibm.com&gt;
Cc: Xiubo Li &lt;xiubli@redhat.com&gt;
Cc: Yu-Sheng Huang &lt;home7438072@gmail.com&gt;
Signed-off-by: Andrew Morton &lt;akpm@linux-foundation.org&gt;
</content>
</entry>
<entry>
<title>lib/base64: add support for multiple variants</title>
<updated>2025-11-20T22:03:43+00:00</updated>
<author>
<name>Kuan-Wei Chiu</name>
<email>visitorckw@gmail.com</email>
</author>
<published>2025-11-14T06:00:45+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=f1e2ca801c54dfc09d6a5540207cec25e8d43f6f'/>
<id>urn:sha1:f1e2ca801c54dfc09d6a5540207cec25e8d43f6f</id>
<content type='text'>
Patch series " lib/base64: add generic encoder/decoder, migrate users", v5.

This series introduces a generic Base64 encoder/decoder to the kernel
library, eliminating duplicated implementations and delivering significant
performance improvements.

The Base64 API has been extended to support multiple variants (Standard,
URL-safe, and IMAP) as defined in RFC 4648 and RFC 3501.  The API now
takes a variant parameter and an option to control padding.  As part of
this series, users are migrated to the new interface while preserving
their specific formats: fscrypt now uses BASE64_URLSAFE, Ceph uses
BASE64_IMAP, and NVMe is updated to BASE64_STD.

On the encoder side, the implementation processes input in 3-byte blocks,
mapping 24 bits directly to 4 output symbols.  This avoids bit-by-bit
streaming and reduces loop overhead, achieving about a 2.7x speedup
compared to previous implementations.

On the decoder side, replace strchr() lookups with per-variant reverse
tables and process input in 4-character groups.  Each group is mapped to
numeric values and combined into 3 bytes.  Padded and unpadded forms are
validated explicitly, rejecting invalid '=' usage and enforcing tail
rules.  This improves throughput by ~43-52x.


This patch (of 6):

Extend the base64 API to support multiple variants (standard, URL-safe,
and IMAP) as defined in RFC 4648 and RFC 3501.  The API now takes a
variant parameter and an option to control padding.  Update NVMe auth code
to use the new interface with BASE64_STD.

Link: https://lkml.kernel.org/r/20251114055829.87814-1-409411716@gms.tku.edu.tw
Link: https://lkml.kernel.org/r/20251114060045.88792-1-409411716@gms.tku.edu.tw
Signed-off-by: Kuan-Wei Chiu &lt;visitorckw@gmail.com&gt;
Co-developed-by: Guan-Chun Wu &lt;409411716@gms.tku.edu.tw&gt;
Signed-off-by: Guan-Chun Wu &lt;409411716@gms.tku.edu.tw&gt;
Reviewed-by: David Laight &lt;david.laight.linux@gmail.com&gt;
Cc: Christoph Hellwig &lt;hch@lst.de&gt;
Cc: Eric Biggers &lt;ebiggers@kernel.org&gt;
Cc: Ilya Dryomov &lt;idryomov@gmail.com&gt;
Cc: Jaegeuk Kim &lt;jaegeuk@kernel.org&gt;
Cc: Jens Axboe &lt;axboe@kernel.dk&gt;
Cc: Keith Busch &lt;kbusch@kernel.org&gt;
Cc: Sagi Grimberg &lt;sagi@grimberg.me&gt;
Cc: "Theodore Y. Ts'o" &lt;tytso@mit.edu&gt;
Cc: Viacheslav Dubeyko &lt;Slava.Dubeyko@ibm.com&gt;
Cc: Xiubo Li &lt;xiubli@redhat.com&gt;
Cc: Yu-Sheng Huang &lt;home7438072@gmail.com&gt;
Signed-off-by: Andrew Morton &lt;akpm@linux-foundation.org&gt;
</content>
</entry>
<entry>
<title>lib/base64: RFC4648-compliant base64 encoding</title>
<updated>2022-08-02T23:14:47+00:00</updated>
<author>
<name>Hannes Reinecke</name>
<email>hare@suse.de</email>
</author>
<published>2022-06-27T09:51:59+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=a116e1cdc64a743c36c40e6fa639dec991c157a3'/>
<id>urn:sha1:a116e1cdc64a743c36c40e6fa639dec991c157a3</id>
<content type='text'>
Add RFC4648-compliant base64 encoding and decoding routines, based on
the base64url encoding in fs/crypto/fname.c.

Signed-off-by: Hannes Reinecke &lt;hare@suse.de&gt;
Reviewed-by: Himanshu Madhani &lt;himanshu.madhani@oracle.com&gt;
Reviewed-by: Sagi Grimberg &lt;sagi@grimberg.me&gt;
Cc: Eric Biggers &lt;ebiggers@kernel.org&gt;
Signed-off-by: Christoph Hellwig &lt;hch@lst.de&gt;
Signed-off-by: Jens Axboe &lt;axboe@kernel.dk&gt;
</content>
</entry>
</feed>
