Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / fs / cifs / unc.c
blob394aa00cea40e77427be39320652b0b4197a5399
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020, Microsoft Corporation.
5 * Author(s): Steve French <stfrench@microsoft.com>
6 * Suresh Jayaraman <sjayaraman@suse.de>
7 * Jeff Layton <jlayton@kernel.org>
8 */
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/inet.h>
13 #include <linux/ctype.h>
14 #include "cifsglob.h"
15 #include "cifsproto.h"
17 /* extract the host portion of the UNC string */
18 char *extract_hostname(const char *unc)
20 const char *src;
21 char *dst, *delim;
22 unsigned int len;
24 /* skip double chars at beginning of string */
25 /* BB: check validity of these bytes? */
26 if (strlen(unc) < 3)
27 return ERR_PTR(-EINVAL);
28 for (src = unc; *src && *src == '\\'; src++)
30 if (!*src)
31 return ERR_PTR(-EINVAL);
33 /* delimiter between hostname and sharename is always '\\' now */
34 delim = strchr(src, '\\');
35 if (!delim)
36 return ERR_PTR(-EINVAL);
38 len = delim - src;
39 dst = kmalloc((len + 1), GFP_KERNEL);
40 if (dst == NULL)
41 return ERR_PTR(-ENOMEM);
43 memcpy(dst, src, len);
44 dst[len] = '\0';
46 return dst;
49 char *extract_sharename(const char *unc)
51 const char *src;
52 char *delim, *dst;
53 int len;
55 /* skip double chars at the beginning */
56 src = unc + 2;
58 /* share name is always preceded by '\\' now */
59 delim = strchr(src, '\\');
60 if (!delim)
61 return ERR_PTR(-EINVAL);
62 delim++;
63 len = strlen(delim);
65 /* caller has to free the memory */
66 dst = kstrndup(delim, len, GFP_KERNEL);
67 if (!dst)
68 return ERR_PTR(-ENOMEM);
70 return dst;