Ordinals.pm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. package OpenSSL::Ordinals;
  9. use strict;
  10. use warnings;
  11. use Carp;
  12. use Scalar::Util qw(blessed);
  13. use OpenSSL::Util;
  14. use constant {
  15. # "magic" filters, see the filters at the end of the file
  16. F_NAME => 1,
  17. F_NUMBER => 2,
  18. };
  19. =head1 NAME
  20. OpenSSL::Ordinals - a private module to read and walk through ordinals
  21. =head1 SYNOPSIS
  22. use OpenSSL::Ordinals;
  23. my $ordinals = OpenSSL::Ordinals->new(from => "foo.num");
  24. # or alternatively
  25. my $ordinals = OpenSSL::Ordinals->new();
  26. $ordinals->load("foo.num");
  27. foreach ($ordinals->items(comparator => by_name()) {
  28. print $_->name(), "\n";
  29. }
  30. =head1 DESCRIPTION
  31. This is a OpenSSL private module to load an ordinals (F<.num>) file and
  32. write out the data you want, sorted and filtered according to your rules.
  33. An ordinals file is a file that enumerates all the symbols that a shared
  34. library or loadable module must export. Each of them have a unique
  35. assigned number as well as other attributes to indicate if they only exist
  36. on a subset of the supported platforms, or if they are specific to certain
  37. features.
  38. The unique numbers each symbol gets assigned needs to be maintained for a
  39. shared library or module to stay compatible with previous versions on
  40. platforms that maintain a transfer vector indexed by position rather than
  41. by name. They also help keep information on certain symbols that are
  42. aliases for others for certain platforms, or that have different forms
  43. on different platforms.
  44. =head2 Main methods
  45. =over 4
  46. =cut
  47. =item B<new> I<%options>
  48. Creates a new instance of the C<OpenSSL::Ordinals> class. It takes options
  49. in keyed pair form, i.e. a series of C<< key => value >> pairs. Available
  50. options are:
  51. =over 4
  52. =item B<< from => FILENAME >>
  53. Not only create a new instance, but immediately load it with data from the
  54. ordinals file FILENAME.
  55. =back
  56. =cut
  57. sub new {
  58. my $class = shift;
  59. my %opts = @_;
  60. my $instance = {
  61. filename => undef, # File name registered when loading
  62. loaded_maxnum => 0, # Highest allocated item number when loading
  63. loaded_contents => [], # Loaded items, if loading there was
  64. maxassigned => 0, # Current highest assigned item number
  65. maxnum => 0, # Current highest allocated item number
  66. contents => [], # Items, indexed by number
  67. name2num => {}, # Name to number dictionary
  68. aliases => {}, # Aliases cache.
  69. stats => {}, # Statistics, see 'sub validate'
  70. debug => $opts{debug},
  71. };
  72. bless $instance, $class;
  73. $instance->set_version($opts{version});
  74. $instance->load($opts{from}) if defined($opts{from});
  75. return $instance;
  76. }
  77. =item B<< $ordinals->load FILENAME >>
  78. Loads the data from FILENAME into the instance. Any previously loaded data
  79. is dropped.
  80. Two internal databases are created. One database is simply a copy of the file
  81. contents and is treated as read-only. The other database is an exact copy of
  82. the first, but is treated as a work database, i.e. it can be modified and added
  83. to.
  84. =cut
  85. sub load {
  86. my $self = shift;
  87. my $filename = shift;
  88. croak "Undefined filename" unless defined($filename);
  89. my @tmp_contents = ();
  90. my %tmp_name2num = ();
  91. my $max_assigned = 0;
  92. my $max_num = 0;
  93. open F, '<', $filename or croak "Unable to open $filename";
  94. while (<F>) {
  95. s|\R$||; # Better chomp
  96. s|#.*||;
  97. next if /^\s*$/;
  98. my $item = OpenSSL::Ordinals::Item->new(source => $filename, from => $_);
  99. my $num = $item->number();
  100. if ($num eq '?') {
  101. $num = ++$max_num;
  102. } elsif ($num eq '?+') {
  103. $num = $max_num;
  104. } else {
  105. croak "Disordered ordinals, number sequence restarted"
  106. if $max_num > $max_assigned && $num < $max_num;
  107. croak "Disordered ordinals, $num < $max_num"
  108. if $num < $max_num;
  109. $max_assigned = $max_num = $num;
  110. }
  111. $item->intnum($num);
  112. push @{$tmp_contents[$num]}, $item;
  113. $tmp_name2num{$item->name()} = $num;
  114. }
  115. close F;
  116. $self->{contents} = [ @tmp_contents ];
  117. $self->{name2num} = { %tmp_name2num };
  118. $self->{maxassigned} = $max_assigned;
  119. $self->{maxnum} = $max_num;
  120. $self->{filename} = $filename;
  121. # Make a deep copy, allowing {contents} to be an independent work array
  122. foreach my $i (1..$max_num) {
  123. if ($tmp_contents[$i]) {
  124. $self->{loaded_contents}->[$i] =
  125. [ map { OpenSSL::Ordinals::Item->new($_) }
  126. @{$tmp_contents[$i]} ];
  127. }
  128. }
  129. $self->{loaded_maxnum} = $max_num;
  130. return 1;
  131. }
  132. =item B<< $ordinals->renumber >>
  133. Renumber any item that doesn't have an assigned number yet.
  134. =cut
  135. sub renumber {
  136. my $self = shift;
  137. my $max_assigned = 0;
  138. foreach ($self->items(sort => by_number())) {
  139. $_->number($_->intnum()) if $_->number() =~ m|^\?|;
  140. if ($max_assigned < $_->number()) {
  141. $max_assigned = $_->number();
  142. }
  143. }
  144. $self->{maxassigned} = $max_assigned;
  145. }
  146. =item B<< $ordinals->rewrite >>
  147. =item B<< $ordinals->rewrite >>, I<%options>
  148. If an ordinals file has been loaded, it gets rewritten with the data from
  149. the current work database.
  150. If there are more arguments, they are used as I<%options> with the
  151. same semantics as for B<< $ordinals->items >> described below, apart
  152. from B<sort>, which is forbidden here.
  153. =cut
  154. sub rewrite {
  155. my $self = shift;
  156. my %opts = @_;
  157. $self->write($self->{filename}, %opts);
  158. }
  159. =item B<< $ordinals->write FILENAME >>
  160. =item B<< $ordinals->write FILENAME >>, I<%options>
  161. Writes the current work database data to the ordinals file FILENAME.
  162. This also validates the data, see B<< $ordinals->validate >> below.
  163. If there are more arguments, they are used as I<%options> with the
  164. same semantics as for B<< $ordinals->items >> described next, apart
  165. from B<sort>, which is forbidden here.
  166. =cut
  167. sub write {
  168. my $self = shift;
  169. my $filename = shift;
  170. my %opts = @_;
  171. croak "Undefined filename" unless defined($filename);
  172. croak "The 'sort' option is not allowed" if $opts{sort};
  173. $self->validate();
  174. open F, '>', $filename or croak "Unable to open $filename";
  175. foreach ($self->items(%opts, sort => by_number())) {
  176. print F $_->to_string(),"\n";
  177. }
  178. close F;
  179. $self->{filename} = $filename;
  180. $self->{loaded_maxnum} = $self->{maxnum};
  181. return 1;
  182. }
  183. =item B<< $ordinals->items >> I<%options>
  184. Returns a list of items according to a set of criteria. The criteria is
  185. given in form keyed pair form, i.e. a series of C<< key => value >> pairs.
  186. Available options are:
  187. =over 4
  188. =item B<< sort => SORTFUNCTION >>
  189. SORTFUNCTION is a reference to a function that takes two arguments, which
  190. correspond to the classic C<$a> and C<$b> that are available in a C<sort>
  191. block.
  192. =item B<< filter => FILTERFUNCTION >>
  193. FILTERFUNCTION is a reference to a function that takes one argument, which
  194. is every OpenSSL::Ordinals::Item element available.
  195. =back
  196. =cut
  197. sub items {
  198. my $self = shift;
  199. my %opts = @_;
  200. my $comparator = $opts{sort};
  201. my $filter = $opts{filter} // sub { 1; };
  202. my @l = undef;
  203. if (ref($filter) eq 'ARRAY') {
  204. # run a "magic" filter
  205. if ($filter->[0] == F_NUMBER) {
  206. my $index = $filter->[1];
  207. @l = $index ? @{$self->{contents}->[$index] // []} : ();
  208. } elsif ($filter->[0] == F_NAME) {
  209. my $index = $self->{name2num}->{$filter->[1]};
  210. @l = $index ? @{$self->{contents}->[$index] // []} : ();
  211. } else {
  212. croak __PACKAGE__."->items called with invalid filter";
  213. }
  214. } elsif (ref($filter) eq 'CODE') {
  215. @l = grep { $filter->($_) }
  216. map { @{$_ // []} }
  217. @{$self->{contents}};
  218. } else {
  219. croak __PACKAGE__."->items called with invalid filter";
  220. }
  221. return sort { $comparator->($a, $b); } @l
  222. if (defined $comparator);
  223. return @l;
  224. }
  225. # Put an array of items back into the object after having checked consistency
  226. # If there are exactly two items:
  227. # - They MUST have the same number
  228. # - They MUST have the same version
  229. # - For platforms, both MUST hold the same ones, but with opposite values
  230. # - For features, both MUST hold the same ones.
  231. # - They MUST NOT have identical name, type, numeral, version, platforms, and features
  232. # If there's just one item, just put it in the slot of its number
  233. # In all other cases, something is wrong
  234. sub _putback {
  235. my $self = shift;
  236. my @items = @_;
  237. if (scalar @items < 1 || scalar @items > 2) {
  238. croak "Wrong number of items: ", scalar @items, "\n ",
  239. join("\n ", map { $_->{source}.": ".$_->name() } @items), "\n";
  240. }
  241. if (scalar @items == 2) {
  242. # Collect some data
  243. my %numbers = ();
  244. my %versions = ();
  245. my %features = ();
  246. foreach (@items) {
  247. $numbers{$_->intnum()} = 1;
  248. $versions{$_->version()} = 1;
  249. foreach ($_->features()) {
  250. $features{$_}++;
  251. }
  252. }
  253. # Check that all items we're trying to put back have the same number
  254. croak "Items don't have the same numeral: ",
  255. join(", ", map { $_->name()." => ".$_->intnum() } @items), "\n"
  256. if (scalar keys %numbers > 1);
  257. croak "Items don't have the same version: ",
  258. join(", ", map { $_->name()." => ".$_->version() } @items), "\n"
  259. if (scalar keys %versions > 1);
  260. # Check that both items run with the same features
  261. foreach (@items) {
  262. }
  263. foreach (keys %features) {
  264. delete $features{$_} if $features{$_} == 2;
  265. }
  266. croak "Features not in common between ",
  267. $items[0]->name(), " and ", $items[1]->name(), ":",
  268. join(", ", sort keys %features), "\n"
  269. if %features;
  270. # Check for in addition identical name, type, and platforms
  271. croak "Duplicate entries for ".$items[0]->name()." from ".
  272. $items[0]->source()." and ".$items[1]->source()."\n"
  273. if $items[0]->name() eq $items[1]->name()
  274. && $items[0]->type() eq $items[1]->type()
  275. && $items[0]->platforms() eq $items[1]->platforms();
  276. # Check that all platforms exist in both items, and have opposite values
  277. my @platforms = ( { $items[0]->platforms() },
  278. { $items[1]->platforms() } );
  279. foreach my $platform (keys %{$platforms[0]}) {
  280. if (exists $platforms[1]->{$platform}) {
  281. if ($platforms[0]->{$platform} != !$platforms[1]->{$platform}) {
  282. croak "Platforms aren't opposite: ",
  283. join(", ",
  284. map { my %tmp_h = $_->platforms();
  285. $_->name().":".$platform
  286. ." => "
  287. .$tmp_h{$platform} } @items),
  288. "\n";
  289. }
  290. # We're done with these
  291. delete $platforms[0]->{$platform};
  292. delete $platforms[1]->{$platform};
  293. }
  294. }
  295. # If there are any remaining platforms, something's wrong
  296. if (%{$platforms[0]} || %{$platforms[0]}) {
  297. croak "There are platforms not in common between ",
  298. $items[0]->name(), " and ", $items[1]->name(), "\n";
  299. }
  300. }
  301. $self->{contents}->[$items[0]->intnum()] = [ @items ];
  302. }
  303. sub _parse_platforms {
  304. my $self = shift;
  305. my @defs = @_;
  306. my %platforms = ();
  307. foreach (@defs) {
  308. m{^(!)?};
  309. my $op = !(defined $1 && $1 eq '!');
  310. my $def = $';
  311. if ($def =~ m{^_?WIN32$}) { $platforms{$&} = $op; }
  312. if ($def =~ m{^__FreeBSD__$}) { $platforms{$&} = $op; }
  313. # For future support
  314. # if ($def =~ m{^__DragonFly__$}) { $platforms{$&} = $op; }
  315. # if ($def =~ m{^__OpenBSD__$}) { $platforms{$&} = $op; }
  316. # if ($def =~ m{^__NetBSD__$}) { $platforms{$&} = $op; }
  317. if ($def =~ m{^OPENSSL_SYS_}) { $platforms{$'} = $op; }
  318. }
  319. return %platforms;
  320. }
  321. sub _parse_features {
  322. my $self = shift;
  323. my @defs = @_;
  324. my %features = ();
  325. foreach (@defs) {
  326. m{^(!)?};
  327. my $op = !(defined $1 && $1 eq '!');
  328. my $def = $';
  329. if ($def =~ m{^ZLIB$}) { $features{$&} = $op; }
  330. if ($def =~ m{^BROTLI$}) { $features{$&} = $op; }
  331. if ($def =~ m{^ZSTD$}) { $features{$&} = $op; }
  332. if ($def =~ m{^OPENSSL_USE_}) { $features{$'} = $op; }
  333. if ($def =~ m{^OPENSSL_NO_}) { $features{$'} = !$op; }
  334. }
  335. return %features;
  336. }
  337. sub _adjust_version {
  338. my $self = shift;
  339. my $version = shift;
  340. my $baseversion = $self->{baseversion};
  341. $version = $baseversion
  342. if ($baseversion ne '*' && $version ne '*'
  343. && cmp_versions($baseversion, $version) > 0);
  344. return $version;
  345. }
  346. =item B<< $ordinals->add SOURCE, NAME, TYPE, LIST >>
  347. Adds a new item from file SOURCE named NAME with the type TYPE,
  348. and a set of C macros in
  349. LIST that are expected to be defined or undefined to use this symbol, if
  350. any. For undefined macros, they each must be prefixed with a C<!>.
  351. If this symbol already exists in loaded data, it will be rewritten using
  352. the new input data, but will keep the same ordinal number and version.
  353. If it's entirely new, it will get a '?' and the current default version.
  354. =cut
  355. sub add {
  356. my $self = shift;
  357. my $source = shift; # file where item was defined
  358. my $name = shift;
  359. my $type = shift; # FUNCTION or VARIABLE
  360. my @defs = @_; # Macros from #ifdef and #ifndef
  361. # (the latter prefixed with a '!')
  362. # call signature for debug output
  363. my $verbsig = "add('$name' , '$type' , [ " . join(', ', @defs) . " ])";
  364. croak __PACKAGE__."->add got a bad type '$type'"
  365. unless $type eq 'FUNCTION' || $type eq 'VARIABLE';
  366. my %platforms = _parse_platforms(@defs);
  367. my %features = _parse_features(@defs);
  368. my @items = $self->items(filter => f_name($name));
  369. my $version = @items ? $items[0]->version() : $self->{currversion};
  370. my $intnum = @items ? $items[0]->intnum() : ++$self->{maxnum};
  371. my $number = @items ? $items[0]->number() : '?';
  372. print STDERR "DEBUG[",__PACKAGE__,":add] $verbsig\n",
  373. @items ? map { "\t".$_->to_string()."\n" } @items : "No previous items\n",
  374. if $self->{debug};
  375. @items = grep { $_->exists() } @items;
  376. my $new_item =
  377. OpenSSL::Ordinals::Item->new( source => $source,
  378. name => $name,
  379. type => $type,
  380. number => $number,
  381. intnum => $intnum,
  382. version =>
  383. $self->_adjust_version($version),
  384. exists => 1,
  385. platforms => { %platforms },
  386. features => [
  387. grep { $features{$_} } keys %features
  388. ] );
  389. push @items, $new_item;
  390. print STDERR "DEBUG[",__PACKAGE__,"::add] $verbsig\n", map { "\t".$_->to_string()."\n" } @items
  391. if $self->{debug};
  392. $self->_putback(@items);
  393. # If an alias was defined beforehand, add an item for it now
  394. my $alias = $self->{aliases}->{$name};
  395. delete $self->{aliases}->{$name};
  396. # For the caller to show
  397. my @returns = ( $new_item );
  398. push @returns, $self->add_alias($source, $alias->{name}, $name, @{$alias->{defs}})
  399. if defined $alias;
  400. return @returns;
  401. }
  402. =item B<< $ordinals->add_alias SOURCE, ALIAS, NAME, LIST >>
  403. Adds an alias ALIAS for the symbol NAME from file SOURCE, and a set of C macros
  404. in LIST that are expected to be defined or undefined to use this symbol, if any.
  405. For undefined macros, they each must be prefixed with a C<!>.
  406. If this symbol already exists in loaded data, it will be rewritten using
  407. the new input data. Otherwise, the data will just be store away, to wait
  408. that the symbol NAME shows up.
  409. =cut
  410. sub add_alias {
  411. my $self = shift;
  412. my $source = shift;
  413. my $alias = shift; # This is the alias being added
  414. my $name = shift; # For this name (assuming it exists)
  415. my @defs = @_; # Platform attributes for the alias
  416. # call signature for debug output
  417. my $verbsig =
  418. "add_alias('$source' , '$alias' , '$name' , [ " . join(', ', @defs) . " ])";
  419. croak "You're kidding me... $alias == $name" if $alias eq $name;
  420. my %platforms = _parse_platforms(@defs);
  421. my %features = _parse_features(@defs);
  422. croak "Alias with associated features is forbidden\n"
  423. if %features;
  424. my $f_byalias = f_name($alias);
  425. my $f_byname = f_name($name);
  426. my @items = $self->items(filter => $f_byalias);
  427. foreach my $item ($self->items(filter => $f_byname)) {
  428. push @items, $item unless grep { $_ == $item } @items;
  429. }
  430. @items = grep { $_->exists() } @items;
  431. croak "Alias already exists ($alias => $name)"
  432. if scalar @items > 1;
  433. if (scalar @items == 0) {
  434. # The item we want to alias for doesn't exist yet, so we cache the
  435. # alias and hope the item we're making an alias of shows up later
  436. $self->{aliases}->{$name} = { source => $source,
  437. name => $alias, defs => [ @defs ] };
  438. print STDERR "DEBUG[",__PACKAGE__,":add_alias] $verbsig\n",
  439. "\tSet future alias $alias => $name\n"
  440. if $self->{debug};
  441. return ();
  442. } elsif (scalar @items == 1) {
  443. # The rule is that an alias is more or less a copy of the original
  444. # item, just with another name. Also, the platforms given here are
  445. # given to the original item as well, with opposite values.
  446. my %alias_platforms = $items[0]->platforms();
  447. foreach (keys %platforms) {
  448. $alias_platforms{$_} = !$platforms{$_};
  449. }
  450. # We supposedly do now know how to do this... *ahem*
  451. $items[0]->{platforms} = { %alias_platforms };
  452. my $number =
  453. $items[0]->number() =~ m|^\?| ? '?+' : $items[0]->number();
  454. my $alias_item = OpenSSL::Ordinals::Item->new(
  455. source => $source,
  456. name => $alias,
  457. type => $items[0]->type(),
  458. number => $number,
  459. intnum => $items[0]->intnum(),
  460. version => $self->_adjust_version($items[0]->version()),
  461. exists => $items[0]->exists(),
  462. platforms => { %platforms },
  463. features => [ $items[0]->features() ]
  464. );
  465. push @items, $alias_item;
  466. print STDERR "DEBUG[",__PACKAGE__,":add_alias] $verbsig\n",
  467. map { "\t".$_->to_string()."\n" } @items
  468. if $self->{debug};
  469. $self->_putback(@items);
  470. # For the caller to show
  471. return ( $alias_item->to_string() );
  472. }
  473. croak "$name has an alias already (trying to add alias $alias)\n",
  474. "\t", join(", ", map { $_->name() } @items), "\n";
  475. }
  476. =item B<< $ordinals->set_version VERSION >>
  477. =item B<< $ordinals->set_version VERSION BASEVERSION >>
  478. Sets the default version for new symbol to VERSION.
  479. If given, BASEVERSION sets the base version, i.e. the minimum version
  480. for all symbols. If not given, it will be calculated as follows:
  481. =over 4
  482. If the given version is '*', then the base version will also be '*'.
  483. If the given version starts with '0.', the base version will be '0.0.0'.
  484. If the given version starts with '1.0.', the base version will be '1.0.0'.
  485. If the given version starts with '1.1.', the base version will be '1.1.0'.
  486. If the given version has a first number C<N> that's greater than 1, the
  487. base version will be formed from C<N>: 'N.0.0'.
  488. =back
  489. =cut
  490. sub set_version {
  491. my $self = shift;
  492. # '*' is for "we don't care"
  493. my $version = shift // '*';
  494. my $baseversion = shift // '*';
  495. if ($baseversion eq '*') {
  496. $baseversion = $version;
  497. if ($baseversion ne '*') {
  498. if ($baseversion =~ m|^(\d+)\.|, $1 > 1) {
  499. $baseversion = "$1.0.0";
  500. } else {
  501. $baseversion =~ s|^0\..*$|0.0.0|;
  502. $baseversion =~ s|^1\.0\..*$|1.0.0|;
  503. $baseversion =~ s|^1\.1\..*$|1.1.0|;
  504. die 'Invalid version'
  505. if ($baseversion ne '0.0.0'
  506. && $baseversion !~ m|^1\.[01]\.0$|);
  507. }
  508. }
  509. }
  510. die 'Invalid base version'
  511. if ($baseversion ne '*' && $version ne '*'
  512. && cmp_versions($baseversion, $version) > 0);
  513. $self->{currversion} = $version;
  514. $self->{baseversion} = $baseversion;
  515. foreach ($self->items(filter => sub { $_[0] eq '*' })) {
  516. $_->{version} = $self->{currversion};
  517. }
  518. return 1;
  519. }
  520. =item B<< $ordinals->invalidate >>
  521. Invalidates the whole working database. The practical effect is that all
  522. symbols are set to not exist, but are kept around in the database to retain
  523. ordinal numbers and versions.
  524. =cut
  525. sub invalidate {
  526. my $self = shift;
  527. foreach (@{$self->{contents}}) {
  528. foreach (@{$_ // []}) {
  529. $_->{exists} = 0;
  530. }
  531. }
  532. $self->{stats} = {};
  533. }
  534. =item B<< $ordinals->validate >>
  535. Validates the current working database by collection statistics on how many
  536. symbols were added and how many were changed. These numbers can be retrieved
  537. with B<< $ordinals->stats >>.
  538. =cut
  539. sub validate {
  540. my $self = shift;
  541. $self->{stats} = {};
  542. for my $i (1..$self->{maxnum}) {
  543. if ($i > $self->{loaded_maxnum}
  544. || (!@{$self->{loaded_contents}->[$i] // []}
  545. && @{$self->{contents}->[$i] // []})) {
  546. $self->{stats}->{new}++;
  547. }
  548. if ($i <= $self->{maxassigned}) {
  549. $self->{stats}->{assigned}++;
  550. } else {
  551. $self->{stats}->{unassigned}++;
  552. }
  553. next if ($i > $self->{loaded_maxnum});
  554. my @loaded_strings =
  555. map { $_->to_string() } @{$self->{loaded_contents}->[$i] // []};
  556. my @current_strings =
  557. map { $_->to_string() } @{$self->{contents}->[$i] // []};
  558. foreach my $str (@current_strings) {
  559. @loaded_strings = grep { $str ne $_ } @loaded_strings;
  560. }
  561. if (@loaded_strings) {
  562. $self->{stats}->{modified}++;
  563. }
  564. }
  565. }
  566. =item B<< $ordinals->stats >>
  567. Returns the statistics that B<validate> calculate.
  568. =cut
  569. sub stats {
  570. my $self = shift;
  571. return %{$self->{stats}};
  572. }
  573. =back
  574. =head2 Data elements
  575. Data elements, which is each line in an ordinals file, are instances
  576. of a separate class, OpenSSL::Ordinals::Item, with its own methods:
  577. =over 4
  578. =cut
  579. package OpenSSL::Ordinals::Item;
  580. use strict;
  581. use warnings;
  582. use Carp;
  583. =item B<new> I<%options>
  584. Creates a new instance of the C<OpenSSL::Ordinals::Item> class. It takes
  585. options in keyed pair form, i.e. a series of C<< key => value >> pairs.
  586. Available options are:
  587. =over 4
  588. =item B<< source => FILENAME >>, B<< from => STRING >>
  589. This will create a new item from FILENAME, filled with data coming from STRING.
  590. STRING must conform to the following EBNF description:
  591. ordinal string = symbol, spaces, ordinal, spaces, version, spaces,
  592. exist, ":", platforms, ":", type, ":", features;
  593. spaces = space, { space };
  594. space = " " | "\t";
  595. symbol = ( letter | "_" ), { letter | digit | "_" };
  596. ordinal = number | "?" | "?+";
  597. version = number, "_", number, "_", number, [ letter, [ letter ] ];
  598. exist = "EXIST" | "NOEXIST";
  599. platforms = platform, { ",", platform };
  600. platform = ( letter | "_" ) { letter | digit | "_" };
  601. type = "FUNCTION" | "VARIABLE";
  602. features = feature, { ",", feature };
  603. feature = ( letter | "_" ) { letter | digit | "_" };
  604. number = digit, { digit };
  605. (C<letter> and C<digit> are assumed self evident)
  606. =item B<< source => FILENAME >>, B<< name => STRING >>, B<< number => NUMBER >>,
  607. B<< version => STRING >>, B<< exists => BOOLEAN >>, B<< type => STRING >>,
  608. B<< platforms => HASHref >>, B<< features => LISTref >>
  609. This will create a new item with data coming from the arguments.
  610. =back
  611. =cut
  612. sub new {
  613. my $class = shift;
  614. if (ref($_[0]) eq $class) {
  615. return $class->new( map { $_ => $_[0]->{$_} } keys %{$_[0]} );
  616. }
  617. my %opts = @_;
  618. croak "No argument given" unless %opts;
  619. my $instance = undef;
  620. if ($opts{from}) {
  621. my @a = split /\s+/, $opts{from};
  622. croak "Badly formatted ordinals string: $opts{from}"
  623. unless ( scalar @a == 4
  624. && $a[0] =~ /^[A-Za-z_][A-Za-z_0-9]*$/
  625. && $a[1] =~ /^\d+|\?\+?$/
  626. && $a[2] =~ /^(?:\*|\d+_\d+_\d+[a-z]{0,2})$/
  627. && $a[3] =~ /^
  628. (?:NO)?EXIST:
  629. [^:]*:
  630. (?:FUNCTION|VARIABLE):
  631. [^:]*
  632. $
  633. /x );
  634. my @b = split /:/, $a[3];
  635. %opts = ( source => $opts{source},
  636. name => $a[0],
  637. number => $a[1],
  638. version => $a[2],
  639. exists => $b[0] eq 'EXIST',
  640. platforms => { map { m|^(!)?|; $' => !$1 }
  641. split /,/,$b[1] },
  642. type => $b[2],
  643. features => [ split /,/,$b[3] // '' ] );
  644. }
  645. if ($opts{name} && $opts{version} && defined $opts{exists} && $opts{type}
  646. && ref($opts{platforms} // {}) eq 'HASH'
  647. && ref($opts{features} // []) eq 'ARRAY') {
  648. my $version = $opts{version};
  649. $version =~ s|_|.|g;
  650. $instance = { source => $opts{source},
  651. name => $opts{name},
  652. type => $opts{type},
  653. number => $opts{number},
  654. intnum => $opts{intnum},
  655. version => $version,
  656. exists => !!$opts{exists},
  657. platforms => { %{$opts{platforms} // {}} },
  658. features => [ sort @{$opts{features} // []} ] };
  659. } else {
  660. croak __PACKAGE__."->new() called with bad arguments\n".
  661. join("", map { " $_\t=> ".$opts{$_}."\n" } sort keys %opts);
  662. }
  663. return bless $instance, $class;
  664. }
  665. sub DESTROY {
  666. }
  667. =item B<< $item->name >>
  668. The symbol name for this item.
  669. =item B<< $item->number >> (read-write)
  670. The positional number for this item.
  671. This may be '?' for an unassigned symbol, or '?+' for an unassigned symbol
  672. that's an alias for the previous symbol. '?' and '?+' must be properly
  673. handled by the caller. The caller may change this to an actual number.
  674. =item B<< $item->version >> (read-only)
  675. The version number for this item. Please note that these version numbers
  676. have underscore (C<_>) as a separator for the version parts.
  677. =item B<< $item->exists >> (read-only)
  678. A boolean that tells if this symbol exists in code or not.
  679. =item B<< $item->platforms >> (read-only)
  680. A hash table reference. The keys of the hash table are the names of
  681. the specified platforms, with a value of 0 to indicate that this symbol
  682. isn't available on that platform, and 1 to indicate that it is. Platforms
  683. that aren't mentioned default to 1.
  684. =item B<< $item->type >> (read-only)
  685. C<FUNCTION> or C<VARIABLE>, depending on what the symbol represents.
  686. Some platforms do not care about this, others do.
  687. =item B<< $item->features >> (read-only)
  688. An array reference, where every item indicates a feature where this symbol
  689. is available. If no features are mentioned, the symbol is always available.
  690. If any feature is mentioned, this symbol is I<only> available when those
  691. features are enabled.
  692. =cut
  693. our $AUTOLOAD;
  694. # Generic getter
  695. sub AUTOLOAD {
  696. my $self = shift;
  697. my $funcname = $AUTOLOAD;
  698. (my $item = $funcname) =~ s|.*::||g;
  699. croak "$funcname called as setter" if @_;
  700. croak "$funcname invalid" unless exists $self->{$item};
  701. return $self->{$item} if ref($self->{$item}) eq '';
  702. return @{$self->{$item}} if ref($self->{$item}) eq 'ARRAY';
  703. return %{$self->{$item}} if ref($self->{$item}) eq 'HASH';
  704. }
  705. =item B<< $item->intnum >> (read-write)
  706. Internal positional number. If I<< $item->number >> is '?' or '?+', the
  707. caller can use this to set a number for its purposes.
  708. If I<< $item->number >> is a number, I<< $item->intnum >> should be the
  709. same
  710. =cut
  711. # Getter/setters
  712. sub intnum {
  713. my $self = shift;
  714. my $value = shift;
  715. my $item = 'intnum';
  716. croak "$item called with extra arguments" if @_;
  717. $self->{$item} = "$value" if defined $value;
  718. return $self->{$item};
  719. }
  720. sub number {
  721. my $self = shift;
  722. my $value = shift;
  723. my $item = 'number';
  724. croak "$item called with extra arguments" if @_;
  725. $self->{$item} = "$value" if defined $value;
  726. return $self->{$item};
  727. }
  728. =item B<< $item->to_string >>
  729. Converts the item to a string that can be saved in an ordinals file.
  730. =cut
  731. sub to_string {
  732. my $self = shift;
  733. croak "Too many arguments" if @_;
  734. my %platforms = $self->platforms();
  735. my @features = $self->features();
  736. my $version = $self->version();
  737. $version =~ s|\.|_|g;
  738. return sprintf "%-39s %s\t%s\t%s:%s:%s:%s",
  739. $self->name(),
  740. $self->number(),
  741. $version,
  742. $self->exists() ? 'EXIST' : 'NOEXIST',
  743. join(',', (map { ($platforms{$_} ? '' : '!') . $_ }
  744. sort keys %platforms)),
  745. $self->type(),
  746. join(',', @features);
  747. }
  748. =back
  749. =head2 Comparators and filters
  750. For the B<< $ordinals->items >> method, there are a few functions to create
  751. comparators based on specific data:
  752. =over 4
  753. =cut
  754. # Go back to the main package to create comparators and filters
  755. package OpenSSL::Ordinals;
  756. # Comparators...
  757. =item B<by_name>
  758. Returns a comparator that will compare the names of two OpenSSL::Ordinals::Item
  759. objects.
  760. =cut
  761. sub by_name {
  762. return sub { $_[0]->name() cmp $_[1]->name() };
  763. }
  764. =item B<by_number>
  765. Returns a comparator that will compare the ordinal numbers of two
  766. OpenSSL::Ordinals::Item objects.
  767. =cut
  768. sub by_number {
  769. return sub { $_[0]->intnum() <=> $_[1]->intnum() };
  770. }
  771. =item B<by_version>
  772. Returns a comparator that will compare the version of two
  773. OpenSSL::Ordinals::Item objects.
  774. =cut
  775. sub by_version {
  776. return sub {
  777. # cmp_versions comes from OpenSSL::Util
  778. return cmp_versions($_[0]->version(), $_[1]->version());
  779. }
  780. }
  781. =back
  782. There are also the following filters:
  783. =over 4
  784. =cut
  785. # Filters... these are called by grep, the return sub must use $_ for
  786. # the item to check
  787. =item B<f_version VERSION>
  788. Returns a filter that only lets through symbols with a version number
  789. matching B<VERSION>.
  790. =cut
  791. sub f_version {
  792. my $version = shift;
  793. croak "No version specified"
  794. unless $version && $version =~ /^\d+\.\d+\.\d+[a-z]{0,2}$/;
  795. return sub { $_[0]->version() eq $version };
  796. }
  797. =item B<f_number NUMBER>
  798. Returns a filter that only lets through symbols with the ordinal number
  799. matching B<NUMBER>.
  800. NOTE that this returns a "magic" value that can not be used as a function.
  801. It's only useful when passed directly as a filter to B<items>.
  802. =cut
  803. sub f_number {
  804. my $number = shift;
  805. croak "No number specified"
  806. unless $number && $number =~ /^\d+$/;
  807. return [ F_NUMBER, $number ];
  808. }
  809. =item B<f_name NAME>
  810. Returns a filter that only lets through symbols with the symbol name
  811. matching B<NAME>.
  812. NOTE that this returns a "magic" value that can not be used as a function.
  813. It's only useful when passed directly as a filter to B<items>.
  814. =cut
  815. sub f_name {
  816. my $name = shift;
  817. croak "No name specified"
  818. unless $name;
  819. return [ F_NAME, $name ];
  820. }
  821. =back
  822. =head1 AUTHORS
  823. Richard Levitte E<lt>levitte@openssl.orgE<gt>.
  824. =cut
  825. 1;