LibreOffice Module ucb (master)  1
LockSequence.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #include <config_lgpl.h>
30 #include <string.h>
31 #include <ne_xml.h>
32 #include "LockSequence.hxx"
33 #include <memory>
34 #include <sal/log.hxx>
35 
36 using namespace webdav_ucp;
37 using namespace com::sun::star;
38 
40 {
41  std::unique_ptr<ucb::Lock> pLock;
44  bool hasDepth;
45  bool hasHREF;
46  bool hasTimeout;
47 
49  : hasLockScope( false ), hasLockType( false ),
50  hasDepth( false ), hasHREF( false ), hasTimeout( false ) {}
51 };
52 
53 #define STATE_TOP (1)
54 
55 #define STATE_ACTIVELOCK (STATE_TOP)
56 #define STATE_LOCKSCOPE (STATE_TOP + 1)
57 #define STATE_LOCKTYPE (STATE_TOP + 2)
58 #define STATE_DEPTH (STATE_TOP + 3)
59 #define STATE_OWNER (STATE_TOP + 4)
60 #define STATE_TIMEOUT (STATE_TOP + 5)
61 #define STATE_LOCKTOKEN (STATE_TOP + 6)
62 #define STATE_EXCLUSIVE (STATE_TOP + 7)
63 #define STATE_SHARED (STATE_TOP + 8)
64 #define STATE_WRITE (STATE_TOP + 9)
65 #define STATE_HREF (STATE_TOP + 10)
66 
67 
68 extern "C" {
69 
71  void *,
72  int parent,
73  const char * /*nspace*/,
74  const char *name,
75  const char ** )
76 {
77  if ( name != nullptr )
78  {
79  switch ( parent )
80  {
81  case NE_XML_STATEROOT:
82  if ( strcmp( name, "activelock" ) == 0 )
83  return STATE_ACTIVELOCK;
84  break;
85 
86  case STATE_ACTIVELOCK:
87  if ( strcmp( name, "lockscope" ) == 0 )
88  return STATE_LOCKSCOPE;
89  else if ( strcmp( name, "locktype" ) == 0 )
90  return STATE_LOCKTYPE;
91  else if ( strcmp( name, "depth" ) == 0 )
92  return STATE_DEPTH;
93  else if ( strcmp( name, "owner" ) == 0 )
94  return STATE_OWNER;
95  else if ( strcmp( name, "timeout" ) == 0 )
96  return STATE_TIMEOUT;
97  else if ( strcmp( name, "locktoken" ) == 0 )
98  return STATE_LOCKTOKEN;
99  break;
100 
101  case STATE_LOCKSCOPE:
102  if ( strcmp( name, "exclusive" ) == 0 )
103  return STATE_EXCLUSIVE;
104  else if ( strcmp( name, "shared" ) == 0 )
105  return STATE_SHARED;
106  break;
107 
108  case STATE_LOCKTYPE:
109  if ( strcmp( name, "write" ) == 0 )
110  return STATE_WRITE;
111  break;
112 
113  case STATE_LOCKTOKEN:
114  if ( strcmp( name, "href" ) == 0 )
115  return STATE_HREF;
116  break;
117 
118  case STATE_OWNER:
119  // owner elem contains ANY. Accept anything; no state change.
120  return STATE_OWNER;
121  }
122  }
123  return NE_XML_DECLINE;
124 }
125 
126 
128  void *userdata,
129  int state,
130  const char *buf,
131  size_t len )
132 {
134  = static_cast< LockSequenceParseContext * >( userdata );
135  if ( !pCtx->pLock )
136  pCtx->pLock.reset( new ucb::Lock );
137 
138  // Beehive sends XML values containing trailing newlines.
139  if ( buf[ len - 1 ] == 0x0a )
140  len--;
141 
142  switch ( state )
143  {
144  case STATE_DEPTH:
145  if ( rtl_str_compareIgnoreAsciiCase_WithLength(
146  buf, len, "0", 1 ) == 0 )
147  {
148  pCtx->pLock->Depth = ucb::LockDepth_ZERO;
149  pCtx->hasDepth = true;
150  }
151  else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
152  buf, len, "1", 1 ) == 0 )
153  {
154  pCtx->pLock->Depth = ucb::LockDepth_ONE;
155  pCtx->hasDepth = true;
156  }
157  else if ( rtl_str_compareIgnoreAsciiCase_WithLength(
158  buf, len, "infinity", 8 ) == 0 )
159  {
160  pCtx->pLock->Depth = ucb::LockDepth_INFINITY;
161  pCtx->hasDepth = true;
162  }
163  else
164  SAL_WARN( "ucb.ucp.webdav", "LockSequence_chardata_callback - Unknown depth!" );
165  break;
166 
167  case STATE_OWNER:
168  {
169  // collect raw XML data... (owner contains ANY)
170  OUString aValue;
171  pCtx->pLock->Owner >>= aValue;
172  aValue += OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
173  pCtx->pLock->Owner <<= aValue;
174  break;
175  }
176 
177  case STATE_TIMEOUT:
178 
179  // RFC2518, RFC2616:
180 
181  // TimeType = ("Second-" DAVTimeOutVal | "Infinite" | Other)
182  // DAVTimeOutVal = 1*digit
183  // Other = "Extend" field-value
184  // field-value = *( field-content | LWS )
185  // field-content = <the OCTETs making up the field-value
186  // and consisting of either *TEXT or combinations
187  // of token, separators, and quoted-string>
188  //
189  // RFC4918, <http://tools.ietf.org/html/rfc4918#section-10.7>
190  // "The timeout value for TimeType "Second" MUST
191  // NOT be greater than 2^32-1."
192 
193  if ( rtl_str_compareIgnoreAsciiCase_WithLength(
194  buf, len, "Infinite", 8 ) == 0 )
195  {
196  pCtx->pLock->Timeout = sal_Int64( -1 );
197  pCtx->hasTimeout = true;
198  }
199  else if ( rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
200  buf, len, "Second-", 7, 7 ) == 0 )
201  {
202  pCtx->pLock->Timeout
203  = OString( buf + 7, len - 7 ).toInt64();
204  pCtx->hasTimeout = true;
205  }
206 // else if ( rtl_str_shortenedCompareIgnoreCase_WithLength(
207 // buf, len, "Extend", 6, 6 ) == 0 )
208 // {
209 // @@@
210 // }
211  else
212  {
213  pCtx->pLock->Timeout = sal_Int64( -1 );
214  pCtx->hasTimeout = true;
215  SAL_WARN( "ucb.ucp.webdav", "LockSequence_chardata_callback - Unknown timeout!" );
216  }
217  break;
218 
219  case STATE_HREF:
220  {
221  // collect hrefs.
222  sal_Int32 nPos = pCtx->pLock->LockTokens.getLength();
223  pCtx->pLock->LockTokens.realloc( nPos + 1 );
224  pCtx->pLock->LockTokens[ nPos ]
225  = OUString( buf, len, RTL_TEXTENCODING_ASCII_US );
226  pCtx->hasHREF = true;
227  break;
228  }
229 
230  }
231 
232  return 0; // zero to continue, non-zero to abort parsing
233 }
234 
235 
237  void *userdata,
238  int state,
239  const char *,
240  const char * )
241 {
243  = static_cast< LockSequenceParseContext * >( userdata );
244  if ( !pCtx->pLock )
245  pCtx->pLock.reset( new ucb::Lock );
246 
247  switch ( state )
248  {
249  case STATE_EXCLUSIVE:
250  pCtx->pLock->Scope = ucb::LockScope_EXCLUSIVE;
251  pCtx->hasLockScope = true;
252  break;
253 
254  case STATE_SHARED:
255  pCtx->pLock->Scope = ucb::LockScope_SHARED;
256  pCtx->hasLockScope = true;
257  break;
258 
259  case STATE_WRITE:
260  pCtx->pLock->Type = ucb::LockType_WRITE;
261  pCtx->hasLockType = true;
262  break;
263 
264  case STATE_DEPTH:
265  if ( !pCtx->hasDepth )
266  return 1; // abort
267  break;
268 
269  case STATE_HREF:
270  if ( !pCtx->hasHREF )
271  return 1; // abort
272  break;
273 
274  case STATE_TIMEOUT:
275  if ( !pCtx->hasTimeout )
276  return 1; // abort
277  break;
278 
279  case STATE_LOCKSCOPE:
280  if ( !pCtx->hasLockScope )
281  return 1; // abort
282  break;
283 
284  case STATE_LOCKTYPE:
285  if ( !pCtx->hasLockType )
286  return 1; // abort
287  break;
288 
289  case STATE_ACTIVELOCK:
290  if ( !pCtx->hasLockType || !pCtx->hasDepth )
291  return 1; // abort
292  break;
293 
294  default:
295  break;
296  }
297  return 0; // zero to continue, non-zero to abort parsing
298 }
299 
300 }
301 
302 // static
303 bool LockSequence::createFromXML( const OString & rInData,
304  uno::Sequence< ucb::Lock > & rOutData )
305 {
306  const sal_Int32 TOKEN_LENGTH = 13; // </activelock>
307  bool success = true;
308 
309  // rInData may contain multiple <activelock>...</activelock> tags.
310  sal_Int32 nCount = 0;
311  sal_Int32 nStart = 0;
312  sal_Int32 nEnd = rInData.indexOf( "</activelock>" );
313  while ( nEnd > -1 )
314  {
315  ne_xml_parser * parser = ne_xml_create();
316  if ( !parser )
317  {
318  success = false;
319  break;
320  }
321 
323  ne_xml_push_handler( parser,
327  &aCtx );
328 
329  ne_xml_parse( parser,
330  rInData.getStr() + nStart,
331  nEnd - nStart + TOKEN_LENGTH );
332 
333  success = !ne_xml_failed( parser );
334 
335  ne_xml_destroy( parser );
336 
337  if ( !success )
338  break;
339 
340  if ( aCtx.pLock )
341  {
342  nCount++;
343  if ( nCount > rOutData.getLength() )
344  rOutData.realloc( rOutData.getLength() + 1 );
345 
346  rOutData[ nCount - 1 ] = *aCtx.pLock;
347  }
348 
349  nStart = nEnd + TOKEN_LENGTH;
350  nEnd = rInData.indexOf( "</activelock>", nStart );
351  }
352 
353  return success;
354 }
355 
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define STATE_WRITE
#define STATE_LOCKTYPE
tuple parser
#define STATE_TIMEOUT
std::unique_ptr< ucb::Lock > pLock
#define STATE_EXCLUSIVE
static int LockSequence_chardata_callback(void *userdata, int state, const char *buf, size_t len)
static bool createFromXML(const OString &rInData, css::uno::Sequence< css::ucb::Lock > &rOutData)
#define STATE_LOCKTOKEN
#define STATE_SHARED
#define STATE_OWNER
#define STATE_ACTIVELOCK
#define STATE_DEPTH
#define SAL_WARN(area, stream)
static int LockSequence_endelement_callback(void *userdata, int state, const char *, const char *)
static int LockSequence_startelement_callback(void *, int parent, const char *, const char *name, const char **)
#define STATE_LOCKSCOPE
sal_Int32 nPos
#define STATE_HREF