| 1 | package Encode::Bijou64; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our @EXPORT = qw( encode_bijou64 decode_bijou64); | |||||||||
| 8 | 2 | our $VERSION = '0.01'; | |||||||||
| 9 | |||||||||||
| 10 | ################################################################################ | ||||||||||
| 11 | ################################################################################ | ||||||||||
| 12 | |||||||||||
| 13 | # tag, num_bytes, max | ||||||||||
| 14 | 2 | my @TIERS = ( | |||||||||
| 15 | [0xF8, 1, 248], | ||||||||||
| 16 | [0xF9, 2, 504], | ||||||||||
| 17 | [0xFA, 3, 66_040], | ||||||||||
| 18 | [0xFB, 4, 16_843_256], | ||||||||||
| 19 | [0xFC, 5, 4_311_810_552], | ||||||||||
| 20 | [0xFD, 6, 1_103_823_438_328], | ||||||||||
| 21 | [0xFE, 7, 282_578_800_148_984], | ||||||||||
| 22 | [0xFF, 8, 72_340_172_838_076_920], | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 2 | my %DECODE; | |||||||||
| 26 | 2 | for my $tier (@TIERS) { | |||||||||
| 27 | 16 | my ($tag, $bytes, $base) = @$tier; | |||||||||
| 28 | |||||||||||
| 29 | 16 | $DECODE{$tag} = [$bytes, $base]; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | ################################################################################ | ||||||||||
| 33 | |||||||||||
| 34 | 32 | ▶ | sub encode_bijou64 { | ||||||||
|
Subroutine
encode_bijou64: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 35 | 32 | my ($n) = @_; | |||||||||
| 36 | |||||||||||
| 37 | 32 | ▶ | if (!defined($n)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 1 | die("encode_bijou64(): undefined value"); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 31 | ▶ | if ($n !~ /^\d+\z/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 4 | die("encode_bijou64(): positive integer required"); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 27 | ▶ | return pack("C", $n) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | if $n <= 0xF7; | ||||||||||
| 47 | |||||||||||
| 48 | 23 | for my $tier (@TIERS) { | |||||||||
| 49 | 83 | my ($tag, $bytes, $base) = @$tier; | |||||||||
| 50 | |||||||||||
| 51 | 83 | my $max = $base + (1 << ($bytes * 8)) - 1; | |||||||||
| 52 | |||||||||||
| 53 | 83 | ▶ | if ($n <= $max) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 23 | my $v = $n - $base; | |||||||||
| 55 | 23 | my $out = pack("C", $tag); | |||||||||
| 56 | |||||||||||
| 57 | 23 | for (reverse 0 .. $bytes - 1) { | |||||||||
| 58 | 83 | $out .= pack("C", ($v >> ($_ * 8)) & 0xFF); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 23 | return $out; | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | die "encode_bijou64(): integer too large"; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 31 | ▶ | sub decode_bijou64 { | ||||||||
|
Subroutine
decode_bijou64: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 69 | 31 | my ($buf) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 31 | ▶ | if (!defined($buf)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | die("decode_bijou64(): undefined value"); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 30 | ▶ | if (!length($buf)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 1 | die("decode_bijou64(): empty buffer"); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 29 | my $tag = ord substr($buf, 0, 1); | |||||||||
| 80 | |||||||||||
| 81 | # Short/simple decode | ||||||||||
| 82 | 29 | ▶ | if ($tag <= 0xF7) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 4 | return $tag; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 25 | my $tier = $DECODE{$tag}; | |||||||||
| 87 | |||||||||||
| 88 | 25 | ▶ | if (!$tier) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | my $msg = sprintf("decode_bijou64(): invalid tag 0x%02X", $tag); | |||||||||
| 90 | 0 | die($msg); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 25 | my ($bytes, $base) = @$tier; | |||||||||
| 94 | |||||||||||
| 95 | 25 | ▶ | if (length($buf) != 1 + $bytes) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 2 | die("decode_bijou64(): buffer too short"); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 23 | my $v = 0; | |||||||||
| 100 | 23 | for my $i (0 .. $bytes - 1) { | |||||||||
| 101 | 83 | $v = ($v << 8) | ord(substr($buf, 1 + $i, 1)); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 23 | return $base + $v; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 2 | 1; | |||||||||
| 108 | |||||||||||
| 109 | ################################################################################ | ||||||||||
| 110 | ################################################################################ | ||||||||||
| 111 | |||||||||||
| 112 | =encoding utf8 | ||||||||||
| 113 | |||||||||||
| 114 | =head1 NAME | ||||||||||
| 115 | |||||||||||
| 116 | Encode::Bijou64 - Encode and decode Bijou64 integers | ||||||||||
| 117 | |||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||
| 119 | |||||||||||
| 120 | use Encode::Bijou64; | ||||||||||
| 121 | |||||||||||
| 122 | my $bytes = encode_bijou64(123456); | ||||||||||
| 123 | my $num = decode_bijou64($bytes); | ||||||||||
| 124 | |||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||
| 126 | |||||||||||
| 127 | Encode::Bijou64 implements the Bijou64 variable-length | ||||||||||
| 128 | integer encoding format described by Ink & Switch. | ||||||||||
| 129 | |||||||||||
| 130 | Small integers occupy fewer bytes while preserving | ||||||||||
| 131 | efficient decoding. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 FUNCTIONS | ||||||||||
| 134 | |||||||||||
| 135 | =head2 encode_bijou64($integer) | ||||||||||
| 136 | |||||||||||
| 137 | Encodes a non-negative integer into a Bijou64 byte string. | ||||||||||
| 138 | |||||||||||
| 139 | =head2 decode_bijou64($bytes) | ||||||||||
| 140 | |||||||||||
| 141 | Decodes a Bijou64 byte string and returns the integer value. | ||||||||||
| 142 | |||||||||||
| 143 | =head1 SEE ALSO | ||||||||||
| 144 | |||||||||||
| 145 | https://www.inkandswitch.com/tangents/bijou64/ | ||||||||||
| 146 | |||||||||||
| 147 | =head1 AUTHOR | ||||||||||
| 148 | |||||||||||
| 149 | Scott Baker | ||||||||||
| 150 | |||||||||||
| 151 | =head1 LICENSE | ||||||||||
| 152 | |||||||||||
| 153 | Same terms as Perl itself. | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | # vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4 | ||||||||||