За последние 24 часа нас посетили 17799 программистов и 1683 робота. Сейчас ищут 977 программистов ...

ASP decode base64, utf-8

Тема в разделе "JavaScript и AJAX", создана пользователем my_house, 19 дек 2010.

  1. my_house

    my_house Активный пользователь

    С нами с:
    19 дек 2010
    Сообщения:
    1
    Симпатии:
    0
    Комрады, помогатие плз.
    Не знаю на правильном ли форуме написал.
    Какая-то ерунда с кодировкой уже весь мозг сломал.
    Не отображается корректно и всё тут...

    Код (Text):
    1. <% @CodePage=65001 %>  
    2. <%
    3. OPTION EXPLICIT
    4. Response.Buffer = True
    5. Response.Expires = 0
    6. Response.Charset = "utf-8"
    7. Response.ContentType = "text/html"
    8. Server.ScriptTimeout = 1000
    9. %>
    10. <!--#include file="common.inc.asp"-->
    11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    12. <html>
    13. <head>
    14.     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    15.     <title>Интернет-приемная мэрии НСО</title>
    16.    
    17. </head>
    18. <body> 
    19. <%
    20. Dim Email, SourceData, FormFields, Source
    21. Dim Field, FieldName, FieldContents
    22.  
    23. SourceData = "person=0JDQtNC80LjQvdC40YHRgtGA0LDRhtC40Y8g0JvQtdC90LjQvdGB0LrQvtCz0L4g0YAt0L3QsA%3D%3D&hidden_person=dmVnYXJ1bGV6QG5ncy5ydQ%3D%3D&family=0KPQo9Cj&name=0JrQmtCa&patronymic=0JXQldCV&scope=0JDRgdC%2F0LjRgNCw0L3RgtGL&hidden_scope=&novosib_check=b24%3D&phone=MTIzNDU2Nzg5&district=0JfQsNC10LvRjNGG0L7QstGB0LrQuNC5&hidden_district=&ZIP_code=&street=0JrQo9Ca0KPQmtCj&house=Nzg%3D&building=&flat=&email=&content=0KbQo9Ca0JXQoA%3D%3D"
    24. Source=SourceData
    25. SourceData = split(SourceData, "&")
    26.  
    27. For Each Field In SourceData
    28.   Field = split(Field, "=")
    29.   FieldName = URLDecode(Field(0))
    30.   if Trim(URLDecode(Field(1)))<>"" then
    31.    FieldContents =DecodeUTF8(Base64Decode(URLDecode(Field(1))))
    32.   Response.Write FieldContents&"</br>"
    33.   end if
    34.   FormFields = FormFields & FieldName & "=" & FieldContents & chr(13) & chr(10)
    35. Next
    36.  
    37.  
    38.  
    39. %>
    40. </body>
    41. </html>
    и сообственно файл перекодировки

    Код (Text):
    1. <%
    2. ' Simple functions to convert the first 256 characters
    3. ' of the Windows character set from and to UTF-8.
    4.  
    5. ' Written by Hans Kalle for Fisz
    6. ' http://www.fisz.nl
    7.  
    8. 'IsValidUTF8
    9. '  Tells if the string is valid UTF-8 encoded
    10. 'Returns:
    11. '  true (valid UTF-8)
    12. '  false (invalid UTF-8 or not UTF-8 encoded string)
    13. function IsValidUTF8(s)
    14.   dim i
    15.   dim c
    16.   dim n
    17.  
    18.   IsValidUTF8 = false
    19.   i = 1
    20.   do while i <= len(s)
    21.     c = asc(mid(s,i,1))
    22.     if c and &H80 then
    23.       n = 1
    24.       do while i + n < len(s)
    25.         if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then
    26.           exit do
    27.         end if
    28.         n = n + 1
    29.       loop
    30.       select case n
    31.       case 1
    32.         exit function
    33.       case 2
    34.         if (c and &HE0) <> &HC0 then
    35.           exit function
    36.         end if
    37.       case 3
    38.         if (c and &HF0) <> &HE0 then
    39.           exit function
    40.         end if
    41.       case 4
    42.         if (c and &HF8) <> &HF0 then
    43.           exit function
    44.         end if
    45.       case else
    46.         exit function
    47.       end select
    48.       i = i + n
    49.     else
    50.       i = i + 1
    51.     end if
    52.   loop
    53.   IsValidUTF8 = true
    54. end function
    55.  
    56. 'DecodeUTF8
    57. '  Decodes a UTF-8 string to the Windows character set
    58. '  Non-convertable characters are replace by an upside
    59. '  down question mark.
    60. 'Returns:
    61. '  A Windows string
    62. function DecodeUTF8(s)
    63.   dim i
    64.   dim c
    65.   dim n
    66.  
    67.   i = 1
    68.   do while i <= len(s)
    69.     c = asc(mid(s,i,1))
    70.     if c and &H80 then
    71.       n = 1
    72.       do while i + n < len(s)
    73.         if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then
    74.           exit do
    75.         end if
    76.         n = n + 1
    77.       loop
    78.       if n = 2 and ((c and &HE0) = &HC0) then
    79.         c = asc(mid(s,i+1,1)) + &H40 * (c and &H01)
    80.       else
    81.         c = 191
    82.       end if
    83.       s = left(s,i-1) + chr(c) + mid(s,i+n)
    84.     end if
    85.     i = i + 1
    86.   loop
    87.   DecodeUTF8 = s
    88. end function
    89.  
    90. 'EncodeUTF8
    91. '  Encodes a Windows string in UTF-8
    92. 'Returns:
    93. '  A UTF-8 encoded string
    94. function EncodeUTF8(s)
    95.   dim i
    96.   dim c
    97.  
    98.   i = 1
    99.   do while i <= len(s)
    100.     c = asc(mid(s,i,1))
    101.     if c >= &H80 then
    102.       s = left(s,i-1) + chr(&HC2 + ((c and &H40) / &H40)) + chr(c and &HBF) + mid(s,i+1)
    103.       i = i + 1
    104.     end if
    105.     i = i + 1
    106.   loop
    107.   EncodeUTF8 = s
    108. end function
    109.  
    110.  
    111. ' Decodes a base-64 encoded string (BSTR type).
    112. ' 1999 - 2004 Antonin Foller, http://www.motobit.com
    113. ' 1.01 - solves problem with Access And 'Compare Database' (InStr)
    114. Function Base64Decode(ByVal base64String)
    115.   'rfc1521
    116.   '1999 Antonin Foller, Motobit Software, http://Motobit.cz
    117.   Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    118.   Dim dataLength, sOut, groupBegin
    119.  
    120.   'remove white spaces, If any
    121.   base64String = Replace(base64String, vbCrLf, "")
    122.   base64String = Replace(base64String, vbTab, "")
    123.   base64String = Replace(base64String, " ", "")
    124.  
    125.   'The source must consists from groups with Len of 4 chars
    126.   dataLength = Len(base64String)
    127.   If dataLength Mod 4 <> 0 Then
    128.     Err.Raise 1, "Base64Decode", "Bad Base64 string."
    129.     Exit Function
    130.   End If
    131.  
    132.  
    133.   ' Now decode each group:
    134.   For groupBegin = 1 To dataLength Step 4
    135.     Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
    136.     ' Each data group encodes up To 3 actual bytes.
    137.     numDataBytes = 3
    138.     nGroup = 0
    139.  
    140.     For CharCounter = 0 To 3
    141.       ' Convert each character into 6 bits of data, And add it To
    142.       ' an integer For temporary storage.  If a character is a '=', there
    143.       ' is one fewer data byte.  (There can only be a maximum of 2 '=' In
    144.       ' the whole string.)
    145.  
    146.       thisChar = Mid(base64String, groupBegin + CharCounter, 1)
    147.  
    148.       If thisChar = "=" Then
    149.         numDataBytes = numDataBytes - 1
    150.         thisData = 0
    151.       Else
    152.         thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
    153.       End If
    154.       If thisData = -1 Then
    155.         Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
    156.         Exit Function
    157.       End If
    158.  
    159.       nGroup = 64 * nGroup + thisData
    160.     Next
    161.    
    162.     'Hex splits the long To 6 groups with 4 bits
    163.     nGroup = Hex(nGroup)
    164.    
    165.     'Add leading zeros
    166.     nGroup = String(6 - Len(nGroup), "0") & nGroup
    167.    
    168.     'Convert the 3 byte hex integer (6 chars) To 3 characters
    169.     pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
    170.       Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
    171.       Chr(CByte("&H" & Mid(nGroup, 5, 2)))
    172.    
    173.     'add numDataBytes characters To out string
    174.     sOut = sOut & Left(pOut, numDataBytes)
    175.   Next
    176.  
    177.   Base64Decode = sOut
    178. End Function
    179. %>