1dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt/* -*- Mode: C; tab-width: 4 -*-
2dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt *
3dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * Copyright (c) 2008 Apple Inc. All rights reserved.
4dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt *
5dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
6dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * ("Apple") in consideration of your agreement to the following terms, and your
7dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * use, installation, modification or redistribution of this Apple software
8dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * constitutes acceptance of these terms.  If you do not agree with these terms,
9dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * please do not use, install, modify or redistribute this Apple software.
10dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt *
11dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * In consideration of your agreement to abide by the following terms, and subject
12dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
13dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * copyrights in this original Apple software (the "Apple Software"), to use,
14dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * reproduce, modify and redistribute the Apple Software, with or without
15dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * modifications, in source and/or binary forms; provided that if you redistribute
16dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * the Apple Software in its entirety and without modifications, you must retain
17dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * this notice and the following text and disclaimers in all such redistributions of
18dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * the Apple Software.  Neither the name, trademarks, service marks or logos of
19dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * Apple Computer, Inc. may be used to endorse or promote products derived from the
20dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * Apple Software without specific prior written permission from Apple.  Except as
21dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * expressly stated in this notice, no other rights or licenses, express or implied,
22dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * are granted by Apple herein, including but not limited to any patent rights that
23dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * may be infringed by your derivative works or by other works in which the Apple
24dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * Software may be incorporated.
25dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt *
26dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
27dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
28dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
30dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * COMBINATION WITH YOUR PRODUCTS.
31dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt *
32dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
33dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
36dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
37dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
38dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt */
40dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt
41dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt#include <ctype.h>
42dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt#include <stdio.h>			// For stdout, stderr
43dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt
44dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt#include "ClientCommon.h"
45dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt
46dd52342e088715929196886494c5055f9186d5c3Robert Greenwaltconst char *GetNextLabel(const char *cstr, char label[64])
47dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	{
48dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	char *ptr = label;
49dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	while (*cstr && *cstr != '.')				// While we have characters in the label...
50dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		{
51dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		char c = *cstr++;
52dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		if (c == '\\' && *cstr)					// If we have a backslash, and it's not the last character of the string
53dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt			{
54dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt			c = *cstr++;
55dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt			if (isdigit(cstr[-1]) && isdigit(cstr[0]) && isdigit(cstr[1]))
56dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				{
57dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				int v0 = cstr[-1] - '0';						// then interpret as three-digit decimal
58dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				int v1 = cstr[ 0] - '0';
59dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				int v2 = cstr[ 1] - '0';
60dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				int val = v0 * 100 + v1 * 10 + v2;
61dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				// If valid three-digit decimal value, use it
62dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				// Note that although ascii nuls are possible in DNS labels
63dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				// we're building a C string here so we have no way to represent that
64dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				if (val == 0) val = '-';
65dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				if (val <= 255) { c = (char)val; cstr += 2; }
66dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt				}
67dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt			}
68dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		*ptr++ = c;
69dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		if (ptr >= label+64) { label[63] = 0; return(NULL); }	// Illegal label more than 63 bytes
70dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt		}
71dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	*ptr = 0;													// Null-terminate label text
72dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	if (ptr == label) return(NULL);								// Illegal empty label
73dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	if (*cstr) cstr++;											// Skip over the trailing dot (if present)
74dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	return(cstr);
75dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt	}
76