Skip to content

Commit

Permalink
0.1.2
Browse files Browse the repository at this point in the history
This release adds type hinting, and automatic unsigned->signed casting.
  • Loading branch information
dspearson committed Apr 12, 2019
1 parent 6a7de21 commit d3843eb
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Plan 9 Filesystem Protocol, as implemented in Clojure.

```clj
[phlegyas "0.1.1"]
[phlegyas "0.1.2"]
```

The vast majority of the protocol-level documentation was sourced from the wonderful [Plan 9 from User Space](https://9fans.github.io/plan9port/man/man9/) project.
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject phlegyas "0.1.1"
(defproject phlegyas "0.1.2"
:description "phlegyas: an implementation of 9P2000"
:url "https://github.com/dspearson/phlegyas"
:license {:name "ISC Licence"}
Expand Down
124 changes: 62 additions & 62 deletions src/phlegyas/buffers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,173 +6,173 @@

(defn get-tag
"Read tag[2] from the byte buffer."
[buffer]
(-> buffer .getShort short->ushort))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Short .getShort short->ushort))

(defn get-oldtag
"Read oldtag[2] from the byte buffer."
[buffer]
(-> buffer .getShort short->ushort))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Short .getShort short->ushort))

(defn get-msize
"Read msize[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-string
"Read string[s] from the byte buffer."
[buffer]
(let [string-size (-> buffer .getShort short->ushort)]
(String. (byte-array (map byte (for [i (range string-size)] (.get buffer)))) "UTF-8")))
[^java.nio.ByteBuffer buffer]
(let [string-size (-> buffer ^Short .getShort short->ushort)]
(String. (byte-array (map byte (for [i (range string-size)] (^Byte .get buffer)))) "UTF-8")))

(defn get-version
"Read version[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-uname
"Read uname[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-aname
"Read aname[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-ename
"Read ename[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-fid
"Read fid[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-newfid
"Read newfid[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-afid
"Read afid[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-perm
"Read perm[4] from the byte buffer"
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-offset
"Read offset[8] from the byte buffer."
[buffer]
(-> buffer .getLong long->ulong))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Long .getLong long->ulong))

(defn get-count
"Read count[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-size
"Read size[2] from the byte buffer."
[buffer]
(-> buffer .getShort short->ushort))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Short .getShort short->ushort))

(defn get-ssize
"Read size[2] from the byte buffer. Rstat and Twstat have repeated
size field, with our ssize being +2 more than size.
See BUGS section of stat(9) manual for more information."
[buffer]
(-> buffer .getShort short->ushort))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Short .getShort short->ushort))

(defn get-type
"Read type[2] from the byte buffer."
[buffer]
(-> buffer .getShort short->ushort))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Short .getShort short->ushort))

(defn get-dev
"Read dev[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-qid-type
"Read qid.type[1] from the byte buffer."
[buffer]
(-> buffer .get))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Byte .get))

(defn get-qid-vers
"Read qid.vers[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-qid-path
"Read qid.path[8] from the byte buffer."
[buffer]
(-> buffer .getLong long->ulong))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Long .getLong long->ulong))

(defn get-mode
"Read mode[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-atime
"Read atime[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-mtime
"Read mtime[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-length
"Read length[8] from the byte buffer."
[buffer]
(-> buffer .getLong long->ulong))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Long .getLong long->ulong))

(defn get-name
"Read name[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-uid
"Read uid[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-gid
"Read gid[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-muid
"Read muid[s] from the byte buffer."
[buffer]
[^java.nio.ByteBuffer buffer]
(get-string buffer))

(defn get-iomode
"Read mode[1] from the byte buffer."
[buffer]
(-> buffer .get))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Byte .get))

(defn get-iounit
"Read iounit[4] from the byte buffer."
[buffer]
(-> buffer .getInt int->uint))
[^java.nio.ByteBuffer buffer]
(-> buffer ^Integer .getInt int->uint))

(defn get-data
"Read count[4] bytes of data from the byte buffer."
[buffer]
(let [data-size (-> buffer .getInt int->uint)]
(byte-array (map byte (for [i (range data-size)] (.get buffer))))))
[^java.nio.ByteBuffer buffer]
(let [data-size (-> buffer ^Integer .getInt int->uint)]
(byte-array (map byte (for [i (range data-size)] (^Byte .get buffer))))))

(defn get-wnames
"Read nwname[2] of wname[s] from the byte buffer."
[buffer]
(let [nwname (-> buffer .getShort short->ushort)]
[^java.nio.ByteBuffer buffer]
(let [nwname (-> buffer ^Short .getShort short->ushort)]
(if (= nwname 0)
[]
(loop [wnames []
Expand All @@ -183,8 +183,8 @@

(defn get-nwqids
"Read nqwid[2] of qid[13] from the byte buffer."
[buffer]
(let [nwqid (-> buffer .getShort short->ushort)]
[^java.nio.ByteBuffer buffer]
(let [nwqid (-> buffer ^Short .getShort short->ushort)]
(if (= nwqid 0)
[]
(loop [qids []
Expand Down
27 changes: 16 additions & 11 deletions src/phlegyas/frames.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
(ns phlegyas.frames
(:require [phlegyas.util :refer :all]
[phlegyas.types :refer :all]
[phlegyas.transformers :refer :all]))
[phlegyas.transformers :refer :all]
[primitive-math :as math
:refer [ubyte->byte
uint->int
ushort->short
ulong->long]]))

(defmacro frame-length
"Check reported frame length."
[buffer]
`(if (< (.remaining ~buffer) 4)
`(if (< (^Integer .remaining ^java.nio.ByteBuffer ~buffer) 4)
0
(.getInt ~buffer)))
(^Integer .getInt ^java.nio.ByteBuffer ~buffer)))

(defmacro frame-type
"Look up frame type in the reverse lookup table `reverse-message-type`,
"Look up frame type in the reverse lookup table `reverse-frame-byte`,
defined in the `phlegyas.types` namespace, and `keywordizes` it for us."
[buffer]
`((keywordize (.get ~buffer)) '~reverse-frame-byte))
`((keywordize (^Byte .get ^java.nio.ByteBuffer ~buffer)) '~reverse-frame-byte))

(defn disassemble-packet
"Takes in a byte-array, and attempts to decode it. Produces a map, matching that
of the message type found in the `phlegyas.types` namespace."
[packet]
(let [frame (wrap-buffer packet)
len (frame-length frame)
(let [^java.nio.ByteBuffer frame (wrap-buffer packet)
len (uint->int (frame-length frame))
frame-typ (frame-type frame)
layout (get frame-layouts frame-typ)]
(into {:frame frame-typ} (for [typ layout] {typ ((get buffer-functions typ) frame)}))))
Expand All @@ -33,14 +38,14 @@
it as `buffer`, and using ByteBuffer operations to populate `frame-bytes`, finally
returning the assembled output."
[frame ftype]
(let [frame-size (+ 5 (count frame))
(let [frame-size (uint->int (+ 5 (count frame)))
type-bytes (get frame-byte ftype)
frame-bytes (byte-array frame-size)
buffer (wrap-buffer frame-bytes)]
^java.nio.ByteBuffer buffer (wrap-buffer frame-bytes)]
(doto buffer
(.putInt frame-size)
(^Integer .putInt frame-size)
(.put (byte type-bytes))
(.put frame))
(.put ^bytes frame))
frame-bytes))

(defn assemble-packet
Expand Down
Loading

0 comments on commit d3843eb

Please sign in to comment.