| 1 | package Sereal::Encoder; | ||
| 2 | use 5.014; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw/croak/; | ||
| 6 | use XSLoader; | ||
| 7 | |||
| 8 | our $VERSION = '7.002'; | ||
| 9 | our $XS_VERSION = $VERSION; $VERSION = eval $VERSION; | ||
| 10 | |||
| 11 | our $ChecksumPolicy; | ||
| 12 | our $UnknownOptionPolicy; | ||
| 13 | our $ChecksumMode; | ||
| 14 | our $DefaultKey; | ||
| 15 | $Sereal::ChecksumMode = $ENV{SEREAL_CHECKSUM_MODE} | ||
| 16 | if !defined($Sereal::ChecksumMode) && defined($ENV{SEREAL_CHECKSUM_MODE}) && length($ENV{SEREAL_CHECKSUM_MODE}); | ||
| 17 | $ChecksumMode = $ENV{SEREAL_ENCODER_CHECKSUM_MODE} | ||
| 18 | if !defined($ChecksumMode) && defined($ENV{SEREAL_ENCODER_CHECKSUM_MODE}) && length($ENV{SEREAL_ENCODER_CHECKSUM_MODE}); | ||
| 19 | $Sereal::ChecksumPolicy = $ENV{SEREAL_CHECKSUM_POLICY} | ||
| 20 | if !defined($Sereal::ChecksumPolicy) && defined($ENV{SEREAL_CHECKSUM_POLICY}) && length($ENV{SEREAL_CHECKSUM_POLICY}); | ||
| 21 | $ChecksumPolicy = $ENV{SEREAL_ENCODER_CHECKSUM_POLICY} | ||
| 22 | if !defined($ChecksumPolicy) && defined($ENV{SEREAL_ENCODER_CHECKSUM_POLICY}) && length($ENV{SEREAL_ENCODER_CHECKSUM_POLICY}); | ||
| 23 | $Sereal::UnknownOptionPolicy = $ENV{SEREAL_UNKNOWN_OPTION_POLICY} | ||
| 24 | if !defined($Sereal::UnknownOptionPolicy) && defined($ENV{SEREAL_UNKNOWN_OPTION_POLICY}) && length($ENV{SEREAL_UNKNOWN_OPTION_POLICY}); | ||
| 25 | $UnknownOptionPolicy = $ENV{SEREAL_ENCODER_UNKNOWN_OPTION_POLICY} | ||
| 26 | if !defined($UnknownOptionPolicy) && defined($ENV{SEREAL_ENCODER_UNKNOWN_OPTION_POLICY}) && length($ENV{SEREAL_ENCODER_UNKNOWN_OPTION_POLICY}); | ||
| 27 | |||
| 28 | # Make sure to keep these constants in sync with the C code in srl_encoder.c. | ||
| 29 | # I know they could be exported from C using things like ExtUtils::Constant, | ||
| 30 | # but that's too much of a hassle for just three numbers. | ||
| 31 | #start-no-tidy | ||
| 32 | my ($compress_consts, $full_consts); | ||
| 33 | BEGIN { | ||
| 34 | $compress_consts= { | ||
| 35 | SRL_UNCOMPRESSED => 0, | ||
| 36 | SRL_SNAPPY => 1, | ||
| 37 | SRL_ZLIB => 2, | ||
| 38 | SRL_ZSTD => 3, | ||
| 39 | }; | ||
| 40 | $full_consts= | ||
| 41 | #begin generated | ||
| 42 | { | ||
| 43 | 'SRL_F_ALIASED_DEDUPE_STRINGS' => 4096, | ||
| 44 | 'SRL_F_CANONICAL_REFS' => 32768, | ||
| 45 | 'SRL_F_COMPRESS_SNAPPY' => 64, | ||
| 46 | 'SRL_F_COMPRESS_SNAPPY_INCREMENTAL' => 128, | ||
| 47 | 'SRL_F_COMPRESS_ZLIB' => 256, | ||
| 48 | 'SRL_F_COMPRESS_ZSTD' => 262144, | ||
| 49 | 'SRL_F_CROAK_ON_BLESS' => 4, | ||
| 50 | 'SRL_F_DEDUPE_STRINGS' => 2048, | ||
| 51 | 'SRL_F_ENABLE_FREEZE_SUPPORT' => 16384, | ||
| 52 | 'SRL_F_ENCODER_COMPRESS_FLAGS_MASK' => 262592, | ||
| 53 | 'SRL_F_FREEZE_DENY_SERIALIZE' => 1048576, | ||
| 54 | 'SRL_F_NOWARN_UNKNOWN_OVERLOAD' => 512, | ||
| 55 | 'SRL_F_NO_BLESS_OBJECTS' => 8192, | ||
| 56 | 'SRL_F_REUSE_ENCODER' => 2, | ||
| 57 | 'SRL_F_SHARED_HASHKEYS' => 1, | ||
| 58 | 'SRL_F_SORT_KEYS' => 1024, | ||
| 59 | 'SRL_F_SORT_KEYS_PERL' => 65536, | ||
| 60 | 'SRL_F_SORT_KEYS_PERL_REV' => 131072, | ||
| 61 | 'SRL_F_STRINGIFY_UNKNOWN' => 16, | ||
| 62 | 'SRL_F_UNDEF_UNKNOWN' => 8, | ||
| 63 | 'SRL_F_USE_NATIVE_LONG_DOUBLE' => 2097152, | ||
| 64 | 'SRL_F_USE_STANDARD_DOUBLE' => 524288, | ||
| 65 | 'SRL_F_WARN_UNKNOWN' => 32, | ||
| 66 | '_FLAG_NAME' => [ | ||
| 67 | 'COMPRESS_SNAPPY', | ||
| 68 | 'COMPRESS_SNAPPY_INCREMENTAL', | ||
| 69 | 'COMPRESS_ZLIB', | ||
| 70 | 'COMPRESS_ZSTD', | ||
| 71 | 'SHARED_HASHKEYS', | ||
| 72 | 'REUSE', | ||
| 73 | 'CROAK_ON_BLESS', | ||
| 74 | 'UNDEF_UNKNOWN', | ||
| 75 | 'STRINGIFY_UNKNOWN', | ||
| 76 | 'WARN_UNKNOWN', | ||
| 77 | 'COMPRESS_SNAPPY', | ||
| 78 | 'COMPRESS_SNAPPY_INCREMENTAL', | ||
| 79 | 'COMPRESS_ZLIB', | ||
| 80 | 'NOWARN_UNKNOWN_OVERLOAD', | ||
| 81 | 'SORT_KEYS', | ||
| 82 | 'DEDUPE_STRINGS', | ||
| 83 | 'ALIASED_DEDUPE_STRINGS', | ||
| 84 | 'NO_BLESS_OBJECTS', | ||
| 85 | 'ENABLE_FREEZE_SUPPORT', | ||
| 86 | 'CANONICAL_REFS', | ||
| 87 | 'SORT_KEYS_PERL', | ||
| 88 | 'SORT_KEYS_PERL_REV', | ||
| 89 | 'COMPRESS_ZSTD', | ||
| 90 | 'USE_STANDARD_DOUBLE', | ||
| 91 | 'FREEZE_DENY_SERIALIZE', | ||
| 92 | 'USE_NATIVE_LONG_DOUBLE' | ||
| 93 | ] | ||
| 94 | }; #end generated | ||
| 95 | } | ||
| 96 | use constant $compress_consts; | ||
| 97 | use constant $full_consts; | ||
| 98 | #end-no-tidy | ||
| 99 | |||
| 100 | use Exporter 'import'; | ||
| 101 | our @EXPORT_OK = ( qw( | ||
| 102 | encode_sereal | ||
| 103 | encode_sereal_with_header_data | ||
| 104 | prepare_checksum_key | ||
| 105 | siphash24 | ||
| 106 | siphash24_128 | ||
| 107 | siphash36_128 | ||
| 108 | sereal_encode_with_object | ||
| 109 | ), | ||
| 110 | sort { $a cmp $b } ( | ||
| 111 | keys(%$compress_consts), | ||
| 112 | keys(%$full_consts) ) ); | ||
| 113 | our %EXPORT_TAGS = ( | ||
| 114 | all => \@EXPORT_OK, | ||
| 115 | compress_const => [ sort keys %$compress_consts ], | ||
| 116 | full_const => [ sort( keys(%$compress_consts), keys(%$full_consts) ) ], | ||
| 117 | ); | ||
| 118 | |||
| 119 | # export by default if run from command line | ||
| 120 | our @EXPORT = ( ( caller() )[1] eq '-e' ? @EXPORT_OK : () ); | ||
| 121 | |||
| 122 | sub CLONE_SKIP { 1 } | ||
| 123 | |||
| 124 | XSLoader::load( 'Sereal::Encoder', $XS_VERSION ); | ||
| 125 | if ( !defined($DefaultKey) && defined( $ENV{SEREAL_ENCODER_DEFAULT_KEY} ) ) { | ||
| 126 | my $key = $ENV{SEREAL_ENCODER_DEFAULT_KEY}; | ||
| 127 | if ( $key =~ /\A[0-9a-fA-F]{32}\z/ ) { | ||
| 128 | $DefaultKey = pack( 'H*', $key ); | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | warn "SEREAL_ENCODER_DEFAULT_KEY is not exactly 32 hexadecimal characters; normalizing with Sereal::Encoder::prepare_checksum_key()\n"; | ||
| 132 | $DefaultKey = prepare_checksum_key($key); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | unless ($Sereal::_DefaultKeyInitialized) { | ||
| 136 | if ( !defined($Sereal::DefaultKey) && defined( $ENV{SEREAL_DEFAULT_KEY} ) ) { | ||
| 137 | my $key = $ENV{SEREAL_DEFAULT_KEY}; | ||
| 138 | if ( $key =~ /\A[0-9a-fA-F]{32}\z/ ) { | ||
| 139 | $Sereal::DefaultKey = pack( 'H*', $key ); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | warn | ||
| 143 | "SEREAL_DEFAULT_KEY is not exactly 32 hexadecimal characters; normalizing with Sereal::Encoder::prepare_checksum_key()\n"; | ||
| 144 | $Sereal::DefaultKey = prepare_checksum_key($key); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | $Sereal::_DefaultKeyInitialized = 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub encode_to_file { | ||
| 151 | my ( $self, $file, $struct, $append ) = @_; | ||
| 152 | $self = $self->new() unless ref $self; | ||
| 153 | my $mode = $append ? ">>" : ">"; | ||
| 154 | open my $fh, $mode, $file | ||
| 155 | or die "Failed to open '$file' for " . ( $append ? "append" : "write" ) . ": $!"; | ||
| 156 | print $fh $self->encode($struct) | ||
| 157 | or die "Failed to print to '$file': $!"; | ||
| 158 | close $fh | ||
| 159 | or die "Failed to close '$file': $!"; | ||
| 160 | } | ||
| 161 | |||
| 162 | my $flags = sub { | ||
| 163 | my ( $int, $ary ) = @_; | ||
| 164 | return map { ( $ary->[$_] and $int & ( 1 << $_ ) ) ? $ary->[$_] : () } ( 0 .. $#$ary ); | ||
| 165 | }; | ||
| 166 | |||
| 167 | sub flag_names { | ||
| 168 | my ( $self, $val ) = @_; | ||
| 169 | return $flags->( defined $val ? $val : $self->flags, _FLAG_NAME ); | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | __END__ |