client/ftpd/src/se/lu/thep/coreftpd/common/UnescapeMethods.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25
741 10 Oct 06 olle 26 //  UnescapeMethods library
741 10 Oct 06 olle 27 //  Copyright (C) 2003 Omid Rouhani
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //  Note that is not a part of "Xerver Free Web Server".
741 10 Oct 06 olle 30
1652 22 May 07 gregory 31 /*******************************************************************
1652 22 May 07 gregory 32  *  Everything below this line is part of the HTTPClient package    *
1652 22 May 07 gregory 33  *  URI.java, 0.3-3 06/05/2001                                      *
1652 22 May 07 gregory 34  *  Copyright (C) 1996-2001 Ronald Tschal�r                         *
1652 22 May 07 gregory 35  *                                                                  *
1652 22 May 07 gregory 36  *  Contact: ronald@innovation.ch                                   *
1652 22 May 07 gregory 37  *                                                                  *
1652 22 May 07 gregory 38  *  The HTTPClient's home page is located at:                       *
1652 22 May 07 gregory 39  *  http://www.innovation.ch/java/HTTPClient/                       *
1652 22 May 07 gregory 40  *                                                                  *
1652 22 May 07 gregory 41  ********************************************************************
1652 22 May 07 gregory 42  *                                                                  *
1652 22 May 07 gregory 43  * Source below has been modified by me,                            *
1652 22 May 07 gregory 44  * Omid Rouhani, author of Xerver.                                  *
1652 22 May 07 gregory 45  *                                                                  *
1652 22 May 07 gregory 46  * Note that this single file is released under LGPL,               *
1652 22 May 07 gregory 47  * GNU Lesser General Public License,                               *
1652 22 May 07 gregory 48  * which is not the same license as the rest of                     *
1652 22 May 07 gregory 49  * Xerver is released under, GPL - GNU General Public License.      *
1652 22 May 07 gregory 50  *                                                                  *
1652 22 May 07 gregory 51  * This file is considered as a modified version of                 *
1652 22 May 07 gregory 52  * Ronald Tschal�r's HTTPClient package.                            *
1652 22 May 07 gregory 53  *                                                                  *
1652 22 May 07 gregory 54  * Any copy of this single file shall be released under LGPL        *
1652 22 May 07 gregory 55  * and shall not be mixed with the source of the rest of Xerver,    *
1652 22 May 07 gregory 56  * which must be released under GPL.                                *
1652 22 May 07 gregory 57  *                                                                  *
1652 22 May 07 gregory 58  *******************************************************************/
741 10 Oct 06 olle 59
741 10 Oct 06 olle 60 /*******************************************************************************
741 10 Oct 06 olle 61
1652 22 May 07 gregory 62  GNU LESSER GENERAL PUBLIC LICENSE
1652 22 May 07 gregory 63  Version 2.1, February 1999
741 10 Oct 06 olle 64
741 10 Oct 06 olle 65  Copyright (C) 1991, 1999 Free Software Foundation, Inc.
1652 22 May 07 gregory 66  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
741 10 Oct 06 olle 67  Everyone is permitted to copy and distribute verbatim copies
741 10 Oct 06 olle 68  of this license document, but changing it is not allowed.
741 10 Oct 06 olle 69
1652 22 May 07 gregory 70  [This is the first released version of the Lesser GPL.  It also counts
741 10 Oct 06 olle 71  as the successor of the GNU Library Public License, version 2, hence
741 10 Oct 06 olle 72  the version number 2.1.]
741 10 Oct 06 olle 73
1652 22 May 07 gregory 74  Preamble
741 10 Oct 06 olle 75
1652 22 May 07 gregory 76  The licenses for most software are designed to take away your
1652 22 May 07 gregory 77  freedom to share and change it.  By contrast, the GNU General Public
1652 22 May 07 gregory 78  Licenses are intended to guarantee your freedom to share and change
1652 22 May 07 gregory 79  free software--to make sure the software is free for all its users.
741 10 Oct 06 olle 80
1652 22 May 07 gregory 81  This license, the Lesser General Public License, applies to some
1652 22 May 07 gregory 82  specially designated software packages--typically libraries--of the
1652 22 May 07 gregory 83  Free Software Foundation and other authors who decide to use it.  You
1652 22 May 07 gregory 84  can use it too, but we suggest you first think carefully about whether
1652 22 May 07 gregory 85  this license or the ordinary General Public License is the better
1652 22 May 07 gregory 86  strategy to use in any particular case, based on the explanations below.
741 10 Oct 06 olle 87
1652 22 May 07 gregory 88  When we speak of free software, we are referring to freedom of use,
1652 22 May 07 gregory 89  not price.  Our General Public Licenses are designed to make sure that
1652 22 May 07 gregory 90  you have the freedom to distribute copies of free software (and charge
1652 22 May 07 gregory 91  for this service if you wish); that you receive source code or can get
1652 22 May 07 gregory 92  it if you want it; that you can change the software and use pieces of
1652 22 May 07 gregory 93  it in new free programs; and that you are informed that you can do
1652 22 May 07 gregory 94  these things.
741 10 Oct 06 olle 95
1652 22 May 07 gregory 96  To protect your rights, we need to make restrictions that forbid
1652 22 May 07 gregory 97  distributors to deny you these rights or to ask you to surrender these
1652 22 May 07 gregory 98  rights.  These restrictions translate to certain responsibilities for
1652 22 May 07 gregory 99  you if you distribute copies of the library or if you modify it.
741 10 Oct 06 olle 100
1652 22 May 07 gregory 101  For example, if you distribute copies of the library, whether gratis
1652 22 May 07 gregory 102  or for a fee, you must give the recipients all the rights that we gave
1652 22 May 07 gregory 103  you.  You must make sure that they, too, receive or can get the source
1652 22 May 07 gregory 104  code.  If you link other code with the library, you must provide
1652 22 May 07 gregory 105  complete object files to the recipients, so that they can relink them
1652 22 May 07 gregory 106  with the library after making changes to the library and recompiling
1652 22 May 07 gregory 107  it.  And you must show them these terms so they know their rights.
741 10 Oct 06 olle 108
1652 22 May 07 gregory 109  We protect your rights with a two-step method: (1) we copyright the
1652 22 May 07 gregory 110  library, and (2) we offer you this license, which gives you legal
1652 22 May 07 gregory 111  permission to copy, distribute and/or modify the library.
741 10 Oct 06 olle 112
1652 22 May 07 gregory 113  To protect each distributor, we want to make it very clear that
1652 22 May 07 gregory 114  there is no warranty for the free library.  Also, if the library is
1652 22 May 07 gregory 115  modified by someone else and passed on, the recipients should know
1652 22 May 07 gregory 116  that what they have is not the original version, so that the original
1652 22 May 07 gregory 117  author's reputation will not be affected by problems that might be
1652 22 May 07 gregory 118  introduced by others.
741 10 Oct 06 olle 119
1652 22 May 07 gregory 120  Finally, software patents pose a constant threat to the existence of
1652 22 May 07 gregory 121  any free program.  We wish to make sure that a company cannot
1652 22 May 07 gregory 122  effectively restrict the users of a free program by obtaining a
1652 22 May 07 gregory 123  restrictive license from a patent holder.  Therefore, we insist that
1652 22 May 07 gregory 124  any patent license obtained for a version of the library must be
1652 22 May 07 gregory 125  consistent with the full freedom of use specified in this license.
741 10 Oct 06 olle 126
1652 22 May 07 gregory 127  Most GNU software, including some libraries, is covered by the
1652 22 May 07 gregory 128  ordinary GNU General Public License.  This license, the GNU Lesser
1652 22 May 07 gregory 129  General Public License, applies to certain designated libraries, and
1652 22 May 07 gregory 130  is quite different from the ordinary General Public License.  We use
1652 22 May 07 gregory 131  this license for certain libraries in order to permit linking those
1652 22 May 07 gregory 132  libraries into non-free programs.
741 10 Oct 06 olle 133
1652 22 May 07 gregory 134  When a program is linked with a library, whether statically or using
1652 22 May 07 gregory 135  a shared library, the combination of the two is legally speaking a
1652 22 May 07 gregory 136  combined work, a derivative of the original library.  The ordinary
1652 22 May 07 gregory 137  General Public License therefore permits such linking only if the
1652 22 May 07 gregory 138  entire combination fits its criteria of freedom.  The Lesser General
1652 22 May 07 gregory 139  Public License permits more lax criteria for linking other code with
1652 22 May 07 gregory 140  the library.
741 10 Oct 06 olle 141
1652 22 May 07 gregory 142  We call this license the "Lesser" General Public License because it
1652 22 May 07 gregory 143  does Less to protect the user's freedom than the ordinary General
1652 22 May 07 gregory 144  Public License.  It also provides other free software developers Less
1652 22 May 07 gregory 145  of an advantage over competing non-free programs.  These disadvantages
1652 22 May 07 gregory 146  are the reason we use the ordinary General Public License for many
1652 22 May 07 gregory 147  libraries.  However, the Lesser license provides advantages in certain
1652 22 May 07 gregory 148  special circumstances.
741 10 Oct 06 olle 149
1652 22 May 07 gregory 150  For example, on rare occasions, there may be a special need to
1652 22 May 07 gregory 151  encourage the widest possible use of a certain library, so that it becomes
1652 22 May 07 gregory 152  a de-facto standard.  To achieve this, non-free programs must be
1652 22 May 07 gregory 153  allowed to use the library.  A more frequent case is that a free
1652 22 May 07 gregory 154  library does the same job as widely used non-free libraries.  In this
1652 22 May 07 gregory 155  case, there is little to gain by limiting the free library to free
1652 22 May 07 gregory 156  software only, so we use the Lesser General Public License.
741 10 Oct 06 olle 157
1652 22 May 07 gregory 158  In other cases, permission to use a particular library in non-free
1652 22 May 07 gregory 159  programs enables a greater number of people to use a large body of
1652 22 May 07 gregory 160  free software.  For example, permission to use the GNU C Library in
1652 22 May 07 gregory 161  non-free programs enables many more people to use the whole GNU
1652 22 May 07 gregory 162  operating system, as well as its variant, the GNU/Linux operating
1652 22 May 07 gregory 163  system.
741 10 Oct 06 olle 164
1652 22 May 07 gregory 165  Although the Lesser General Public License is Less protective of the
1652 22 May 07 gregory 166  users' freedom, it does ensure that the user of a program that is
1652 22 May 07 gregory 167  linked with the Library has the freedom and the wherewithal to run
1652 22 May 07 gregory 168  that program using a modified version of the Library.
741 10 Oct 06 olle 169
1652 22 May 07 gregory 170  The precise terms and conditions for copying, distribution and
1652 22 May 07 gregory 171  modification follow.  Pay close attention to the difference between a
1652 22 May 07 gregory 172  "work based on the library" and a "work that uses the library".  The
1652 22 May 07 gregory 173  former contains code derived from the library, whereas the latter must
1652 22 May 07 gregory 174  be combined with the library in order to run.
741 10 Oct 06 olle 175
1652 22 May 07 gregory 176  GNU LESSER GENERAL PUBLIC LICENSE
1652 22 May 07 gregory 177  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
741 10 Oct 06 olle 178
1652 22 May 07 gregory 179  0. This License Agreement applies to any software library or other
1652 22 May 07 gregory 180  program which contains a notice placed by the copyright holder or
1652 22 May 07 gregory 181  other authorized party saying it may be distributed under the terms of
1652 22 May 07 gregory 182  this Lesser General Public License (also called "this License").
1652 22 May 07 gregory 183  Each licensee is addressed as "you".
741 10 Oct 06 olle 184
1652 22 May 07 gregory 185  A "library" means a collection of software functions and/or data
1652 22 May 07 gregory 186  prepared so as to be conveniently linked with application programs
1652 22 May 07 gregory 187  (which use some of those functions and data) to form executables.
741 10 Oct 06 olle 188
1652 22 May 07 gregory 189  The "Library", below, refers to any such software library or work
1652 22 May 07 gregory 190  which has been distributed under these terms.  A "work based on the
1652 22 May 07 gregory 191  Library" means either the Library or any derivative work under
1652 22 May 07 gregory 192  copyright law: that is to say, a work containing the Library or a
1652 22 May 07 gregory 193  portion of it, either verbatim or with modifications and/or translated
1652 22 May 07 gregory 194  straightforwardly into another language.  (Hereinafter, translation is
1652 22 May 07 gregory 195  included without limitation in the term "modification".)
741 10 Oct 06 olle 196
1652 22 May 07 gregory 197  "Source code" for a work means the preferred form of the work for
1652 22 May 07 gregory 198  making modifications to it.  For a library, complete source code means
1652 22 May 07 gregory 199  all the source code for all modules it contains, plus any associated
1652 22 May 07 gregory 200  interface definition files, plus the scripts used to control compilation
1652 22 May 07 gregory 201  and installation of the library.
741 10 Oct 06 olle 202
1652 22 May 07 gregory 203  Activities other than copying, distribution and modification are not
1652 22 May 07 gregory 204  covered by this License; they are outside its scope.  The act of
1652 22 May 07 gregory 205  running a program using the Library is not restricted, and output from
1652 22 May 07 gregory 206  such a program is covered only if its contents constitute a work based
1652 22 May 07 gregory 207  on the Library (independent of the use of the Library in a tool for
1652 22 May 07 gregory 208  writing it).  Whether that is true depends on what the Library does
1652 22 May 07 gregory 209  and what the program that uses the Library does.
741 10 Oct 06 olle 210
1652 22 May 07 gregory 211  1. You may copy and distribute verbatim copies of the Library's
1652 22 May 07 gregory 212  complete source code as you receive it, in any medium, provided that
1652 22 May 07 gregory 213  you conspicuously and appropriately publish on each copy an
1652 22 May 07 gregory 214  appropriate copyright notice and disclaimer of warranty; keep intact
1652 22 May 07 gregory 215  all the notices that refer to this License and to the absence of any
1652 22 May 07 gregory 216  warranty; and distribute a copy of this License along with the
1652 22 May 07 gregory 217  Library.
741 10 Oct 06 olle 218
1652 22 May 07 gregory 219  You may charge a fee for the physical act of transferring a copy,
1652 22 May 07 gregory 220  and you may at your option offer warranty protection in exchange for a
1652 22 May 07 gregory 221  fee.
741 10 Oct 06 olle 222
1652 22 May 07 gregory 223  2. You may modify your copy or copies of the Library or any portion
1652 22 May 07 gregory 224  of it, thus forming a work based on the Library, and copy and
1652 22 May 07 gregory 225  distribute such modifications or work under the terms of Section 1
1652 22 May 07 gregory 226  above, provided that you also meet all of these conditions:
741 10 Oct 06 olle 227
1652 22 May 07 gregory 228  a) The modified work must itself be a software library.
741 10 Oct 06 olle 229
1652 22 May 07 gregory 230  b) You must cause the files modified to carry prominent notices
1652 22 May 07 gregory 231  stating that you changed the files and the date of any change.
741 10 Oct 06 olle 232
1652 22 May 07 gregory 233  c) You must cause the whole of the work to be licensed at no
1652 22 May 07 gregory 234  charge to all third parties under the terms of this License.
741 10 Oct 06 olle 235
1652 22 May 07 gregory 236  d) If a facility in the modified Library refers to a function or a
1652 22 May 07 gregory 237  table of data to be supplied by an application program that uses
1652 22 May 07 gregory 238  the facility, other than as an argument passed when the facility
1652 22 May 07 gregory 239  is invoked, then you must make a good faith effort to ensure that,
1652 22 May 07 gregory 240  in the event an application does not supply such function or
1652 22 May 07 gregory 241  table, the facility still operates, and performs whatever part of
1652 22 May 07 gregory 242  its purpose remains meaningful.
741 10 Oct 06 olle 243
1652 22 May 07 gregory 244  (For example, a function in a library to compute square roots has
1652 22 May 07 gregory 245  a purpose that is entirely well-defined independent of the
1652 22 May 07 gregory 246  application.  Therefore, Subsection 2d requires that any
1652 22 May 07 gregory 247  application-supplied function or table used by this function must
1652 22 May 07 gregory 248  be optional: if the application does not supply it, the square
1652 22 May 07 gregory 249  root function must still compute square roots.)
741 10 Oct 06 olle 250
1652 22 May 07 gregory 251  These requirements apply to the modified work as a whole.  If
1652 22 May 07 gregory 252  identifiable sections of that work are not derived from the Library,
1652 22 May 07 gregory 253  and can be reasonably considered independent and separate works in
1652 22 May 07 gregory 254  themselves, then this License, and its terms, do not apply to those
1652 22 May 07 gregory 255  sections when you distribute them as separate works.  But when you
1652 22 May 07 gregory 256  distribute the same sections as part of a whole which is a work based
1652 22 May 07 gregory 257  on the Library, the distribution of the whole must be on the terms of
1652 22 May 07 gregory 258  this License, whose permissions for other licensees extend to the
1652 22 May 07 gregory 259  entire whole, and thus to each and every part regardless of who wrote
1652 22 May 07 gregory 260  it.
741 10 Oct 06 olle 261
1652 22 May 07 gregory 262  Thus, it is not the intent of this section to claim rights or contest
1652 22 May 07 gregory 263  your rights to work written entirely by you; rather, the intent is to
1652 22 May 07 gregory 264  exercise the right to control the distribution of derivative or
1652 22 May 07 gregory 265  collective works based on the Library.
741 10 Oct 06 olle 266
1652 22 May 07 gregory 267  In addition, mere aggregation of another work not based on the Library
1652 22 May 07 gregory 268  with the Library (or with a work based on the Library) on a volume of
1652 22 May 07 gregory 269  a storage or distribution medium does not bring the other work under
1652 22 May 07 gregory 270  the scope of this License.
741 10 Oct 06 olle 271
1652 22 May 07 gregory 272  3. You may opt to apply the terms of the ordinary GNU General Public
1652 22 May 07 gregory 273  License instead of this License to a given copy of the Library.  To do
1652 22 May 07 gregory 274  this, you must alter all the notices that refer to this License, so
1652 22 May 07 gregory 275  that they refer to the ordinary GNU General Public License, version 2,
1652 22 May 07 gregory 276  instead of to this License.  (If a newer version than version 2 of the
1652 22 May 07 gregory 277  ordinary GNU General Public License has appeared, then you can specify
1652 22 May 07 gregory 278  that version instead if you wish.)  Do not make any other change in
1652 22 May 07 gregory 279  these notices.
741 10 Oct 06 olle 280
1652 22 May 07 gregory 281  Once this change is made in a given copy, it is irreversible for
1652 22 May 07 gregory 282  that copy, so the ordinary GNU General Public License applies to all
1652 22 May 07 gregory 283  subsequent copies and derivative works made from that copy.
741 10 Oct 06 olle 284
1652 22 May 07 gregory 285  This option is useful when you wish to copy part of the code of
1652 22 May 07 gregory 286  the Library into a program that is not a library.
741 10 Oct 06 olle 287
1652 22 May 07 gregory 288  4. You may copy and distribute the Library (or a portion or
1652 22 May 07 gregory 289  derivative of it, under Section 2) in object code or executable form
1652 22 May 07 gregory 290  under the terms of Sections 1 and 2 above provided that you accompany
1652 22 May 07 gregory 291  it with the complete corresponding machine-readable source code, which
1652 22 May 07 gregory 292  must be distributed under the terms of Sections 1 and 2 above on a
1652 22 May 07 gregory 293  medium customarily used for software interchange.
741 10 Oct 06 olle 294
1652 22 May 07 gregory 295  If distribution of object code is made by offering access to copy
1652 22 May 07 gregory 296  from a designated place, then offering equivalent access to copy the
1652 22 May 07 gregory 297  source code from the same place satisfies the requirement to
1652 22 May 07 gregory 298  distribute the source code, even though third parties are not
1652 22 May 07 gregory 299  compelled to copy the source along with the object code.
741 10 Oct 06 olle 300
1652 22 May 07 gregory 301  5. A program that contains no derivative of any portion of the
1652 22 May 07 gregory 302  Library, but is designed to work with the Library by being compiled or
1652 22 May 07 gregory 303  linked with it, is called a "work that uses the Library".  Such a
1652 22 May 07 gregory 304  work, in isolation, is not a derivative work of the Library, and
1652 22 May 07 gregory 305  therefore falls outside the scope of this License.
741 10 Oct 06 olle 306
1652 22 May 07 gregory 307  However, linking a "work that uses the Library" with the Library
1652 22 May 07 gregory 308  creates an executable that is a derivative of the Library (because it
1652 22 May 07 gregory 309  contains portions of the Library), rather than a "work that uses the
1652 22 May 07 gregory 310  library".  The executable is therefore covered by this License.
1652 22 May 07 gregory 311  Section 6 states terms for distribution of such executables.
741 10 Oct 06 olle 312
1652 22 May 07 gregory 313  When a "work that uses the Library" uses material from a header file
1652 22 May 07 gregory 314  that is part of the Library, the object code for the work may be a
1652 22 May 07 gregory 315  derivative work of the Library even though the source code is not.
1652 22 May 07 gregory 316  Whether this is true is especially significant if the work can be
1652 22 May 07 gregory 317  linked without the Library, or if the work is itself a library.  The
1652 22 May 07 gregory 318  threshold for this to be true is not precisely defined by law.
741 10 Oct 06 olle 319
1652 22 May 07 gregory 320  If such an object file uses only numerical parameters, data
1652 22 May 07 gregory 321  structure layouts and accessors, and small macros and small inline
1652 22 May 07 gregory 322  functions (ten lines or less in length), then the use of the object
1652 22 May 07 gregory 323  file is unrestricted, regardless of whether it is legally a derivative
1652 22 May 07 gregory 324  work.  (Executables containing this object code plus portions of the
1652 22 May 07 gregory 325  Library will still fall under Section 6.)
741 10 Oct 06 olle 326
1652 22 May 07 gregory 327  Otherwise, if the work is a derivative of the Library, you may
1652 22 May 07 gregory 328  distribute the object code for the work under the terms of Section 6.
1652 22 May 07 gregory 329  Any executables containing that work also fall under Section 6,
1652 22 May 07 gregory 330  whether or not they are linked directly with the Library itself.
741 10 Oct 06 olle 331
1652 22 May 07 gregory 332  6. As an exception to the Sections above, you may also combine or
1652 22 May 07 gregory 333  link a "work that uses the Library" with the Library to produce a
1652 22 May 07 gregory 334  work containing portions of the Library, and distribute that work
1652 22 May 07 gregory 335  under terms of your choice, provided that the terms permit
1652 22 May 07 gregory 336  modification of the work for the customer's own use and reverse
1652 22 May 07 gregory 337  engineering for debugging such modifications.
741 10 Oct 06 olle 338
1652 22 May 07 gregory 339  You must give prominent notice with each copy of the work that the
1652 22 May 07 gregory 340  Library is used in it and that the Library and its use are covered by
1652 22 May 07 gregory 341  this License.  You must supply a copy of this License.  If the work
1652 22 May 07 gregory 342  during execution displays copyright notices, you must include the
1652 22 May 07 gregory 343  copyright notice for the Library among them, as well as a reference
1652 22 May 07 gregory 344  directing the user to the copy of this License.  Also, you must do one
1652 22 May 07 gregory 345  of these things:
741 10 Oct 06 olle 346
1652 22 May 07 gregory 347  a) Accompany the work with the complete corresponding
1652 22 May 07 gregory 348  machine-readable source code for the Library including whatever
1652 22 May 07 gregory 349  changes were used in the work (which must be distributed under
1652 22 May 07 gregory 350  Sections 1 and 2 above); and, if the work is an executable linked
1652 22 May 07 gregory 351  with the Library, with the complete machine-readable "work that
1652 22 May 07 gregory 352  uses the Library", as object code and/or source code, so that the
1652 22 May 07 gregory 353  user can modify the Library and then relink to produce a modified
1652 22 May 07 gregory 354  executable containing the modified Library.  (It is understood
1652 22 May 07 gregory 355  that the user who changes the contents of definitions files in the
1652 22 May 07 gregory 356  Library will not necessarily be able to recompile the application
1652 22 May 07 gregory 357  to use the modified definitions.)
741 10 Oct 06 olle 358
1652 22 May 07 gregory 359  b) Use a suitable shared library mechanism for linking with the
1652 22 May 07 gregory 360  Library.  A suitable mechanism is one that (1) uses at run time a
1652 22 May 07 gregory 361  copy of the library already present on the user's computer system,
1652 22 May 07 gregory 362  rather than copying library functions into the executable, and (2)
1652 22 May 07 gregory 363  will operate properly with a modified version of the library, if
1652 22 May 07 gregory 364  the user installs one, as long as the modified version is
1652 22 May 07 gregory 365  interface-compatible with the version that the work was made with.
741 10 Oct 06 olle 366
1652 22 May 07 gregory 367  c) Accompany the work with a written offer, valid for at
1652 22 May 07 gregory 368  least three years, to give the same user the materials
1652 22 May 07 gregory 369  specified in Subsection 6a, above, for a charge no more
1652 22 May 07 gregory 370  than the cost of performing this distribution.
741 10 Oct 06 olle 371
1652 22 May 07 gregory 372  d) If distribution of the work is made by offering access to copy
1652 22 May 07 gregory 373  from a designated place, offer equivalent access to copy the above
1652 22 May 07 gregory 374  specified materials from the same place.
741 10 Oct 06 olle 375
1652 22 May 07 gregory 376  e) Verify that the user has already received a copy of these
1652 22 May 07 gregory 377  materials or that you have already sent this user a copy.
741 10 Oct 06 olle 378
1652 22 May 07 gregory 379  For an executable, the required form of the "work that uses the
1652 22 May 07 gregory 380  Library" must include any data and utility programs needed for
1652 22 May 07 gregory 381  reproducing the executable from it.  However, as a special exception,
1652 22 May 07 gregory 382  the materials to be distributed need not include anything that is
1652 22 May 07 gregory 383  normally distributed (in either source or binary form) with the major
1652 22 May 07 gregory 384  components (compiler, kernel, and so on) of the operating system on
1652 22 May 07 gregory 385  which the executable runs, unless that component itself accompanies
1652 22 May 07 gregory 386  the executable.
741 10 Oct 06 olle 387
1652 22 May 07 gregory 388  It may happen that this requirement contradicts the license
1652 22 May 07 gregory 389  restrictions of other proprietary libraries that do not normally
1652 22 May 07 gregory 390  accompany the operating system.  Such a contradiction means you cannot
1652 22 May 07 gregory 391  use both them and the Library together in an executable that you
1652 22 May 07 gregory 392  distribute.
741 10 Oct 06 olle 393
1652 22 May 07 gregory 394  7. You may place library facilities that are a work based on the
1652 22 May 07 gregory 395  Library side-by-side in a single library together with other library
1652 22 May 07 gregory 396  facilities not covered by this License, and distribute such a combined
1652 22 May 07 gregory 397  library, provided that the separate distribution of the work based on
1652 22 May 07 gregory 398  the Library and of the other library facilities is otherwise
1652 22 May 07 gregory 399  permitted, and provided that you do these two things:
741 10 Oct 06 olle 400
1652 22 May 07 gregory 401  a) Accompany the combined library with a copy of the same work
1652 22 May 07 gregory 402  based on the Library, uncombined with any other library
1652 22 May 07 gregory 403  facilities.  This must be distributed under the terms of the
1652 22 May 07 gregory 404  Sections above.
741 10 Oct 06 olle 405
1652 22 May 07 gregory 406  b) Give prominent notice with the combined library of the fact
1652 22 May 07 gregory 407  that part of it is a work based on the Library, and explaining
1652 22 May 07 gregory 408  where to find the accompanying uncombined form of the same work.
741 10 Oct 06 olle 409
1652 22 May 07 gregory 410  8. You may not copy, modify, sublicense, link with, or distribute
1652 22 May 07 gregory 411  the Library except as expressly provided under this License.  Any
1652 22 May 07 gregory 412  attempt otherwise to copy, modify, sublicense, link with, or
1652 22 May 07 gregory 413  distribute the Library is void, and will automatically terminate your
1652 22 May 07 gregory 414  rights under this License.  However, parties who have received copies,
1652 22 May 07 gregory 415  or rights, from you under this License will not have their licenses
1652 22 May 07 gregory 416  terminated so long as such parties remain in full compliance.
741 10 Oct 06 olle 417
1652 22 May 07 gregory 418  9. You are not required to accept this License, since you have not
1652 22 May 07 gregory 419  signed it.  However, nothing else grants you permission to modify or
1652 22 May 07 gregory 420  distribute the Library or its derivative works.  These actions are
1652 22 May 07 gregory 421  prohibited by law if you do not accept this License.  Therefore, by
1652 22 May 07 gregory 422  modifying or distributing the Library (or any work based on the
1652 22 May 07 gregory 423  Library), you indicate your acceptance of this License to do so, and
1652 22 May 07 gregory 424  all its terms and conditions for copying, distributing or modifying
1652 22 May 07 gregory 425  the Library or works based on it.
741 10 Oct 06 olle 426
1652 22 May 07 gregory 427  10. Each time you redistribute the Library (or any work based on the
1652 22 May 07 gregory 428  Library), the recipient automatically receives a license from the
1652 22 May 07 gregory 429  original licensor to copy, distribute, link with or modify the Library
1652 22 May 07 gregory 430  subject to these terms and conditions.  You may not impose any further
1652 22 May 07 gregory 431  restrictions on the recipients' exercise of the rights granted herein.
1652 22 May 07 gregory 432  You are not responsible for enforcing compliance by third parties with
1652 22 May 07 gregory 433  this License.
741 10 Oct 06 olle 434
1652 22 May 07 gregory 435  11. If, as a consequence of a court judgment or allegation of patent
1652 22 May 07 gregory 436  infringement or for any other reason (not limited to patent issues),
1652 22 May 07 gregory 437  conditions are imposed on you (whether by court order, agreement or
1652 22 May 07 gregory 438  otherwise) that contradict the conditions of this License, they do not
1652 22 May 07 gregory 439  excuse you from the conditions of this License.  If you cannot
1652 22 May 07 gregory 440  distribute so as to satisfy simultaneously your obligations under this
1652 22 May 07 gregory 441  License and any other pertinent obligations, then as a consequence you
1652 22 May 07 gregory 442  may not distribute the Library at all.  For example, if a patent
1652 22 May 07 gregory 443  license would not permit royalty-free redistribution of the Library by
1652 22 May 07 gregory 444  all those who receive copies directly or indirectly through you, then
1652 22 May 07 gregory 445  the only way you could satisfy both it and this License would be to
1652 22 May 07 gregory 446  refrain entirely from distribution of the Library.
741 10 Oct 06 olle 447
1652 22 May 07 gregory 448  If any portion of this section is held invalid or unenforceable under any
1652 22 May 07 gregory 449  particular circumstance, the balance of the section is intended to apply,
1652 22 May 07 gregory 450  and the section as a whole is intended to apply in other circumstances.
741 10 Oct 06 olle 451
1652 22 May 07 gregory 452  It is not the purpose of this section to induce you to infringe any
1652 22 May 07 gregory 453  patents or other property right claims or to contest validity of any
1652 22 May 07 gregory 454  such claims; this section has the sole purpose of protecting the
1652 22 May 07 gregory 455  integrity of the free software distribution system which is
1652 22 May 07 gregory 456  implemented by public license practices.  Many people have made
1652 22 May 07 gregory 457  generous contributions to the wide range of software distributed
1652 22 May 07 gregory 458  through that system in reliance on consistent application of that
1652 22 May 07 gregory 459  system; it is up to the author/donor to decide if he or she is willing
1652 22 May 07 gregory 460  to distribute software through any other system and a licensee cannot
1652 22 May 07 gregory 461  impose that choice.
741 10 Oct 06 olle 462
1652 22 May 07 gregory 463  This section is intended to make thoroughly clear what is believed to
1652 22 May 07 gregory 464  be a consequence of the rest of this License.
741 10 Oct 06 olle 465
1652 22 May 07 gregory 466  12. If the distribution and/or use of the Library is restricted in
1652 22 May 07 gregory 467  certain countries either by patents or by copyrighted interfaces, the
1652 22 May 07 gregory 468  original copyright holder who places the Library under this License may add
1652 22 May 07 gregory 469  an explicit geographical distribution limitation excluding those countries,
1652 22 May 07 gregory 470  so that distribution is permitted only in or among countries not thus
1652 22 May 07 gregory 471  excluded.  In such case, this License incorporates the limitation as if
1652 22 May 07 gregory 472  written in the body of this License.
741 10 Oct 06 olle 473
1652 22 May 07 gregory 474  13. The Free Software Foundation may publish revised and/or new
1652 22 May 07 gregory 475  versions of the Lesser General Public License from time to time.
1652 22 May 07 gregory 476  Such new versions will be similar in spirit to the present version,
1652 22 May 07 gregory 477  but may differ in detail to address new problems or concerns.
741 10 Oct 06 olle 478
1652 22 May 07 gregory 479  Each version is given a distinguishing version number.  If the Library
1652 22 May 07 gregory 480  specifies a version number of this License which applies to it and
1652 22 May 07 gregory 481  "any later version", you have the option of following the terms and
1652 22 May 07 gregory 482  conditions either of that version or of any later version published by
1652 22 May 07 gregory 483  the Free Software Foundation.  If the Library does not specify a
1652 22 May 07 gregory 484  license version number, you may choose any version ever published by
1652 22 May 07 gregory 485  the Free Software Foundation.
741 10 Oct 06 olle 486
1652 22 May 07 gregory 487  14. If you wish to incorporate parts of the Library into other free
1652 22 May 07 gregory 488  programs whose distribution conditions are incompatible with these,
1652 22 May 07 gregory 489  write to the author to ask for permission.  For software which is
1652 22 May 07 gregory 490  copyrighted by the Free Software Foundation, write to the Free
1652 22 May 07 gregory 491  Software Foundation; we sometimes make exceptions for this.  Our
1652 22 May 07 gregory 492  decision will be guided by the two goals of preserving the free status
1652 22 May 07 gregory 493  of all derivatives of our free software and of promoting the sharing
1652 22 May 07 gregory 494  and reuse of software generally.
741 10 Oct 06 olle 495
1652 22 May 07 gregory 496  NO WARRANTY
741 10 Oct 06 olle 497
1652 22 May 07 gregory 498  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
1652 22 May 07 gregory 499  WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
1652 22 May 07 gregory 500  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
1652 22 May 07 gregory 501  OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
1652 22 May 07 gregory 502  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
1652 22 May 07 gregory 503  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1652 22 May 07 gregory 504  PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
1652 22 May 07 gregory 505  LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
1652 22 May 07 gregory 506  THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
741 10 Oct 06 olle 507
1652 22 May 07 gregory 508  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
1652 22 May 07 gregory 509  WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
1652 22 May 07 gregory 510  AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
1652 22 May 07 gregory 511  FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
1652 22 May 07 gregory 512  CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
1652 22 May 07 gregory 513  LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
1652 22 May 07 gregory 514  RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
1652 22 May 07 gregory 515  FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
1652 22 May 07 gregory 516  SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1652 22 May 07 gregory 517  DAMAGES.
741 10 Oct 06 olle 518
1652 22 May 07 gregory 519  END OF TERMS AND CONDITIONS
741 10 Oct 06 olle 520
1652 22 May 07 gregory 521  How to Apply These Terms to Your New Libraries
741 10 Oct 06 olle 522
1652 22 May 07 gregory 523  If you develop a new library, and you want it to be of the greatest
1652 22 May 07 gregory 524  possible use to the public, we recommend making it free software that
1652 22 May 07 gregory 525  everyone can redistribute and change.  You can do so by permitting
1652 22 May 07 gregory 526  redistribution under these terms (or, alternatively, under the terms of the
1652 22 May 07 gregory 527  ordinary General Public License).
741 10 Oct 06 olle 528
1652 22 May 07 gregory 529  To apply these terms, attach the following notices to the library.  It is
1652 22 May 07 gregory 530  safest to attach them to the start of each source file to most effectively
1652 22 May 07 gregory 531  convey the exclusion of warranty; and each file should have at least the
1652 22 May 07 gregory 532  "copyright" line and a pointer to where the full notice is found.
741 10 Oct 06 olle 533
1652 22 May 07 gregory 534  <one line to give the library's name and a brief idea of what it does.>
1652 22 May 07 gregory 535  Copyright (C) <year>  <name of author>
741 10 Oct 06 olle 536
1652 22 May 07 gregory 537  This library is free software; you can redistribute it and/or
1652 22 May 07 gregory 538  modify it under the terms of the GNU Lesser General Public
1652 22 May 07 gregory 539  License as published by the Free Software Foundation; either
1652 22 May 07 gregory 540  version 2.1 of the License, or (at your option) any later version.
741 10 Oct 06 olle 541
1652 22 May 07 gregory 542  This library is distributed in the hope that it will be useful,
1652 22 May 07 gregory 543  but WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 544  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 545  Lesser General Public License for more details.
741 10 Oct 06 olle 546
1652 22 May 07 gregory 547  You should have received a copy of the GNU Lesser General Public
1652 22 May 07 gregory 548  License along with this library; if not, write to the Free Software
1652 22 May 07 gregory 549  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
741 10 Oct 06 olle 550
1652 22 May 07 gregory 551  Also add information on how to contact you by electronic and paper mail.
741 10 Oct 06 olle 552
1652 22 May 07 gregory 553  You should also get your employer (if you work as a programmer) or your
1652 22 May 07 gregory 554  school, if any, to sign a "copyright disclaimer" for the library, if
1652 22 May 07 gregory 555  necessary.  Here is a sample; alter the names:
741 10 Oct 06 olle 556
1652 22 May 07 gregory 557  Yoyodyne, Inc., hereby disclaims all copyright interest in the
1652 22 May 07 gregory 558  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
741 10 Oct 06 olle 559
1652 22 May 07 gregory 560  <signature of Ty Coon>, 1 April 1990
1652 22 May 07 gregory 561  Ty Coon, President of Vice
741 10 Oct 06 olle 562
1652 22 May 07 gregory 563  That's all there is to it!
741 10 Oct 06 olle 564
741 10 Oct 06 olle 565
1652 22 May 07 gregory 566  *******************************************************************************/
741 10 Oct 06 olle 567
1652 22 May 07 gregory 568 package se.lu.thep.coreftpd.common;
741 10 Oct 06 olle 569
741 10 Oct 06 olle 570 import java.util.BitSet;
741 10 Oct 06 olle 571
741 10 Oct 06 olle 572 /**
1652 22 May 07 gregory 573  * 
1652 22 May 07 gregory 574  * <B>About this class:</B> <BR>
741 10 Oct 06 olle 575  * Contains a few very useful methods for unescaping <CODE>String</CODE>s.
1652 22 May 07 gregory 576  * 
741 10 Oct 06 olle 577  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 578  * @version 1.0
741 10 Oct 06 olle 579  */
741 10 Oct 06 olle 580
1652 22 May 07 gregory 581 final public class UnescapeMethods {
741 10 Oct 06 olle 582
1652 22 May 07 gregory 583   private static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
1652 22 May 07 gregory 584       '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
741 10 Oct 06 olle 585
1652 22 May 07 gregory 586   private static final int enc(char[] out, int pos, int c) {
1652 22 May 07 gregory 587     out[pos++] = '%';
1652 22 May 07 gregory 588     out[pos++] = hex[(c >> 4) & 0xf];
1652 22 May 07 gregory 589     out[pos++] = hex[c & 0xf];
1652 22 May 07 gregory 590     return pos;
1652 22 May 07 gregory 591   }
741 10 Oct 06 olle 592
1652 22 May 07 gregory 593   // REMOVED: * @exception ParseException if the two digits following a `%'
1652 22 May 07 gregory 594   // are
1652 22 May 07 gregory 595   // * not a valid hex number
1652 22 May 07 gregory 596   /**
1652 22 May 07 gregory 597    * Unescape escaped characters (i.e. %xx) except reserved ones.
1652 22 May 07 gregory 598    * 
1652 22 May 07 gregory 599    * @param str
1652 22 May 07 gregory 600    *            the string to unescape
1652 22 May 07 gregory 601    * @param reserved
1652 22 May 07 gregory 602    *            the characters which may not be unescaped, or null
1652 22 May 07 gregory 603    * @return the unescaped string
1652 22 May 07 gregory 604    */
741 10 Oct 06 olle 605
1652 22 May 07 gregory 606   public static final String unescape(String str, BitSet reserved)
1652 22 May 07 gregory 607   // throws ParseException
1652 22 May 07 gregory 608   {
1652 22 May 07 gregory 609     if (str == null || str.indexOf('%') == -1)
1652 22 May 07 gregory 610       return str; // an optimization
741 10 Oct 06 olle 611
1652 22 May 07 gregory 612     char[] buf = str.toCharArray();
1652 22 May 07 gregory 613     char[] res = new char[buf.length];
741 10 Oct 06 olle 614
1652 22 May 07 gregory 615     char[] utf = new char[4];
1652 22 May 07 gregory 616     int utf_idx = 0, utf_len = -1;
1652 22 May 07 gregory 617     int didx = 0;
1652 22 May 07 gregory 618     for (int sidx = 0; sidx < buf.length; sidx++) {
1652 22 May 07 gregory 619       if (buf[sidx] == '%') {
1652 22 May 07 gregory 620         int ch;
1652 22 May 07 gregory 621         try {
1652 22 May 07 gregory 622           if (sidx + 3 > buf.length)
1652 22 May 07 gregory 623             throw new NumberFormatException();
1652 22 May 07 gregory 624           ch = Integer
1652 22 May 07 gregory 625               .parseInt(str.substring(sidx + 1, sidx + 3), 16);
1652 22 May 07 gregory 626           if (ch < 0)
1652 22 May 07 gregory 627             throw new NumberFormatException();
1652 22 May 07 gregory 628           sidx += 2;
1652 22 May 07 gregory 629         } catch (NumberFormatException e) {
1652 22 May 07 gregory 630           /*
1652 22 May 07 gregory 631            * Hmm, people not reading specs again, so we just ignore
1652 22 May 07 gregory 632            * it... throw new ParseException(str.substring(sidx,sidx+3) + "
1652 22 May 07 gregory 633            * is an invalid code");
1652 22 May 07 gregory 634            */
1652 22 May 07 gregory 635           ch = buf[sidx];
1652 22 May 07 gregory 636         }
741 10 Oct 06 olle 637
1652 22 May 07 gregory 638         // check if we're working on a utf-char
1652 22 May 07 gregory 639         if (utf_len > 0) {
1652 22 May 07 gregory 640           if ((ch & 0xC0) != 0x80) // oops, we misinterpreted
1652 22 May 07 gregory 641           {
1652 22 May 07 gregory 642             didx = copyBuf(utf, utf_idx, ch, res, didx, reserved,
1652 22 May 07 gregory 643                 false);
1652 22 May 07 gregory 644             utf_len = -1;
1652 22 May 07 gregory 645           } else if (utf_idx == utf_len - 1) // end-of-char
1652 22 May 07 gregory 646           {
1652 22 May 07 gregory 647             if ((utf[0] & 0xE0) == 0xC0)
1652 22 May 07 gregory 648               ch = (utf[0] & 0x1F) << 6 | (ch & 0x3F);
1652 22 May 07 gregory 649             else if ((utf[0] & 0xF0) == 0xE0)
1652 22 May 07 gregory 650               ch = (utf[0] & 0x0F) << 12 | (utf[1] & 0x3F) << 6
1652 22 May 07 gregory 651                   | (ch & 0x3F);
1652 22 May 07 gregory 652             else
1652 22 May 07 gregory 653               ch = (utf[0] & 0x07) << 18 | (utf[1] & 0x3F) << 12
1652 22 May 07 gregory 654                   | (utf[2] & 0x3F) << 6 | (ch & 0x3F);
1652 22 May 07 gregory 655             if (reserved != null && reserved.get(ch))
1652 22 May 07 gregory 656               didx = copyBuf(utf, utf_idx, ch, res, didx, null,
1652 22 May 07 gregory 657                   true);
1652 22 May 07 gregory 658             else if (utf_len < 4)
1652 22 May 07 gregory 659               res[didx++] = (char) ch;
1652 22 May 07 gregory 660             else {
1652 22 May 07 gregory 661               ch -= 0x10000;
1652 22 May 07 gregory 662               res[didx++] = (char) ((ch >> 10) | 0xD800);
1652 22 May 07 gregory 663               res[didx++] = (char) ((ch & 0x03FF) | 0xDC00);
1652 22 May 07 gregory 664             }
1652 22 May 07 gregory 665             utf_len = -1;
1652 22 May 07 gregory 666           } else
1652 22 May 07 gregory 667             // continue
1652 22 May 07 gregory 668             utf[utf_idx++] = (char) ch;
1652 22 May 07 gregory 669         }
1652 22 May 07 gregory 670         // check if this is the start of a utf-char
1652 22 May 07 gregory 671         else if ((ch & 0xE0) == 0xC0 || (ch & 0xF0) == 0xE0
1652 22 May 07 gregory 672             || (ch & 0xF8) == 0xF0) {
1652 22 May 07 gregory 673           if ((ch & 0xE0) == 0xC0)
1652 22 May 07 gregory 674             utf_len = 2;
1652 22 May 07 gregory 675           else if ((ch & 0xF0) == 0xE0)
1652 22 May 07 gregory 676             utf_len = 3;
1652 22 May 07 gregory 677           else
1652 22 May 07 gregory 678             utf_len = 4;
1652 22 May 07 gregory 679           utf[0] = (char) ch;
1652 22 May 07 gregory 680           utf_idx = 1;
1652 22 May 07 gregory 681         }
1652 22 May 07 gregory 682         // leave reserved alone
1652 22 May 07 gregory 683         else if (reserved != null && reserved.get(ch)) {
1652 22 May 07 gregory 684           res[didx++] = buf[sidx];
1652 22 May 07 gregory 685           sidx -= 2;
1652 22 May 07 gregory 686         }
1652 22 May 07 gregory 687         // just use the decoded version
1652 22 May 07 gregory 688         else
1652 22 May 07 gregory 689           res[didx++] = (char) ch;
1652 22 May 07 gregory 690       } else if (utf_len > 0) // oops, we misinterpreted
1652 22 May 07 gregory 691       {
1652 22 May 07 gregory 692         didx = copyBuf(utf, utf_idx, buf[sidx], res, didx, reserved,
1652 22 May 07 gregory 693             false);
1652 22 May 07 gregory 694         utf_len = -1;
1652 22 May 07 gregory 695       } else
1652 22 May 07 gregory 696         res[didx++] = buf[sidx];
1652 22 May 07 gregory 697     }
1652 22 May 07 gregory 698     if (utf_len > 0) // oops, we misinterpreted
1652 22 May 07 gregory 699       didx = copyBuf(utf, utf_idx, -1, res, didx, reserved, false);
741 10 Oct 06 olle 700
1652 22 May 07 gregory 701     return new String(res, 0, didx);
1652 22 May 07 gregory 702   }
741 10 Oct 06 olle 703
1652 22 May 07 gregory 704   private static final int copyBuf(char[] utf, int utf_idx, int ch,
1652 22 May 07 gregory 705       char[] res, int didx, BitSet reserved, boolean escapeAll) {
1652 22 May 07 gregory 706     if (ch >= 0)
1652 22 May 07 gregory 707       utf[utf_idx++] = (char) ch;
741 10 Oct 06 olle 708
1652 22 May 07 gregory 709     for (int idx = 0; idx < utf_idx; idx++) {
1652 22 May 07 gregory 710       if (reserved != null && reserved.get(utf[idx]) || escapeAll)
1652 22 May 07 gregory 711         didx = enc(res, didx, utf[idx]);
741 10 Oct 06 olle 712       else
1652 22 May 07 gregory 713         res[didx++] = utf[idx];
741 10 Oct 06 olle 714     }
741 10 Oct 06 olle 715
1652 22 May 07 gregory 716     return didx;
741 10 Oct 06 olle 717   }
741 10 Oct 06 olle 718
1652 22 May 07 gregory 719   /**
1652 22 May 07 gregory 720    * Unescape escaped characters (i.e. %xx). If a ParseException would be
1652 22 May 07 gregory 721    * thrown then just return the original string.
1652 22 May 07 gregory 722    * 
1652 22 May 07 gregory 723    * @param str
1652 22 May 07 gregory 724    *            the string to unescape
1652 22 May 07 gregory 725    * @param reserved
1652 22 May 07 gregory 726    *            the characters which may not be unescaped, or null
1652 22 May 07 gregory 727    * @return the unescaped string, or the original string if unescaping would
1652 22 May 07 gregory 728    *         throw a ParseException
1652 22 May 07 gregory 729    * @see #unescape(java.lang.String, java.util.BitSet)
1652 22 May 07 gregory 730    */
1652 22 May 07 gregory 731   /*
1652 22 May 07 gregory 732    * public static final String unescapeNoPE(String str, BitSet reserved) { //
1652 22 May 07 gregory 733    * try { return unescape(str, reserved); } // catch (ParseException pe) // {
1652 22 May 07 gregory 734    * System.out.println("OJ H�R BLEV DET VISST ERROR!");return str; } }
1652 22 May 07 gregory 735    */
741 10 Oct 06 olle 736
1652 22 May 07 gregory 737   /**
1652 22 May 07 gregory 738    * Escape any character not in the given character class. Characters greater
1652 22 May 07 gregory 739    * 255 are always escaped according to ??? .
1652 22 May 07 gregory 740    * 
1652 22 May 07 gregory 741    * @param elem
1652 22 May 07 gregory 742    *            the string to escape
1652 22 May 07 gregory 743    * @param allowed_char
1652 22 May 07 gregory 744    *            the BitSet of all allowed characters
1652 22 May 07 gregory 745    * @param utf8
1652 22 May 07 gregory 746    *            if true, will first UTF-8 encode unallowed characters
1652 22 May 07 gregory 747    * @return the string with all characters not in allowed_char escaped
1652 22 May 07 gregory 748    */
741 10 Oct 06 olle 749
1652 22 May 07 gregory 750   /*
1652 22 May 07 gregory 751    * We temporary remove this..... public static String escape(String elem,
1652 22 May 07 gregory 752    * BitSet allowed_char, boolean utf8) { if (elem==null) return null; else
1652 22 May 07 gregory 753    * return new String(escape(elem.toCharArray(), allowed_char, utf8)); }
1652 22 May 07 gregory 754    */
741 10 Oct 06 olle 755
1652 22 May 07 gregory 756   /**
1652 22 May 07 gregory 757    * Escape any character not in the given character class. Characters greater
1652 22 May 07 gregory 758    * 255 are always escaped according to ??? .
1652 22 May 07 gregory 759    * 
1652 22 May 07 gregory 760    * @param elem
1652 22 May 07 gregory 761    *            the array of characters to escape
1652 22 May 07 gregory 762    * @param allowed_char
1652 22 May 07 gregory 763    *            the BitSet of all allowed characters
1652 22 May 07 gregory 764    * @param utf8
1652 22 May 07 gregory 765    *            if true, will first UTF-8 encode unallowed characters
1652 22 May 07 gregory 766    * @return the elem array with all characters not in allowed_char escaped
1652 22 May 07 gregory 767    */
741 10 Oct 06 olle 768
1652 22 May 07 gregory 769   // We temporary remove this.....
1652 22 May 07 gregory 770   //
1652 22 May 07 gregory 771   // public static char[] escape(char[] elem, BitSet allowed_char, boolean
1652 22 May 07 gregory 772   // utf8)
1652 22 May 07 gregory 773   // {
1652 22 May 07 gregory 774   // int cnt=0;
1652 22 May 07 gregory 775   // for (int idx=0; idx<elem.length; idx++)
1652 22 May 07 gregory 776   // {
1652 22 May 07 gregory 777   // if (!allowed_char.get(elem[idx]))
1652 22 May 07 gregory 778   // {
1652 22 May 07 gregory 779   // cnt += 2;
1652 22 May 07 gregory 780   // if (utf8)
1652 22 May 07 gregory 781   // {
1652 22 May 07 gregory 782   // if (elem[idx] >= 0x0080)
1652 22 May 07 gregory 783   // cnt += 3;
1652 22 May 07 gregory 784   // if (elem[idx] >= 0x00800)
1652 22 May 07 gregory 785   // cnt += 3;
1652 22 May 07 gregory 786   // if ((elem[idx] & 0xFC00) == 0xD800 && idx+1 < elem.length &&
1652 22 May 07 gregory 787   // (elem[idx+1] & 0xFC00) == 0xDC00)
1652 22 May 07 gregory 788   // cnt -= 6;
1652 22 May 07 gregory 789   // }
1652 22 May 07 gregory 790   // }
1652 22 May 07 gregory 791   // }
1652 22 May 07 gregory 792   //
1652 22 May 07 gregory 793   // if (cnt == 0) return elem;
1652 22 May 07 gregory 794   //
1652 22 May 07 gregory 795   // char[] tmp = new char[elem.length + cnt];
1652 22 May 07 gregory 796   // for (int idx=0, pos=0; idx<elem.length; idx++)
1652 22 May 07 gregory 797   // {
1652 22 May 07 gregory 798   // char c = elem[idx];
1652 22 May 07 gregory 799   // if (allowed_char.get(c))
1652 22 May 07 gregory 800   // tmp[pos++] = c;
1652 22 May 07 gregory 801   // else if (utf8)
1652 22 May 07 gregory 802   // {
1652 22 May 07 gregory 803   // /* We're UTF-8 encoding the chars first, as recommended in
1652 22 May 07 gregory 804   // * the HTML 4.0 specification:
1652 22 May 07 gregory 805   // * http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.1
1652 22 May 07 gregory 806   // * Note that this doesn't change things for ASCII chars
1652 22 May 07 gregory 807   // */
1652 22 May 07 gregory 808   // if (c <= 0x007F)
1652 22 May 07 gregory 809   // {
1652 22 May 07 gregory 810   // pos = enc(tmp, pos, c);
1652 22 May 07 gregory 811   // }
1652 22 May 07 gregory 812   // else if (c <= 0x07FF)
1652 22 May 07 gregory 813   // {
1652 22 May 07 gregory 814   // pos = enc(tmp, pos, 0xC0 | ((c >> 6) & 0x1F));
1652 22 May 07 gregory 815   // pos = enc(tmp, pos, 0x80 | ((c >> 0) & 0x3F));
1652 22 May 07 gregory 816   // }
1652 22 May 07 gregory 817   // else if (!((c & 0xFC00) == 0xD800 && idx+1 < elem.length &&
1652 22 May 07 gregory 818   // (elem[idx+1] & 0xFC00) == 0xDC00))
1652 22 May 07 gregory 819   // {
1652 22 May 07 gregory 820   // pos = enc(tmp, pos, 0xE0 | ((c >> 12) & 0x0F));
1652 22 May 07 gregory 821   // pos = enc(tmp, pos, 0x80 | ((c >> 6) & 0x3F));
1652 22 May 07 gregory 822   // pos = enc(tmp, pos, 0x80 | ((c >> 0) & 0x3F));
1652 22 May 07 gregory 823   // }
1652 22 May 07 gregory 824   // else
1652 22 May 07 gregory 825   // {
1652 22 May 07 gregory 826   // int ch = ((c & 0x03FF) << 10) | (elem[++idx] & 0x03FF);
1652 22 May 07 gregory 827   // ch += 0x10000;
1652 22 May 07 gregory 828   // pos = enc(tmp, pos, 0xF0 | ((ch >> 18) & 0x07));
1652 22 May 07 gregory 829   // pos = enc(tmp, pos, 0x80 | ((ch >> 12) & 0x3F));
1652 22 May 07 gregory 830   // pos = enc(tmp, pos, 0x80 | ((ch >> 6) & 0x3F));
1652 22 May 07 gregory 831   // pos = enc(tmp, pos, 0x80 | ((ch >> 0) & 0x3F));
1652 22 May 07 gregory 832   // }
1652 22 May 07 gregory 833   // }
1652 22 May 07 gregory 834   // else
1652 22 May 07 gregory 835   // pos = enc(tmp, pos, c);
1652 22 May 07 gregory 836   // }
1652 22 May 07 gregory 837   //
1652 22 May 07 gregory 838   // return tmp;
1652 22 May 07 gregory 839   // }
741 10 Oct 06 olle 840 }