Add a helper to copy a ByteString to a buffer

We have a few repetitive patterns similar to:
  if (buffer && len <= buflen)
    memcpy(buffer, byteString.c_str(), len);
  return len;

Replace them with a new helper, NulTerminateMaybeCopyAndReturnLength().
Add tests for the new helper.

Bug: pdfium:1514
Change-Id: I0d35cc775f6963833524c2eab1847aafa8e29a10
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/68695
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index b0a2324..0a5f593 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -192,10 +192,7 @@
 
   CPDF_Action cAction(CPDFDictionaryFromFPDFAction(action));
   ByteString path = cAction.GetFilePath().ToUTF8();
-  unsigned long len = path.GetLength() + 1;
-  if (buffer && len <= buflen)
-    memcpy(buffer, path.c_str(), len);
-  return len;
+  return NulTerminateMaybeCopyAndReturnLength(path, buffer, buflen);
 }
 
 FPDF_EXPORT unsigned long FPDF_CALLCONV