Revision 11e90588 src/Ganeti/Utils.hs
b/src/Ganeti/Utils.hs | ||
---|---|---|
58 | 58 |
, splitEithers |
59 | 59 |
, recombineEithers |
60 | 60 |
, setOwnerAndGroupFromNames |
61 |
, b64StringToBitString |
|
62 |
, bitStringToB64String |
|
61 | 63 |
) where |
62 | 64 |
|
63 | 65 |
import Data.Char (toUpper, isAlphaNum, isDigit, isSpace) |
... | ... | |
77 | 79 |
import System.Posix.Files |
78 | 80 |
import System.Time |
79 | 81 |
|
82 |
import qualified Data.ByteString as BS |
|
83 |
import Data.ByteString.Base64 (decodeLenient, encode) |
|
84 |
import qualified Data.ByteString.Char8 as BSC |
|
85 |
import Data.Word (Word8) |
|
86 |
import Data.Char (intToDigit, digitToInt) |
|
87 |
import Numeric (showIntAtBase, readInt) |
|
88 |
|
|
80 | 89 |
-- * Debug functions |
81 | 90 |
|
82 | 91 |
-- | To be used only for debugging, breaks referential integrity. |
... | ... | |
458 | 467 |
let uid = fst ents M.! daemon |
459 | 468 |
let gid = snd ents M.! dGroup |
460 | 469 |
setOwnerAndGroup filename uid gid |
470 |
|
|
471 |
type BitString = String |
|
472 |
|
|
473 |
-- | Base 64 encoded String to BitString |
|
474 |
wordsToBitString :: [Word8] -> BitString |
|
475 |
wordsToBitString = |
|
476 |
concatMap (padBits . wordToBits) |
|
477 |
where |
|
478 |
wordToBits = flip (showIntAtBase 2 intToDigit) "" |
|
479 |
padBits bs = replicate (8 - length bs) '0' ++ bs |
|
480 |
|
|
481 |
decodeB64String :: String -> [Word8] |
|
482 |
decodeB64String = BS.unpack . decodeLenient . BSC.pack |
|
483 |
|
|
484 |
b64StringToBitString :: String -> BitString |
|
485 |
b64StringToBitString = wordsToBitString . decodeB64String |
|
486 |
|
|
487 |
-- | A BitString to Base 64 encoded String |
|
488 |
bitStringToWords :: BitString -> [Word8] |
|
489 |
bitStringToWords [] = [] |
|
490 |
bitStringToWords bs = |
|
491 |
bitStringToWord c : bitStringToWords rest |
|
492 |
where |
|
493 |
bitStringToWord = fst . head . readInt 2 (const True) digitToInt |
|
494 |
(c, rest) = splitAt 8 bs |
|
495 |
|
|
496 |
encodeB64String :: [Word8] -> String |
|
497 |
encodeB64String = BSC.unpack . encode . BS.pack |
|
498 |
|
|
499 |
bitStringToB64String :: BitString -> String |
|
500 |
bitStringToB64String = encodeB64String . bitStringToWords |
Also available in: Unified diff