Skip to content

Commit

Permalink
pcapng - codestyle and scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nikiforo committed Aug 24, 2022
1 parent f100073 commit 18af9a3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import shapeless.{::, HList, HNil}

object BlockCodec {

// format: off
private def commonStructure[A, L <: HList, LB <: HList](blockType: Codec[A])(f: Length => Codec[L])(
implicit
prepend: Prepend.Aux[L, Unit :: HNil, LB],
init: Init.Aux[LB, L],
last: Last.Aux[LB, Unit]
private def commonStructure[A, L <: HList, LB <: HList](
blockType: Codec[A]
)(f: Length => Codec[L])(implicit
prepend: Prepend.Aux[L, Unit :: HNil, LB],
init: Init.Aux[LB, L],
last: Last.Aux[LB, Unit]
): Codec[A :: Length :: LB] =
// format: off
("Block Type" | blockType ) ::
("Block Total Length" | bytes(4).xmapc(Length)(_.bv) ).flatPrepend { length =>
("Block Bytes" | f(length) ) :+
Expand Down
4 changes: 2 additions & 2 deletions protocols/shared/src/main/scala-3/BlockCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import scodec.codecs._

object BlockCodec {

// format: off
private inline def commonStructure[A, L <: Tuple](
blockType: Codec[A]
blockType: Codec[A]
)(f: Length => Codec[L]): Codec[Tuple.Concat[A *: Length *: L, Unit *: EmptyTuple]] =
// format: off
("Block Type" | blockType ) ::
("Block Total Length" | bytes(4).xmapc(Length(_))(_.bv) ).flatPrepend { length =>
("Block Bytes" | f(length) ) :+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import scodec.bits._
import scodec.codecs._

object ByteOrderMagic extends Codec[ByteOrdering] {

private val BigEndian = hex"1a2b3c4d"

private val LittleEndian = hex"4d3c2b1a"

def sizeBound = SizeBound.exact(32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package fs2.protocols.pcapng
package fs2.protocols
package pcapng

import cats.effect.MonadCancelThrow
import fs2.interop.scodec.StreamDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ case class EnhancedPacketBlock(

object EnhancedPacketBlock {

// format: off
def codec(implicit ord: ByteOrdering): Codec[EnhancedPacketBlock] =
"EPB" | BlockCodec.byBlockBytesCodec(hexConstant, epbCodec).dropUnits.as[EnhancedPacketBlock]

private def hexConstant(implicit ord: ByteOrdering): ByteVector =
orderDependent(hex"00000006", hex"06000000")

private def epbCodec(implicit ord: ByteOrdering) =
// format: off
("Interface ID" | guint32 ) ::
("Timestamp (High)" | guint32 ) ::
("Timestamp (Low)" | guint32 ) ::
Expand All @@ -55,9 +58,6 @@ object EnhancedPacketBlock {
("Options" | bytes )}
// format: on

private def hexConstant(implicit ord: ByteOrdering): ByteVector =
orderDependent(hex"00000006", hex"06000000")

private def padTo32Bits(length: Int): Long = {
val rem = length % 4
val bytes = if (rem == 0) 0 else 4 - rem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ case class InterfaceDescriptionBlock(

object InterfaceDescriptionBlock {

// format: off
def codec(implicit ord: ByteOrdering): Codec[InterfaceDescriptionBlock] =
"IDB" | BlockCodec.byBlockBytesCodec(hexConstant, idbCodec).dropUnits.as[InterfaceDescriptionBlock]
def codec(implicit ord: ByteOrdering): Codec[InterfaceDescriptionBlock] = {
val rawCodec = BlockCodec.byBlockBytesCodec(hexConstant, idbCodec)
"IDB" | rawCodec.dropUnits.as[InterfaceDescriptionBlock]
}

private def hexConstant(implicit ord: ByteOrdering): ByteVector =
orderDependent(hex"00000001", hex"01000000")

private def idbCodec(implicit ord: ByteOrdering) =
// format: off
("LinkType" | guint16.xmap[LinkType](LinkType.fromInt, LinkType.toInt) ) ::
("Reserved" | ignore(16) ) ::
("SnapLen" | guint32 ) ::
("Block Bytes" | bytes )
// format: on

private def hexConstant(implicit ord: ByteOrdering) =
orderDependent(hex"00000001", hex"01000000")
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ object SectionHeaderBlock {

private val hexConstant = hex"0A0D0D0A"

// format: off
val codec: Codec[SectionHeaderBlock] =
"SHB" | BlockCodec.unknownByteOrder(hexConstant) { length =>
("Byte-Order Magic" | ByteOrderMagic ).flatPrepend { implicit byteOrder =>
("Major Version" | guint16 ) ::
("Minor Version" | guint16 ) ::
("Block Bytes" | bytes(length.toLong.toInt - 20) )
}}.dropUnits.as[SectionHeaderBlock]
"SHB" | BlockCodec.unknownByteOrder(hexConstant)(shbCodec).dropUnits.as[SectionHeaderBlock]

private def shbCodec(length: Length) =
// format: off
("Byte-Order Magic" | ByteOrderMagic ).flatPrepend { implicit byteOrder =>
("Major Version" | guint16 ) ::
("Minor Version" | guint16 ) ::
("Block Bytes" | bytes(length.toLong.toInt - 20) )}
// format: on
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class BlockTest extends munit.FunSuite {

private object BlockTest {

// https://gitlab.com/wireshark/wireshark/-/wikis/Development/PcapNg dhcp.pcapng
// This data is taken from dhcp.pcapng file from
// https://gitlab.com/wireshark/wireshark/-/wikis/Development/PcapNg
object DHCP {
object SHB {
val header = hex"0a0d0d0a"
Expand Down

0 comments on commit 18af9a3

Please sign in to comment.