Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/bytefield/0.0.8/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 jomaway

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions packages/preview/bytefield/0.0.8/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# typst-bytefield

A simple way to create network protocol headers, memory maps, register definitions and more in typst.

⚠️ Warning. As this package is still in an early stage, things might break with the next version.

ℹ️ If you find a bug or a feature which is missing, please open an issue and/or send an PR.

## Example

![random colored bytefield example](https://github.com/jomaway/typst-bytefield/blob/a7c649a4d0e4ab634e4afa6d170e000b023e4313/docs/bytefield_example.png)

```typst
#import "@preview/bytefield:0.0.8": *

#bytefield(
// Config the header
bitheader(
"bytes",
// adds every multiple of 8 to the header.
0, [start], // number with label
5,
// number without label
12, [#text(14pt, fill: red, "test")],
23, [end_test],
24, [start_break],
36, [Fix], // will not be shown
angle: -50deg, // angle (default: -60deg)
text-size: 8pt, // length (default: global header_font_size or 9pt)
),
// Add data fields (bit, bits, byte, bytes) and notes
// A note always aligns on the same row as the start of the next data field.
note(left)[#text(16pt, fill: blue, "Testing")],
bytes(3,fill: red.lighten(30%))[Test],
note(right)[#set text(9pt); #sym.arrow.l This field \ breaks into 2 rows.],
bytes(2)[Break],
note(left)[#set text(9pt); and continues \ here #sym.arrow],
bits(24,fill: green.lighten(30%))[Fill],
group(right,3)[spanning 3 rows],
bytes(12)[#set text(20pt); *Multi* Row],
note(left, bracket: true)[Flags],
bits(4)[#text(8pt)[reserved]],
flag[#text(8pt)[SYN]],
flag(fill: orange.lighten(60%))[#text(8pt)[ACK]],
flag[#text(8pt)[BOB]],
bits(25, fill: purple.lighten(60%))[Padding],
// padding(fill: purple.lighten(40%))[Padding],
bytes(2)[Next],
bytes(8, fill: yellow.lighten(60%))[Multi break],
note(right)[#emoji.checkmark Finish],
bytes(2)[_End_],
)
```


## Usage

To use this library through the Typst package manager import bytefield with `#import "@preview/bytefield:0.0.8": *` at the top of your file.

The package contains some of the most common network protocol headers which are available under: `common.ipv4`, `common.ipv6`, `common.icmp`, `common.icmpv6`, `common.dns`, `common.tcp`, `common.udp`.

## Features

Here is a unsorted list of features which is possible right now.

- Adding fields with `bit`, `bits`, `byte` or `bytes` function.
- Fields can be colored
- Multirow and breaking fields are supported.
- Adding notes to the left or right with `note` or `group` function.
- Config the header with the `bitheader` function. !Only one header per bytefield is processed currently.
- Show numbers
- Show numbers and labels
- Show only labels
- Change the bit order in the header with `msb:left` or `msb:right` (default)


See [example.typ](https://github.com/jomaway/typst-bytefield/blob/a7c649a4d0e4ab634e4afa6d170e000b023e4313/examples/example.typ) for more information.

7 changes: 7 additions & 0 deletions packages/preview/bytefield/0.0.8/bytefield.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Bytefield - generate protocol headers and more

// pull in the user facing api
#import "lib/api.typ": bytefield, bit, bits, byte, bytes, flag, group, note, bitheader, section
#import "lib/states.typ": bf-config
// import common network protocols
#import "common.typ";
93 changes: 93 additions & 0 deletions packages/preview/bytefield/0.0.8/common.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#import "lib/api.typ": *

// Common network protocols
#let eth = bytefield(
bpr: 8 * 6,
bitheader("bytes"),
bytes(6)[Destination MAC],
bytes(6)[Source MAC],
bytes(2)[Ethertype],
bytes(42)[Payload (42-1500)],
bytes(4)[CRC],
)

#let ipv6_frag_ext_h = bytefield(
bitheader("bytes"),
bits(8)[Next Header],
bits(8)[Reserved],
bits(13)[Fragment offset],
bits(2)[res],
flag[M],
bits(4 * 8)[Identification],
)

#let ipv4 = bytefield(
bitheader("bytes"),
bits(4)[Version], bits(4)[IHL], bytes(1)[TOS], bytes(2)[Total Length],
bytes(2)[Identification], bits(3)[Flags], bits(13)[Fragment Offset],
bytes(1)[TTL], bytes(1)[Protocol], bytes(2)[Header Checksum],
bytes(4)[Source Address],
bytes(4)[Destination Address],
bytes(3)[Options], bytes(1)[Padding]
)

#let ipv6 = bytefield(
bitheader("bytes"),
bits(4)[Version], bytes(1)[Traffic Class], bits(20)[Flowlabel],
bytes(2)[Payload Length], bytes(1)[Next Header], bytes(1)[Hop Limit],
bytes(int(128/8))[Source Address],
bytes(int(128/8))[Destination Address],
)

#let icmp = bytefield(
bitheader("bytes"),
byte[Type], byte[Code], bytes(2)[Checksum],
bytes(2)[Identifier], bytes(2)[Sequence Number],
bits(auto)[Optional Data]
)

#let icmpv6 = bytefield(
bitheader("bytes"),
byte[Type], byte[Code], bytes(2)[Checksum],
bits(auto)[Internet Header + 64 bits of Original Data Datagram]
)

#let dns = bytefield(
bitheader("bytes"),
bytes(2)[Identification], bytes(2)[Flags],
bytes(2)[Number of Questions], bytes(2)[Number of answer RRs],
bytes(2)[Number of authority RRs], bytes(2)[Number of additional RRs],
bytes(8)[Questions],
bytes(8)[Answers (variable number of resource records)],
bytes(8)[Authority (variable number of resource records)],
bytes(8)[Additional information (variable number of resource records)],
)

#let tcp = bytefield(
bitheader("bytes"),
bytes(2)[Source Port], bytes(2)[Destination Port],
bytes(4)[Sequence Number],
bytes(4)[Acknowledgment Number],
bits(4)[Data Offset],bits(6)[Reserved], bits(6)[Flags], bytes(2)[Window],
bytes(2)[Checksum], bytes(2)[Urgent Pointer],
bytes(3)[Options], byte[Padding],
bits(auto)[...DATA...]
)

#let tcp_detailed = bytefield(
bitheader("bytes"),
bytes(2)[Source Port], bytes(2)[ Destination Port],
bytes(4)[Sequence Number],
bytes(4)[Acknowledgment Number],
bits(4)[Data Offset],bits(6)[Reserved], flag("URG"), flag("ACK"), flag("PSH"), flag("RST"), flag("SYN"), flag("FIN"), bytes(2)[Window],
bytes(2)[Checksum], bytes(2)[Urgent Pointer],
bytes(3)[Options], byte[Padding],
bits(auto)[...DATA...]
)

#let udp = bytefield(
bitheader("bytes"),
bytes(2)[Source Port], bytes(2)[ Destination Port],
bytes(2)[Length], bytes(2)[Checksum],
bits(auto)[...DATA...]
)
Loading