COMMON: Fix punycode decoding and implement reverse utility for escaped filenames
This commit is contained in:
parent
73fd4a44e6
commit
56f7af8d2e
2 changed files with 35 additions and 9 deletions
|
@ -115,7 +115,7 @@ static size_t encode_var_int(const size_t bias, const size_t delta, char *const
|
||||||
|
|
||||||
static size_t decode_digit(uint32_t v) {
|
static size_t decode_digit(uint32_t v) {
|
||||||
if (Common::isDigit(v)) {
|
if (Common::isDigit(v)) {
|
||||||
return 22 + (v - '0');
|
return 26 + (v - '0');
|
||||||
}
|
}
|
||||||
if (Common::isLower(v)) {
|
if (Common::isLower(v)) {
|
||||||
return v - 'a';
|
return v - 'a';
|
||||||
|
@ -195,9 +195,8 @@ String punycode_decode(const String src1) {
|
||||||
if (!src1.hasPrefix("xn--"))
|
if (!src1.hasPrefix("xn--"))
|
||||||
return src1;
|
return src1;
|
||||||
|
|
||||||
String src(src1.c_str()[4]); // Skip the prefix for simplification
|
String src(&src1.c_str()[4]); // Skip the prefix for simplification
|
||||||
|
int srclen = src.size();
|
||||||
int srclen = src1.size();
|
|
||||||
String dst;
|
String dst;
|
||||||
|
|
||||||
/* Ensure that the input contains only ASCII characters. */
|
/* Ensure that the input contains only ASCII characters. */
|
||||||
|
@ -207,7 +206,7 @@ String punycode_decode(const String src1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t di = src.rfind('-');
|
size_t di = src.findLastOf('-');
|
||||||
|
|
||||||
if (di == String::npos)
|
if (di == String::npos)
|
||||||
return src;
|
return src;
|
||||||
|
@ -273,8 +272,9 @@ String punycode_decode(const String src1) {
|
||||||
i %= (di + 1);
|
i %= (di + 1);
|
||||||
|
|
||||||
String dst1(dst.c_str(), i);
|
String dst1(dst.c_str(), i);
|
||||||
dst1 += n;
|
dst1 += (char )n;
|
||||||
dst1 += String(dst.c_str()[i + 1]);
|
dst1 += String(&dst.c_str()[i]);
|
||||||
|
dst = dst1;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,4 +283,24 @@ fail:
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String punycode_decodefilename(const String src1) {
|
||||||
|
String dst;
|
||||||
|
String src = punycode_decode(src1);
|
||||||
|
|
||||||
|
for (int i = 0; i < src.size(); i++) {
|
||||||
|
if ((byte)src[i] == 0x81 && i + 1 < src.size()) {
|
||||||
|
i++;
|
||||||
|
if (src[i] == 0x79)
|
||||||
|
dst += 0x81;
|
||||||
|
else
|
||||||
|
dst += src[i] - 0x80;
|
||||||
|
} else {
|
||||||
|
dst + src[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // end of namespace Common
|
} // end of namespace Common
|
||||||
|
|
|
@ -49,15 +49,21 @@
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Unicode to Punycode. Returns the number of Unicode characters that were converted.
|
* Convert Binary to Punycode. Returns the encoded string.
|
||||||
*/
|
*/
|
||||||
size_t punycode_encode(const uint32_t *const src, const size_t srclen, char *const dst, size_t *const dstlen);
|
size_t punycode_encode(const uint32_t *const src, const size_t srclen, char *const dst, size_t *const dstlen);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert Punycode to Unicode. Returns the number of bytes that were converted.
|
* Convert Punycode to Binary. Returns the decoded string
|
||||||
*/
|
*/
|
||||||
String punycode_decode(const String src);
|
String punycode_decode(const String src);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Punycode filename to Binary using special 0x81 escape character. Returns the decoded string
|
||||||
|
*/
|
||||||
|
String punycode_decodefilename(const String src1);
|
||||||
|
|
||||||
|
|
||||||
} // end of namespace Common
|
} // end of namespace Common
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue