string_helpers.c 10.0 KB
Newer Older
1 2 3 4
/*
 * Helpers for formatting and printing strings
 *
 * Copyright 31 August 2008 James Bottomley
5
 * Copyright (C) 2013, Intel Corporation
6 7 8
 */
#include <linux/kernel.h>
#include <linux/math64.h>
9
#include <linux/export.h>
10
#include <linux/ctype.h>
11 12
#include <linux/errno.h>
#include <linux/string.h>
13 14 15 16 17 18 19 20 21 22
#include <linux/string_helpers.h>

/**
 * string_get_size - get the size in the specified units
 * @size:	The size to be converted
 * @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
23 24
 * giving the size in the required units.  @buf should have room for
 * at least 9 bytes and will always be zero terminated.
25 26
 *
 */
27 28
void string_get_size(u64 size, const enum string_size_units units,
		     char *buf, int len)
29
{
30
	static const char *const units_10[] = {
31
		"B", "kB", "MB", "GB", "TB", "PB", "EB"
32 33
	};
	static const char *const units_2[] = {
34
		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
35 36 37
	};
	static const char *const *const units_str[] = {
		[STRING_UNITS_10] = units_10,
38 39
		[STRING_UNITS_2] = units_2,
	};
40
	static const unsigned int divisor[] = {
41 42 43 44
		[STRING_UNITS_10] = 1000,
		[STRING_UNITS_2] = 1024,
	};
	int i, j;
45
	u32 remainder = 0, sf_cap;
46 47 48
	char tmp[8];

	tmp[0] = '\0';
49 50
	i = 0;
	if (size >= divisor[units]) {
51
		while (size >= divisor[units]) {
52 53 54
			remainder = do_div(size, divisor[units]);
			i++;
		}
55

56 57 58
		sf_cap = size;
		for (j = 0; sf_cap*10 < 1000; j++)
			sf_cap *= 10;
59

60 61
		if (j) {
			remainder *= 1000;
62 63
			remainder /= divisor[units];
			snprintf(tmp, sizeof(tmp), ".%03u", remainder);
64 65
			tmp[j+1] = '\0';
		}
66 67
	}

68
	snprintf(buf, len, "%u%s %s", (u32)size,
69 70 71
		 tmp, units_str[units][i]);
}
EXPORT_SYMBOL(string_get_size);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

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;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/**
 * 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)
 * @flags:	combination of the flags (bitwise OR):
 *	%UNESCAPE_SPACE:
 *		'\f' - form feed
 *		'\n' - new line
 *		'\r' - carriage return
 *		'\t' - horizontal tab
 *		'\v' - vertical tab
 *	%UNESCAPE_OCTAL:
 *		'\NNN' - byte with octal value NNN (1 to 3 digits)
 *	%UNESCAPE_HEX:
 *		'\xHH' - byte with hexadecimal value HH (1 to 2 digits)
 *	%UNESCAPE_SPECIAL:
 *		'\"' - double quote
 *		'\\' - backslash
 *		'\a' - alert (BEL)
 *		'\e' - escape
 *	%UNESCAPE_ANY:
 *		all previous together
 *
 * 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.
 *
 * Return:
 * The amount of the characters processed to the destination buffer excluding
 * trailing '\0' is returned.
 */
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
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);
241

242
static bool escape_passthrough(unsigned char c, char **dst, char *end)
243 244 245
{
	char *out = *dst;

246 247 248 249
	if (out < end)
		*out = c;
	*dst = out + 1;
	return true;
250 251
}

252
static bool escape_space(unsigned char c, char **dst, char *end)
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
{
	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:
274
		return false;
275 276
	}

277 278 279 280
	if (out + 2 > end) {
		*dst = out + 2;
		return true;
	}
281

282 283 284 285 286 287
	if (out < end)
		*out = '\\';
	++out;
	if (out < end)
		*out = to;
	++out;
288

289 290
	*dst = out;
	return true;
291 292
}

293
static bool escape_special(unsigned char c, char **dst, char *end)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
	char *out = *dst;
	unsigned char to;

	switch (c) {
	case '\\':
		to = '\\';
		break;
	case '\a':
		to = 'a';
		break;
	case '\e':
		to = 'e';
		break;
	default:
309
		return false;
310 311
	}

312 313 314 315
	if (out + 2 > end) {
		*dst = out + 2;
		return true;
	}
316

