aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/enumXmlBuffer.cpp
blob: 9e782a0bd089e782a1ce0af5ba769d9ddd05d68d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "main.h"
#include "./enumXmlBuffer.h"
#include "./service.h"
#include "./ifc_omservicehost.h"
#include "./ifc_omstorage.h"
#include "./ifc_wasabihelper.h"
#include "../xml/obj_xml.h"

#include <shlwapi.h>
#include <strsafe.h>

#define XMLSIG		"<?xml"
#define XMLSIG_LEN	5

	ULONG ref;
	const void *buffer;
	size_t bufferSize;
	size_t cursor;
	obj_xml *reader;
	INT readerError;
	XmlResponseParser parser;
	Dispatchable *bufferOwner;

EnumXmlBuffer::EnumXmlBuffer(obj_xml *xmlReader, const void *buffer, size_t bufferSize, Dispatchable *bufferOwner, ifc_omservicehost *serviceHost)
	: ref(1), buffer(buffer), bufferSize(bufferSize), cursor(0), reader(xmlReader), readerError(OBJ_XML_SUCCESS), bufferOwner(bufferOwner)
{
	if (NULL != reader) 
		reader->AddRef();

	if (NULL != bufferOwner)
		bufferOwner->AddRef();

	parser.Initialize(reader, serviceHost);
}

EnumXmlBuffer::~EnumXmlBuffer()
{
	parser.Finish();

	if (NULL != reader)
	{
		if (OBJ_XML_SUCCESS == readerError)
		{
			reader->xmlreader_feed(0, 0);
		}
		reader->xmlreader_close();
		ifc_wasabihelper *wasabi;
		if (SUCCEEDED(Plugin_GetWasabiHelper(&wasabi)))
		{
			wasabi->ReleaseWasabiInterface(&obj_xmlGUID, reader);
			wasabi->Release();
		}
	}

	if (NULL != bufferOwner)
		bufferOwner->Release();
}

HRESULT EnumXmlBuffer::CheckXmlHeader(const void *buffer, size_t bufferSize)
{
	if (NULL == buffer) 
		return E_INVALIDARG;

	if (bufferSize >= XMLSIG_LEN)
	{
		if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, XMLSIG, XMLSIG_LEN, (LPSTR)buffer, XMLSIG_LEN) ||
			CSTR_EQUAL == CompareStringW(CSTR_INVARIANT, NORM_IGNORECASE, WTEXT(XMLSIG), XMLSIG_LEN, (LPWSTR)buffer, XMLSIG_LEN))
		{
			return S_OK;
		}
	}
	return OMSTORAGE_E_UNKNOWN_FORMAT;
}

HRESULT EnumXmlBuffer::CreateInstance(const void *buffer, size_t bufferSize, Dispatchable *bufferOwner, ifc_omservicehost *host, EnumXmlBuffer **instance)
{
	if (NULL == instance) return E_POINTER;
	*instance = NULL;

		
	HRESULT hr = CheckXmlHeader(buffer, bufferSize);
	
	if (SUCCEEDED(hr))
	{		
		ifc_wasabihelper *wasabi;
		hr = Plugin_GetWasabiHelper(&wasabi);
        if (SUCCEEDED(hr))
		{		
			obj_xml *reader;
			wasabi->QueryWasabiInterface(&obj_xmlGUID, (void**)&reader);
			
			if (NULL != reader && OBJ_XML_SUCCESS == reader->xmlreader_open())
			{
				*instance = new EnumXmlBuffer(reader, buffer, bufferSize, bufferOwner, host);
				if (NULL == *instance) 
				{
					hr = E_OUTOFMEMORY;
					// reader stil has no support for AddRef()/Release()
					wasabi->ReleaseWasabiInterface(&obj_xmlGUID, reader);

				}
				reader->Release();
			}
			else 
			{
				hr = E_UNEXPECTED;
			}
			wasabi->Release();
		}
	}
	return hr;
}

size_t EnumXmlBuffer::AddRef()
{
	return InterlockedIncrement((LONG*)&ref);
}

