Skip to content

Commit

Permalink
FEAT: minimalistic Unix Archive File (AR) decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Oct 7, 2021
1 parent 4c6e884 commit aaa70ec
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions make/rebol3.nest
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ include-codec-ssh-key: [mezz-lib-files: %mezz/codec-ssh-key.reb ]
include-codec-gzip: [mezz-lib-files: %mezz/codec-gzip.reb ]
include-codec-zip: [mezz-lib-files: %mezz/codec-zip.reb ]
include-codec-tar: [mezz-lib-files: %mezz/codec-tar.reb ]
include-codec-ar: [mezz-lib-files: %mezz/codec-ar.reb ]
; other codecs:
include-codec-bbcode: [mezz-lib-files: %mezz/codec-bbcode.reb ]
include-codec-html-entities: [mezz-lib-files: %mezz/codec-html-entities.reb]
Expand Down Expand Up @@ -545,6 +546,7 @@ include-rebol-core: [
:include-https

:include-codec-unixtime
:include-codec-ar
:include-codec-gzip
:include-codec-zip
:include-codec-tar
Expand Down
61 changes: 61 additions & 0 deletions src/mezz/codec-ar.reb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
REBOL [
title: "REBOL 3 codec for AR files"
name: 'codec-ar
author: rights: "Oldes"
version: 0.0.1
specification: https://en.wikipedia.org/wiki/Ar_(Unix)
history: [7-Oct-2021 "Oldes" {Initial version of the AR decoder}]
todo: {AR encoder}
]

system/options/log/ar: 2
register-codec [
name: 'ar
type: 'compression
title: "Unix archive file"
suffixes: [%.ar %.a]

decode: function [
{Extract content of the AR file}
data [binary! file! url!]
return: [block!] "Result is in format: [NAME [DATE UID GID MODE DATA] ...]"
] [
unless binary? data [data: read data]
sys/log/info 'AR ["^[[1;32mDecode AR data^[[m (^[[1m" length? data "^[[mbytes )"]
unless parse data ["!<arch>^/" data: to end][return none]
bin: binary data
out: make block! 32

while [58 <= length? bin/buffer][
binary/read bin [
file: STRING-BYTES 16
info: STRING-BYTES 42
end: UI16LE ;#{600A}
]
unless all [
try [info: load info]
end = 2656
info/1: to date! info/1
][ return none]

data: binary/read bin take/last info
if parse file ["#1/" copy len to end ][
; BSD variant
len: to integer! to string! len
file: take/part data len ; real file name in front of the data section
file: binary/read file 'string ; removes null padding
]
sys/log/info 'AR ["File:^[[33m" pad copy file 20 mold info]
append info data
append/only append out as file! file info
]
new-line/all out true
out
]

;encode: function [data [binary!]][TODO]

identify: function [data [binary!]][
parse data ["!<arch>^/" to end]
]
]

0 comments on commit aaa70ec

Please sign in to comment.