|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules; |
| 42 | + |
| 43 | +import java.util.List; |
| 44 | +import java.util.zip.CRC32; |
| 45 | + |
| 46 | +import com.oracle.graal.python.builtins.Builtin; |
| 47 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 48 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 49 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 50 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 51 | +import com.oracle.graal.python.builtins.objects.bytes.PBytes; |
| 52 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
| 53 | +import com.oracle.graal.python.builtins.objects.module.PythonModule; |
| 54 | +import com.oracle.graal.python.builtins.objects.type.PythonClass; |
| 55 | +import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode; |
| 56 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 57 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 58 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 59 | +import com.oracle.graal.python.runtime.PythonCore; |
| 60 | +import com.oracle.graal.python.runtime.exception.PException; |
| 61 | +import com.oracle.truffle.api.CompilerDirectives; |
| 62 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 63 | +import com.oracle.truffle.api.dsl.Cached; |
| 64 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 65 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 66 | +import com.oracle.truffle.api.dsl.Specialization; |
| 67 | +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; |
| 68 | + |
| 69 | +@CoreFunctions(defineModule = "binascii") |
| 70 | +public class BinasciiModuleBuiltins extends PythonBuiltins { |
| 71 | + private static final String INCOMPLETE = "Incomplete"; |
| 72 | + private static final String ERROR = "Error"; |
| 73 | + |
| 74 | + @Override |
| 75 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 76 | + return BinasciiModuleBuiltinsFactory.getFactories(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void initialize(PythonCore core) { |
| 81 | + super.initialize(core); |
| 82 | + String pre = "binascii."; |
| 83 | + builtinConstants.put(ERROR, core.factory().createPythonClass(PythonBuiltinClassType.PythonClass, pre + ERROR, new PythonClass[]{core.lookupType(PythonBuiltinClassType.ValueError)})); |
| 84 | + builtinConstants.put(INCOMPLETE, core.factory().createPythonClass(PythonBuiltinClassType.PythonClass, pre + INCOMPLETE, new PythonClass[]{core.lookupType(PythonBuiltinClassType.Exception)})); |
| 85 | + } |
| 86 | + |
| 87 | + @Builtin(name = "a2b_base64", fixedNumOfPositionalArgs = 1) |
| 88 | + @GenerateNodeFactory |
| 89 | + static abstract class A2bBase64Node extends PythonUnaryBuiltinNode { |
| 90 | + @Specialization |
| 91 | + @TruffleBoundary |
| 92 | + PBytes a2b(String data) { |
| 93 | + return factory().createBytes(Base64.decode(data)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Builtin(name = "a2b_hex", fixedNumOfPositionalArgs = 2, declaresExplicitSelf = true) |
| 98 | + @GenerateNodeFactory |
| 99 | + static abstract class A2bHexNode extends PythonBinaryBuiltinNode { |
| 100 | + private ReadAttributeFromObjectNode getAttrNode; |
| 101 | + |
| 102 | + private PException raise(PythonClass klass, String string) { |
| 103 | + return raise(factory().createBaseException(klass, string, new Object[0])); |
| 104 | + } |
| 105 | + |
| 106 | + @Specialization |
| 107 | + @TruffleBoundary |
| 108 | + PBytes a2b(PythonModule self, String data) { |
| 109 | + int length = data.length(); |
| 110 | + if (length % 2 != 0) { |
| 111 | + throw oddLengthError(self); |
| 112 | + } |
| 113 | + byte[] output = new byte[length / 2]; |
| 114 | + for (int i = 0; i < length / 2; i++) { |
| 115 | + try { |
| 116 | + output[i] = Byte.valueOf(data.substring(i, i + 2), 16); |
| 117 | + } catch (NumberFormatException e) { |
| 118 | + throw nonHexError(self); |
| 119 | + } |
| 120 | + } |
| 121 | + return factory().createBytes(output); |
| 122 | + } |
| 123 | + |
| 124 | + private PException oddLengthError(PythonModule self) { |
| 125 | + return raise((PythonClass) getAttrNode().execute(self, ERROR), "Odd-length string"); |
| 126 | + } |
| 127 | + |
| 128 | + private PException nonHexError(PythonModule self) { |
| 129 | + return raise((PythonClass) getAttrNode().execute(self, ERROR), "Non-hexadecimal digit found"); |
| 130 | + } |
| 131 | + |
| 132 | + private ReadAttributeFromObjectNode getAttrNode() { |
| 133 | + if (getAttrNode == null) { |
| 134 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 135 | + getAttrNode = insert(ReadAttributeFromObjectNode.create()); |
| 136 | + } |
| 137 | + return getAttrNode; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Builtin(name = "b2a_base64", fixedNumOfPositionalArgs = 1, keywordArguments = {"newline"}) |
| 142 | + @GenerateNodeFactory |
| 143 | + static abstract class B2aBase64Node extends PythonBinaryBuiltinNode { |
| 144 | + @Specialization(guards = "isNoValue(newline)") |
| 145 | + @TruffleBoundary |
| 146 | + String b2a(PBytes data, @SuppressWarnings("unused") PNone newline, |
| 147 | + @Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArray) { |
| 148 | + return b2a(data, true, toByteArray); |
| 149 | + } |
| 150 | + |
| 151 | + @Specialization |
| 152 | + @TruffleBoundary |
| 153 | + String b2a(PBytes data, boolean newline, |
| 154 | + @Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArray) { |
| 155 | + String encode = Base64.encode(toByteArray.execute(data.getSequenceStorage())); |
| 156 | + if (newline) { |
| 157 | + return encode + "\n"; |
| 158 | + } else { |
| 159 | + return encode; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Builtin(name = "b2a_hex", fixedNumOfPositionalArgs = 1) |
| 165 | + @GenerateNodeFactory |
| 166 | + static abstract class B2aHexNode extends PythonUnaryBuiltinNode { |
| 167 | + @Specialization |
| 168 | + @TruffleBoundary |
| 169 | + String b2a(PBytes data, |
| 170 | + @Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArray) { |
| 171 | + byte[] bytes = toByteArray.execute(data.getSequenceStorage()); |
| 172 | + StringBuilder sb = new StringBuilder(); |
| 173 | + for (int i = 0; i < bytes.length; i++) { |
| 174 | + String hexString = Integer.toHexString(bytes[i]); |
| 175 | + if (hexString.length() < 2) { |
| 176 | + sb.append("0"); |
| 177 | + } |
| 178 | + sb.append(hexString); |
| 179 | + } |
| 180 | + return sb.toString(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @Builtin(name = "crc32", fixedNumOfPositionalArgs = 1, keywordArguments = "crc") |
| 185 | + @GenerateNodeFactory |
| 186 | + static abstract class Crc32Node extends PythonBinaryBuiltinNode { |
| 187 | + @Specialization(guards = "isNoValue(crc)") |
| 188 | + @TruffleBoundary |
| 189 | + long b2a(PBytes data, @SuppressWarnings("unused") PNone crc, |
| 190 | + @Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArray) { |
| 191 | + byte[] bytes = toByteArray.execute(data.getSequenceStorage()); |
| 192 | + CRC32 crc32 = new CRC32(); |
| 193 | + crc32.update(bytes); |
| 194 | + return crc32.getValue(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Builtin(name = "hexlify", fixedNumOfPositionalArgs = 1) |
| 199 | + @GenerateNodeFactory |
| 200 | + static abstract class HexlifyNode extends B2aHexNode { |
| 201 | + } |
| 202 | + |
| 203 | + @Builtin(name = "unhexlify", fixedNumOfPositionalArgs = 2, declaresExplicitSelf = true) |
| 204 | + @GenerateNodeFactory |
| 205 | + static abstract class UnhexlifyNode extends A2bHexNode { |
| 206 | + } |
| 207 | +} |
0 commit comments