| 1 | /* |
| 2 | * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include <wtf/StringPrintStream.h> |
| 28 | |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <wtf/FastMalloc.h> |
| 32 | |
| 33 | namespace WTF { |
| 34 | |
| 35 | StringPrintStream::StringPrintStream() |
| 36 | : m_buffer(m_inlineBuffer) |
| 37 | , m_next(0) |
| 38 | , m_size(sizeof(m_inlineBuffer)) |
| 39 | { |
| 40 | m_buffer[0] = 0; // Make sure that we always have a null terminator. |
| 41 | } |
| 42 | |
| 43 | StringPrintStream::~StringPrintStream() |
| 44 | { |
| 45 | if (m_buffer == m_inlineBuffer) |
| 46 | return; |
| 47 | fastFree(m_buffer); |
| 48 | } |
| 49 | |
| 50 | void StringPrintStream::vprintf(const char* format, va_list argList) |
| 51 | { |
| 52 | ASSERT_WITH_SECURITY_IMPLICATION(m_next < m_size); |
| 53 | ASSERT(!m_buffer[m_next]); |
| 54 | |
| 55 | va_list firstPassArgList; |
| 56 | va_copy(firstPassArgList, argList); |
| 57 | |
| 58 | int numberOfBytesNotIncludingTerminatorThatWouldHaveBeenWritten = |
| 59 | vsnprintf(m_buffer + m_next, m_size - m_next, format, firstPassArgList); |
| 60 | |
| 61 | va_end(firstPassArgList); |
| 62 | |
| 63 | int numberOfBytesThatWouldHaveBeenWritten = |
| 64 | numberOfBytesNotIncludingTerminatorThatWouldHaveBeenWritten + 1; |
| 65 | |
| 66 | if (m_next + numberOfBytesThatWouldHaveBeenWritten <= m_size) { |
| 67 | m_next += numberOfBytesNotIncludingTerminatorThatWouldHaveBeenWritten; |
| 68 | return; // This means that vsnprintf() succeeded. |
| 69 | } |
| 70 | |
| 71 | increaseSize(m_next + numberOfBytesThatWouldHaveBeenWritten); |
| 72 | |
| 73 | int numberOfBytesNotIncludingTerminatorThatWereWritten = |
| 74 | vsnprintf(m_buffer + m_next, m_size - m_next, format, argList); |
| 75 | |
| 76 | int numberOfBytesThatWereWritten = numberOfBytesNotIncludingTerminatorThatWereWritten + 1; |
| 77 | |
| 78 | ASSERT_UNUSED(numberOfBytesThatWereWritten, m_next + numberOfBytesThatWereWritten <= m_size); |
| 79 | |
| 80 | m_next += numberOfBytesNotIncludingTerminatorThatWereWritten; |
| 81 | |
| 82 | ASSERT_WITH_SECURITY_IMPLICATION(m_next < m_size); |
| 83 | ASSERT(!m_buffer[m_next]); |
| 84 | } |
| 85 | |
| 86 | CString StringPrintStream::toCString() |
| 87 | { |
| 88 | ASSERT(m_next == strlen(m_buffer)); |
| 89 | return CString(m_buffer, m_next); |
| 90 | } |
| 91 | |
| 92 | void StringPrintStream::reset() |
| 93 | { |
| 94 | m_next = 0; |
| 95 | m_buffer[0] = 0; |
| 96 | } |
| 97 | |
| 98 | String StringPrintStream::toString() |
| 99 | { |
| 100 | ASSERT(m_next == strlen(m_buffer)); |
| 101 | return String::fromUTF8(m_buffer, m_next); |
| 102 | } |
| 103 | |
| 104 | String StringPrintStream::toStringWithLatin1Fallback() |
| 105 | { |
| 106 | ASSERT(m_next == strlen(m_buffer)); |
| 107 | return String::fromUTF8WithLatin1Fallback(m_buffer, m_next); |
| 108 | } |
| 109 | |
| 110 | void StringPrintStream::increaseSize(size_t newSize) |
| 111 | { |
| 112 | ASSERT_WITH_SECURITY_IMPLICATION(newSize > m_size); |
| 113 | ASSERT(newSize > sizeof(m_inlineBuffer)); |
| 114 | |
| 115 | // Use exponential resizing to reduce thrashing. |
| 116 | m_size = newSize << 1; |
| 117 | |
| 118 | // Use fastMalloc instead of fastRealloc because we know that for the sizes we're using, |
| 119 | // fastRealloc will just do malloc+free anyway. Also, this simplifies the code since |
| 120 | // we can't realloc the inline buffer. |
| 121 | char* newBuffer = static_cast<char*>(fastMalloc(m_size)); |
| 122 | memcpy(newBuffer, m_buffer, m_next + 1); |
| 123 | if (m_buffer != m_inlineBuffer) |
| 124 | fastFree(m_buffer); |
| 125 | m_buffer = newBuffer; |
| 126 | } |
| 127 | |
| 128 | } // namespace WTF |
| 129 | |
| 130 | |