Skip to content

Commit

Permalink
std.base64: Introduce Base64Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Feb 11, 2013
1 parent 1a5f7f3 commit ba922fd
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions std/base64.d
Expand Up @@ -486,7 +486,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
*/
void popFront()
{
enforce(!empty, "Cannot call popFront on Encoder with no data remaining");
enforce(!empty, new Base64Exception("Cannot call popFront on Encoder with no data remaining"));

range_.popFront();

Expand Down Expand Up @@ -597,7 +597,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
*/
void popFront()
{
enforce(!empty, "Cannot call popFront on Encoder with no data remaining");
enforce(!empty, new Base64Exception("Cannot call popFront on Encoder with no data remaining"));

static if (Padding != NoPadding)
if (padding) {
Expand Down Expand Up @@ -807,7 +807,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
if (srcLen == 0)
return [];
static if (Padding != NoPadding)
enforce(srcLen % 4 == 0, "Invalid length of encoded data");
enforce(srcLen % 4 == 0, new Base64Exception("Invalid length of encoded data"));

immutable blocks = srcLen / 4;
auto srcptr = source.ptr;
Expand Down Expand Up @@ -875,7 +875,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
if (srcLen == 0)
return [];
static if (Padding != NoPadding)
enforce(srcLen % 4 == 0, "Invalid length of encoded data");
enforce(srcLen % 4 == 0, new Base64Exception("Invalid length of encoded data"));

immutable blocks = srcLen / 4;
auto bufptr = buffer.ptr;
Expand Down Expand Up @@ -953,7 +953,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
if (srcLen == 0)
return 0;
static if (Padding != NoPadding)
enforce(srcLen % 4 == 0, "Invalid length of encoded data");
enforce(srcLen % 4 == 0, new Base64Exception("Invalid length of encoded data"));

immutable blocks = srcLen / 4;
auto srcptr = source.ptr;
Expand Down Expand Up @@ -1023,7 +1023,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
if (srcLen == 0)
return 0;
static if (Padding != NoPadding)
enforce(srcLen % 4 == 0, "Invalid length of encoded data");
enforce(srcLen % 4 == 0, new Base64Exception("Invalid length of encoded data"));

immutable blocks = srcLen / 4;
size_t pcount;
Expand Down Expand Up @@ -1152,7 +1152,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
*/
void popFront()
{
enforce(!empty, "Cannot call popFront on Decoder with no data remaining.");
enforce(!empty, new Base64Exception("Cannot call popFront on Decoder with no data remaining."));

range_.popFront();

Expand Down Expand Up @@ -1230,7 +1230,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
range_ = range_.save;

static if (Padding != NoPadding && hasLength!Range)
enforce(range_.length % 4 == 0);
enforce(range_.length % 4 == 0, new Base64Exception("Invalid length of encoded data"));

if (range_.empty)
pos = -1;
Expand Down Expand Up @@ -1273,7 +1273,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
*/
void popFront()
{
enforce(!empty, "Cannot call popFront on Decoder with no data remaining");
enforce(!empty, new Base64Exception("Cannot call popFront on Decoder with no data remaining"));

static if (Padding == NoPadding) {
bool endCondition()
Expand All @@ -1283,7 +1283,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
} else {
bool endCondition()
{
enforce(!range_.empty, "Missing padding");
enforce(!range_.empty, new Base64Exception("Missing padding"));
return range_.front == Padding;
}
}
Expand All @@ -1295,12 +1295,12 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')

final switch (pos) {
case 0:
enforce(!endCondition(), "Premature end of data found");
enforce(!endCondition(), new Base64Exception("Premature end of data found"));

immutable t = DecodeMap[range_.front] << 2;
range_.popFront();

enforce(!endCondition(), "Premature end of data found");
enforce(!endCondition(), new Base64Exception("Premature end of data found"));
first = cast(ubyte)(t | (DecodeMap[range_.front] >> 4));
break;
case 1:
Expand Down Expand Up @@ -1407,7 +1407,7 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')

// enforce can't be a pure function, so I use trivial check.
if (val == 0 && chr != 'A')
throw new Exception("Invalid character: " ~ chr);
throw new Base64Exception("Invalid character: " ~ chr);

return val;
}
Expand All @@ -1418,13 +1418,25 @@ template Base64Impl(char Map62th, char Map63th, char Padding = '=')
{
// See above comment.
if (chr > 0x7f)
throw new Exception("Base64-encoded character must be a single byte");
throw new Base64Exception("Base64-encoded character must be a single byte");

return decodeChar(cast(char)chr);
}
}


/**
* Exception thrown on Base64 errors.
*/
class Base64Exception : Exception
{
this(string s, string fn = __FILE__, size_t ln = __LINE__)
{
super(s, fn, ln);
}
}


unittest
{
alias Base64Impl!('!', '=', Base64.NoPadding) Base64Re;
Expand Down Expand Up @@ -1475,7 +1487,7 @@ unittest
assert(Base64.decode(Base64.encode(tv["fooba"])) == tv["fooba"]);
assert(Base64.decode(Base64.encode(tv["foobar"])) == tv["foobar"]);

assertThrown(Base64.decode("ab|c"));
assertThrown!Base64Exception(Base64.decode("ab|c"));

// Test decoding incomplete strings. RFC does not specify the correct
// behavior, but the code should never throw Errors on invalid input.
Expand All @@ -1486,10 +1498,10 @@ unittest
assert(Base64.decodeLength(3) <= 2);

// may throw Exceptions, may not throw Errors
assertThrown(Base64.decode("Zg"));
assertThrown(Base64.decode("Zg="));
assertThrown(Base64.decode("Zm8"));
assertThrown(Base64.decode("Zg==;"));
assertThrown!Base64Exception(Base64.decode("Zg"));
assertThrown!Base64Exception(Base64.decode("Zg="));
assertThrown!Base64Exception(Base64.decode("Zm8"));
assertThrown!Base64Exception(Base64.decode("Zg==;"));
}

{ // No padding
Expand Down

0 comments on commit ba922fd

Please sign in to comment.