LCOV - code coverage report
Current view: top level - entities - KeyUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 50 56 89.3 %
Date: 2022-11-19 15:00:39 Functions: 3 4 75.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.entities;
      16             : 
      17             : import static java.nio.charset.StandardCharsets.UTF_8;
      18             : 
      19             : import java.util.Arrays;
      20             : 
      21           0 : public class KeyUtil {
      22         156 :   private static final char[] hexc = {
      23             :     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
      24             :   };
      25             :   private static final char safe[];
      26             :   private static final byte hexb[];
      27             : 
      28             :   static {
      29         156 :     safe = new char[256];
      30         156 :     safe['-'] = '-';
      31         156 :     safe['_'] = '_';
      32         156 :     safe['.'] = '.';
      33         156 :     safe['!'] = '!';
      34         156 :     safe['~'] = '~';
      35         156 :     safe['*'] = '*';
      36         156 :     safe['\''] = '\'';
      37         156 :     safe['('] = '(';
      38         156 :     safe[')'] = ')';
      39         156 :     safe['/'] = '/';
      40         156 :     safe[' '] = '+';
      41         156 :     for (char c = '0'; c <= '9'; c++) safe[c] = c;
      42         156 :     for (char c = 'A'; c <= 'Z'; c++) safe[c] = c;
      43         156 :     for (char c = 'a'; c <= 'z'; c++) safe[c] = c;
      44             : 
      45         156 :     hexb = new byte['f' + 1];
      46         156 :     Arrays.fill(hexb, (byte) -1);
      47         156 :     for (char i = '0'; i <= '9'; i++) hexb[i] = (byte) (i - '0');
      48         156 :     for (char i = 'A'; i <= 'F'; i++) hexb[i] = (byte) ((i - 'A') + 10);
      49         156 :     for (char i = 'a'; i <= 'f'; i++) hexb[i] = (byte) ((i - 'a') + 10);
      50         156 :   }
      51             : 
      52             :   public static String encode(final String key) {
      53         154 :     final byte[] b = key.getBytes(UTF_8);
      54         154 :     final StringBuilder r = new StringBuilder(b.length);
      55         154 :     for (int i = 0; i < b.length; i++) {
      56         154 :       final int c = b[i] & 0xff;
      57         154 :       final char s = safe[c];
      58         154 :       if (s == 0) {
      59          16 :         r.append('%');
      60          16 :         r.append(hexc[c >> 4]);
      61          16 :         r.append(hexc[c & 15]);
      62             :       } else {
      63         154 :         r.append(s);
      64             :       }
      65             :     }
      66         154 :     return r.toString();
      67             :   }
      68             : 
      69             :   public static String decode(final String key) {
      70          51 :     if (key.indexOf('%') < 0) {
      71          51 :       return key.replace('+', ' ');
      72             :     }
      73             : 
      74           5 :     final byte[] b = new byte[key.length()];
      75           5 :     int bPtr = 0;
      76             :     try {
      77           5 :       for (int i = 0; i < key.length(); ) {
      78           5 :         final char c = key.charAt(i);
      79           5 :         if (c == '%' && i + 2 < key.length()) {
      80           5 :           final int v = (hexb[key.charAt(i + 1)] << 4) | hexb[key.charAt(i + 2)];
      81           5 :           if (v < 0) {
      82           0 :             throw new IllegalArgumentException(key.substring(i, i + 3));
      83             :           }
      84           5 :           b[bPtr++] = (byte) v;
      85           5 :           i += 3;
      86           5 :         } else if (c == '+') {
      87           0 :           b[bPtr++] = ' ';
      88           0 :           i++;
      89             :         } else {
      90           5 :           b[bPtr++] = (byte) c;
      91           5 :           i++;
      92             :         }
      93           5 :       }
      94           0 :     } catch (ArrayIndexOutOfBoundsException err) {
      95           0 :       throw new IllegalArgumentException("Bad encoding" + key, err);
      96           5 :     }
      97           5 :     return new String(b, 0, bPtr, UTF_8);
      98             :   }
      99             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750