Last active
June 25, 2019 09:17
-
-
Save Araq/507d5e336eec15951aec153e4581bd14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, std / varints | |
proc main = | |
let self = os.getAppFilename() | |
let content = readFile(self) | |
let origSize = int(uint32(content[^4]) shl 24u32 or uint32(content[^3]) shl 16u32 or | |
uint32(content[^2]) shl 8u32 or uint32(content[^1])) | |
var filenameLen = 0u64 | |
echo "origSIze ", origSize, " ", content.len | |
var pos = origSize + readVu64(toOpenArrayByte(content, origSize, min(content.len, origSize + maxVarIntLen)), filenameLen) | |
echo "Filename: ", content.substr(pos, pos+filenameLen.int-1) | |
inc pos, filenameLen.int | |
var filecontentLen = 0u64 | |
inc pos, readVu64(toOpenArrayByte(content, pos, min(content.len, pos + maxVarIntLen)), filecontentLen) | |
echo "File contents: ", content.substr(pos, pos+filecontentLen.int-1) | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import parseopt, std / varints | |
proc writeU32(f: var File; y: uint32) = | |
var z: array[4, byte] | |
z[0] = uint8(y shr 24) | |
z[1] = uint8(y shr 16) | |
z[2] = uint8(y shr 8) | |
z[3] = uint8(y) | |
discard writeBytes(f, z, 0, 4) | |
proc appendNumber(f: var File; u: uint64) = | |
var buf: array[maxVarIntLen, byte] | |
let bufLen = writeVu64(buf, u) | |
discard writeBytes(f, buf, 0, bufLen) | |
proc appendEntry(f: var File; filename: string) = | |
appendNumber(f, uint64 filename.len) | |
write(f, filename) | |
let content = readFile(filename) | |
appendNumber(f, uint64 content.len) | |
write(f, content) | |
when isMainModule: | |
proc main = | |
var dest: File | |
var oldSize = -1i64 | |
for kind, key, val in getopt(): | |
case kind | |
of cmdArgument: | |
if dest.isNil: | |
dest = open(key, fmAppend) | |
oldSize = getFileSize(dest) | |
else: | |
appendEntry(dest, key) | |
else: | |
quit "only commands are supported, no options!" | |
if oldSize > 0: | |
echo "writing old size ", oldSize | |
writeU32(dest, uint32 oldSize) | |
if not dest.isNil: close(dest) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment