15ffb0c9bSToomas Soome /* -*- Mode: C; tab-width: 4 -*-
25ffb0c9bSToomas Soome  *
3cda73f64SToomas Soome  * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
45ffb0c9bSToomas Soome  *
5cda73f64SToomas Soome  * Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
65ffb0c9bSToomas Soome  * ("Apple") in consideration of your agreement to the following terms, and your
75ffb0c9bSToomas Soome  * use, installation, modification or redistribution of this Apple software
85ffb0c9bSToomas Soome  * constitutes acceptance of these terms.  If you do not agree with these terms,
95ffb0c9bSToomas Soome  * please do not use, install, modify or redistribute this Apple software.
105ffb0c9bSToomas Soome  *
115ffb0c9bSToomas Soome  * In consideration of your agreement to abide by the following terms, and subject
125ffb0c9bSToomas Soome  * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
135ffb0c9bSToomas Soome  * copyrights in this original Apple software (the "Apple Software"), to use,
145ffb0c9bSToomas Soome  * reproduce, modify and redistribute the Apple Software, with or without
155ffb0c9bSToomas Soome  * modifications, in source and/or binary forms; provided that if you redistribute
165ffb0c9bSToomas Soome  * the Apple Software in its entirety and without modifications, you must retain
175ffb0c9bSToomas Soome  * this notice and the following text and disclaimers in all such redistributions of
185ffb0c9bSToomas Soome  * the Apple Software.  Neither the name, trademarks, service marks or logos of
19cda73f64SToomas Soome  * Apple Inc. may be used to endorse or promote products derived from the
205ffb0c9bSToomas Soome  * Apple Software without specific prior written permission from Apple.  Except as
215ffb0c9bSToomas Soome  * expressly stated in this notice, no other rights or licenses, express or implied,
225ffb0c9bSToomas Soome  * are granted by Apple herein, including but not limited to any patent rights that
235ffb0c9bSToomas Soome  * may be infringed by your derivative works or by other works in which the Apple
245ffb0c9bSToomas Soome  * Software may be incorporated.
255ffb0c9bSToomas Soome  *
265ffb0c9bSToomas Soome  * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
275ffb0c9bSToomas Soome  * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
285ffb0c9bSToomas Soome  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
295ffb0c9bSToomas Soome  * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
305ffb0c9bSToomas Soome  * COMBINATION WITH YOUR PRODUCTS.
315ffb0c9bSToomas Soome  *
325ffb0c9bSToomas Soome  * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
335ffb0c9bSToomas Soome  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
345ffb0c9bSToomas Soome  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355ffb0c9bSToomas Soome  * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
365ffb0c9bSToomas Soome  * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
375ffb0c9bSToomas Soome  * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
385ffb0c9bSToomas Soome  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
395ffb0c9bSToomas Soome  */
405ffb0c9bSToomas Soome 
415ffb0c9bSToomas Soome #include <ctype.h>
425ffb0c9bSToomas Soome #include <stdio.h>          // For stdout, stderr
435ffb0c9bSToomas Soome 
445ffb0c9bSToomas Soome #include "ClientCommon.h"
455ffb0c9bSToomas Soome 
GetNextLabel(const char * cstr,char label[64])465ffb0c9bSToomas Soome const char *GetNextLabel(const char *cstr, char label[64])
475ffb0c9bSToomas Soome {
485ffb0c9bSToomas Soome     char *ptr = label;
495ffb0c9bSToomas Soome     while (*cstr && *cstr != '.')               // While we have characters in the label...
505ffb0c9bSToomas Soome     {
515ffb0c9bSToomas Soome         char c = *cstr++;
52*c65ebfc7SToomas Soome         if (c == '\\')                          // If escape character, check next character
535ffb0c9bSToomas Soome         {
54*c65ebfc7SToomas Soome             if (*cstr == '\0') break;           // If this is the end of the string, then break
555ffb0c9bSToomas Soome             c = *cstr++;
565ffb0c9bSToomas Soome             if (isdigit(cstr[-1]) && isdigit(cstr[0]) && isdigit(cstr[1]))
575ffb0c9bSToomas Soome             {
585ffb0c9bSToomas Soome                 int v0 = cstr[-1] - '0';                        // then interpret as three-digit decimal
595ffb0c9bSToomas Soome                 int v1 = cstr[ 0] - '0';
605ffb0c9bSToomas Soome                 int v2 = cstr[ 1] - '0';
615ffb0c9bSToomas Soome                 int val = v0 * 100 + v1 * 10 + v2;
625ffb0c9bSToomas Soome                 // If valid three-digit decimal value, use it
635ffb0c9bSToomas Soome                 // Note that although ascii nuls are possible in DNS labels
645ffb0c9bSToomas Soome                 // we're building a C string here so we have no way to represent that
655ffb0c9bSToomas Soome                 if (val == 0) val = '-';
665ffb0c9bSToomas Soome                 if (val <= 255) { c = (char)val; cstr += 2; }
675ffb0c9bSToomas Soome             }
685ffb0c9bSToomas Soome         }
695ffb0c9bSToomas Soome         *ptr++ = c;
705ffb0c9bSToomas Soome         if (ptr >= label+64) { label[63] = 0; return(NULL); }   // Illegal label more than 63 bytes
715ffb0c9bSToomas Soome     }
725ffb0c9bSToomas Soome     *ptr = 0;                                                   // Null-terminate label text
735ffb0c9bSToomas Soome     if (ptr == label) return(NULL);                             // Illegal empty label
745ffb0c9bSToomas Soome     if (*cstr) cstr++;                                          // Skip over the trailing dot (if present)
755ffb0c9bSToomas Soome     return(cstr);
765ffb0c9bSToomas Soome }
77