unc.c 1.4 KB
Newer Older
S
Steve French 已提交
1 2 3 4 5 6 7 8 9
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2020, Microsoft Corporation.
 *
 *   Author(s): Steve French <stfrench@microsoft.com>
 *              Suresh Jayaraman <sjayaraman@suse.de>
 *              Jeff Layton <jlayton@kernel.org>
 */

10
#include <linux/fs.h>
S
Steve French 已提交
11
#include <linux/slab.h>
12 13 14
#include <linux/inet.h>
#include <linux/ctype.h>
#include "cifsglob.h"
S
Steve French 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "cifsproto.h"

/* extract the host portion of the UNC string */
char *extract_hostname(const char *unc)
{
	const char *src;
	char *dst, *delim;
	unsigned int len;

	/* skip double chars at beginning of string */
	/* BB: check validity of these bytes? */
	if (strlen(unc) < 3)
		return ERR_PTR(-EINVAL);
	for (src = unc; *src && *src == '\\'; src++)
		;
	if (!*src)
		return ERR_PTR(-EINVAL);

	/* delimiter between hostname and sharename is always '\\' now */
	delim = strchr(src, '\\');
	if (!delim)
		return ERR_PTR(-EINVAL);

	len = delim - src;
	dst = kmalloc((len + 1), GFP_KERNEL);
	if (dst == NULL)
		return ERR_PTR(-ENOMEM);

	memcpy(dst, src, len);
	dst[len] = '\0';

	return dst;
}

char *extract_sharename(const char *unc)
{
	const char *src;
	char *delim, *dst;

	/* skip double chars at the beginning */
	src = unc + 2;

	/* share name is always preceded by '\\' now */
	delim = strchr(src, '\\');
	if (!delim)
		return ERR_PTR(-EINVAL);
	delim++;

	/* caller has to free the memory */
A
Al Viro 已提交
64
	dst = kstrdup(delim, GFP_KERNEL);
S
Steve French 已提交
65 66 67 68 69
	if (!dst)
		return ERR_PTR(-ENOMEM);

	return dst;
}