summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-08-15 00:22:48 +0200
committerAraq <rumpf_a@web.de>2013-08-15 00:22:48 +0200
commit50403afb5c8d49f2d9046498ea714251a3e4ad90 (patch)
tree38484ed872ba8d92f52e3d3c8ce473ab71f68a80 /lib/pure
parentd53f313599111576c9eb290d679d6ed518582530 (diff)
downloadNim-50403afb5c8d49f2d9046498ea714251a3e4ad90.tar.gz
fixes base64 module
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/base64.nim14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim
index 8c4883c11..685313551 100644
--- a/lib/pure/base64.nim
+++ b/lib/pure/base64.nim
@@ -16,7 +16,7 @@ proc encode*(s: string, lineLen = 75, newLine="\13\10"): string =
   ## encodes `s` into base64 representation. After `lineLen` characters, a 
   ## `newline` is added.
   var total = ((len(s) + 2) div 3) * 4
-  var numLines = (total + (lineLen - 1)) div lineLen
+  var numLines = (total + lineLen - 1) div lineLen
   if numLines > 0: inc(total, (numLines-1) * newLine.len)

 
   result = newString(total)
@@ -47,14 +47,16 @@ proc encode*(s: string, lineLen = 75, newLine="\13\10"): string =
     result[r+1] = cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
     result[r+2] = cb64[((b and 0x0F) shl 2)] 
     result[r+3] = '='
-    assert(r+4 == result.len)
+    if r+4 != result.len:
+      setLen(result, r+4)
   elif i < s.len:
     var a = ord(s[i])
     result[r] = cb64[a shr 2]
     result[r+1] = cb64[(a and 3) shl 4]
     result[r+2] = '='
     result[r+3] = '='
-    assert(r+4 == result.len)
+    if r+4 != result.len:
+      setLen(result, r+4)
   else:
     assert(r == result.len)
 
@@ -84,9 +86,9 @@ proc decode*(s: string): string =
       var c = s[i+2].decodeByte
       var d = s[i+3].decodeByte
       
-      result[r] = chr((a shl 2) or ((b shr 4) and 0x03))
-      result[r+1] = chr((b shl 4) or ((c shr 2) and 0x0F))
-      result[r+2] = chr((c shl 6) or (d and 0x3F))
+      result[r] = chr((a shl 2) and 0xff or ((b shr 4) and 0x03))
+      result[r+1] = chr((b shl 4) and 0xff or ((c shr 2) and 0x0F))
+      result[r+2] = chr((c shl 6) and 0xff or (d and 0x3F))
       inc(r, 3)
       inc(i, 4)
     else: break