From 80a6cbe0a427e155de8555bc867af745d10f9777 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 12 Apr 2018 19:45:45 +0000 Subject: Return pdfium::span from ByteString::GetBuffer(). Get bounds checking "for free". Change-Id: I7b14cacbc7130ced7b5cb1869b82c96ccff8e642 Reviewed-on: https://pdfium-review.googlesource.com/30451 Commit-Queue: Tom Sepez Reviewed-by: dsinclair --- core/fpdfapi/parser/fpdf_parser_decode.cpp | 45 ++++++++++++++------------ core/fpdfapi/parser/fpdf_parser_utility.cpp | 50 ++++++++++++++++------------- 2 files changed, 53 insertions(+), 42 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 90dca2edcb..2fdc4daa49 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -469,20 +469,22 @@ ByteString PDF_EncodeText(const wchar_t* pString, int len) { if (len == -1) len = wcslen(pString); - ByteString result; - char* dest_buf1 = result.GetBuffer(len); int i; - for (i = 0; i < len; ++i) { - int code; - for (code = 0; code < 256; ++code) { - if (PDFDocEncoding[code] == pString[i]) - break; - } + ByteString result; + { + pdfium::span dest_buf = result.GetBuffer(len); + for (i = 0; i < len; ++i) { + int code; + for (code = 0; code < 256; ++code) { + if (PDFDocEncoding[code] == pString[i]) + break; + } - if (code == 256) - break; + if (code == 256) + break; - dest_buf1[i] = code; + dest_buf[i] = code; + } } result.ReleaseBuffer(i); if (i == len) @@ -493,15 +495,18 @@ ByteString PDF_EncodeText(const wchar_t* pString, int len) { return result; } - int encLen = len * 2 + 2; - - uint8_t* dest_buf2 = reinterpret_cast(result.GetBuffer(encLen)); - dest_buf2[0] = 0xfe; - dest_buf2[1] = 0xff; - dest_buf2 += 2; - for (int j = 0; j < len; ++j) { - *dest_buf2++ = pString[j] >> 8; - *dest_buf2++ = static_cast(pString[j]); + size_t dest_index = 0; + size_t encLen = len * 2 + 2; + { + pdfium::span cspan = result.GetBuffer(encLen); + auto dest_buf = pdfium::make_span(reinterpret_cast(cspan.data()), + cspan.size()); + dest_buf[dest_index++] = 0xfe; + dest_buf[dest_index++] = 0xff; + for (int j = 0; j < len; ++j) { + dest_buf[dest_index++] = pString[j] >> 8; + dest_buf[dest_index++] = static_cast(pString[j]); + } } result.ReleaseBuffer(encLen); return result; diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp index c486a76706..b6b94aa84e 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp @@ -93,20 +93,23 @@ ByteString PDF_NameDecode(const ByteStringView& bstr) { if (!bstr.Contains('#')) return ByteString(bstr); - int size = bstr.GetLength(); + size_t src_size = bstr.GetLength(); + size_t out_index = 0; ByteString result; - char* pDestStart = result.GetBuffer(size); - char* pDest = pDestStart; - for (int i = 0; i < size; i++) { - if (bstr[i] == '#' && i < size - 2) { - *pDest++ = FXSYS_HexCharToInt(bstr[i + 1]) * 16 + - FXSYS_HexCharToInt(bstr[i + 2]); - i += 2; - } else { - *pDest++ = bstr[i]; + { + // Span's lifetime must end before ReleaseBuffer() below. + pdfium::span pDest = result.GetBuffer(src_size); + for (size_t i = 0; i < src_size; i++) { + if (bstr[i] == '#' && i < src_size - 2) { + pDest[out_index++] = FXSYS_HexCharToInt(bstr[i + 1]) * 16 + + FXSYS_HexCharToInt(bstr[i + 2]); + i += 2; + } else { + pDest[out_index++] = bstr[i]; + } } } - result.ReleaseBuffer(static_cast(pDest - pDestStart)); + result.ReleaseBuffer(out_index); return result; } @@ -128,20 +131,23 @@ ByteString PDF_NameEncode(const ByteString& orig) { return orig; ByteString res; - char* dest_buf = res.GetBuffer(dest_len); - dest_len = 0; - for (i = 0; i < src_len; i++) { - uint8_t ch = src_buf[i]; - if (ch >= 0x80 || PDFCharIsWhitespace(ch) || ch == '#' || - PDFCharIsDelimiter(ch)) { - dest_buf[dest_len++] = '#'; - FXSYS_IntToTwoHexChars(ch, dest_buf + dest_len); - dest_len += 2; - } else { + { + // Span's lifetime must end before ReleaseBuffer() below. + pdfium::span dest_buf = res.GetBuffer(dest_len); + dest_len = 0; + for (i = 0; i < src_len; i++) { + uint8_t ch = src_buf[i]; + if (ch >= 0x80 || PDFCharIsWhitespace(ch) || ch == '#' || + PDFCharIsDelimiter(ch)) { + dest_buf[dest_len++] = '#'; + FXSYS_IntToTwoHexChars(ch, &dest_buf[dest_len]); + dest_len += 2; + continue; + } dest_buf[dest_len++] = ch; } + dest_buf[dest_len] = 0; } - dest_buf[dest_len] = 0; res.ReleaseBuffer(res.GetStringLength()); return res; } -- cgit v1.2.3