libdap++  Updated for version 3.12.0
escaping.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // Copyright (c) 1996, California Institute of Technology.
27 // ALL RIGHTS RESERVED. U.S. Government Sponsorship acknowledged.
28 //
29 // Please read the full copyright notice in the file COPYRIGHT_URI
30 // in this directory.
31 //
32 // Author: Todd Karakashian, NASA/Jet Propulsion Laboratory
33 // Todd.K.Karakashian@jpl.nasa.gov
34 //
35 // $RCSfile: escaping.cc,v $ - Miscellaneous routines for OPeNDAP HDF server
36 //
37 // These two routines are for escaping/unescaping strings that are identifiers
38 // in DAP2
39 // id2www() -- escape (using WWW hex codes) non-allowable characters in a
40 // DAP2 identifier
41 // www2id() -- given an WWW hexcode escaped identifier, restore it
42 //
43 // These two routines are for escaping/unescaping strings storing attribute
44 // values. They use traditional octal escapes (\nnn) because they are
45 // intended to be viewed by a user
46 // escattr() -- escape (using traditional octal backslash) non-allowable
47 // characters in the value of a DAP2 attribute
48 // unescattr() -- given an octally escaped string, restore it
49 //
50 // These are routines used by the above, not intended to be called directly:
51 //
52 // hexstring()
53 // unhexstring()
54 // octstring()
55 // unoctstring()
56 //
57 // -Todd
58 
59 #include <ctype.h>
60 
61 #include <iomanip>
62 #include <string>
63 #include <sstream>
64 
65 #include "GNURegex.h"
66 #include "Error.h"
67 #include "InternalErr.h"
68 //#define DODS_DEBUG
69 #include "debug.h"
70 
71 using namespace std;
72 
73 namespace libdap {
74 
75 // The next four functions were originally defined static, but I removed that
76 // to make testing them (see generalUtilTest.cc) easier to write. 5/7/2001
77 // jhrg
78 
79 string
80 hexstring(unsigned char val)
81 {
82  ostringstream buf;
83  buf << hex << setw(2) << setfill('0') << static_cast<unsigned int>(val);
84 
85  return buf.str();
86 }
87 
88 string
89 unhexstring(string s)
90 {
91  int val;
92  istringstream ss(s);
93  ss >> hex >> val;
94  char tmp_str[2];
95  tmp_str[0] = static_cast<char>(val);
96  tmp_str[1] = '\0';
97  return string(tmp_str);
98 }
99 
100 string
101 octstring(unsigned char val)
102 {
103  ostringstream buf;
104  buf << oct << setw(3) << setfill('0')
105  << static_cast<unsigned int>(val);
106 
107  return buf.str();
108 }
109 
110 string
111 unoctstring(string s)
112 {
113  int val;
114 
115  istringstream ss(s);
116  ss >> oct >> val;
117 
118  DBG(cerr << "unoctstring: " << val << endl);
119 
120  char tmp_str[2];
121  tmp_str[0] = static_cast<char>(val);
122  tmp_str[1] = '\0';
123  return string(tmp_str);
124 }
125 
150 string
151 id2www(string in, const string &allowable)
152 {
153  string::size_type i = 0;
154  DBG(cerr<<"Input string: [" << in << "]" << endl);
155  while ((i = in.find_first_not_of(allowable, i)) != string::npos) {
156  DBG(cerr<<"Found escapee: [" << in[i] << "]");
157  in.replace(i, 1, "%" + hexstring(in[i]));
158  DBGN(cerr<<" now the string is: " << in << endl);
159  i += 3;//i++;
160  }
161 
162  return in;
163 }
164 
175 string
176 id2www_ce(string in, const string &allowable)
177 {
178  return id2www(in, allowable);
179 
180 
181 }
182 
217 string
218 www2id(const string &in, const string &escape, const string &except)
219 {
220  string::size_type i = 0;
221  string res = in;
222  while ((i = res.find_first_of(escape, i)) != string::npos) {
223  if (except.find(res.substr(i, 3)) != string::npos) {
224  i += 3;
225  continue;
226  }
227  res.replace(i, 3, unhexstring(res.substr(i + 1, 2)));
228  ++i;
229  }
230 
231  return res;
232 }
233 
234 static string
235 entity(char c)
236 {
237  switch (c) {
238  case '>': return "&gt;";
239  case '<': return "&lt;";
240  case '&': return "&amp;";
241  case '\'': return "&apos;";
242  case '\"': return "&quot;";
243  default:
244  throw InternalErr(__FILE__, __LINE__, "Unrecognized character.");
245  }
246 }
247 
248 // Assumption: There are always exactly two octal digits in the input
249 // and two hex digits in the result.
250 string
251 octal_to_hex(const string &octal_digits)
252 {
253  int val;
254 
255  istringstream ss(octal_digits);
256  ss >> oct >> val;
257 
258  ostringstream ds;
259  ds << hex << setw(2) << setfill('0') << val;
260  return ds.str();
261 }
262 
269 string
270 id2xml(string in, const string &not_allowed)
271 {
272  string::size_type i = 0;
273 
274  while ((i = in.find_first_of(not_allowed, i)) != string::npos) {
275  in.replace(i, 1, entity(in[i]));
276  ++i;
277  }
278 #if 0
279  // Removed the encoding of octal escapes. This function is used by
280  // AttrTable to encode the stuff that is the value of the <value>
281  // element in the DDX. The problem is that some of the values are not
282  // valid UTF-8 and that makes a XML parser gag.; ticket 1512.
283  // jhrg 3/19/10
284 
285  // OK, now scan for octal escape sequences like \\012 (where the '\'
286  // is itself escaped). This type of attribute value comes from the netCDF
287  // handler and maybe others. Assumption: The '\' will always appear as
288  // in its escaped form: '\\'. NB: Both backslashes must be escaped in the
289  // C++ string.
290  string octal_escape = "\\\\";
291  i = 0;
292  string::size_type length = in.length();
293  while ((i = in.find(octal_escape, i)) != string::npos) {
294  // Get the three octal digits following the '\\0'
295  string::size_type j = i + 2;
296  if (j + 1 >= length) // Check that we're not past the end
297  break;
298  string octal_digits = in.substr(j, 3);
299  // convert to a &#xdd; XML escape
300  string hex_escape = string("&#x");
301  hex_escape.append(octal_to_hex(octal_digits));
302  hex_escape.append(string(";"));
303 
304  // replace the octal escape with an XML/hex escape
305  in.replace(i, 5, hex_escape);
306 
307  // increment i
308  i += 6;
309  }
310 #endif
311  return in;
312 }
313 
319 string
320 xml2id(string in)
321 {
322  string::size_type i = 0;
323 
324  while ((i = in.find("&gt;", i)) != string::npos)
325  in.replace(i, 4, ">");
326 
327  i = 0;
328  while ((i = in.find("&lt;", i)) != string::npos)
329  in.replace(i, 4, "<");
330 
331  i = 0;
332  while ((i = in.find("&amp;", i)) != string::npos)
333  in.replace(i, 5, "&");
334 
335  i = 0;
336  while ((i = in.find("&apos;", i)) != string::npos)
337  in.replace(i, 6, "'");
338 
339  i = 0;
340  while ((i = in.find("&quot;", i)) != string::npos)
341  in.replace(i, 6, "\"");
342 
343  return in;
344 }
345 
351 string
352 esc2underscore(string s)
353 {
354  string::size_type pos;
355  while ((pos = s.find('%')) != string::npos)
356  s.replace(pos, 3, "_");
357 
358  return s;
359 }
360 
361 
365 string
366 escattr(string s)
367 {
368  const string printable = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"";
369  const string ESC = "\\";
370  const string DOUBLE_ESC = ESC + ESC;
371  const string QUOTE = "\"";
372  const string ESCQUOTE = ESC + QUOTE;
373 
374  // escape non-printing characters with octal escape
375  string::size_type ind = 0;
376  while ((ind = s.find_first_not_of(printable, ind)) != s.npos)
377  s.replace(ind, 1, ESC + octstring(s[ind]));
378 
379  // escape \ with a second backslash
380  ind = 0;
381  while ((ind = s.find(ESC, ind)) != s.npos) {
382  s.replace(ind, 1, DOUBLE_ESC);
383  ind += DOUBLE_ESC.length();
384  }
385 
386  // escape " with backslash
387  ind = 0;
388  while ((ind = s.find(QUOTE, ind)) != s.npos) {
389  s.replace(ind, 1, ESCQUOTE);
390  ind += ESCQUOTE.length();
391  }
392 
393  return s;
394 }
395 
404 string
405 unescattr(string s)
406 {
407  Regex octal("\\\\[0-3][0-7][0-7]"); // matches 4 characters
408  Regex esc_quote("\\\\\""); // matches 3 characters
409  Regex esc_esc("\\\\\\\\"); // matches 2 characters
410  const string ESC = "\\";
411  const string QUOTE = "\"";
412  int matchlen;
413  unsigned int index;
414 
415  DBG(cerr << "0XX" << s << "XXX" << endl);
416  // unescape any escaped backslashes
417  index = esc_esc.search(s.c_str(), s.length(), matchlen, 0);
418  while (index < s.length()) {
419  DBG(cerr << "1aXX" << s << "XXX index: " << index << endl);
420  s.replace(index, 2, ESC);
421  DBG(cerr << "1bXX" << s << "XXX index: " << index << endl);
422  index = esc_esc.search(s.c_str(), s.length(), matchlen, 0);
423  }
424 
425  // unescape any escaped double quote characters
426  index = esc_quote.search(s.c_str(), s.length(), matchlen, 0);
427  while (index < s.length()) {
428  s.replace(index, 2, QUOTE);
429  DBG(cerr << "2XX" << s << "XXX index: " << index << endl);
430  index = esc_quote.search(s.c_str(), s.length(), matchlen, 0);
431  }
432 
433  // unescape octal characters
434  index = octal.search(s.c_str(), s.length(), matchlen, 0);
435  while (index < s.length()) {
436  s.replace(index, 4, unoctstring(s.substr(index + 1, 3)));
437  DBG(cerr << "3XX" << s << "XXX index: " << index << endl);
438  index = octal.search(s.c_str(), s.length(), matchlen, 0);
439  }
440 
441  DBG(cerr << "4XX" << s << "XXX" << endl);
442  return s;
443 }
444 
445 string
447 {
448  // First, add enclosing quotes if needed.
449  if (*msg.begin() != '"')
450  msg.insert(msg.begin(), '"');
451  if (*(msg.end() - 1) != '"')
452  msg += "\"";
453 
454  // Now escape any internal double quotes that aren't escaped.
455  string::iterator miter;
456  for (miter = msg.begin() + 1; miter != msg.end() - 1; miter++)
457  if (*miter == '"' && *(miter - 1) != '\\')
458  miter = msg.insert(miter, '\\');
459 
460  return msg;
461 }
462 
467 string
468 escape_double_quotes(string source)
469 {
470  string::size_type idx = 0;
471  while((idx = source.find('\"', idx)) != string::npos) {
472  source.replace(idx, 1, "\\\""); // a backslash and a double quote
473  idx += 2;
474  }
475 
476  return source;
477 }
478 
484 string
486 {
487  string::size_type idx = 0;
488  while((idx = source.find("\\\"", idx)) != string::npos) {
489  source.replace(idx, 2, "\""); // a backslash and a double quote
490  ++idx;
491  }
492 
493  return source;
494 }
495 
496 } // namespace libdap
497 
string id2www_ce(string in, const string &allowable)
Definition: escaping.cc:176
#define DBGN(x)
Definition: debug.h:59
string id2xml(string in, const string &not_allowed)
Definition: escaping.cc:270
string escape_double_quotes(string source)
Definition: escaping.cc:468
string octal_to_hex(const string &octal_digits)
Definition: escaping.cc:251
string unoctstring(string s)
Definition: escaping.cc:111
#define DBG(x)
Definition: debug.h:58
string munge_error_message(string msg)
Definition: escaping.cc:446
string xml2id(string in)
Definition: escaping.cc:320
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:218
string esc2underscore(string s)
Definition: escaping.cc:352
string unhexstring(string s)
Definition: escaping.cc:89
string hexstring(unsigned char val)
Definition: escaping.cc:80
string octstring(unsigned char val)
Definition: escaping.cc:101
string unescattr(string s)
Definition: escaping.cc:405
string id2www(string in, const string &allowable)
Definition: escaping.cc:151
string unescape_double_quotes(string source)
Definition: escaping.cc:485
string escattr(string s)
Definition: escaping.cc:366