]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/DLLX/TXTRecord.cpp
mDNSResponder-212.1.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / DLLX / TXTRecord.cpp
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 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.cpp,v $
20 Revision 1.1 2009/05/26 04:43:54 herscher
21 <rdar://problem/3948252> COM component that can be used with any .NET language and VB.
22
23
24 */
25
26 #include "stdafx.h"
27 #include "TXTRecord.h"
28 #include "StringServices.h"
29 #include <DebugServices.h>
30
31
32 // CTXTRecord
33
34
35 STDMETHODIMP CTXTRecord::SetValue(BSTR key, VARIANT value)
36 {
37 std::string keyUTF8;
38 ByteArray valueArray;
39 BOOL ok;
40 DNSServiceErrorType err;
41 HRESULT hr = S_OK;
42
43 if ( !m_allocated )
44 {
45 TXTRecordCreate( &m_tref, 0, NULL );
46 m_allocated = TRUE;
47 }
48
49 ok = BSTRToUTF8( key, keyUTF8 );
50 require_action( ok, exit, hr = S_FALSE );
51
52 ok = VariantToByteArray( &value, valueArray );
53 require_action( ok, exit, hr = S_FALSE );
54
55 err = TXTRecordSetValue( &m_tref, keyUTF8.c_str(), ( uint8_t ) valueArray.size(), &valueArray[ 0 ] );
56 require_action( !err, exit, hr = S_FALSE );
57
58 exit:
59
60 return hr;
61 }
62
63 STDMETHODIMP CTXTRecord::RemoveValue(BSTR key)
64 {
65 HRESULT hr = S_OK;
66
67 if ( m_allocated )
68 {
69 std::string keyUTF8;
70 BOOL ok;
71 DNSServiceErrorType err;
72
73 ok = BSTRToUTF8( key, keyUTF8 );
74 require_action( ok, exit, hr = S_FALSE );
75
76 err = TXTRecordRemoveValue( &m_tref, keyUTF8.c_str() );
77 require_action( !err, exit, hr = S_FALSE );
78 }
79
80 exit:
81
82 return hr;
83 }
84
85 STDMETHODIMP CTXTRecord::ContainsKey(BSTR key, VARIANT_BOOL* retval)
86 {
87 std::string keyUTF8;
88 int ret = 0;
89 HRESULT err = S_OK;
90
91 if ( m_byteArray.size() > 0 )
92 {
93 BOOL ok;
94
95 ok = BSTRToUTF8( key, keyUTF8 );
96 require_action( ok, exit, err = S_FALSE );
97
98 ret = TXTRecordContainsKey( ( uint16_t ) m_byteArray.size(), &m_byteArray[ 0 ], keyUTF8.c_str() );
99 }
100
101 *retval = ( ret ) ? VARIANT_TRUE : VARIANT_FALSE;
102
103 exit:
104
105 return err;
106 }
107
108 STDMETHODIMP CTXTRecord::GetValueForKey(BSTR key, VARIANT* value)
109 {
110 std::string keyUTF8;
111 const void * rawValue;
112 uint8_t rawValueLen;
113 BOOL ok = TRUE;
114 HRESULT hr = S_OK;
115
116 VariantClear( value );
117
118 if ( m_byteArray.size() > 0 )
119 {
120 ok = BSTRToUTF8( key, keyUTF8 );
121 require_action( ok, exit, hr = S_FALSE );
122
123 rawValue = TXTRecordGetValuePtr( ( uint16_t ) m_byteArray.size(), &m_byteArray[ 0 ], keyUTF8.c_str(), &rawValueLen );
124
125 if ( rawValue )
126 {
127 ok = ByteArrayToVariant( rawValue, rawValueLen, value );
128 require_action( ok, exit, hr = S_FALSE );
129 }
130 }
131
132 exit:
133
134 return hr;
135 }
136
137 STDMETHODIMP CTXTRecord::GetCount(ULONG* count)
138 {
139 *count = 0;
140
141 if ( m_byteArray.size() > 0 )
142 {
143 *count = TXTRecordGetCount( ( uint16_t ) m_byteArray.size(), &m_byteArray[ 0 ] );
144 }
145
146 return S_OK;
147 }
148
149 STDMETHODIMP CTXTRecord::GetKeyAtIndex(ULONG index, BSTR* retval)
150 {
151 char keyBuf[ 64 ];
152 uint8_t rawValueLen;
153 const void * rawValue;
154 CComBSTR temp;
155 DNSServiceErrorType err;
156 BOOL ok;
157 HRESULT hr = S_OK;
158
159 err = TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray.size(), &m_byteArray[ 0 ], ( uint16_t ) index, sizeof( keyBuf ), keyBuf, &rawValueLen, &rawValue );
160 require_action( !err, exit, hr = S_FALSE );
161
162 ok = UTF8ToBSTR( keyBuf, temp );
163 require_action( ok, exit, hr = S_FALSE );
164
165 *retval = temp;
166
167 exit:
168
169 return hr;
170 }
171
172 STDMETHODIMP CTXTRecord::GetValueAtIndex(ULONG index, VARIANT* retval)
173 {
174 char keyBuf[ 64 ];
175 uint8_t rawValueLen;
176 const void * rawValue;
177 CComBSTR temp;
178 DNSServiceErrorType err;
179 BOOL ok;
180 HRESULT hr = S_OK;
181
182 err = TXTRecordGetItemAtIndex( ( uint16_t ) m_byteArray.size(), &m_byteArray[ 0 ], ( uint16_t ) index, sizeof( keyBuf ), keyBuf, &rawValueLen, &rawValue );
183 require_action( !err, exit, hr = S_FALSE );
184
185 ok = ByteArrayToVariant( rawValue, rawValueLen, retval );
186 require_action( ok, exit, hr = S_FALSE );
187
188 exit:
189
190 return hr;
191 }
192
193
194 void
195 CTXTRecord::SetBytes
196 (
197 const unsigned char * bytes,
198 uint16_t len
199 )
200 {
201 check ( bytes != NULL );
202 check( len );
203
204 m_byteArray.reserve( len );
205 m_byteArray.assign( bytes, bytes + len );
206 }