PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-2.0.0/src/rtl/base64.prg

#
Unknown | 119 lines | 103 code | 16 blank | 0 comment | 0 complexity | ada4bfada40209386f9f74893d4d00a6 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: base64.prg 11680 2009-07-09 13:03:59Z vszakats $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * HB_BASE64DECODE() function
  7. *
  8. * Based on VB code by: 1999-2004 Antonin Foller, http://www.motobit.com, http://motobit.cz
  9. * Converted to Clipper and optimized by Viktor Szakats (harbour.01 syenar.hu)
  10. * www - http://www.harbour-project.org
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this software; see the file COPYING. If not, write to
  24. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  25. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  26. *
  27. * As a special exception, the Harbour Project gives permission for
  28. * additional uses of the text contained in its release of Harbour.
  29. *
  30. * The exception is that, if you link the Harbour libraries with other
  31. * files to produce an executable, this does not by itself cause the
  32. * resulting executable to be covered by the GNU General Public License.
  33. * Your use of that executable is in no way restricted on account of
  34. * linking the Harbour library code into it.
  35. *
  36. * This exception does not however invalidate any other reasons why
  37. * the executable file might be covered by the GNU General Public License.
  38. *
  39. * This exception applies only to the code released by the Harbour
  40. * Project under the name Harbour. If you copy code from other
  41. * Harbour Project or Free Software Foundation releases into a copy of
  42. * Harbour, as the General Public License permits, the exception does
  43. * not apply to the code that you add in this way. To avoid misleading
  44. * anyone as to the status of such modified files, you must delete
  45. * this exception notice from them.
  46. *
  47. * If you write modifications of your own for Harbour, it is your choice
  48. * whether to permit this exception to apply to your modifications.
  49. * If you do not wish that, delete this exception notice.
  50. *
  51. */
  52. FUNCTION HB_BASE64DECODE( cString )
  53. LOCAL cResult
  54. LOCAL nLen
  55. LOCAL nGroupPos
  56. LOCAL nGroup
  57. LOCAL nCharPos
  58. LOCAL nDataLen
  59. LOCAL nData
  60. /* remove white spaces, If any */
  61. cString := StrTran( cString, Chr( 10 ) )
  62. cString := StrTran( cString, Chr( 13 ) )
  63. cString := StrTran( cString, Chr( 9 ) )
  64. cString := StrTran( cString, " " )
  65. /* The source must consists from groups with Len of 4 chars */
  66. IF ( nLen := Len( cString ) ) % 4 != 0
  67. RETURN "" /* Bad Base64 string */
  68. ENDIF
  69. #if 0
  70. IF nLen > Int( MAXSTRINGLENGTH / 1.34 ) /* Base64 is 1/3rd larger than source text. */
  71. RETURN "" /* Not enough memory to decode */
  72. ENDIF
  73. #endif
  74. cResult := ""
  75. /* Now decode each group: */
  76. FOR nGroupPos := 1 TO nLen STEP 4
  77. /* Each data group encodes up To 3 actual bytes */
  78. nDataLen := 3
  79. nGroup := 0
  80. FOR nCharPos := 0 TO 3
  81. /* Convert each character into 6 bits of data, And add it To
  82. an integer For temporary storage. If a character is a '=', there
  83. is one fewer data byte. (There can only be a maximum of 2 '=' In
  84. the whole string.) */
  85. nData := At( SubStr( cString, nGroupPos + nCharPos, 1 ), "=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) - 2
  86. DO CASE
  87. CASE nData >= 0
  88. /* Do nothing (for speed) */
  89. CASE nData == -1
  90. nData := 0
  91. nDataLen--
  92. CASE nData == -2
  93. RETURN "" /* Bad character In Base64 string */
  94. ENDCASE
  95. nGroup := 64 * nGroup + nData
  96. NEXT
  97. /* Convert the 24 bits to 3 characters
  98. and add nDataLen characters To out string */
  99. cResult += Left( Chr( nGroup / 65536 ) +; /* bitwise AND 255, which is done by Chr() automatically */
  100. Chr( nGroup / 256 ) +; /* bitwise AND 255, which is done by Chr() automatically */
  101. Chr( nGroup ), nDataLen ) /* bitwise AND 255, which is done by Chr() automatically */
  102. NEXT
  103. RETURN cResult