size_t EnumXmlBuffer::Release()
{
	if (0 == ref)
		return ref;
	
	LONG r = InterlockedDecrement((LONG*)&ref);
	if (0 == r)
		delete(this);
	
	return r;
}

int EnumXmlBuffer::QueryInterface(GUID interface_guid, void **object)
{
	if (NULL == object) return E_POINTER;
	
	if (IsEqualIID(interface_guid, IFC_OmServiceEnum))
		*object = static_cast<ifc_omserviceenum*>(this);
	else if (IsEqualIID(interface_guid, IFC_OmXmlServiceEnum))
		*object = static_cast<ifc_omxmlserviceenum*>(this);
	else
	{
		*object = NULL;
		return E_NOINTERFACE;
	}

	if (NULL == *object)
		return E_UNEXPECTED;

	AddRef();
	return S_OK;
}

HRESULT EnumXmlBuffer::Next(ULONG listSize, ifc_omservice **elementList, ULONG *elementCount)
{
	if(NULL != elementCount)
		*elementCount = 0;
	
	if (0 == listSize || NULL == elementList) 
		return E_INVALIDARG;
	
	ULONG counter = 0;
	HRESULT hr = S_OK;
	ifc_omservice *service;

	while(counter < listSize)
	{
		hr = parser.PeekService(&service);
		if (FAILED(hr)) return hr;
		if (S_OK == hr)
		{
			elementList[counter] = service;
			counter++;
		}
		else
		{
			if (cursor < bufferSize)
			{
				readerError = reader->xmlreader_feed((BYTE*)buffer + cursor, bufferSize - cursor);
				if (OBJ_XML_SUCCESS != readerError) return E_FAIL;	
				cursor = bufferSize;
			}
			else
			{
				if (OBJ_XML_SUCCESS == readerError)
					reader->xmlreader_feed(0, 0);
				break;
			}

			
		}

	}

	if(NULL != elementCount)
		*elementCount = counter;
			
	return (counter > 0) ? S_OK : S_FALSE;
}

HRESULT EnumXmlBuffer::Reset(void)
{
	cursor = 0;
	readerError = OBJ_XML_SUCCESS;
	reader->xmlreader_reset();
	parser.Reset();
	return E_NOTIMPL;
}

HRESULT EnumXmlBuffer::Skip(ULONG elementCount)
{
	return E_NOTIMPL;
}

HRESULT EnumXmlBuffer::GetStatusCode(UINT *code)
{
	return parser.GetCode(code);
}

HRESULT EnumXmlBuffer::GetStatusText(LPWSTR pszBuffer, UINT cchBufferMax)
{
	return parser.GetText(pszBuffer, cchBufferMax);
}

#define CBCLASS EnumXmlBuffer
START_MULTIPATCH;
 START_PATCH(MPIID_OMSERVICEENUM)

  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, ADDREF, AddRef);
  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, RELEASE, Release);
  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, QUERYINTERFACE, QueryInterface);
  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, API_NEXT, Next);
  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, API_RESET, Reset);
  M_CB(MPIID_OMSERVICEENUM, ifc_omserviceenum, API_SKIP, Skip);
   
 NEXT_PATCH(MPIID_OMXMLSERVICEENUM)
  M_CB(MPIID_OMXMLSERVICEENUM, ifc_omxmlserviceenum, ADDREF, AddRef);
  M_CB(MPIID_OMXMLSERVICEENUM, ifc_omxmlserviceenum, RELEASE, Release);
  M_CB(MPIID_OMXMLSERVICEENUM, ifc_omxmlserviceenum, QUERYINTERFACE, QueryInterface);
  M_CB(MPIID_OMXMLSERVICEENUM, ifc_omxmlserviceenum, API_GETSTATUSCODE, GetStatusCode);
  M_CB(MPIID_OMXMLSERVICEENUM, ifc_omxmlserviceenum, API_GETSTATUSTEXT, GetStatusText);
 
 END_PATCH
END_MULTIPATCH;
#undef CBCLASS