|
| 1 | +/* |
| 2 | + **** BEGIN LICENSE BLOCK ***** |
| 3 | + * BSD 2-Clause License |
| 4 | + * |
| 5 | + * Copyright (c) 2026, Olle Jonsson |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + ***** END LICENSE BLOCK *****/ |
| 29 | + |
| 30 | +package org.jruby.ext.digest; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +import org.jruby.Ruby; |
| 35 | +import org.jruby.RubyClass; |
| 36 | +import org.jruby.RubyFixnum; |
| 37 | +import org.jruby.RubyModule; |
| 38 | +import org.jruby.RubyObject; |
| 39 | +import org.jruby.RubyString; |
| 40 | +import org.jruby.anno.JRubyClass; |
| 41 | +import org.jruby.anno.JRubyMethod; |
| 42 | +import org.jruby.api.Access; |
| 43 | +import org.jruby.runtime.ThreadContext; |
| 44 | +import org.jruby.runtime.Visibility; |
| 45 | +import org.jruby.runtime.builtin.IRubyObject; |
| 46 | +import org.jruby.runtime.load.Library; |
| 47 | +import org.jruby.util.ByteList; |
| 48 | + |
| 49 | +public class CRC32 implements Library { |
| 50 | + |
| 51 | + public void load(final Ruby runtime, boolean wrap) throws IOException { |
| 52 | + runtime.getLoadService().require("digest"); |
| 53 | + ThreadContext context = runtime.getCurrentContext(); |
| 54 | + RubyModule Digest = Access.getModule(context, "Digest"); |
| 55 | + RubyClass Base = Digest.getClass(context, "Base"); |
| 56 | + RubyClass crc32 = Digest.defineClassUnder(context, "CRC32", Base, DigestCRC32::new); |
| 57 | + crc32.defineMethods(context, DigestCRC32.class); |
| 58 | + } |
| 59 | + |
| 60 | + private static final class CloneableCRC32 extends java.util.zip.CRC32 implements Cloneable { |
| 61 | + @Override |
| 62 | + public CloneableCRC32 clone() throws CloneNotSupportedException { |
| 63 | + return (CloneableCRC32) super.clone(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @JRubyClass(name="Digest::CRC32", parent="Digest::Base") |
| 68 | + public static class DigestCRC32 extends RubyObject { |
| 69 | + private static final long serialVersionUID = -2811470471354871234L; |
| 70 | + |
| 71 | + private transient CloneableCRC32 crc = new CloneableCRC32(); |
| 72 | + |
| 73 | + public DigestCRC32(Ruby runtime, RubyClass type) { |
| 74 | + super(runtime, type); |
| 75 | + } |
| 76 | + |
| 77 | + @JRubyMethod(required = 1, visibility = Visibility.PRIVATE) |
| 78 | + @Override |
| 79 | + public IRubyObject initialize_copy(ThreadContext context, IRubyObject obj) { |
| 80 | + if (this == obj) return this; |
| 81 | + DigestCRC32 from = (DigestCRC32) obj; |
| 82 | + this.checkFrozen(); |
| 83 | + try { |
| 84 | + this.crc = from.crc.clone(); |
| 85 | + } catch (CloneNotSupportedException e) { |
| 86 | + throw getRuntime().newRaiseException(getRuntime().getTypeError(), "Could not initialize copy of Digest::CRC32"); |
| 87 | + } |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @JRubyMethod(name = {"update", "<<"}, required = 1) |
| 92 | + public IRubyObject update(IRubyObject obj) { |
| 93 | + ByteList bytes = obj.convertToString().getByteList(); |
| 94 | + crc.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize()); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + @JRubyMethod() |
| 99 | + public IRubyObject finish() { |
| 100 | + long val = crc.getValue(); |
| 101 | + crc.reset(); |
| 102 | + byte[] digest = new byte[] { |
| 103 | + (byte)(val >>> 24), |
| 104 | + (byte)(val >>> 16), |
| 105 | + (byte)(val >>> 8), |
| 106 | + (byte) val |
| 107 | + }; |
| 108 | + return RubyString.newStringNoCopy(getRuntime(), digest); |
| 109 | + } |
| 110 | + |
| 111 | + @JRubyMethod() |
| 112 | + public IRubyObject reset() { |
| 113 | + crc.reset(); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + @JRubyMethod() |
| 118 | + public IRubyObject digest_length() { |
| 119 | + return RubyFixnum.newFixnum(getRuntime(), 4); |
| 120 | + } |
| 121 | + |
| 122 | + @JRubyMethod() |
| 123 | + public IRubyObject block_length() { |
| 124 | + return RubyFixnum.newFixnum(getRuntime(), 8); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments