| 1 | # Copyright (c) 2024-2025 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module for reading and writing ValueFile files | ||
| 6 | |||
| 7 | package File::ValueFile::Simple::Reader; | ||
| 8 | |||
| 9 | use v5.10; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects); | ||
| 14 | |||
| 15 | use Carp; | ||
| 16 | use Fcntl qw(SEEK_SET); | ||
| 17 | use URI::Escape qw(uri_unescape); | ||
| 18 | use Encode (); | ||
| 19 | |||
| 20 | use Data::Identifier v0.06; | ||
| 21 | use File::ValueFile; | ||
| 22 | |||
| 23 | use constant { | ||
| 24 | RE_ISE => qr/^(?:[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|[0-2](?:\.(?:0|[1-9][0-9]*))+|[a-zA-Z][a-zA-Z0-9\+\.\-]+:.*)$/, | ||
| 25 | KEYWORD_OK => qr/^[a-zA-Z0-9\-:\._~]*$/, | ||
| 26 | FORMAT_ISE => '54bf8af4-b1d7-44da-af48-5278d11e8f32', | ||
| 27 | ASI_ISE => 'ddd60c5c-2934-404f-8f2d-fcb4da88b633', | ||
| 28 | TAGNAME_ISE => 'bfae7574-3dae-425d-89b1-9c087c140c23', | ||
| 29 | DOT_REPEAT_ISE => '2ec67bbe-4698-4a0c-921d-1f0951923ee6', | ||
| 30 | }; | ||
| 31 | |||
| 32 | our $VERSION = v0.10; | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | sub new { | ||
| 37 | my ($pkg, $in, %opts) = @_; | ||
| 38 | my $fh; | ||
| 39 | my $self = bless \%opts; | ||
| 40 | |||
| 41 | if (ref $in) { | ||
| 42 | $fh = $in; | ||
| 43 | } else { | ||
| 44 | open($fh, '<', $in) or croak $!; | ||
| 45 | } | ||
| 46 | |||
| 47 | $self->{fh} = $fh; | ||
| 48 | |||
| 49 | foreach my $key (qw(supported_formats supported_features)) { | ||
| 50 | $self->{$key} ||= 'all'; | ||
| 51 | if (ref($self->{$key}) ne 'ARRAY' && $self->{$key} ne 'all') { | ||
| 52 | $self->{$key} = [$self->{$key}]; | ||
| 53 | } | ||
| 54 | if (ref($self->{$key})) { | ||
| 55 | foreach my $entry (@{$self->{$key}}) { | ||
| 56 | $entry = Data::Identifier->new(ise => $entry) unless ref $entry; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | if (ref($self->{supported_features})) { | ||
| 62 | push(@{$self->{supported_features}}, Data::Identifier->new(ise => DOT_REPEAT_ISE)); | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->{utf8} = $opts{utf8} //= 'auto'; | ||
| 66 | if ($opts{utf8} && $opts{utf8} ne 'auto') { | ||
| 67 | $self->{unescape} = \&_unescape_utf8; | ||
| 68 | } else { | ||
| 69 | $self->{unescape} = \&uri_unescape; | ||
| 70 | } | ||
| 71 | |||
| 72 | $self->{dot_repreat} = 0; | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _special { | ||
| 78 | my ($str) = @_; | ||
| 79 | |||
| 80 | if ($str eq '!null') { | ||
| 81 | return undef; | ||
| 82 | } elsif ($str eq '!empty') { | ||
| 83 | return ''; | ||
| 84 | } else { | ||
| 85 | croak 'Invalid input'; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _check_supported { | ||
| 90 | my ($self, $key, $value) = @_; | ||
| 91 | my $list = $self->{$key}; | ||
| 92 | my $ise = $value->ise; | ||
| 93 | |||
| 94 | return if $list eq 'all'; | ||
| 95 | |||
| 96 | foreach my $entry (@{$list}) { | ||
| 97 | return if $entry->ise eq $ise; | ||
| 98 | } | ||
| 99 | |||
| 100 | croak 'Unsupported value for '.$key.': '.$ise; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _handle_special { | ||
| 104 | my ($self, $type, $marker, @args) = @_; | ||
| 105 | my $line = $self->{fh}->input_line_number; | ||
| 106 | |||
| 107 | if ($marker eq 'ValueFile') { | ||
| 108 | @args = @args[0,1] if scalar(@args) == 4 && !defined($args[-1]) && !defined($args[-2]); | ||
| 109 | croak 'ValueFile (magic) marker at wrong line' unless $line == 1; | ||
| 110 | croak 'ValueFile (magic) marker not marked required' unless $type eq '!'; | ||
| 111 | croak 'ValueFile (magic) marker with wrong number of arguments' unless scalar(@args) && scalar(@args) <= 2; | ||
| 112 | croak 'ValueFile (magic) marker not using supported format' unless $args[0] eq FORMAT_ISE; | ||
| 113 | |||
| 114 | if (scalar(@args) > 1) { | ||
| 115 | $self->_check_supported(supported_formats => $self->{format} = Data::Identifier->new(ise => $args[1])); | ||
| 116 | } | ||
| 117 | |||
| 118 | $self->_check_utf8($marker => $self->{format}) if $self->{utf8} eq 'auto'; | ||
| 119 | |||
| 120 | return; | ||
| 121 | } elsif ($marker eq 'Feature') { | ||
| 122 | my $id; | ||
| 123 | |||
| 124 | croak 'Feature marker with wrong number of arguments' unless scalar(@args) == 1; | ||
| 125 | |||
| 126 | push(@{$self->{features} //= []}, $id = Data::Identifier->new(ise => $args[0])); | ||
| 127 | |||
| 128 | $self->_check_supported(supported_features => $id) if $type eq '!'; | ||
| 129 | $self->_check_utf8($marker => $id) if $self->{utf8} eq 'auto'; | ||
| 130 | $self->{dot_repreat} ||= $id->eq(DOT_REPEAT_ISE); | ||
| 131 | |||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | croak 'Invalid marker: '.$marker; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _check_utf8 { | ||
| 139 | my ($self, $marker, $id) = @_; | ||
| 140 | if (File::ValueFile->_is_utf8($id)) { | ||
| 141 | $self->{unescape} = \&_unescape_utf8; | ||
| 142 | $self->{utf8} = 1; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub read_to_cb { | ||
| 148 | my ($self, $cb) = @_; | ||
| 149 | my $fh = $self->{fh}; | ||
| 150 | my $unescape = $self->{unescape}; | ||
| 151 | my @last_line; | ||
| 152 | |||
| 153 | $fh->seek(0, SEEK_SET); | ||
| 154 | $fh->input_line_number(0); | ||
| 155 | |||
| 156 | delete $self->{format}; | ||
| 157 | delete $self->{features}; | ||
| 158 | |||
| 159 | while (my $line = <$fh>) { | ||
| 160 | $line =~ s/\r?\n$//; | ||
| 161 | $line =~ s/#.*$//; | ||
| 162 | $line =~ s/^\xEF\xBB\xBF//; # skip BOMs. | ||
| 163 | $line =~ s/\s+/ /g; | ||
| 164 | $line =~ s/ $//; | ||
| 165 | $line =~ s/^ //; | ||
| 166 | |||
| 167 | next unless length $line; | ||
| 168 | |||
| 169 | if ($line =~ s/^\!([\!\?\&])//) { | ||
| 170 | my $type = $1; | ||
| 171 | |||
| 172 | if ($self->{dot_repreat}) { | ||
| 173 | my @line = split(/\s+/, $line); | ||
| 174 | my $x = 0; | ||
| 175 | foreach my $e (@line) { | ||
| 176 | if ($e eq '.') { | ||
| 177 | $e = $last_line[$x]; | ||
| 178 | } elsif ($e =~ s/^\.\.+$//) { | ||
| 179 | # done in match | ||
| 180 | } elsif ($e =~ KEYWORD_OK) { | ||
| 181 | # no-op | ||
| 182 | } elsif ($e =~ /^\!/) { | ||
| 183 | $e = _special($_); | ||
| 184 | } else { | ||
| 185 | $e = $unescape->($e); | ||
| 186 | } | ||
| 187 | $x++; | ||
| 188 | } | ||
| 189 | |||
| 190 | $self->_handle_special($type, @line); | ||
| 191 | @last_line = @line; | ||
| 192 | } else { | ||
| 193 | $self->_handle_special($type, map{ | ||
| 194 | $_ =~ KEYWORD_OK ? $_ : | ||
| 195 | $_ =~ /^\!/ ? _special($_) : $unescape->($_) | ||
| 196 | }(split(/\s+/, $line))); | ||
| 197 | } | ||
| 198 | |||
| 199 | # Reload: | ||
| 200 | $unescape = $self->{unescape}; | ||
| 201 | |||
| 202 | next; | ||
| 203 | } | ||
| 204 | |||
| 205 | if ($self->{dot_repreat}) { | ||
| 206 | my @line = split(/\s+/, $line); | ||
| 207 | my $x = 0; | ||
| 208 | foreach my $e (@line) { | ||
| 209 | if ($e eq '.') { | ||
| 210 | $e = $last_line[$x]; | ||
| 211 | } elsif ($e =~ /^\.+$/) { | ||
| 212 | $e =~ s/^\.//; | ||
| 213 | } elsif ($e =~ KEYWORD_OK) { | ||
| 214 | # no-op | ||
| 215 | } elsif ($e =~ /^\!/) { | ||
| 216 | $e = _special($e); | ||
| 217 | } else { | ||
| 218 | $e = $unescape->($e); | ||
| 219 | } | ||
| 220 | $x++; | ||
| 221 | } | ||
| 222 | |||
| 223 | $self->$cb(@line); | ||
| 224 | @last_line = @line; | ||
| 225 | } else { | ||
| 226 | $self->$cb(map{ | ||
| 227 | $_ =~ KEYWORD_OK ? $_ : | ||
| 228 | $_ =~ /^\!/ ? _special($_) : $unescape->($_) | ||
| 229 | }(split(/\s+/, $line))); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | sub read_as_hash { | ||
| 236 | my ($self) = @_; | ||
| 237 | my %hash; | ||
| 238 | |||
| 239 | $self->read_to_cb(sub { | ||
| 240 | my (undef, @line) = @_; | ||
| 241 | croak 'Invalid data: Not key-value' unless scalar(@line) == 2; | ||
| 242 | croak 'Invalid data: Null key' unless defined($line[0]); | ||
| 243 | croak 'Invalid data: Duplicate key: '.$line[0] if exists $hash{$line[0]}; | ||
| 244 | $hash{$line[0]} = $line[1]; | ||
| 245 | }); | ||
| 246 | |||
| 247 | return \%hash; | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | sub read_as_hash_of_arrays { | ||
| 252 | my ($self) = @_; | ||
| 253 | my %hash; | ||
| 254 | |||
| 255 | $self->read_to_cb(sub { | ||
| 256 | my (undef, @line) = @_; | ||
| 257 | croak 'Invalid data: Not key-value' unless scalar(@line) == 2; | ||
| 258 | croak 'Invalid data: Null key' unless defined($line[0]); | ||
| 259 | push(@{$hash{$line[0]} //=[]}, $line[1]); | ||
| 260 | }); | ||
| 261 | |||
| 262 | return \%hash; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub read_as_simple_tree { | ||
| 267 | my ($self) = @_; | ||
| 268 | my $tree; | ||
| 269 | |||
| 270 | $self->read_to_cb(sub { | ||
| 271 | my (undef, @line) = @_; | ||
| 272 | my $root = \$tree; | ||
| 273 | |||
| 274 | while (scalar(@line) > 1) { | ||
| 275 | my $el = shift(@line); | ||
| 276 | |||
| 277 | if (ref(${$root})) { | ||
| 278 | $root = \${$root}->{$el}; | ||
| 279 | } else { | ||
| 280 | ${$root} = { | ||
| 281 | (defined(${$root}) ? (_ => ${$root}) : ()), | ||
| 282 | $el => undef, | ||
| 283 | }; | ||
| 284 | $root = \${$root}->{$el}; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | if (ref(${$root}) eq 'ARRAY') { | ||
| 289 | push(@{${$root}}, @line); | ||
| 290 | } elsif (defined ${$root}) { | ||
| 291 | croak 'Invalid data with mixed number of levels' if ref ${$root}; | ||
| 292 | ${$root} = [${$root}, @line]; | ||
| 293 | } else { | ||
| 294 | ${$root} = $line[0]; | ||
| 295 | } | ||
| 296 | }); | ||
| 297 | |||
| 298 | return $tree; | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | sub read_as_taglist { | ||
| 303 | state $tagpool_source_format = Data::Identifier->new(uuid => 'e5da6a39-46d5-48a9-b174-5c26008e208e', displayname => 'tagpool-source-format')->register; | ||
| 304 | state $tagpool_taglist_format_v1 = Data::Identifier->new(uuid => 'afdb46f2-e13f-4419-80d7-c4b956ed85fa', displayname => 'tagpool-taglist-format-v1')->register; | ||
| 305 | state $tagpool_httpd_htdirectories_format = Data::Identifier->new(uuid => '25990339-3913-4b5a-8bcf-5042ef6d8b5e', displayname => 'tagpool-httpd-htdirectories-format')->register; | ||
| 306 | my ($self, %opts) = @_; | ||
| 307 | my $as = delete $opts{as}; | ||
| 308 | my %list; | ||
| 309 | my $format; | ||
| 310 | |||
| 311 | croak 'Stray options passed' if scalar keys %opts; | ||
| 312 | |||
| 313 | $self->read_to_cb(sub { | ||
| 314 | my (undef, @line) = @_; | ||
| 315 | my $tag; | ||
| 316 | |||
| 317 | $format //= $self->format(default => undef); | ||
| 318 | |||
| 319 | if ((Data::Identifier::eq($format, $tagpool_source_format) || Data::Identifier::eq($format, $tagpool_taglist_format_v1)) && scalar(@line) >= 2 && defined($line[0]) && defined($line[1])) { | ||
| 320 | if ($line[0] eq 'tag' && scalar(@line) == 3) { | ||
| 321 | $tag = Data::Identifier->new(ise => $line[1], displayname => $line[2]); | ||
| 322 | } elsif ($line[0] eq 'tag-metadata' && scalar(@line) == 7 && defined($line[2]) && !defined($line[3]) && defined($line[4]) && !defined($line[5]) && defined($line[6]) && $line[2] eq ASI_ISE && $line[4] eq TAGNAME_ISE) { | ||
| 323 | $tag = Data::Identifier->new(ise => $line[1], displayname => $line[6]); | ||
| 324 | } elsif ($line[0] =~ /^tag(?:-.+)?$/ || $line[0] eq 'rule' || $line[0] eq 'filter' || $line[0] eq 'subject') { | ||
| 325 | $tag = Data::Identifier->new(ise => $line[1]); | ||
| 326 | } | ||
| 327 | } elsif (Data::Identifier::eq($format, $tagpool_httpd_htdirectories_format) && scalar(@line) == 3 && defined($line[0]) && defined($line[1]) && defined($line[2]) && $line[0] eq 'directory') { | ||
| 328 | $tag = Data::Identifier->new(ise => $line[1]); | ||
| 329 | } elsif (!defined($format)) { | ||
| 330 | if (scalar(@line) > 1 && defined($line[0]) && defined($line[1]) && $line[0] =~ /^tag-(?:ise|metadata|relation)$/) { | ||
| 331 | if ($line[0] eq 'tag-metadata' && scalar(@line) == 7 && defined($line[2]) && !defined($line[3]) && defined($line[4]) && !defined($line[5]) && defined($line[6]) && $line[2] eq ASI_ISE && $line[4] eq TAGNAME_ISE) { | ||
| 332 | $tag = Data::Identifier->new(ise => $line[1], displayname => $line[6]); | ||
| 333 | } else { | ||
| 334 | $tag = Data::Identifier->new(ise => $line[1]); | ||
| 335 | } | ||
| 336 | } elsif ($line[0] eq 'tag' && scalar(@line) == 3) { | ||
| 337 | $tag = Data::Identifier->new(ise => $line[1], displayname => $line[2]); | ||
| 338 | } | ||
| 339 | |||
| 340 | unless (defined $tag) { | ||
| 341 | foreach my $entry (@line) { | ||
| 342 | if (defined($entry) && $entry =~ RE_ISE) { | ||
| 343 | my $tag = Data::Identifier->new(ise => $entry); | ||
| 344 | $list{$tag->ise} //= $tag; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | if (defined $tag) { | ||
| 351 | my $ise = $tag->ise; | ||
| 352 | my $old = $list{$ise}; | ||
| 353 | |||
| 354 | if (defined $old) { | ||
| 355 | $tag = $old if defined $old->displayname(default => undef, no_defaults => 1); | ||
| 356 | } | ||
| 357 | |||
| 358 | $list{$tag->ise} = $tag; | ||
| 359 | } | ||
| 360 | }); | ||
| 361 | |||
| 362 | return [map {$_->as($as, so => $self)} values %list] if defined $as; | ||
| 363 | |||
| 364 | return [values %list]; | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | sub format { | ||
| 369 | my ($self, %opts) = @_; | ||
| 370 | |||
| 371 | if (defined $self->{format}) { | ||
| 372 | return $self->{format}->as($opts{as}, so => $self) if defined $opts{as}; | ||
| 373 | return $self->{format}; | ||
| 374 | } | ||
| 375 | |||
| 376 | return $opts{default} if exists $opts{default}; | ||
| 377 | croak 'No value for format'; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | sub features { | ||
| 382 | my ($self, %opts) = @_; | ||
| 383 | return @{$self->{features}} if defined $self->{features}; | ||
| 384 | return @{$opts{default}} if exists $opts{default}; | ||
| 385 | croak 'No value for features'; | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | # ---- Private helpers ---- | ||
| 390 | |||
| 391 | sub _unescape_utf8 { | ||
| 392 | my ($text) = @_; | ||
| 393 | state $utf8 = Encode::find_encoding('UTF-8'); | ||
| 394 | return $utf8->decode(uri_unescape($text)); | ||
| 395 | } | ||
| 396 | |||
| 397 | 1; | ||
| 398 | |||
| 399 | __END__ |