]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/DLLX/TXTRecord.cpp
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "TXTRecord.h"
24 #include "StringServices.h"
26 #include <DebugServices.h>
38 STDMETHODIMP
CTXTRecord::SetValue(BSTR key
, VARIANT value
)
48 DNSServiceErrorType err
;
58 TXTRecordCreate( &m_tref
, 0, NULL
);
66 ok
= BSTRToUTF8( key
, keyUTF8
);
68 require_action( ok
, exit
, hr
= S_FALSE
);
72 ok
= VariantToByteArray( &value
, valueArray
);
74 require_action( ok
, exit
, hr
= S_FALSE
);
78 err
= TXTRecordSetValue( &m_tref
, keyUTF8
.c_str(), ( uint8_t ) valueArray
.size(), &valueArray
[ 0 ] );
80 require_action( !err
, exit
, hr
= S_FALSE
);
94 STDMETHODIMP
CTXTRecord::RemoveValue(BSTR key
)
110 DNSServiceErrorType err
;
114 ok
= BSTRToUTF8( key
, keyUTF8
);
116 require_action( ok
, exit
, hr
= S_FALSE
);
120 err
= TXTRecordRemoveValue( &m_tref
, keyUTF8
.c_str() );
122 require_action( !err
, exit
, hr
= S_FALSE
);
138 STDMETHODIMP
CTXTRecord::ContainsKey(BSTR key
, VARIANT_BOOL
* retval
)
150 if ( m_byteArray
.size() > 0 )
158 ok
= BSTRToUTF8( key
, keyUTF8
);
160 require_action( ok
, exit
, err
= S_FALSE
);
164 ret
= TXTRecordContainsKey( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], keyUTF8
.c_str() );
170 *retval
= ( ret
) ? VARIANT_TRUE
: VARIANT_FALSE
;
184 STDMETHODIMP
CTXTRecord::GetValueForKey(BSTR key
, VARIANT
* value
)
190 const void * rawValue
;
200 VariantClear( value
);
204 if ( m_byteArray
.size() > 0 )
208 ok
= BSTRToUTF8( key
, keyUTF8
);
210 require_action( ok
, exit
, hr
= S_FALSE
);
214 rawValue
= TXTRecordGetValuePtr( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], keyUTF8
.c_str(), &rawValueLen
);
222 ok
= ByteArrayToVariant( rawValue
, rawValueLen
, value
);
224 require_action( ok
, exit
, hr
= S_FALSE
);
242 STDMETHODIMP
CTXTRecord::GetCount(ULONG
* count
)
250 *count
= TXTRecordGetCount( TXTRecordGetLength( &m_tref
), TXTRecordGetBytesPtr( &m_tref
) );
252 else if ( m_byteArray
.size() > 0 )
255 *count
= TXTRecordGetCount( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ] );
267 STDMETHODIMP
CTXTRecord::GetKeyAtIndex(ULONG index
, BSTR
* retval
)
275 const void * rawValue
;
279 DNSServiceErrorType err
;
287 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
289 require_action( !err
, exit
, hr
= S_FALSE
);
293 ok
= UTF8ToBSTR( keyBuf
, temp
);
295 require_action( ok
, exit
, hr
= S_FALSE
);
313 STDMETHODIMP
CTXTRecord::GetValueAtIndex(ULONG index
, VARIANT
* retval
)
321 const void * rawValue
;
325 DNSServiceErrorType err
;
333 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
335 require_action( !err
, exit
, hr
= S_FALSE
);
339 ok
= ByteArrayToVariant( rawValue
, rawValueLen
, retval
);
341 require_action( ok
, exit
, hr
= S_FALSE
);
363 const unsigned char * bytes
,
371 check ( bytes
!= NULL
);
377 m_byteArray
.reserve( len
);
379 m_byteArray
.assign( bytes
, bytes
+ len
);