|
| 1 | +package io.ipfs.multibase; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +/* |
| 8 | + * Copyright 2025 Michael Vorburger.ch |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * <a href="https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md">Base256Emoji</a> |
| 25 | + * is an encoding mapping each 0-255 byte value to (or from) a specific single Unicode Emoji character. |
| 26 | + * |
| 27 | + * @author <a href="https://www.vorburger.ch/">Michael Vorburger.ch</a> |
| 28 | + */ |
| 29 | +public class Base256Emoji { |
| 30 | + |
| 31 | + // from https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md |
| 32 | + private static final String[] EMOJIS = { |
| 33 | + "🚀", "🪐", "☄", "🛰", "🌌", "🌑", "🌒", "🌓", "🌔", "🌕", |
| 34 | + "🌖", "🌗", "🌘", "🌍", "🌏", "🌎", "🐉", "☀", "💻", "🖥", |
| 35 | + "💾", "💿", "😂", "❤", "😍", "🤣", "😊", "🙏", "💕", "😭", |
| 36 | + "😘", "👍", "😅", "👏", "😁", "🔥", "🥰", "💔", "💖", "💙", |
| 37 | + "😢", "🤔", "😆", "🙄", "💪", "😉", "☺", "👌", "🤗", "💜", |
| 38 | + "😔", "😎", "😇", "🌹", "🤦", "🎉", "💞", "✌", "✨", "🤷", |
| 39 | + "😱", "😌", "🌸", "🙌", "😋", "💗", "💚", "😏", "💛", "🙂", |
| 40 | + "💓", "🤩", "😄", "😀", "🖤", "😃", "💯", "🙈", "👇", "🎶", |
| 41 | + "😒", "🤭", "❣", "😜", "💋", "👀", "😪", "😑", "💥", "🙋", |
| 42 | + "😞", "😩", "😡", "🤪", "👊", "🥳", "😥", "🤤", "👉", "💃", |
| 43 | + "😳", "✋", "😚", "😝", "😴", "🌟", "😬", "🙃", "🍀", "🌷", |
| 44 | + "😻", "😓", "⭐", "✅", "🥺", "🌈", "😈", "🤘", "💦", "✔", |
| 45 | + "😣", "🏃", "💐", "☹", "🎊", "💘", "😠", "☝", "😕", "🌺", |
| 46 | + "🎂", "🌻", "😐", "🖕", "💝", "🙊", "😹", "🗣", "💫", "💀", |
| 47 | + "👑", "🎵", "🤞", "😛", "🔴", "😤", "🌼", "😫", "⚽", "🤙", |
| 48 | + "☕", "🏆", "🤫", "👈", "😮", "🙆", "🍻", "🍃", "🐶", "💁", |
| 49 | + "😲", "🌿", "🧡", "🎁", "⚡", "🌞", "🎈", "❌", "✊", "👋", |
| 50 | + "😰", "🤨", "😶", "🤝", "🚶", "💰", "🍓", "💢", "🤟", "🙁", |
| 51 | + "🚨", "💨", "🤬", "✈", "🎀", "🍺", "🤓", "😙", "💟", "🌱", |
| 52 | + "😖", "👶", "🥴", "▶", "➡", "❓", "💎", "💸", "⬇", "😨", |
| 53 | + "🌚", "🦋", "😷", "🕺", "⚠", "🙅", "😟", "😵", "👎", "🤲", |
| 54 | + "🤠", "🤧", "📌", "🔵", "💅", "🧐", "🐾", "🍒", "😗", "🤑", |
| 55 | + "🌊", "🤯", "🐷", "☎", "💧", "😯", "💆", "👆", "🎤", "🙇", |
| 56 | + "🍑", "❄", "🌴", "💣", "🐸", "💌", "📍", "🥀", "🤢", "👅", |
| 57 | + "💡", "💩", "👐", "📸", "👻", "🤐", "🤮", "🎼", "🥵", "🚩", |
| 58 | + "🍎", "🍊", "👼", "💍", "📣", "🥂" }; |
| 59 | + |
| 60 | + // TODO Propose adding a Guava dependency to use ImmutableMap instead of this |
| 61 | + |
| 62 | + private static final Map<String, Integer> EMOJI_TO_INDEX; |
| 63 | + private static final int MAP_EXPECTED_SIZE = EMOJIS.length; |
| 64 | + private static final float MAP_LOAD_FACTOR = 1.0f; |
| 65 | + |
| 66 | + static { |
| 67 | + if (EMOJIS.length != 256) { |
| 68 | + throw new IllegalStateException("EMOJIS.length must be 256, but is " + EMOJIS.length); |
| 69 | + } |
| 70 | + |
| 71 | + Map<String, Integer> mutableMap = new HashMap<>(MAP_EXPECTED_SIZE, MAP_LOAD_FACTOR); |
| 72 | + for (int i = 0; i < EMOJIS.length; i++) { |
| 73 | + mutableMap.put(EMOJIS[i], i); |
| 74 | + } |
| 75 | + EMOJI_TO_INDEX = Collections.unmodifiableMap(mutableMap); |
| 76 | + } |
| 77 | + |
| 78 | + public static String encode(byte[] in) { |
| 79 | + StringBuilder sb = new StringBuilder(in.length); |
| 80 | + for (byte b : in) { |
| 81 | + sb.append(EMOJIS[b & 0xFF]); |
| 82 | + } |
| 83 | + return sb.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + public static byte[] decode(String in) { |
| 87 | + int length = in.codePointCount(0, in.length()); |
| 88 | + byte[] bytes = new byte[length]; |
| 89 | + |
| 90 | + for (int i = 0; i < in.codePointCount(0, in.length()); i++) { |
| 91 | + int cp = in.codePointAt(in.offsetByCodePoints(0, i)); |
| 92 | + String emoji = new String(Character.toChars(cp)); |
| 93 | + Integer index = EMOJI_TO_INDEX.get(emoji); |
| 94 | + if (index == null) { |
| 95 | + throw new IllegalArgumentException("Unknown Base256Emoji character: " + emoji); |
| 96 | + } |
| 97 | + bytes[i] = (byte) (index & 0xFF); |
| 98 | + } |
| 99 | + |
| 100 | + return bytes; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments