Занялся старым проектом, которому нужен Unicode. И тут выходит засада - не работает декодер base64, который мне понадобился для хранения картинок. Переписал старый пример, который написан каким-то нубом:
Procedure.s Base64EncodeFile(FileName.s) ; Encode a file to a Base64 string.
Protected FileID.l, FileSize.l, FileBuff.l, Base64Size.l, Base64Buff.s
FileID = ReadFile(#PB_Any, FileName)
If FileID
FileSize = Lof(FileID)
FileBuff = AllocateMemory(FileSize)
If FileBuff
ReadData(FileID, FileBuff, FileSize)
Base64Size = Int(1.35 * FileSize)
;Base64Buff = Space(Base64Size) ; 35% bigger than the original
*Base64Buff = AllocateMemory(Base64Size)
Base64Encoder(FileBuff, FileSize, *Base64Buff, Base64Size)
Base64Buff.s=PeekS(*Base64Buff,-1,#PB_Ascii)
;ShowMemoryViewer(*Base64Buff,1024)
FreeMemory(*Base64Buff)
FreeMemory(FileBuff)
EndIf
CloseFile(FileID)
EndIf
ProcedureReturn Base64Buff
EndProcedure
Procedure.l Base64CatchImage(Image.l, Base64.s, flags.l = 0) ; Create a new image from a Base64 string.
Protected Base64Size.l, ImageBuff.l, ImageSize.l, result.l
Base64Size = Len(Base64)
ImageBuff = AllocateMemory(Base64Size)
If ImageBuff
ImageSize = Base64Decoder(@Base64, Base64Size, ImageBuff, Base64Size)
If ImageSize
result = CatchImage(Image, ImageBuff, ImageSize, flags)
EndIf
FreeMemory(ImageBuff)
EndIf
ProcedureReturn result
EndProcedure
; ;Store image into a database
; If DatabaseUpdate(0, "INSERT INTO tbl_image (id,base64) VALUES('test.bmp','" + Base64EncodeFile("test.bmp") + "')")
; ; Success
; EndIf
;
; ;Restore image from a database
; If DatabaseQuery(0, "SELECT base64 FROM tbl_image WHERE id='test.bmp'")
; If NextDatabaseRow(0)
; If Base64CatchImage(0, GetDatabaseString(0,0))
; ; Success
; EndIf
; EndIf
; EndIf
i$=Base64EncodeFile("voyager.ico")
Base64Size=Len(i$)
*base64=AllocateMemory(Base64Size)
*ib=AllocateMemory(Base64Size+1)
For i=1 To Base64Size
PokeA(*ib+i-1,Asc(Mid(i$,i,1)))
Next i
;ShowMemoryViewer(*ib,1024)
ImageSize = Base64Decoder(*ib, Base64Size,*Base64, Base64Size)
;ShowMemoryViewer(*base64,1024)
Debug CatchImage(0,*base64)
FreeMemory(*ib)
FreeMemory(*base64)
Protected FileID.l, FileSize.l, FileBuff.l, Base64Size.l, Base64Buff.s
FileID = ReadFile(#PB_Any, FileName)
If FileID
FileSize = Lof(FileID)
FileBuff = AllocateMemory(FileSize)
If FileBuff
ReadData(FileID, FileBuff, FileSize)
Base64Size = Int(1.35 * FileSize)
;Base64Buff = Space(Base64Size) ; 35% bigger than the original
*Base64Buff = AllocateMemory(Base64Size)
Base64Encoder(FileBuff, FileSize, *Base64Buff, Base64Size)
Base64Buff.s=PeekS(*Base64Buff,-1,#PB_Ascii)
;ShowMemoryViewer(*Base64Buff,1024)
FreeMemory(*Base64Buff)
FreeMemory(FileBuff)
EndIf
CloseFile(FileID)
EndIf
ProcedureReturn Base64Buff
EndProcedure
Procedure.l Base64CatchImage(Image.l, Base64.s, flags.l = 0) ; Create a new image from a Base64 string.
Protected Base64Size.l, ImageBuff.l, ImageSize.l, result.l
Base64Size = Len(Base64)
ImageBuff = AllocateMemory(Base64Size)
If ImageBuff
ImageSize = Base64Decoder(@Base64, Base64Size, ImageBuff, Base64Size)
If ImageSize
result = CatchImage(Image, ImageBuff, ImageSize, flags)
EndIf
FreeMemory(ImageBuff)
EndIf
ProcedureReturn result
EndProcedure
; ;Store image into a database
; If DatabaseUpdate(0, "INSERT INTO tbl_image (id,base64) VALUES('test.bmp','" + Base64EncodeFile("test.bmp") + "')")
; ; Success
; EndIf
;
; ;Restore image from a database
; If DatabaseQuery(0, "SELECT base64 FROM tbl_image WHERE id='test.bmp'")
; If NextDatabaseRow(0)
; If Base64CatchImage(0, GetDatabaseString(0,0))
; ; Success
; EndIf
; EndIf
; EndIf
i$=Base64EncodeFile("voyager.ico")
Base64Size=Len(i$)
*base64=AllocateMemory(Base64Size)
*ib=AllocateMemory(Base64Size+1)
For i=1 To Base64Size
PokeA(*ib+i-1,Asc(Mid(i$,i,1)))
Next i
;ShowMemoryViewer(*ib,1024)
ImageSize = Base64Decoder(*ib, Base64Size,*Base64, Base64Size)
;ShowMemoryViewer(*base64,1024)
Debug CatchImage(0,*base64)
FreeMemory(*ib)
FreeMemory(*base64)
Комментарии
Отправить комментарий