From 32ad871f5d9c138aa46a935c5f920576964a41de Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 12 Aug 2025 18:36:48 +0900 Subject: [PATCH] mruby-pack: document BER format ('w') template directive - add 'w' directive to supported template table - provide BER encoding/decoding usage example - describe as variable length encoding (no endianness concept) Co-authored-by: Claude --- mrbgems/mruby-pack/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mrbgems/mruby-pack/README.md b/mrbgems/mruby-pack/README.md index 96caac16a..2a9edeefe 100644 --- a/mrbgems/mruby-pack/README.md +++ b/mrbgems/mruby-pack/README.md @@ -53,6 +53,13 @@ arr2 = binary_data2.unpack("C3") # Unpacks three 8-bit unsigned integers binary_data3 = "Hello\x00World" arr3 = binary_data3.unpack("Z*Z*") # Unpacks two null-terminated strings # arr3 will be ["Hello", "World"] + +# BER (Basic Encoding Rules) example +arr4 = [127, 128, 16383] +binary_data4 = arr4.pack("w*") # BER-encodes integers of varying sizes +# binary_data4 will contain BER-compressed data +arr4_unpacked = binary_data4.unpack("w*") # Decodes BER data back to integers +# arr4_unpacked will be [127, 128, 16383] ``` ## Supported Template Directives @@ -93,6 +100,7 @@ Here is a list of supported template characters and their meanings: | `U` | UTF-8 character | | `V` | 32-bit unsigned integer, VAX (little-endian) byte order | | `v` | 16-bit unsigned integer, VAX (little-endian) byte order | +| `w` | BER-compressed integer (variable length encoding) | | `x` | Null byte (skip forward one byte) | | `X` | Back up one byte | | `Z` | Null-terminated string (when unpacking, reads until NULL; when packing, appends a NULL if count is `*`) |