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