PowerBuilder Generate Base64 Without API
Instance Variable
1 | char code[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' |
Encode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | //==================================================================== // Function: n_base64.of_encode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value blob plain //-------------------------------------------------------------------- // Returns: string //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_encode ( blob plain ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== byte lbyte[] Char lc_cypher[] Long i,j,ll_loop ULong lul_len,lul_tmp Int li_mod Integer li_idx, li_max li_max = Len(plain) For li_idx = 1 To li_max lbyte[li_idx] = Byte(BlobMid(plain, li_idx, 1)) Next //lbyte = getByteArray(plain) //pb11.5 later lul_len = UpperBound(lbyte) li_mod = Mod(lul_len,3) Choose Case li_mod Case 1 lbyte[lul_len + 1] = 0 lbyte[lul_len + 2] = 0 Case 2 lbyte[lul_len + 1] = 0 End Choose ll_loop = UpperBound(lbyte) / 3 For i = 1 To ll_loop lul_tmp = lbyte[3 * i - 2] * 65536 + lbyte[3 * i - 1] * 256 + lbyte[3 * i] j += 1 lc_cypher[j] = code[lul_tmp / 262144 + 1] lul_tmp = Mod(lul_tmp,262144) j += 1 lc_cypher[j] = code[lul_tmp / 4096 + 1] lul_tmp = Mod(lul_tmp,4096) j += 1 lc_cypher[j] = code[lul_tmp / 64 + 1] lul_tmp = Mod(lul_tmp,64) j += 1 lc_cypher[j] = code[lul_tmp + 1] Next Choose Case li_mod Case 1 lc_cypher[j] = '=' lc_cypher[j - 1] = '=' Case 2 lc_cypher[j] = '=' End Choose Return lc_cypher |
Decode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | //==================================================================== // Function: n_base64.of_decode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string cypher // value Encoding aEncoding //-------------------------------------------------------------------- // Returns: blob //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_decode ( string cypher, encoding aencoding ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Blob lblb_plain Long i,j,ll_loop Char lc_cypher[] ULong lul_tmp,lul_len byte lbyte[] Int li_mod lc_cypher = cypher lul_len = UpperBound(lc_cypher) If lc_cypher[lul_len] = '=' Then li_mod = 2 lc_cypher[lul_len] = 'A' End If If lc_cypher[lul_len - 1] = '=' Then li_mod = 1 lc_cypher[lul_len - 1] = 'A' End If ll_loop = UpperBound(lc_cypher) / 4 For i = 1 To ll_loop lul_tmp = (Pos(code,lc_cypher[4 * i - 3]) - 1) * 262144 +& (Pos(code,lc_cypher[4 * i - 2]) - 1) * 4096 +& (Pos(code,lc_cypher[4 * i - 1]) - 1) * 64 +& (Pos(code,lc_cypher[4 * i]) - 1) j += 1 lbyte[j] = lul_tmp / 65536 lul_tmp = Mod(lul_tmp,65536) j += 1 lbyte[j] = lul_tmp / 256 lul_tmp = Mod(lul_tmp,256) j += 1 lbyte[j] = lul_tmp Next //lblb_plain = blob(lbyte) //pb11.5 later Long lBlobLen Long lBytePos lBlobLen = UpperBound(lbyte) lblb_plain = Blob(Space(lBlobLen),aEncoding) For lBytePos = 1 To lBlobLen BlobEdit(lblb_plain, lBytePos, lbyte[lBytePos], aEncoding) Next Choose Case li_mod Case 1 lblb_plain = BlobMid(lblb_plain,1,Len(lblb_plain) - 2) Case 2 lblb_plain = BlobMid(lblb_plain,1,Len(lblb_plain) - 1) End Choose Return String(lblb_plain, aEncoding) |
Example Source
n_base64 from nonvisualobject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | forward global type n_base64 from nonvisualobject end type end forward global type n_base64 from nonvisualobject autoinstantiate end type type variables private: char code[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' end variables forward prototypes public function string of_encode (blob plain) public function string of_encode (string as_plain) public function string of_encode (string as_plain, encoding aencoding) public function string of_decode (string cypher, encoding aencoding) public function string of_decode (string cypher) end prototypes public function string of_encode (blob plain);//==================================================================== // Function: n_base64.of_encode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value blob plain //-------------------------------------------------------------------- // Returns: string //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_encode ( blob plain ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== byte lbyte[] Char lc_cypher[] Long i,j,ll_loop ULong lul_len,lul_tmp Int li_mod Integer li_idx, li_max li_max = Len(plain) For li_idx = 1 To li_max lbyte[li_idx] = Byte(BlobMid(plain, li_idx, 1)) Next //lbyte = getByteArray(plain) //pb11.5 later lul_len = UpperBound(lbyte) li_mod = Mod(lul_len,3) Choose Case li_mod Case 1 lbyte[lul_len + 1] = 0 lbyte[lul_len + 2] = 0 Case 2 lbyte[lul_len + 1] = 0 End Choose ll_loop = UpperBound(lbyte) / 3 For i = 1 To ll_loop lul_tmp = lbyte[3 * i - 2] * 65536 + lbyte[3 * i - 1] * 256 + lbyte[3 * i] j += 1 lc_cypher[j] = code[lul_tmp / 262144 + 1] lul_tmp = Mod(lul_tmp,262144) j += 1 lc_cypher[j] = code[lul_tmp / 4096 + 1] lul_tmp = Mod(lul_tmp,4096) j += 1 lc_cypher[j] = code[lul_tmp / 64 + 1] lul_tmp = Mod(lul_tmp,64) j += 1 lc_cypher[j] = code[lul_tmp + 1] Next Choose Case li_mod Case 1 lc_cypher[j] = '=' lc_cypher[j - 1] = '=' Case 2 lc_cypher[j] = '=' End Choose Return lc_cypher end function public function string of_encode (string as_plain);//==================================================================== // Function: n_base64.of_encode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_plain //-------------------------------------------------------------------- // Returns: string //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_encode ( string as_plain ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Blob lb_data lb_data = Blob(as_plain, EncodingUTF8!) Return of_encode(lb_data) end function public function string of_encode (string as_plain, encoding aencoding);//==================================================================== // Function: n_base64.of_encode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_plain // value encoding aencoding //-------------------------------------------------------------------- // Returns: string //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_encode ( string as_plain, encoding aencoding ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Blob lb_data lb_data = Blob(as_plain, aEncoding) Return of_encode(lb_data) end function public function string of_decode (string cypher, encoding aencoding);//==================================================================== // Function: n_base64.of_decode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string cypher // value Encoding aEncoding //-------------------------------------------------------------------- // Returns: blob //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_decode ( string cypher, encoding aencoding ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Blob lblb_plain Long i,j,ll_loop Char lc_cypher[] ULong lul_tmp,lul_len byte lbyte[] Int li_mod lc_cypher = cypher lul_len = UpperBound(lc_cypher) If lc_cypher[lul_len] = '=' Then li_mod = 2 lc_cypher[lul_len] = 'A' End If If lc_cypher[lul_len - 1] = '=' Then li_mod = 1 lc_cypher[lul_len - 1] = 'A' End If ll_loop = UpperBound(lc_cypher) / 4 For i = 1 To ll_loop lul_tmp = (Pos(code,lc_cypher[4 * i - 3]) - 1) * 262144 +& (Pos(code,lc_cypher[4 * i - 2]) - 1) * 4096 +& (Pos(code,lc_cypher[4 * i - 1]) - 1) * 64 +& (Pos(code,lc_cypher[4 * i]) - 1) j += 1 lbyte[j] = lul_tmp / 65536 lul_tmp = Mod(lul_tmp,65536) j += 1 lbyte[j] = lul_tmp / 256 lul_tmp = Mod(lul_tmp,256) j += 1 lbyte[j] = lul_tmp Next //lblb_plain = blob(lbyte) //pb11.5 later Long lBlobLen Long lBytePos lBlobLen = UpperBound(lbyte) lblb_plain = Blob(Space(lBlobLen),aEncoding) For lBytePos = 1 To lBlobLen BlobEdit(lblb_plain, lBytePos, lbyte[lBytePos], aEncoding) Next Choose Case li_mod Case 1 lblb_plain = BlobMid(lblb_plain,1,Len(lblb_plain) - 2) Case 2 lblb_plain = BlobMid(lblb_plain,1,Len(lblb_plain) - 1) End Choose Return String(lblb_plain, aEncoding) end function public function string of_decode (string cypher);//==================================================================== // Function: n_base64.of_decode() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string cypher //-------------------------------------------------------------------- // Returns: string //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/01/09 //-------------------------------------------------------------------- // Usage: n_base64.of_decode ( string cypher ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Return of_decode(cypher, EncodingUTF8!) end function on n_base64.create call super::create TriggerEvent( this, "constructor" ) end on on n_base64.destroy TriggerEvent( this, "destructor" ) call super::destroy end on |
w_main from window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | forward global type w_main from window end type type st_3 from statictext within w_main end type type st_2 from statictext within w_main end type type st_1 from statictext within w_main end type type ddlb_encoding from dropdownlistbox within w_main end type type mle_output from multilineedit within w_main end type type mle_input from multilineedit within w_main end type type cb_decode from commandbutton within w_main end type type cb_encode from commandbutton within w_main end type end forward global type w_main from window integer width = 2318 integer height = 1328 boolean titlebar = true string title = "PowerBuilder Generate Base64 Without API" boolean controlmenu = true boolean minbox = true boolean maxbox = true boolean resizable = true long backcolor = 67108864 string icon = "AppIcon!" boolean center = true st_3 st_3 st_2 st_2 st_1 st_1 ddlb_encoding ddlb_encoding mle_output mle_output mle_input mle_input cb_decode cb_decode cb_encode cb_encode end type global w_main w_main type prototypes Function Boolean CryptBinaryToString ( Blob pbBinary, ULong cbBinary, ULong dwFlags, Ref String pszString, Ref ULong pcchString ) Library "crypt32.dll" Alias For "CryptBinaryToStringW" Function Boolean CryptBinaryToString ( Blob pbBinary, ULong cbBinary, ULong dwFlags, Long pszString, Ref ULong pcchString ) Library "crypt32.dll" Alias For "CryptBinaryToStringW" Function Boolean CryptStringToBinary ( String pszString, ULong cchString, ULong dwFlags, Ref Blob pbBinary, Ref ULong pcbBinary, Ref ULong pdwSkip, Ref ULong pdwFlags ) Library "crypt32.dll" Alias For "CryptStringToBinaryW" end prototypes forward prototypes public function encoding wf_getencoding () end prototypes public function encoding wf_getencoding ();Encoding lEncoding lEncoding = EncodingANSI! //default If ddlb_encoding.Text = "EncodingANSI!" Then lEncoding = EncodingANSI! ElseIf ddlb_encoding.Text = "EncodingUTF8!" Then lEncoding = EncodingUTF8! ElseIf ddlb_encoding.Text = "EncodingUTF16LE!" Then lEncoding = EncodingUTF16LE! ElseIf ddlb_encoding.Text = "EncodingUTF16BE!" Then lEncoding = EncodingUTF16BE! End If Return lEncoding end function on w_main.create this.st_3=create st_3 this.st_2=create st_2 this.st_1=create st_1 this.ddlb_encoding=create ddlb_encoding this.mle_output=create mle_output this.mle_input=create mle_input this.cb_decode=create cb_decode this.cb_encode=create cb_encode this.Control[]={this.st_3,& this.st_2,& this.st_1,& this.ddlb_encoding,& this.mle_output,& this.mle_input,& this.cb_decode,& this.cb_encode} end on on w_main.destroy destroy(this.st_3) destroy(this.st_2) destroy(this.st_1) destroy(this.ddlb_encoding) destroy(this.mle_output) destroy(this.mle_input) destroy(this.cb_decode) destroy(this.cb_encode) end on type st_3 from statictext within w_main integer x = 64 integer y = 696 integer width = 343 integer height = 76 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 string text = "Output Text:" boolean focusrectangle = false end type type st_2 from statictext within w_main integer x = 64 integer y = 180 integer width = 329 integer height = 76 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 string text = "Input Text:" boolean focusrectangle = false end type type st_1 from statictext within w_main integer x = 50 integer y = 44 integer width = 315 integer height = 76 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 string text = "Encoding:" boolean focusrectangle = false end type type ddlb_encoding from dropdownlistbox within w_main integer x = 366 integer y = 40 integer width = 914 integer height = 400 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 boolean sorted = false string item[] = {"EncodingANSI!","EncodingUTF8!","EncodingUTF16LE!","EncodingUTF16BE!"} borderstyle borderstyle = stylelowered! end type event constructor;ddlb_encoding.selectitem( 2) end event type mle_output from multilineedit within w_main integer x = 50 integer y = 784 integer width = 2167 integer height = 400 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 borderstyle borderstyle = stylelowered! end type type mle_input from multilineedit within w_main integer x = 50 integer y = 260 integer width = 2167 integer height = 400 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 string text = "https://pblib.com/" borderstyle borderstyle = stylelowered! end type type cb_decode from commandbutton within w_main integer x = 1664 integer y = 40 integer width = 297 integer height = 96 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Decode" end type event clicked;n_base64 ln_base64 String ls_input, ls_out Encoding lEncoding lEncoding = wf_getencoding( ) ls_input = mle_input.Text ls_out = ln_base64.of_decode( ls_input, lEncoding) mle_output.Text = ls_out end event type cb_encode from commandbutton within w_main integer x = 1353 integer y = 40 integer width = 297 integer height = 96 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Encode" end type event clicked;n_base64 ln_base64 String ls_input, ls_out Encoding lEncoding lEncoding = wf_getencoding( ) ls_input = mle_input.Text ls_out = ln_base64.of_encode( ls_input , lEncoding) mle_output.Text = ls_out end event |
Find Projects On Github click here
Good Luck!
Thanks for you ? ☺ ❤ ?