Пример #1
0
  public static byte[] mixed_prepare(byte[] src)
      throws IOException, ParseException, UnsupportedEncodingException {
    String s = new String(src, "UTF-8");
    int index = s.indexOf(AT_SIGN);
    StringBuffer out = new StringBuffer();

    if (index > -1) {
      /* special prefixes must not be followed by suffixes! */
      String prefixString = s.substring(0, index);
      int i = findStringIndex(special_prefixes, prefixString);
      String suffixString = s.substring(index + 1, s.length());
      if (i > -1 && !suffixString.equals("")) {
        throw new ParseException("Suffix following a special index", -1);
      }
      UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);
      UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);
      out.append(prep.nfsmxp.prepare(prefix, StringPrep.DEFAULT));
      out.append(AT_SIGN); // add the delimiter
      out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
    } else {
      UCharacterIterator iter = UCharacterIterator.getInstance(s);
      out.append(prep.nfsmxp.prepare(iter, StringPrep.DEFAULT));
    }
    return out.toString().getBytes("UTF-8");
  }
Пример #2
0
 private static byte[] prepare(byte[] src, StringPrep prep)
     throws ParseException, UnsupportedEncodingException {
   String s = new String(src, "UTF-8");
   UCharacterIterator iter = UCharacterIterator.getInstance(s);
   StringBuffer out = prep.prepare(iter, StringPrep.DEFAULT);
   return out.toString().getBytes("UTF-8");
 }
Пример #3
0
  //
  // toUnicode operation; should only apply to a single label
  //
  private static String toUnicodeInternal(String label, int flag) {
    boolean[] caseFlags = null;
    StringBuffer dest;

    // step 1
    // find out if all the codepoints in input are ASCII
    boolean isASCII = isAllASCII(label);

    if (!isASCII) {
      // step 2
      // perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
      try {
        UCharacterIterator iter = UCharacterIterator.getInstance(label);
        dest = namePrep.prepare(iter, flag);
      } catch (Exception e) {
        // toUnicode never fails; if any step fails, return the input string
        return label;
      }
    } else {
      dest = new StringBuffer(label);
    }

    // step 3
    // verify ACE Prefix
    if (startsWithACEPrefix(dest)) {

      // step 4
      // Remove the ACE Prefix
      String temp = dest.substring(ACE_PREFIX_LENGTH, dest.length());

      try {
        // step 5
        // Decode using punycode
        StringBuffer decodeOut = Punycode.decode(new StringBuffer(temp), null);

        // step 6
        // Apply toASCII
        String toASCIIOut = toASCII(decodeOut.toString(), flag);

        // step 7
        // verify
        if (toASCIIOut.equalsIgnoreCase(dest.toString())) {
          // step 8
          // return output of step 5
          return decodeOut.toString();
        }
      } catch (Exception ignored) {
        // no-op
      }
    }

    // just return the input
    return label;
  }
Пример #4
0
  //
  // toASCII operation; should only apply to a single label
  //
  private static String toASCIIInternal(String label, int flag) {
    // step 1
    // Check if the string contains code points outside the ASCII range 0..0x7c.
    boolean isASCII = isAllASCII(label);
    StringBuffer dest;

    // step 2
    // perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
    if (!isASCII) {
      UCharacterIterator iter = UCharacterIterator.getInstance(label);
      try {
        dest = namePrep.prepare(iter, flag);
      } catch (java.text.ParseException e) {
        throw new IllegalArgumentException(e);
      }
    } else {
      dest = new StringBuffer(label);
    }

    // step 8, move forward to check the smallest number of the code points
    // the length must be inside 1..63
    if (dest.length() == 0) {
      throw new IllegalArgumentException("Empty label is not a legal name");
    }

    // step 3
    // Verify the absence of non-LDH ASCII code points
    //   0..0x2c, 0x2e..0x2f, 0x3a..0x40, 0x5b..0x60, 0x7b..0x7f
    // Verify the absence of leading and trailing hyphen
    boolean useSTD3ASCIIRules = ((flag & USE_STD3_ASCII_RULES) != 0);
    if (useSTD3ASCIIRules) {
      for (int i = 0; i < dest.length(); i++) {
        int c = dest.charAt(i);
        if (isNonLDHAsciiCodePoint(c)) {
          throw new IllegalArgumentException("Contains non-LDH ASCII characters");
        }
      }

      if (dest.charAt(0) == '-' || dest.charAt(dest.length() - 1) == '-') {

        throw new IllegalArgumentException("Has leading or trailing hyphen");
      }
    }

    if (!isASCII) {
      // step 4
      // If all code points are inside 0..0x7f, skip to step 8
      if (!isAllASCII(dest.toString())) {
        // step 5
        // verify the sequence does not begin with ACE prefix
        if (!startsWithACEPrefix(dest)) {

          // step 6
          // encode the sequence with punycode
          try {
            dest = Punycode.encode(dest, null);
          } catch (java.text.ParseException e) {
            throw new IllegalArgumentException(e);
          }

          dest = toASCIILower(dest);

          // step 7
          // prepend the ACE prefix
          dest.insert(0, ACE_PREFIX);
        } else {
          throw new IllegalArgumentException("The input starts with the ACE Prefix");
        }
      }
    }

    // step 8
    // the length must be inside 1..63
    if (dest.length() > MAX_LABEL_LENGTH) {
      throw new IllegalArgumentException("The label in the input is too long");
    }

    return dest.toString();
  }