317 318 319 320 321 322
	if (out < end)
		*out = '\\';
	++out;
	if (out < end)
		*out = to;
	++out;
323

324 325
	*dst = out;
	return true;
326 327
}

328
static bool escape_null(unsigned char c, char **dst, char *end)
329 330 331 332
{
	char *out = *dst;

	if (c)
333
		return false;
334

335 336 337 338
	if (out + 2 > end) {
		*dst = out + 2;
		return true;
	}
339

340 341 342 343 344 345
	if (out < end)
		*out = '\\';
	++out;
	if (out < end)
		*out = '0';
	++out;
346

347 348
	*dst = out;
	return true;
349 350
}

351
static bool escape_octal(unsigned char c, char **dst, char *end)
352 353 354
{
	char *out = *dst;

355 356 357 358
	if (out + 4 > end) {
		*dst = out + 4;
		return true;
	}
359

360 361 362 363 364 365 366 367 368 369 370 371
	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;
372 373

	*dst = out;
374
	return true;
375 376
}

377
static bool escape_hex(unsigned char c, char **dst, char *end)
378 379 380
{
	char *out = *dst;

381 382 383 384
	if (out + 4 > end) {
		*dst = out + 4;
		return true;
	}
385

386 387 388 389 390 391 392 393 394 395 396 397
	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;
398 399

	*dst = out;
400
	return true;
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
}

/**
 * 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
 * @flags:	combination of the flags (bitwise OR):
 *	%ESCAPE_SPACE:
 *		'\f' - form feed
 *		'\n' - new line
 *		'\r' - carriage return
 *		'\t' - horizontal tab
 *		'\v' - vertical tab
 *	%ESCAPE_SPECIAL:
 *		'\\' - 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:
 *		escape only non-printable characters (checked by isprint)
 *	%ESCAPE_ANY_NP:
 *		all previous together
 *	%ESCAPE_HEX:
 *		'\xHH' - byte with hexadecimal value HH (2 digits)
 * @esc:	NULL-terminated string of characters any of which, if found in
 *		the source, has to be escaped
 *
 * Description:
 * The process of escaping byte buffer includes several parts. They are applied
 * in the following sequence.
 *	1. The character is matched to the printable class, if asked, and in
 *	   case of match it passes through to the output.
 *	2. The character is not matched to the one from @esc string and thus
 *	   must go as is to the output.
 *	3. The character is checked if it falls into the class given by @flags.
 *	   %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
 * it if needs.
 *
 * Return:
 * The amount of the characters processed to the destination buffer, or
 * %-ENOMEM if the size of buffer is not enough to put an escaped character is
 * returned.
 *
 * Even in the case of error @dst pointer will be updated to point to the byte
 * after the last processed character.
 */
int string_escape_mem(const char *src, size_t isz, char **dst, size_t osz,
		      unsigned int flags, const char *esc)
{
462 463
	char *p = *dst;
	char *end = p + osz;
464
	bool is_dict = esc && *esc;
465
	int ret;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

	while (isz--) {
		unsigned char c = *src++;

		/*
		 * Apply rules in the following sequence:
		 *	- the character is printable, when @flags has
		 *	  %ESCAPE_NP bit set
		 *	- the @esc string is supplied and does not contain a
		 *	  character under question
		 *	- 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.
		 */
		if ((flags & ESCAPE_NP && isprint(c)) ||
		    (is_dict && !strchr(esc, c))) {
			/* do nothing */
		} else {
485 486 487 488 489 490 491 492
			if (flags & ESCAPE_SPACE && escape_space(c, &p, end))
				continue;

			if (flags & ESCAPE_SPECIAL && escape_special(c, &p, end))
				continue;

			if (flags & ESCAPE_NULL && escape_null(c, &p, end))
				continue;
493 494

			/* ESCAPE_OCTAL and ESCAPE_HEX always go last */
495
			if (flags & ESCAPE_OCTAL && escape_octal(c, &p, end))
496
				continue;
497 498

			if (flags & ESCAPE_HEX && escape_hex(c, &p, end))
499 500 501
				continue;
		}

502
		escape_passthrough(c, &p, end);
503 504
	}

505 506 507 508
	if (p > end) {
		*dst = end;
		return -ENOMEM;
	}
509

510 511 512
	ret = p - *dst;
	*dst = p;
	return ret;
513 514
}
EXPORT_SYMBOL(string_escape_mem);