security: don't call front() on empty vector

Change-Id: Ie267005f2ecc8c57d847ad35095a6df283e146b9
diff --git a/src/security/transform/hex-decode.cpp b/src/security/transform/hex-decode.cpp
index aa82461..c887626 100644
--- a/src/security/transform/hex-decode.cpp
+++ b/src/security/transform/hex-decode.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -25,7 +25,8 @@
 namespace security {
 namespace transform {
 
-static const int8_t C2H[256] = { // hex decoding pad.
+// hex decoding pad
+static const int8_t C2H[] = {
 // 0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
@@ -44,6 +45,8 @@
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 240-255
 };
+static_assert(std::extent<decltype(C2H)>::value == 256, "");
+
 
 HexDecode::HexDecode()
   : m_hasOddByte(false)
@@ -82,24 +85,24 @@
 {
   size_t bufferSize = (hexLen + (m_hasOddByte ? 1 : 0)) >> 1;
   auto buffer = make_unique<OBuffer>(bufferSize);
-  uint8_t* buf = &buffer->front();
+  auto it = buffer->begin();
 
   if (m_hasOddByte) {
     if (C2H[hex[0]] < 0 || C2H[m_oddByte] < 0)
       BOOST_THROW_EXCEPTION(Error(getIndex(), "Wrong input byte"));
 
-    buf[0] = (C2H[m_oddByte] << 4) + (C2H[hex[0]]);
-    buf += 1;
+    *it = (C2H[m_oddByte] << 4) + C2H[hex[0]];
+    ++it;
     hex += 1;
     hexLen -= 1;
   }
 
-  while (hexLen > 1) {
+  while (hexLen >= 2) {
     if (C2H[hex[0]] < 0 || C2H[hex[1]] < 0)
       BOOST_THROW_EXCEPTION(Error(getIndex(), "Wrong input byte"));
 
-    buf[0] = (C2H[hex[0]] << 4) + (C2H[hex[1]]);
-    buf += 1;
+    *it = (C2H[hex[0]] << 4) + C2H[hex[1]];
+    ++it;
     hex += 2;
     hexLen -= 2;
   }
diff --git a/src/security/transform/hex-encode.cpp b/src/security/transform/hex-encode.cpp
index 6d54f0e..8264eee 100644
--- a/src/security/transform/hex-encode.cpp
+++ b/src/security/transform/hex-encode.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -25,15 +25,18 @@
 namespace security {
 namespace transform {
 
-static const char H2CL[16] = {
+static const uint8_t H2CL[] = {
   '0', '1', '2', '3', '4', '5', '6', '7',
   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 };
+static_assert(std::extent<decltype(H2CL)>::value == 16, "");
 
-static const char H2CU[16] = {
+static const uint8_t H2CU[] = {
   '0', '1', '2', '3', '4', '5', '6', '7',
   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
 };
+static_assert(std::extent<decltype(H2CU)>::value == 16, "");
+
 
 HexEncode::HexEncode(bool useUpperCase)
   : m_useUpperCase(useUpperCase)
@@ -50,21 +53,19 @@
 unique_ptr<Transform::OBuffer>
 HexEncode::toHex(const uint8_t* data, size_t dataLen)
 {
-  const char* encodePad = (m_useUpperCase) ? H2CU : H2CL;
-
   auto encoded = make_unique<OBuffer>(dataLen * 2);
-  uint8_t* buf = &encoded->front();
+  uint8_t* buf = encoded->data();
+  const uint8_t* encodePad = m_useUpperCase ? H2CU : H2CL;
+
   for (size_t i = 0; i < dataLen; i++) {
-    buf[0] = encodePad[((data[i] >> 4) & 0x0F)];
-    buf++;
-    buf[0] = encodePad[(data[i] & 0x0F)];
-    buf++;
+    buf[0] = encodePad[(data[i] >> 4) & 0x0F];
+    buf[1] = encodePad[data[i] & 0x0F];
+    buf += 2;
   }
+
   return encoded;
 }
 
-
-
 unique_ptr<Transform>
 hexEncode(bool useUpperCase)
 {