]>
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 if ( m_byteArray
.size() > 0 )
254 *count
= TXTRecordGetCount( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ] );
266 STDMETHODIMP
CTXTRecord::GetKeyAtIndex(ULONG index
, BSTR
* retval
)
274 const void * rawValue
;
278 DNSServiceErrorType err
;
286 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
288 require_action( !err
, exit
, hr
= S_FALSE
);
292 ok
= UTF8ToBSTR( keyBuf
, temp
);
294 require_action( ok
, exit
, hr
= S_FALSE
);
312 STDMETHODIMP
CTXTRecord::GetValueAtIndex(ULONG index
, VARIANT
* retval
)
320 const void * rawValue
;
324 DNSServiceErrorType err
;
332 err
= TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray
.size(), &m_byteArray
[ 0 ], ( uint16_t ) index
, sizeof( keyBuf
), keyBuf
, &rawValueLen
, &rawValue
);
334 require_action( !err
, exit
, hr
= S_FALSE
);
338 ok
= ByteArrayToVariant( rawValue
, rawValueLen
, retval
);
340 require_action( ok
, exit
, hr
= S_FALSE
);
362 const unsigned char * bytes
,
370 check ( bytes
!= NULL
);
376 m_byteArray
.reserve( len
);
378 m_byteArray
.assign( bytes
, bytes
+ len
);