0 | module Data.String.Base64
3 | import Data.Buffer.Core
4 | import Data.Buffer.Indexed
5 | import Data.List as List
9 | setBytes : Buffer -> Int -> List Bits8 -> IO Unit
10 | setBytes buf offset [] = pure ()
11 | setBytes buf offset (x :: xs) = do
12 | Buffer.setBits8 buf offset x
13 | setBytes buf (offset + 1) xs
17 | base64EncodeLength: Int -> Int
18 | base64EncodeLength a = (a+2) `div` 3 * 4
22 | base64DecodeLength: Int -> Int
23 | base64DecodeLength a = a `div` 4 * 3 + 2
25 | %foreign "C:fast_avx512bw_base64_encode,libbase64-idris"
26 | prim__enc: Buffer -> Buffer -> Int -> PrimIO Int
28 | %foreign "C:fast_avx512bw_base64_decode,libbase64-idris"
29 | prim__dec: Buffer -> Buffer -> Int -> PrimIO Int
35 | base64EncodeBuffer: Buffer -> Buffer -> Int -> IO Int
36 | base64EncodeBuffer dest str len = primIO $
prim__enc dest str len
39 | base64Encode: List Bits8 -> List Bits8
40 | base64Encode inp = unsafePerformIO $
do
43 | len = cast $
the Nat $
List.length inp
44 | Just destBuf <- newBuffer (base64EncodeLength len)
45 | | Nothing => pure []
46 | Just srcBuf <- newBuffer len
47 | | Nothing => pure []
48 | setBytes srcBuf 0 inp
49 | _ <- base64EncodeBuffer destBuf srcBuf len
50 | ints <- bufferData destBuf
51 | pure (map (the (Int -> Bits8) cast) ints)
54 | base64EncodeString: List Bits8 -> String
55 | base64EncodeString inp =
58 | let inp' = base64Encode inp
59 | inp'' = bufferL inp'
67 | base64DecodeBuffer: Buffer -> Buffer -> Int -> IO Int
68 | base64DecodeBuffer out src srclen = primIO $
prim__dec out src srclen
70 | decodeBuf : Buffer -> IO (Maybe (List Bits8))
71 | decodeBuf srcBuf = do
72 | srcLen <- rawSize srcBuf
73 | Just out <- newBuffer (base64DecodeLength srcLen)
74 | | Nothing => pure Nothing
75 | ret <- base64DecodeBuffer out srcBuf srcLen
76 | if ret == 0xFFFF_FFFF_FFFF_FFFF
79 | Just resized <- resizeBuffer out ret
80 | | Nothing => pure Nothing
81 | ints <- bufferData resized
82 | pure . Just $
map (the (Int -> Bits8) cast) ints
85 | base64DecodeString: String -> IO (Maybe (List Bits8))
86 | base64DecodeString str =
89 | let srcBuf = utf8Encode str
90 | in decodeBuf $
unsafeGetBuffer srcBuf
93 | base64DecodeBits8: List Bits8 -> Maybe (List Bits8)
94 | base64DecodeBits8 bits8 = unsafePerformIO $
do
95 | Just srcBuf <- newBuffer (the (Nat -> Int) cast $
List.length bits8)
96 | | Nothing => pure Nothing
97 | setBytes srcBuf 0 bits8