2019-05-19 12:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-08-31 15:13:54 +00:00
|
|
|
/*
|
|
|
|
* Helpers for formatting and printing strings
|
|
|
|
*
|
|
|
|
* Copyright 31 August 2008 James Bottomley
|
2013-04-30 22:27:30 +00:00
|
|
|
* Copyright (C) 2013, Intel Corporation
|
2008-08-31 15:13:54 +00:00
|
|
|
*/
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
#include <linux/bug.h>
|
2008-08-31 15:13:54 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/math64.h>
|
2011-11-17 02:29:17 +00:00
|
|
|
#include <linux/export.h>
|
2013-04-30 22:27:30 +00:00
|
|
|
#include <linux/ctype.h>
|
2014-10-13 22:55:16 +00:00
|
|
|
#include <linux/errno.h>
|
2016-04-20 22:46:25 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/limits.h>
|
2016-04-20 22:46:24 +00:00
|
|
|
#include <linux/mm.h>
|
2016-04-20 22:46:23 +00:00
|
|
|
#include <linux/slab.h>
|
2014-10-13 22:55:16 +00:00
|
|
|
#include <linux/string.h>
|
2008-08-31 15:13:54 +00:00
|
|
|
#include <linux/string_helpers.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* string_get_size - get the size in the specified units
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
* @size: The size to be converted in blocks
|
|
|
|
* @blk_size: Size of the block (use 1 for size in bytes)
|
2008-08-31 15:13:54 +00:00
|
|
|
* @units: units to use (powers of 1000 or 1024)
|
|
|
|
* @buf: buffer to format to
|
|
|
|
* @len: length of buffer
|
|
|
|
*
|
|
|
|
* This function returns a string formatted to 3 significant figures
|
2015-02-12 23:01:50 +00:00
|
|
|
* giving the size in the required units. @buf should have room for
|
|
|
|
* at least 9 bytes and will always be zero terminated.
|
2008-08-31 15:13:54 +00:00
|
|
|
*
|
|
|
|
*/
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
|
2015-02-12 23:01:50 +00:00
|
|
|
char *buf, int len)
|
2008-08-31 15:13:54 +00:00
|
|
|
{
|
2014-08-06 23:09:31 +00:00
|
|
|
static const char *const units_10[] = {
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
|
2014-08-06 23:09:31 +00:00
|
|
|
};
|
|
|
|
static const char *const units_2[] = {
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
|
2014-08-06 23:09:31 +00:00
|
|
|
};
|
|
|
|
static const char *const *const units_str[] = {
|
|
|
|
[STRING_UNITS_10] = units_10,
|
2008-08-31 15:13:54 +00:00
|
|
|
[STRING_UNITS_2] = units_2,
|
|
|
|
};
|
2012-05-29 22:07:32 +00:00
|
|
|
static const unsigned int divisor[] = {
|
2008-08-31 15:13:54 +00:00
|
|
|
[STRING_UNITS_10] = 1000,
|
|
|
|
[STRING_UNITS_2] = 1024,
|
|
|
|
};
|
2016-01-20 22:58:29 +00:00
|
|
|
static const unsigned int rounding[] = { 500, 50, 5 };
|
|
|
|
int i = 0, j;
|
|
|
|
u32 remainder = 0, sf_cap;
|
2008-08-31 15:13:54 +00:00
|
|
|
char tmp[8];
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
const char *unit;
|
2008-08-31 15:13:54 +00:00
|
|
|
|
|
|
|
tmp[0] = '\0';
|
2016-01-20 22:58:29 +00:00
|
|
|
|
|
|
|
if (blk_size == 0)
|
|
|
|
size = 0;
|
|
|
|
if (size == 0)
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
goto out;
|
2008-08-31 15:13:54 +00:00
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
/* This is Napier's algorithm. Reduce the original block size to
|
|
|
|
*
|
|
|
|
* coefficient * divisor[units]^i
|
|
|
|
*
|
|
|
|
* we do the reduction so both coefficients are just under 32 bits so
|
|
|
|
* that multiplying them together won't overflow 64 bits and we keep
|
|
|
|
* as much precision as possible in the numbers.
|
|
|
|
*
|
|
|
|
* Note: it's safe to throw away the remainders here because all the
|
|
|
|
* precision is in the coefficients.
|
|
|
|
*/
|
|
|
|
while (blk_size >> 32) {
|
|
|
|
do_div(blk_size, divisor[units]);
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
while (size >> 32) {
|
|
|
|
do_div(size, divisor[units]);
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
/* now perform the actual multiplication keeping i as the sum of the
|
|
|
|
* two logarithms */
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
size *= blk_size;
|
2008-08-31 15:13:54 +00:00
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
/* and logarithmically reduce it until it's just under the divisor */
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
while (size >= divisor[units]) {
|
|
|
|
remainder = do_div(size, divisor[units]);
|
|
|
|
i++;
|
2008-08-31 15:13:54 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
/* work out in j how many digits of precision we need from the
|
|
|
|
* remainder */
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
sf_cap = size;
|
|
|
|
for (j = 0; sf_cap*10 < 1000; j++)
|
|
|
|
sf_cap *= 10;
|
|
|
|
|
2016-01-20 22:58:29 +00:00
|
|
|
if (units == STRING_UNITS_2) {
|
|
|
|
/* express the remainder as a decimal. It's currently the
|
|
|
|
* numerator of a fraction whose denominator is
|
|
|
|
* divisor[units], which is 1 << 10 for STRING_UNITS_2 */
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
remainder *= 1000;
|
2016-01-20 22:58:29 +00:00
|
|
|
remainder >>= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a 5 to the digit below what will be printed to ensure
|
|
|
|
* an arithmetical round up and carry it through to size */
|
|
|
|
remainder += rounding[j];
|
|
|
|
if (remainder >= 1000) {
|
|
|
|
remainder -= 1000;
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j) {
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), ".%03u", remainder);
|
|
|
|
tmp[j+1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (i >= ARRAY_SIZE(units_2))
|
|
|
|
unit = "UNK";
|
|
|
|
else
|
|
|
|
unit = units_str[units][i];
|
|
|
|
|
2015-02-12 23:01:48 +00:00
|
|
|
snprintf(buf, len, "%u%s %s", (u32)size,
|
sd, mmc, virtio_blk, string_helpers: fix block size units
The current string_get_size() overflows when the device size goes over
2^64 bytes because the string helper routine computes the suffix from
the size in bytes. However, the entirety of Linux thinks in terms of
blocks, not bytes, so this will artificially induce an overflow on very
large devices. Fix this by making the function string_get_size() take
blocks and the block size instead of bytes. This should allow us to
keep working until the current SCSI standard overflows.
Also fix virtio_blk and mmc (both of which were also artificially
multiplying by the block size to pass a byte side to string_get_size()).
The mathematics of this is pretty simple: we're taking a product of
size in blocks (S) and block size (B) and trying to re-express this in
exponential form: S*B = R*N^E (where N, the exponent is either 1000 or
1024) and R < N. Mathematically, S = RS*N^ES and B=RB*N^EB, so if RS*RB
< N it's easy to see that S*B = RS*RB*N^(ES+EB). However, if RS*BS > N,
we can see that this can be re-expressed as RS*BS = R*N (where R =
RS*BS/N < N) so the whole exponent becomes R*N^(ES+EB+1)
[jejb: fix incorrect 32 bit do_div spotted by kbuild test robot <fengguang.wu@intel.com>]
Acked-by: Ulf Hansson <ulf.hansson@linaro.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: James Bottomley <JBottomley@Odin.com>
2015-03-06 02:47:01 +00:00
|
|
|
tmp, unit);
|
2008-08-31 15:13:54 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(string_get_size);
|
2013-04-30 22:27:30 +00:00
|
|
|
|
|
|
|
static bool unescape_space(char **src, char **dst)
|
|
|
|
{
|
|
|
|
char *p = *dst, *q = *src;
|
|
|
|
|
|
|
|
switch (*q) {
|
|
|
|
case 'n':
|
|
|
|
*p = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*p = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
*p = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
*p = '\v';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
*p = '\f';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*dst += 1;
|
|
|
|
*src += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool unescape_octal(char **src, char **dst)
|
|
|
|
{
|
|
|
|
char *p = *dst, *q = *src;
|
|
|
|
u8 num;
|
|
|
|
|
|
|
|
if (isodigit(*q) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
num = (*q++) & 7;
|
|
|
|
while (num < 32 && isodigit(*q) && (q - *src < 3)) {
|
|
|
|
num <<= 3;
|
|
|
|
num += (*q++) & 7;
|
|
|
|
}
|
|
|
|
*p = num;
|
|
|
|
*dst += 1;
|
|
|
|
*src = q;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool unescape_hex(char **src, char **dst)
|
|
|
|
{
|
|
|
|
char *p = *dst, *q = *src;
|
|
|
|
int digit;
|
|
|
|
u8 num;
|
|
|
|
|
|
|
|
if (*q++ != 'x')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
num = digit = hex_to_bin(*q++);
|
|
|
|
if (digit < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
digit = hex_to_bin(*q);
|
|
|
|
if (digit >= 0) {
|
|
|
|
q++;
|
|
|
|
num = (num << 4) | digit;
|
|
|
|
}
|
|
|
|
*p = num;
|
|
|
|
*dst += 1;
|
|
|
|
*src = q;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool unescape_special(char **src, char **dst)
|
|
|
|
{
|
|
|
|
char *p = *dst, *q = *src;
|
|
|
|
|
|
|
|
switch (*q) {
|
|
|
|
case '\"':
|
|
|
|
*p = '\"';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
*p = '\\';
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
*p = '\a';
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
*p = '\e';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*dst += 1;
|
|
|
|
*src += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:11 +00:00
|
|
|
/**
|
|
|
|
* string_unescape - unquote characters in the given string
|
|
|
|
* @src: source buffer (escaped)
|
|
|
|
* @dst: destination buffer (unescaped)
|
|
|
|
* @size: size of the destination buffer (0 to unlimit)
|
2019-07-16 23:27:36 +00:00
|
|
|
* @flags: combination of the flags.
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The function unquotes characters in the given string.
|
|
|
|
*
|
|
|
|
* Because the size of the output will be the same as or less than the size of
|
|
|
|
* the input, the transformation may be performed in place.
|
|
|
|
*
|
|
|
|
* Caller must provide valid source and destination pointers. Be aware that
|
|
|
|
* destination buffer will always be NULL-terminated. Source string must be
|
|
|
|
* NULL-terminated as well. The supported flags are::
|
|
|
|
*
|
|
|
|
* UNESCAPE_SPACE:
|
2014-10-13 22:55:11 +00:00
|
|
|
* '\f' - form feed
|
|
|
|
* '\n' - new line
|
|
|
|
* '\r' - carriage return
|
|
|
|
* '\t' - horizontal tab
|
|
|
|
* '\v' - vertical tab
|
2019-07-16 23:27:36 +00:00
|
|
|
* UNESCAPE_OCTAL:
|
2014-10-13 22:55:11 +00:00
|
|
|
* '\NNN' - byte with octal value NNN (1 to 3 digits)
|
2019-07-16 23:27:36 +00:00
|
|
|
* UNESCAPE_HEX:
|
2014-10-13 22:55:11 +00:00
|
|
|
* '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
|
2019-07-16 23:27:36 +00:00
|
|
|
* UNESCAPE_SPECIAL:
|
2014-10-13 22:55:11 +00:00
|
|
|
* '\"' - double quote
|
|
|
|
* '\\' - backslash
|
|
|
|
* '\a' - alert (BEL)
|
|
|
|
* '\e' - escape
|
2019-07-16 23:27:36 +00:00
|
|
|
* UNESCAPE_ANY:
|
2014-10-13 22:55:11 +00:00
|
|
|
* all previous together
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* The amount of the characters processed to the destination buffer excluding
|
|
|
|
* trailing '\0' is returned.
|
|
|
|
*/
|
2013-04-30 22:27:30 +00:00
|
|
|
int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
|
|
|
|
{
|
|
|
|
char *out = dst;
|
|
|
|
|
|
|
|
while (*src && --size) {
|
|
|
|
if (src[0] == '\\' && src[1] != '\0' && size > 1) {
|
|
|
|
src++;
|
|
|
|
size--;
|
|
|
|
|
|
|
|
if (flags & UNESCAPE_SPACE &&
|
|
|
|
unescape_space(&src, &out))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (flags & UNESCAPE_OCTAL &&
|
|
|
|
unescape_octal(&src, &out))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (flags & UNESCAPE_HEX &&
|
|
|
|
unescape_hex(&src, &out))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (flags & UNESCAPE_SPECIAL &&
|
|
|
|
unescape_special(&src, &out))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*out++ = '\\';
|
|
|
|
}
|
|
|
|
*out++ = *src++;
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
|
|
|
|
return out - dst;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(string_unescape);
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_passthrough(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = c;
|
|
|
|
*dst = out + 1;
|
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_space(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
unsigned char to;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\n':
|
|
|
|
to = 'n';
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
to = 'r';
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
to = 't';
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
to = 'v';
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
to = 'f';
|
|
|
|
break;
|
|
|
|
default:
|
2015-04-15 23:17:25 +00:00
|
|
|
return false;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = '\\';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = to;
|
|
|
|
++out;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
*dst = out;
|
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_special(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
unsigned char to;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
to = '\\';
|
|
|
|
break;
|
|
|
|
case '\a':
|
|
|
|
to = 'a';
|
|
|
|
break;
|
|
|
|
case '\e':
|
|
|
|
to = 'e';
|
|
|
|
break;
|
2021-06-15 16:52:45 +00:00
|
|
|
case '"':
|
|
|
|
to = '"';
|
|
|
|
break;
|
2014-10-13 22:55:16 +00:00
|
|
|
default:
|
2015-04-15 23:17:25 +00:00
|
|
|
return false;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = '\\';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = to;
|
|
|
|
++out;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
*dst = out;
|
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_null(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
|
|
|
|
if (c)
|
2015-04-15 23:17:25 +00:00
|
|
|
return false;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = '\\';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = '0';
|
|
|
|
++out;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
*dst = out;
|
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_octal(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = '\\';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = ((c >> 6) & 0x07) + '0';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = ((c >> 3) & 0x07) + '0';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = ((c >> 0) & 0x07) + '0';
|
|
|
|
++out;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
*dst = out;
|
2015-04-15 23:17:25 +00:00
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
static bool escape_hex(unsigned char c, char **dst, char *end)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
|
|
|
char *out = *dst;
|
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
if (out < end)
|
|
|
|
*out = '\\';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = 'x';
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = hex_asc_hi(c);
|
|
|
|
++out;
|
|
|
|
if (out < end)
|
|
|
|
*out = hex_asc_lo(c);
|
|
|
|
++out;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
*dst = out;
|
2015-04-15 23:17:25 +00:00
|
|
|
return true;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* string_escape_mem - quote characters in the given memory buffer
|
|
|
|
* @src: source buffer (unescaped)
|
|
|
|
* @isz: source buffer size
|
|
|
|
* @dst: destination buffer (escaped)
|
|
|
|
* @osz: destination buffer size
|
2019-07-16 23:27:36 +00:00
|
|
|
* @flags: combination of the flags
|
|
|
|
* @only: NULL-terminated string containing characters used to limit
|
|
|
|
* the selected escape class. If characters are included in @only
|
|
|
|
* that would not normally be escaped by the classes selected
|
|
|
|
* in @flags, they will be copied to @dst unescaped.
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The process of escaping byte buffer includes several parts. They are applied
|
|
|
|
* in the following sequence.
|
|
|
|
*
|
2021-07-01 01:55:08 +00:00
|
|
|
* 1. The character is not matched to the one from @only string and thus
|
2019-07-16 23:27:36 +00:00
|
|
|
* must go as-is to the output.
|
2021-07-01 01:55:17 +00:00
|
|
|
* 2. The character is matched to the printable and ASCII classes, if asked,
|
2021-07-01 01:55:14 +00:00
|
|
|
* and in case of match it passes through to the output.
|
2021-07-01 01:55:17 +00:00
|
|
|
* 3. The character is matched to the printable or ASCII class, if asked,
|
|
|
|
* and in case of match it passes through to the output.
|
|
|
|
* 4. The character is checked if it falls into the class given by @flags.
|
2019-07-16 23:27:36 +00:00
|
|
|
* %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any
|
|
|
|
* character. Note that they actually can't go together, otherwise
|
|
|
|
* %ESCAPE_HEX will be ignored.
|
|
|
|
*
|
|
|
|
* Caller must provide valid source and destination pointers. Be aware that
|
|
|
|
* destination buffer will not be NULL-terminated, thus caller have to append
|
2021-07-01 01:55:14 +00:00
|
|
|
* it if needs. The supported flags are::
|
2019-07-16 23:27:36 +00:00
|
|
|
*
|
2015-09-09 22:37:14 +00:00
|
|
|
* %ESCAPE_SPACE: (special white space, not space itself)
|
2014-10-13 22:55:16 +00:00
|
|
|
* '\f' - form feed
|
|
|
|
* '\n' - new line
|
|
|
|
* '\r' - carriage return
|
|
|
|
* '\t' - horizontal tab
|
|
|
|
* '\v' - vertical tab
|
|
|
|
* %ESCAPE_SPECIAL:
|
2021-06-15 16:52:45 +00:00
|
|
|
* '\"' - double quote
|
2014-10-13 22:55:16 +00:00
|
|
|
* '\\' - backslash
|
|
|
|
* '\a' - alert (BEL)
|
|
|
|
* '\e' - escape
|
|
|
|
* %ESCAPE_NULL:
|
|
|
|
* '\0' - null
|
|
|
|
* %ESCAPE_OCTAL:
|
|
|
|
* '\NNN' - byte with octal value NNN (3 digits)
|
|
|
|
* %ESCAPE_ANY:
|
|
|
|
* all previous together
|
|
|
|
* %ESCAPE_NP:
|
2021-07-01 01:55:14 +00:00
|
|
|
* escape only non-printable characters, checked by isprint()
|
2014-10-13 22:55:16 +00:00
|
|
|
* %ESCAPE_ANY_NP:
|
|
|
|
* all previous together
|
|
|
|
* %ESCAPE_HEX:
|
|
|
|
* '\xHH' - byte with hexadecimal value HH (2 digits)
|
2021-07-01 01:55:14 +00:00
|
|
|
* %ESCAPE_NA:
|
|
|
|
* escape only non-ascii characters, checked by isascii()
|
2021-07-01 01:55:17 +00:00
|
|
|
* %ESCAPE_NAP:
|
|
|
|
* escape only non-printable or non-ascii characters
|
2021-07-01 01:55:20 +00:00
|
|
|
* %ESCAPE_APPEND:
|
|
|
|
* append characters from @only to be escaped by the given classes
|
|
|
|
*
|
|
|
|
* %ESCAPE_APPEND would help to pass additional characters to the escaped, when
|
|
|
|
* one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided.
|
2021-07-01 01:55:14 +00:00
|
|
|
*
|
2021-07-01 01:55:17 +00:00
|
|
|
* One notable caveat, the %ESCAPE_NAP, %ESCAPE_NP and %ESCAPE_NA have the
|
|
|
|
* higher priority than the rest of the flags (%ESCAPE_NAP is the highest).
|
2021-07-01 01:55:14 +00:00
|
|
|
* It doesn't make much sense to use either of them without %ESCAPE_OCTAL
|
|
|
|
* or %ESCAPE_HEX, because they cover most of the other character classes.
|
2021-07-01 01:55:17 +00:00
|
|
|
* %ESCAPE_NAP can utilize %ESCAPE_SPACE or %ESCAPE_SPECIAL in addition to
|
|
|
|
* the above.
|
2014-10-13 22:55:16 +00:00
|
|
|
*
|
|
|
|
* Return:
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
* The total size of the escaped output that would be generated for
|
|
|
|
* the given input and flags. To check whether the output was
|
|
|
|
* truncated, compare the return value to osz. There is room left in
|
|
|
|
* dst for a '\0' terminator if and only if ret < osz.
|
2014-10-13 22:55:16 +00:00
|
|
|
*/
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
|
2015-09-09 22:37:16 +00:00
|
|
|
unsigned int flags, const char *only)
|
2014-10-13 22:55:16 +00:00
|
|
|
{
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
char *p = dst;
|
2015-04-15 23:17:25 +00:00
|
|
|
char *end = p + osz;
|
2015-09-09 22:37:16 +00:00
|
|
|
bool is_dict = only && *only;
|
2021-07-01 01:55:20 +00:00
|
|
|
bool is_append = flags & ESCAPE_APPEND;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
while (isz--) {
|
|
|
|
unsigned char c = *src++;
|
2021-07-01 01:55:20 +00:00
|
|
|
bool in_dict = is_dict && strchr(only, c);
|
2014-10-13 22:55:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply rules in the following sequence:
|
2015-09-09 22:37:16 +00:00
|
|
|
* - the @only string is supplied and does not contain a
|
2014-10-13 22:55:16 +00:00
|
|
|
* character under question
|
2021-07-01 01:55:17 +00:00
|
|
|
* - the character is printable and ASCII, when @flags has
|
|
|
|
* %ESCAPE_NAP bit set
|
2021-07-01 01:55:08 +00:00
|
|
|
* - the character is printable, when @flags has
|
|
|
|
* %ESCAPE_NP bit set
|
2021-07-01 01:55:14 +00:00
|
|
|
* - the character is ASCII, when @flags has
|
|
|
|
* %ESCAPE_NA bit set
|
2014-10-13 22:55:16 +00:00
|
|
|
* - the character doesn't fall into a class of symbols
|
|
|
|
* defined by given @flags
|
|
|
|
* In these cases we just pass through a character to the
|
|
|
|
* output buffer.
|
2021-07-01 01:55:20 +00:00
|
|
|
*
|
|
|
|
* When %ESCAPE_APPEND is passed, the characters from @only
|
|
|
|
* have been excluded from the %ESCAPE_NAP, %ESCAPE_NP, and
|
|
|
|
* %ESCAPE_NA cases.
|
2014-10-13 22:55:16 +00:00
|
|
|
*/
|
2021-07-01 01:55:20 +00:00
|
|
|
if (!(is_append || in_dict) && is_dict &&
|
2021-07-01 01:55:11 +00:00
|
|
|
escape_passthrough(c, &p, end))
|
|
|
|
continue;
|
2021-07-01 01:55:08 +00:00
|
|
|
|
2021-07-01 01:55:20 +00:00
|
|
|
if (!(is_append && in_dict) && isascii(c) && isprint(c) &&
|
2021-07-01 01:55:17 +00:00
|
|
|
flags & ESCAPE_NAP && escape_passthrough(c, &p, end))
|
|
|
|
continue;
|
|
|
|
|
2021-07-01 01:55:20 +00:00
|
|
|
if (!(is_append && in_dict) && isprint(c) &&
|
2021-07-01 01:55:11 +00:00
|
|
|
flags & ESCAPE_NP && escape_passthrough(c, &p, end))
|
|
|
|
continue;
|
2015-04-15 23:17:25 +00:00
|
|
|
|
2021-07-01 01:55:20 +00:00
|
|
|
if (!(is_append && in_dict) && isascii(c) &&
|
2021-07-01 01:55:14 +00:00
|
|
|
flags & ESCAPE_NA && escape_passthrough(c, &p, end))
|
|
|
|
continue;
|
|
|
|
|
2021-07-01 01:55:11 +00:00
|
|
|
if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
|
|
|
|
continue;
|
2015-04-15 23:17:25 +00:00
|
|
|
|
2021-07-01 01:55:11 +00:00
|
|
|
if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
|
|
|
|
continue;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2021-07-01 01:55:11 +00:00
|
|
|
if (flags & ESCAPE_NULL && escape_null(c, &p, end))
|
|
|
|
continue;
|
2015-04-15 23:17:25 +00:00
|
|
|
|
2021-07-01 01:55:11 +00:00
|
|
|
/* ESCAPE_OCTAL and ESCAPE_HEX always go last */
|
|
|
|
if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
|
|
|
|
continue;
|
2014-10-13 22:55:16 +00:00
|
|
|
|
2015-04-15 23:17:25 +00:00
|
|
|
escape_passthrough(c, &p, end);
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its
current users, vsnprintf(). If that is to honour its contract, it must
know how much space would be needed for the entire escaped buffer, and
string_escape_mem provides no way of obtaining that (short of allocating a
large enough buffer (~4 times input string) to let it play with, and
that's definitely a big no-no inside vsnprintf).
So change the semantics for string_escape_mem to be more snprintf-like:
Return the size of the output that would be generated if the destination
buffer was big enough, but of course still only write to the part of dst
it is allowed to, and (contrary to snprintf) don't do '\0'-termination.
It is then up to the caller to detect whether output was truncated and to
append a '\0' if desired. Also, we must output partial escape sequences,
otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause
printf to write a \0 to buf[2] but leaving buf[0] and buf[1] with whatever
they previously contained.
This also fixes a bug in the escaped_string() helper function, which used
to unconditionally pass a length of "end-buf" to string_escape_mem();
since the latter doesn't check osz for being insanely large, it would
happily write to dst. For example, kasprintf(GFP_KERNEL, "something and
then %pE", ...); is an easy way to trigger an oops.
In test-string_helpers.c, the -ENOMEM test is replaced with testing for
getting the expected return value even if the buffer is too small. We
also ensure that nothing is written (by relying on a NULL pointer deref)
if the output size is 0 by passing NULL - this has to work for
kasprintf("%pE") to work.
In net/sunrpc/cache.c, I think qword_add still has the same semantics.
Someone should definitely double-check this.
In fs/proc/array.c, I made the minimum possible change, but longer-term it
should stop poking around in seq_file internals.
[andriy.shevchenko@linux.intel.com: simplify qword_add]
[andriy.shevchenko@linux.intel.com: add missed curly braces]
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-15 23:17:28 +00:00
|
|
|
return p - dst;
|
2014-10-13 22:55:16 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(string_escape_mem);
|
2016-04-20 22:46:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an allocated string that has been escaped of special characters
|
|
|
|
* and double quotes, making it safe to log in quotes.
|
|
|
|
*/
|
|
|
|
char *kstrdup_quotable(const char *src, gfp_t gfp)
|
|
|
|
{
|
|
|
|
size_t slen, dlen;
|
|
|
|
char *dst;
|
|
|
|
const int flags = ESCAPE_HEX;
|
|
|
|
const char esc[] = "\f\n\r\t\v\a\e\\\"";
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return NULL;
|
|
|
|
slen = strlen(src);
|
|
|
|
|
|
|
|
dlen = string_escape_mem(src, slen, NULL, 0, flags, esc);
|
|
|
|
dst = kmalloc(dlen + 1, gfp);
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
WARN_ON(string_escape_mem(src, slen, dst, dlen, flags, esc) != dlen);
|
|
|
|
dst[dlen] = '\0';
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kstrdup_quotable);
|
2016-04-20 22:46:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns allocated NULL-terminated string containing process
|
|
|
|
* command line, with inter-argument NULLs replaced with spaces,
|
|
|
|
* and other special characters escaped.
|
|
|
|
*/
|
|
|
|
char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
|
|
|
|
{
|
|
|
|
char *buffer, *quoted;
|
|
|
|
int i, res;
|
|
|
|
|
2017-09-13 23:28:29 +00:00
|
|
|
buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
2016-04-20 22:46:24 +00:00
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
res = get_cmdline(task, buffer, PAGE_SIZE - 1);
|
|
|
|
buffer[res] = '\0';
|
|
|
|
|
|
|
|
/* Collapse trailing NULLs, leave res pointing to last non-NULL. */
|
|
|
|
while (--res >= 0 && buffer[res] == '\0')
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Replace inter-argument NULLs. */
|
|
|
|
for (i = 0; i <= res; i++)
|
|
|
|
if (buffer[i] == '\0')
|
|
|
|
buffer[i] = ' ';
|
|
|
|
|
|
|
|
/* Make sure result is printable. */
|
|
|
|
quoted = kstrdup_quotable(buffer, gfp);
|
|
|
|
kfree(buffer);
|
|
|
|
return quoted;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kstrdup_quotable_cmdline);
|
2016-04-20 22:46:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns allocated NULL-terminated string containing pathname,
|
|
|
|
* with special characters escaped, able to be safely logged. If
|
|
|
|
* there is an error, the leading character will be "<".
|
|
|
|
*/
|
|
|
|
char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
|
|
|
|
{
|
|
|
|
char *temp, *pathname;
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return kstrdup("<unknown>", gfp);
|
|
|
|
|
|
|
|
/* We add 11 spaces for ' (deleted)' to be appended */
|
2017-09-13 23:28:29 +00:00
|
|
|
temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
|
2016-04-20 22:46:25 +00:00
|
|
|
if (!temp)
|
|
|
|
return kstrdup("<no_memory>", gfp);
|
|
|
|
|
|
|
|
pathname = file_path(file, temp, PATH_MAX + 11);
|
|
|
|
if (IS_ERR(pathname))
|
|
|
|
pathname = kstrdup("<too_long>", gfp);
|
|
|
|
else
|
|
|
|
pathname = kstrdup_quotable(pathname, gfp);
|
|
|
|
|
|
|
|
kfree(temp);
|
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kstrdup_quotable_file);
|
2020-09-29 10:09:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* kfree_strarray - free a number of dynamically allocated strings contained
|
|
|
|
* in an array and the array itself
|
|
|
|
*
|
|
|
|
* @array: Dynamically allocated array of strings to free.
|
|
|
|
* @n: Number of strings (starting from the beginning of the array) to free.
|
|
|
|
*
|
|
|
|
* Passing a non-NULL @array and @n == 0 as well as NULL @array are valid
|
|
|
|
* use-cases. If @array is NULL, the function does nothing.
|
|
|
|
*/
|
|
|
|
void kfree_strarray(char **array, size_t n)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!array)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
kfree(array[i]);
|
|
|
|
kfree(array);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kfree_strarray);
|
2021-06-18 17:57:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* strscpy_pad() - Copy a C-string into a sized buffer
|
|
|
|
* @dest: Where to copy the string to
|
|
|
|
* @src: Where to copy the string from
|
|
|
|
* @count: Size of destination buffer
|
|
|
|
*
|
|
|
|
* Copy the string, or as much of it as fits, into the dest buffer. The
|
|
|
|
* behavior is undefined if the string buffers overlap. The destination
|
|
|
|
* buffer is always %NUL terminated, unless it's zero-sized.
|
|
|
|
*
|
|
|
|
* If the source string is shorter than the destination buffer, zeros
|
|
|
|
* the tail of the destination buffer.
|
|
|
|
*
|
|
|
|
* For full explanation of why you may want to consider using the
|
|
|
|
* 'strscpy' functions please see the function docstring for strscpy().
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* * The number of characters copied (not including the trailing %NUL)
|
|
|
|
* * -E2BIG if count is 0 or @src was truncated.
|
|
|
|
*/
|
|
|
|
ssize_t strscpy_pad(char *dest, const char *src, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
written = strscpy(dest, src, count);
|
|
|
|
if (written < 0 || written == count - 1)
|
|
|
|
return written;
|
|
|
|
|
|
|
|
memset(dest + written + 1, 0, count - written - 1);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strscpy_pad);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* skip_spaces - Removes leading whitespace from @str.
|
|
|
|
* @str: The string to be stripped.
|
|
|
|
*
|
|
|
|
* Returns a pointer to the first non-whitespace character in @str.
|
|
|
|
*/
|
|
|
|
char *skip_spaces(const char *str)
|
|
|
|
{
|
|
|
|
while (isspace(*str))
|
|
|
|
++str;
|
|
|
|
return (char *)str;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(skip_spaces);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* strim - Removes leading and trailing whitespace from @s.
|
|
|
|
* @s: The string to be stripped.
|
|
|
|
*
|
|
|
|
* Note that the first trailing whitespace is replaced with a %NUL-terminator
|
|
|
|
* in the given string @s. Returns a pointer to the first non-whitespace
|
|
|
|
* character in @s.
|
|
|
|
*/
|
|
|
|
char *strim(char *s)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
size = strlen(s);
|
|
|
|
if (!size)
|
|
|
|
return s;
|
|
|
|
|
|
|
|
end = s + size - 1;
|
|
|
|
while (end >= s && isspace(*end))
|
|
|
|
end--;
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
|
|
|
|
return skip_spaces(s);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strim);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_streq - return true if strings are equal, modulo trailing newline
|
|
|
|
* @s1: one string
|
|
|
|
* @s2: another string
|
|
|
|
*
|
|
|
|
* This routine returns true iff two strings are equal, treating both
|
|
|
|
* NUL and newline-then-NUL as equivalent string terminations. It's
|
|
|
|
* geared for use with sysfs input strings, which generally terminate
|
|
|
|
* with newlines but are compared against values without newlines.
|
|
|
|
*/
|
|
|
|
bool sysfs_streq(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
while (*s1 && *s1 == *s2) {
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s1 == *s2)
|
|
|
|
return true;
|
|
|
|
if (!*s1 && *s2 == '\n' && !s2[1])
|
|
|
|
return true;
|
|
|
|
if (*s1 == '\n' && !s1[1] && !*s2)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sysfs_streq);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* match_string - matches given string in an array
|
|
|
|
* @array: array of strings
|
|
|
|
* @n: number of strings in the array or -1 for NULL terminated arrays
|
|
|
|
* @string: string to match with
|
|
|
|
*
|
|
|
|
* This routine will look for a string in an array of strings up to the
|
|
|
|
* n-th element in the array or until the first NULL element.
|
|
|
|
*
|
|
|
|
* Historically the value of -1 for @n, was used to search in arrays that
|
|
|
|
* are NULL terminated. However, the function does not make a distinction
|
|
|
|
* when finishing the search: either @n elements have been compared OR
|
|
|
|
* the first NULL element was found.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* index of a @string in the @array if matches, or %-EINVAL otherwise.
|
|
|
|
*/
|
|
|
|
int match_string(const char * const *array, size_t n, const char *string)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
const char *item;
|
|
|
|
|
|
|
|
for (index = 0; index < n; index++) {
|
|
|
|
item = array[index];
|
|
|
|
if (!item)
|
|
|
|
break;
|
|
|
|
if (!strcmp(item, string))
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(match_string);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __sysfs_match_string - matches given string in an array
|
|
|
|
* @array: array of strings
|
|
|
|
* @n: number of strings in the array or -1 for NULL terminated arrays
|
|
|
|
* @str: string to match with
|
|
|
|
*
|
|
|
|
* Returns index of @str in the @array or -EINVAL, just like match_string().
|
|
|
|
* Uses sysfs_streq instead of strcmp for matching.
|
|
|
|
*
|
|
|
|
* This routine will look for a string in an array of strings up to the
|
|
|
|
* n-th element in the array or until the first NULL element.
|
|
|
|
*
|
|
|
|
* Historically the value of -1 for @n, was used to search in arrays that
|
|
|
|
* are NULL terminated. However, the function does not make a distinction
|
|
|
|
* when finishing the search: either @n elements have been compared OR
|
|
|
|
* the first NULL element was found.
|
|
|
|
*/
|
|
|
|
int __sysfs_match_string(const char * const *array, size_t n, const char *str)
|
|
|
|
{
|
|
|
|
const char *item;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < n; index++) {
|
|
|
|
item = array[index];
|
|
|
|
if (!item)
|
|
|
|
break;
|
|
|
|
if (sysfs_streq(item, str))
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__sysfs_match_string);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* strreplace - Replace all occurrences of character in string.
|
|
|
|
* @s: The string to operate on.
|
|
|
|
* @old: The character being replaced.
|
|
|
|
* @new: The character @old is replaced with.
|
|
|
|
*
|
|
|
|
* Returns pointer to the nul byte at the end of @s.
|
|
|
|
*/
|
|
|
|
char *strreplace(char *s, char old, char new)
|
|
|
|
{
|
|
|
|
for (; *s; ++s)
|
|
|
|
if (*s == old)
|
|
|
|
*s = new;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strreplace);
|
|
|
|
|
2021-11-02 14:24:20 +00:00
|
|
|
/**
|
|
|
|
* memcpy_and_pad - Copy one buffer to another with padding
|
|
|
|
* @dest: Where to copy to
|
|
|
|
* @dest_len: The destination buffer size
|
|
|
|
* @src: Where to copy from
|
|
|
|
* @count: The number of bytes to copy
|
|
|
|
* @pad: Character to use for padding if space is left in destination.
|
|
|
|
*/
|
|
|
|
void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
|
|
|
|
int pad)
|
|
|
|
{
|
|
|
|
if (dest_len > count) {
|
|
|
|
memcpy(dest, src, count);
|
|
|
|
memset(dest + count, pad, dest_len - count);
|
|
|
|
} else {
|
|
|
|
memcpy(dest, src, dest_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memcpy_and_pad);
|
|
|
|
|
2021-04-14 22:45:39 +00:00
|
|
|
#ifdef CONFIG_FORTIFY_SOURCE
|
2021-06-18 17:57:38 +00:00
|
|
|
void fortify_panic(const char *name)
|
|
|
|
{
|
|
|
|
pr_emerg("detected buffer overflow in %s\n", name);
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(fortify_panic);
|
2021-04-14 22:45:39 +00:00
|
|
|
#endif /* CONFIG_FORTIFY_SOURCE */
|