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