2N/A/* -*- Mode: Java; tab-width: 4 -*-
2N/A *
2N/A * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
2N/A *
2N/A * Licensed under the Apache License, Version 2.0 (the "License");
2N/A * you may not use this file except in compliance with the License.
2N/A * You may obtain a copy of the License at
2N/A *
2N/A * http://www.apache.org/licenses/LICENSE-2.0
2N/A *
2N/A * Unless required by applicable law or agreed to in writing, software
2N/A * distributed under the License is distributed on an "AS IS" BASIS,
2N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2N/A * See the License for the specific language governing permissions and
2N/A * limitations under the License.
2N/A
2N/A Change History (most recent first):
2N/A
2N/A$Log: TXTRecord.java,v $
2N/ARevision 1.6 2006/08/14 23:25:08 cheshire
2N/ARe-licensed mDNSResponder daemon source code under Apache License, Version 2.0
2N/A
2N/ARevision 1.5 2004/08/25 21:54:36 rpantos
2N/A<rdar://problem/3773973> Fix getValue() for values containing '='.
2N/A
2N/ARevision 1.4 2004/08/04 01:04:50 rpantos
2N/A<rdar://problems/3731579&3731582> Fix set(); add remove() & toString().
2N/A
2N/ARevision 1.3 2004/07/13 21:24:25 rpantos
2N/AFix for <rdar://problem/3701120>.
2N/A
2N/ARevision 1.2 2004/04/30 21:48:27 rpantos
2N/AChange line endings for CVS.
2N/A
2N/ARevision 1.1 2004/04/30 16:29:35 rpantos
2N/AFirst checked in.
2N/A
2N/Aident "%Z%%M% %I% %E% SMI"
2N/A
2N/A To do:
2N/A - implement remove()
2N/A - fix set() to replace existing values
2N/A */
2N/A
2N/A
2N/Apackage com.apple.dnssd;
2N/A
2N/A
2N/A/**
2N/A Object used to construct and parse DNS-SD format TXT records.
2N/A For more info see <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-Based Service Discovery</a>, section 6.
2N/A*/
2N/A
2N/Apublic class TXTRecord
2N/A{
2N/A /*
2N/A DNS-SD specifies that a TXT record corresponding to an SRV record consist of
2N/A a packed array of bytes, each preceded by a length byte. Each string
2N/A is an attribute-value pair.
2N/A
2N/A The TXTRecord object stores the entire TXT data as a single byte array, traversing it
2N/A as need be to implement its various methods.
2N/A */
2N/A
2N/A static final protected byte kAttrSep = '=';
2N/A
2N/A protected byte[] fBytes;
2N/A
2N/A /** Constructs a new, empty TXT record. */
2N/A public TXTRecord()
2N/A { fBytes = new byte[0]; }
2N/A
2N/A /** Constructs a new TXT record from a byte array in the standard format. */
2N/A public TXTRecord( byte[] initBytes)
2N/A { fBytes = (byte[]) initBytes.clone(); }
2N/A
2N/A /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
2N/A @param key
2N/A The key name. Must be ASCII, with no '=' characters.
2N/A <P>
2N/A @param value
2N/A Value to be encoded into bytes using the default platform character set.
2N/A */
2N/A public void set( String key, String value)
2N/A {
2N/A byte[] valBytes = (value != null) ? value.getBytes() : null;
2N/A this.set( key, valBytes);
2N/A }
2N/A
2N/A /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
2N/A @param key
2N/A The key name. Must be ASCII, with no '=' characters.
2N/A <P>
2N/A @param value
2N/A Binary representation of the value.
2N/A */
2N/A public void set( String key, byte[] value)
2N/A {
2N/A byte[] keyBytes;
2N/A int valLen = (value != null) ? value.length : 0;
2N/A
2N/A try {
2N/A keyBytes = key.getBytes( "US-ASCII");
2N/A }
2N/A catch ( java.io.UnsupportedEncodingException uee) {
2N/A throw new IllegalArgumentException();
2N/A }
2N/A
2N/A for ( int i=0; i < keyBytes.length; i++)
2N/A if ( keyBytes[i] == '=')
2N/A throw new IllegalArgumentException();
2N/A
2N/A if ( keyBytes.length + valLen >= 255)
2N/A throw new ArrayIndexOutOfBoundsException();
2N/A
2N/A int prevLoc = this.remove( key);
2N/A if ( prevLoc == -1)
2N/A prevLoc = this.size();
2N/A
2N/A this.insert( keyBytes, value, prevLoc);
2N/A }
2N/A
2N/A protected void insert( byte[] keyBytes, byte[] value, int index)
2N/A // Insert a key-value pair at index
2N/A {
2N/A byte[] oldBytes = fBytes;
2N/A int valLen = (value != null) ? value.length : 0;
2N/A int insertion = 0;
2N/A byte newLen, avLen;
2N/A
2N/A // locate the insertion point
2N/A for ( int i=0; i < index && insertion < fBytes.length; i++)
2N/A insertion += fBytes[ insertion] + 1;
2N/A
2N/A avLen = (byte) ( keyBytes.length + valLen + (value != null ? 1 : 0));
2N/A newLen = (byte) ( avLen + oldBytes.length + 1);
2N/A
2N/A fBytes = new byte[ newLen];
2N/A System.arraycopy( oldBytes, 0, fBytes, 0, insertion);
2N/A int secondHalfLen = oldBytes.length - insertion;
2N/A System.arraycopy( oldBytes, insertion, fBytes, newLen - secondHalfLen, secondHalfLen);
2N/A fBytes[ insertion] = avLen;
2N/A System.arraycopy( keyBytes, 0, fBytes, insertion + 1, keyBytes.length);
2N/A if ( value != null)
2N/A {
2N/A fBytes[ insertion + 1 + keyBytes.length] = kAttrSep;
2N/A System.arraycopy( value, 0, fBytes, insertion + keyBytes.length + 2, valLen);
2N/A }
2N/A }
2N/A
2N/A /** Remove a key/value pair from the TXT record. Returns index it was at, or -1 if not found. */
2N/A public int remove( String key)
2N/A {
2N/A int avStart = 0;
2N/A
2N/A for ( int i=0; avStart < fBytes.length; i++)
2N/A {
2N/A int avLen = fBytes[ avStart];
2N/A if ( key.length() <= avLen &&
2N/A ( key.length() == avLen || fBytes[ avStart + key.length() + 1] == kAttrSep))
2N/A {
2N/A String s = new String( fBytes, avStart + 1, key.length());
2N/A if ( 0 == key.compareToIgnoreCase( s))
2N/A {
2N/A byte[] oldBytes = fBytes;
2N/A fBytes = new byte[ oldBytes.length - avLen - 1];
2N/A System.arraycopy( oldBytes, 0, fBytes, 0, avStart);
2N/A System.arraycopy( oldBytes, avStart + avLen + 1, fBytes, avStart, oldBytes.length - avStart - avLen - 1);
2N/A return i;
2N/A }
2N/A }
2N/A avStart += avLen + 1;
2N/A }
2N/A return -1;
2N/A }
2N/A
2N/A /** Return the number of keys in the TXT record. */
2N/A public int size()
2N/A {
2N/A int i, avStart;
2N/A
2N/A for ( i=0, avStart=0; avStart < fBytes.length; i++)
2N/A avStart += fBytes[ avStart] + 1;
2N/A return i;
2N/A }
2N/A
2N/A /** Return true if key is present in the TXT record, false if not. */
2N/A public boolean contains( String key)
2N/A {
2N/A String s = null;
2N/A
2N/A for ( int i=0; null != ( s = this.getKey( i)); i++)
2N/A if ( 0 == key.compareToIgnoreCase( s))
2N/A return true;
2N/A return false;
2N/A }
2N/A
2N/A /** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
2N/A public String getKey( int index)
2N/A {
2N/A int avStart = 0;
2N/A
2N/A for ( int i=0; i < index && avStart < fBytes.length; i++)
2N/A avStart += fBytes[ avStart] + 1;
2N/A
2N/A if ( avStart < fBytes.length)
2N/A {
2N/A int avLen = fBytes[ avStart];
2N/A int aLen = 0;
2N/A
2N/A for ( aLen=0; aLen < avLen; aLen++)
2N/A if ( fBytes[ avStart + aLen + 1] == kAttrSep)
2N/A break;
2N/A return new String( fBytes, avStart + 1, aLen);
2N/A }
2N/A return null;
2N/A }
2N/A
2N/A /**
2N/A Look up a key in the TXT record by zero-based index and return its value. <P>
2N/A Returns null if index exceeds the total number of keys.
2N/A Returns null if the key is present with no value.
2N/A */
2N/A public byte[] getValue( int index)
2N/A {
2N/A int avStart = 0;
2N/A byte[] value = null;
2N/A
2N/A for ( int i=0; i < index && avStart < fBytes.length; i++)
2N/A avStart += fBytes[ avStart] + 1;
2N/A
2N/A if ( avStart < fBytes.length)
2N/A {
2N/A int avLen = fBytes[ avStart];
2N/A int aLen = 0;
2N/A
2N/A for ( aLen=0; aLen < avLen; aLen++)
2N/A {
2N/A if ( fBytes[ avStart + aLen + 1] == kAttrSep)
2N/A {
2N/A value = new byte[ avLen - aLen - 1];
2N/A System.arraycopy( fBytes, avStart + aLen + 2, value, 0, avLen - aLen - 1);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A return value;
2N/A }
2N/A
2N/A /** Converts the result of getValue() to a string in the platform default character set. */
2N/A public String getValueAsString( int index)
2N/A {
2N/A byte[] value = this.getValue( index);
2N/A return value != null ? new String( value) : null;
2N/A }
2N/A
2N/A /** Get the value associated with a key. Will be null if the key is not defined.
2N/A Array will have length 0 if the key is defined with an = but no value.<P>
2N/A
2N/A @param forKey
2N/A The left-hand side of the key-value pair.
2N/A <P>
2N/A @return The binary representation of the value.
2N/A */
2N/A public byte[] getValue( String forKey)
2N/A {
2N/A String s = null;
2N/A int i;
2N/A
2N/A for ( i=0; null != ( s = this.getKey( i)); i++)
2N/A if ( 0 == forKey.compareToIgnoreCase( s))
2N/A return this.getValue( i);
2N/A return null;
2N/A }
2N/A
2N/A /** Converts the result of getValue() to a string in the platform default character set.<P>
2N/A
2N/A @param forKey
2N/A The left-hand side of the key-value pair.
2N/A <P>
2N/A @return The value represented in the default platform character set.
2N/A */
2N/A public String getValueAsString( String forKey)
2N/A {
2N/A byte[] val = this.getValue( forKey);
2N/A return val != null ? new String( val) : null;
2N/A }
2N/A
2N/A /** Return the contents of the TXT record as raw bytes. */
2N/A public byte[] getRawBytes() { return (byte[]) fBytes.clone(); }
2N/A
2N/A /** Return a string representation of the object. */
2N/A public String toString()
2N/A {
2N/A String a, result = null;
2N/A
2N/A for ( int i=0; null != ( a = this.getKey( i)); i++)
2N/A {
2N/A String av = String.valueOf( i) + "={" + a;
2N/A String val = this.getValueAsString( i);
2N/A if ( val != null)
2N/A av += "=" + val + "}";
2N/A else
2N/A av += "}";
2N/A if ( result == null)
2N/A result = av;
2N/A else
2N/A result = result + ", " + av;
2N/A }
2N/A return result != null ? result : "";
2N/A }
2N/A}
2N/A