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