Simon Hunt | b8d3798 | 2016-08-22 21:29:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | b8d3798 | 2016-08-22 21:29:55 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.impl.birds; |
| 18 | |
| 19 | import com.google.common.io.Files; |
| 20 | import org.junit.Ignore; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.FilenameFilter; |
| 26 | import java.io.IOException; |
| 27 | import java.nio.ByteBuffer; |
| 28 | import java.nio.channels.FileChannel; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Arrays; |
| 31 | import java.util.Collections; |
| 32 | import java.util.Comparator; |
| 33 | import java.util.Date; |
| 34 | import java.util.List; |
| 35 | import java.util.Random; |
| 36 | |
| 37 | /** |
| 38 | * Encrypt png images to .foo format. |
| 39 | */ |
| 40 | public class BirdEncoderTest { |
| 41 | |
| 42 | private static final String ORIG = " original> "; |
| 43 | private static final String DATA = " data> "; |
| 44 | private static final String BEAN = " bean> "; |
| 45 | private static final String SAUCE = " sauce> "; |
| 46 | private static final String NONE = "(none)"; |
| 47 | |
| 48 | private static final String PNG = ".png"; |
| 49 | private static final String FOO = ".foo"; |
| 50 | |
| 51 | private static class Encoding { |
| 52 | private final String name; |
| 53 | private String bean = NONE; |
| 54 | private String sauce = NONE; |
| 55 | |
| 56 | Encoding(String name) { |
| 57 | this.name = name; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String toString() { |
| 62 | return ORIG + name + PNG + |
| 63 | DATA + bean + FOO + |
| 64 | BEAN + bean + |
| 65 | SAUCE + sauce; |
| 66 | } |
| 67 | |
| 68 | private Encoding encode() { |
| 69 | String u = name.toUpperCase(); |
| 70 | int x = u.codePointAt(1) - 64; |
| 71 | int r = (x * 7) % 26; |
| 72 | |
| 73 | String uc = doTransform(r, u); |
| 74 | bean = uc.toLowerCase(); |
| 75 | |
| 76 | StringBuilder sauceSb = new StringBuilder(); |
| 77 | sauceSb.append(r).append(":"); |
| 78 | for (String q : uc.split("")) { |
| 79 | sauceSb.append(q.codePointAt(0)); |
| 80 | } |
| 81 | sauce = sauceSb.toString(); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | private String doTransform(int r, String u) { |
| 86 | final int m = 65; |
| 87 | final int x = 90; |
| 88 | |
| 89 | int d = x - m + 1; |
| 90 | int s = x + m; |
| 91 | String[] letters = u.split(""); |
| 92 | StringBuilder sb = new StringBuilder(); |
| 93 | |
| 94 | for (String j : letters) { |
| 95 | int k = j.codePointAt(0); |
| 96 | int e = s - k + r; |
| 97 | e = e > x ? e - d : e; |
| 98 | sb.append(Character.toChars(e)); |
| 99 | } |
| 100 | |
| 101 | return sb.toString(); |
| 102 | } |
| 103 | |
| 104 | String dataName() { |
| 105 | return bean + FOO; |
| 106 | } |
| 107 | |
| 108 | String pngName() { |
| 109 | return name + PNG; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | private static final FilenameFilter FOO_FILTER = |
| 115 | (dir, name) -> name.endsWith(FOO); |
| 116 | private static final FilenameFilter PNG_FILTER = |
| 117 | (dir, name) -> name.endsWith(PNG); |
| 118 | |
| 119 | private static final Comparator<File> FILE_COMPARATOR = |
| 120 | (o1, o2) -> o1.getName().compareTo(o2.getName()); |
| 121 | |
Thomas Vachuska | 25ffa03 | 2018-08-08 17:27:33 -0700 | [diff] [blame] | 122 | private static final File DIR = new File("tmp"); |
Simon Hunt | b8d3798 | 2016-08-22 21:29:55 -0700 | [diff] [blame] | 123 | |
| 124 | private static final File FOO_DIR = new File(DIR, "foo"); |
| 125 | private static final File ORIG_DIR = new File(DIR, "orig"); |
| 126 | |
| 127 | |
| 128 | private static final byte UPPER = (byte) 0xf0; |
| 129 | private static final byte LOWER = (byte) 0x0f; |
| 130 | |
| 131 | |
| 132 | private static void print(String fmt, Object... params) { |
| 133 | System.out.println(String.format(fmt, params)); |
| 134 | } |
| 135 | |
| 136 | private List<File> filesIn(File dir, FilenameFilter filter) { |
| 137 | File[] files = dir.listFiles(filter); |
| 138 | if (files == null) { |
| 139 | return Collections.emptyList(); |
| 140 | } |
| 141 | Arrays.sort(files, FILE_COMPARATOR); |
| 142 | return Arrays.asList(files); |
| 143 | } |
| 144 | |
| 145 | private String basename(File f) { |
| 146 | String name = f.getName(); |
| 147 | int i = name.indexOf(PNG); |
| 148 | return name.substring(0, i); |
| 149 | } |
| 150 | |
| 151 | private boolean dataRequired(Encoding encoding, List<String> dataFiles) { |
| 152 | return !dataFiles.contains(encoding.dataName()); |
| 153 | } |
| 154 | |
| 155 | private List<String> makeFileNames(List<File> dataFiles) { |
| 156 | List<String> names = new ArrayList<>(dataFiles.size()); |
| 157 | for (File f : dataFiles) { |
| 158 | names.add(f.getName()); |
| 159 | } |
| 160 | return names; |
| 161 | } |
| 162 | |
| 163 | private final Random random = new Random(new Date().getTime()); |
| 164 | private final byte[] rbytes = new byte[2]; |
| 165 | |
| 166 | private int r1; |
| 167 | private int r2; |
| 168 | |
| 169 | private void randomBytes() { |
| 170 | random.nextBytes(rbytes); |
| 171 | r1 = rbytes[0]; |
| 172 | r2 = rbytes[1]; |
| 173 | } |
| 174 | |
| 175 | private void addNoise(ByteBuffer bb, byte b) { |
| 176 | randomBytes(); |
| 177 | |
| 178 | int upper = b & UPPER; |
| 179 | int lower = b & LOWER; |
| 180 | |
| 181 | int uNoise = r1 & LOWER; |
| 182 | int lNoise = r2 & UPPER; |
| 183 | |
| 184 | int uEnc = upper | uNoise; |
| 185 | int lEnc = lower | lNoise; |
| 186 | |
| 187 | bb.put((byte) uEnc); |
| 188 | bb.put((byte) lEnc); |
| 189 | } |
| 190 | |
| 191 | private ByteBuffer encodePng(File file) throws IOException { |
| 192 | byte[] bytes = Files.toByteArray(file); |
| 193 | int size = bytes.length; |
| 194 | ByteBuffer bb = ByteBuffer.allocate(size * 2); |
| 195 | for (int i = 0; i < size; i++) { |
| 196 | addNoise(bb, bytes[i]); |
| 197 | } |
| 198 | return bb; |
| 199 | } |
| 200 | |
| 201 | private void generateDataFile(Encoding encoding) { |
| 202 | File png = new File(ORIG_DIR, encoding.pngName()); |
| 203 | File foo = new File(FOO_DIR, encoding.dataName()); |
| 204 | ByteBuffer bb; |
| 205 | try { |
| 206 | bb = encodePng(png); |
| 207 | writeBufferToFile(bb, foo); |
| 208 | } catch (IOException e) { |
| 209 | e.printStackTrace(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private void writeBufferToFile(ByteBuffer bb, File file) |
| 214 | throws IOException { |
| 215 | bb.flip(); |
| 216 | FileChannel chan = new FileOutputStream(file, false).getChannel(); |
| 217 | chan.write(bb); |
| 218 | chan.close(); |
| 219 | print(" Wrote file: %s (%d bytes)", file, bb.capacity()); |
| 220 | } |
| 221 | |
| 222 | @Test @Ignore |
| 223 | public void encodeThings() { |
| 224 | print("Encoding things..."); |
| 225 | List<File> originals = filesIn(ORIG_DIR, PNG_FILTER); |
| 226 | List<File> dataFiles = filesIn(FOO_DIR, FOO_FILTER); |
| 227 | List<String> fileNames = makeFileNames(dataFiles); |
| 228 | |
| 229 | int count = 0; |
| 230 | for (File f : originals) { |
| 231 | Encoding encoding = new Encoding(basename(f)).encode(); |
| 232 | print("%n%s", encoding); |
| 233 | if (dataRequired(encoding, fileNames)) { |
| 234 | generateDataFile(encoding); |
| 235 | count++; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | print("%nFoo files generated: %d", count); |
| 240 | } |
| 241 | |
| 242 | } |