]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/Java/TXTRecord.java
mDNSResponder-212.1.tar.gz
[apple/mdnsresponder.git] / mDNSShared / Java / TXTRecord.java
1 /* -*- Mode: Java; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 Change History (most recent first):
18
19 $Log: TXTRecord.java,v $
20 Revision 1.8 2007/03/16 23:39:40 vazquez
21 <rdar://problem/4612778> Java: Coding error in java wrappers, limited TXTRecord length
22
23 Revision 1.7 2006/12/13 07:13:23 mkrochma
24 <rdar://problem/4612778> Java: Coding error in java wrappers, limited TXTRecord length
25
26 Revision 1.6 2006/08/14 23:25:08 cheshire
27 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
28
29 Revision 1.5 2004/08/25 21:54:36 rpantos
30 <rdar://problem/3773973> Fix getValue() for values containing '='.
31
32 Revision 1.4 2004/08/04 01:04:50 rpantos
33 <rdar://problems/3731579&3731582> Fix set(); add remove() & toString().
34
35 Revision 1.3 2004/07/13 21:24:25 rpantos
36 Fix for <rdar://problem/3701120>.
37
38 Revision 1.2 2004/04/30 21:48:27 rpantos
39 Change line endings for CVS.
40
41 Revision 1.1 2004/04/30 16:29:35 rpantos
42 First checked in.
43
44 To do:
45 - implement remove()
46 - fix set() to replace existing values
47 */
48
49
50 package com.apple.dnssd;
51
52
53 /**
54 Object used to construct and parse DNS-SD format TXT records.
55 For more info see <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-Based Service Discovery</a>, section 6.
56 */
57
58 public class TXTRecord
59 {
60 /*
61 DNS-SD specifies that a TXT record corresponding to an SRV record consist of
62 a packed array of bytes, each preceded by a length byte. Each string
63 is an attribute-value pair.
64
65 The TXTRecord object stores the entire TXT data as a single byte array, traversing it
66 as need be to implement its various methods.
67 */
68
69 static final protected byte kAttrSep = '=';
70
71 protected byte[] fBytes;
72
73 /** Constructs a new, empty TXT record. */
74 public TXTRecord()
75 { fBytes = new byte[0]; }
76
77 /** Constructs a new TXT record from a byte array in the standard format. */
78 public TXTRecord( byte[] initBytes)
79 { fBytes = (byte[]) initBytes.clone(); }
80
81 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
82 @param key
83 The key name. Must be ASCII, with no '=' characters.
84 <P>
85 @param value
86 Value to be encoded into bytes using the default platform character set.
87 */
88 public void set( String key, String value)
89 {
90 byte[] valBytes = (value != null) ? value.getBytes() : null;
91 this.set( key, valBytes);
92 }
93
94 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
95 @param key
96 The key name. Must be ASCII, with no '=' characters.
97 <P>
98 @param value
99 Binary representation of the value.
100 */
101 public void set( String key, byte[] value)
102 {
103 byte[] keyBytes;
104 int valLen = (value != null) ? value.length : 0;
105
106 try {
107 keyBytes = key.getBytes( "US-ASCII");
108 }
109 catch ( java.io.UnsupportedEncodingException uee) {
110 throw new IllegalArgumentException();
111 }
112
113 for ( int i=0; i < keyBytes.length; i++)
114 if ( keyBytes[i] == '=')
115 throw new IllegalArgumentException();
116
117 if ( keyBytes.length + valLen >= 255)
118 throw new ArrayIndexOutOfBoundsException();
119
120 int prevLoc = this.remove( key);
121 if ( prevLoc == -1)
122 prevLoc = this.size();
123
124 this.insert( keyBytes, value, prevLoc);
125 }
126
127 protected void insert( byte[] keyBytes, byte[] value, int index)
128 // Insert a key-value pair at index
129 {
130 byte[] oldBytes = fBytes;
131 int valLen = (value != null) ? value.length : 0;
132 int insertion = 0;
133 int newLen, avLen;
134
135 // locate the insertion point
136 for ( int i=0; i < index && insertion < fBytes.length; i++)
137 insertion += (0xFF & (fBytes[ insertion] + 1));
138
139 avLen = keyBytes.length + valLen + (value != null ? 1 : 0);
140 newLen = avLen + oldBytes.length + 1;
141
142 fBytes = new byte[ newLen];
143 System.arraycopy( oldBytes, 0, fBytes, 0, insertion);
144 int secondHalfLen = oldBytes.length - insertion;
145 System.arraycopy( oldBytes, insertion, fBytes, newLen - secondHalfLen, secondHalfLen);
146 fBytes[ insertion] = ( byte) avLen;
147 System.arraycopy( keyBytes, 0, fBytes, insertion + 1, keyBytes.length);
148 if ( value != null)
149 {
150 fBytes[ insertion + 1 + keyBytes.length] = kAttrSep;
151 System.arraycopy( value, 0, fBytes, insertion + keyBytes.length + 2, valLen);
152 }
153 }
154
155 /** Remove a key/value pair from the TXT record. Returns index it was at, or -1 if not found. */
156 public int remove( String key)
157 {
158 int avStart = 0;
159
160 for ( int i=0; avStart < fBytes.length; i++)
161 {
162 int avLen = fBytes[ avStart];
163 if ( key.length() <= avLen &&
164 ( key.length() == avLen || fBytes[ avStart + key.length() + 1] == kAttrSep))
165 {
166 String s = new String( fBytes, avStart + 1, key.length());
167 if ( 0 == key.compareToIgnoreCase( s))
168 {
169 byte[] oldBytes = fBytes;
170 fBytes = new byte[ oldBytes.length - avLen - 1];
171 System.arraycopy( oldBytes, 0, fBytes, 0, avStart);
172 System.arraycopy( oldBytes, avStart + avLen + 1, fBytes, avStart, oldBytes.length - avStart - avLen - 1);
173 return i;
174 }
175 }
176 avStart += (0xFF & (avLen + 1));
177 }
178 return -1;
179 }
180
181 /** Return the number of keys in the TXT record. */
182 public int size()
183 {
184 int i, avStart;
185
186 for ( i=0, avStart=0; avStart < fBytes.length; i++)
187 avStart += (0xFF & (fBytes[ avStart] + 1));
188 return i;
189 }
190
191 /** Return true if key is present in the TXT record, false if not. */
192 public boolean contains( String key)
193 {
194 String s = null;
195
196 for ( int i=0; null != ( s = this.getKey( i)); i++)
197 if ( 0 == key.compareToIgnoreCase( s))
198 return true;
199 return false;
200 }
201
202 /** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
203 public String getKey( int index)
204 {
205 int avStart = 0;
206
207 for ( int i=0; i < index && avStart < fBytes.length; i++)
208 avStart += fBytes[ avStart] + 1;
209
210 if ( avStart < fBytes.length)
211 {
212 int avLen = fBytes[ avStart];
213 int aLen = 0;
214
215 for ( aLen=0; aLen < avLen; aLen++)
216 if ( fBytes[ avStart + aLen + 1] == kAttrSep)
217 break;
218 return new String( fBytes, avStart + 1, aLen);
219 }
220 return null;
221 }
222
223 /**
224 Look up a key in the TXT record by zero-based index and return its value. <P>
225 Returns null if index exceeds the total number of keys.
226 Returns null if the key is present with no value.
227 */
228 public byte[] getValue( int index)
229 {
230 int avStart = 0;
231 byte[] value = null;
232
233 for ( int i=0; i < index && avStart < fBytes.length; i++)
234 avStart += fBytes[ avStart] + 1;
235
236 if ( avStart < fBytes.length)
237 {
238 int avLen = fBytes[ avStart];
239 int aLen = 0;
240
241 for ( aLen=0; aLen < avLen; aLen++)
242 {
243 if ( fBytes[ avStart + aLen + 1] == kAttrSep)
244 {
245 value = new byte[ avLen - aLen - 1];
246 System.arraycopy( fBytes, avStart + aLen + 2, value, 0, avLen - aLen - 1);
247 break;
248 }
249 }
250 }
251 return value;
252 }
253
254 /** Converts the result of getValue() to a string in the platform default character set. */
255 public String getValueAsString( int index)
256 {
257 byte[] value = this.getValue( index);
258 return value != null ? new String( value) : null;
259 }
260
261 /** Get the value associated with a key. Will be null if the key is not defined.
262 Array will have length 0 if the key is defined with an = but no value.<P>
263
264 @param forKey
265 The left-hand side of the key-value pair.
266 <P>
267 @return The binary representation of the value.
268 */
269 public byte[] getValue( String forKey)
270 {
271 String s = null;
272 int i;
273
274 for ( i=0; null != ( s = this.getKey( i)); i++)
275 if ( 0 == forKey.compareToIgnoreCase( s))
276 return this.getValue( i);
277 return null;
278 }
279
280 /** Converts the result of getValue() to a string in the platform default character set.<P>
281
282 @param forKey
283 The left-hand side of the key-value pair.
284 <P>
285 @return The value represented in the default platform character set.
286 */
287 public String getValueAsString( String forKey)
288 {
289 byte[] val = this.getValue( forKey);
290 return val != null ? new String( val) : null;
291 }
292
293 /** Return the contents of the TXT record as raw bytes. */
294 public byte[] getRawBytes() { return (byte[]) fBytes.clone(); }
295
296 /** Return a string representation of the object. */
297 public String toString()
298 {
299 String a, result = null;
300
301 for ( int i=0; null != ( a = this.getKey( i)); i++)
302 {
303 String av = String.valueOf( i) + "={" + a;
304 String val = this.getValueAsString( i);
305 if ( val != null)
306 av += "=" + val + "}";
307 else
308 av += "}";
309 if ( result == null)
310 result = av;
311 else
312 result = result + ", " + av;
313 }
314 return result != null ? result : "";
315 }
316 }
317