2 * This file is part of DisOrder
3 * Copyright (C) 2009 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/validity.c
21 * @brief Various validity checks
28 /** @brief Parse a playlist name
29 * @param name Playlist name
30 * @param ownerp Where to put owner, or NULL
31 * @param sharep Where to put default sharing, or NULL
32 * @return 0 on success, -1 on error
34 * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part
35 * is alphanumeric and nonempty. USER is a username (see valid_username()).
37 int playlist_parse_name(const char *name,
40 const char *dot = strchr(name, '.'), *share;
45 owner = xstrndup(name, dot - name);
46 if(!valid_username(owner))
48 if(!valid_username(dot + 1))
53 if(!valid_username(name))
61 *sharep = xstrdup(share);
65 /** @brief Return non-zero for a valid username
66 * @param user Candidate username
67 * @return Nonzero if it's valid
69 * Currently we only allow the letters and digits in ASCII. We could be more
70 * liberal than this but it is a nice simple test. It is critical that
71 * semicolons are never allowed.
73 * NB also used by playlist_parse_name() to validate playlist names!
75 int valid_username(const char *user) {
79 const uint8_t c = *user++;
80 /* For now we are very strict */
81 if((c >= 'a' && c <= 'z')
82 || (c >= 'A' && c <= 'Z')
83 || (c >= '0' && c <= '9'))