| /********************************************************************** |
| // @@@ START COPYRIGHT @@@ |
| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| // @@@ END COPYRIGHT @@@ |
| **********************************************************************/ |
| /* -*-C++-*- |
| ****************************************************************************** |
| * |
| * File: ComSafePrinter.cpp |
| * Description: Safe versions of some sprintf-style functions. They are |
| * "safe" in the sense that they prevent writing beyond the |
| * end of the target buffer. Some platforms, but not OSS, |
| * provide similar functions in the stdio library. |
| * |
| * Created: June 2003 |
| * Language: C++ |
| * |
| * |
| * |
| ****************************************************************************** |
| */ |
| |
| #include "ComSafePrinter.h" |
| #include <limits.h> |
| |
| #define FILENO _fileno |
| |
| THREAD_P FILE *ComSafePrinter::outfile_ = NULL; |
| |
| ComSafePrinter::ComSafePrinter() |
| { |
| } |
| |
| ComSafePrinter::~ComSafePrinter() |
| { |
| } |
| |
| Int32 ComSafePrinter::vsnPrintf(char *str, size_t n, |
| const char *format, va_list args) |
| { |
| |
| if (!str || n == 0 || !format) |
| { |
| return 0; |
| } |
| |
| Int32 targetLen = (n > INT_MAX ? INT_MAX : (Int32) n); |
| Int32 result = -1; |
| |
| |
| // Otherwise we buffer data in a temporary file |
| if (!outfile_) |
| { |
| outfile_ = tmpfile(); |
| #ifdef NA_DEBUG_C_RUNTIME |
| if (!outfile_) |
| { |
| fprintf(stderr, |
| "*** WARNING: ComSafePrinter temp file could not be created"); |
| } |
| #endif |
| } |
| |
| if (outfile_) |
| { |
| rewind(outfile_); |
| result = vfprintf(outfile_, format, args); |
| } |
| |
| if (result > 0) |
| { |
| Int32 totalLength = result; |
| Int32 numWritten = 0; |
| |
| if (totalLength < targetLen) |
| { |
| numWritten = vsprintf(str, format, args); |
| if (numWritten == totalLength) |
| { |
| result = numWritten; |
| } |
| else |
| { |
| result = -1; |
| } |
| } |
| else |
| { |
| rewind(outfile_); |
| numWritten = (Int32) fread(str, sizeof(char), targetLen - 1, outfile_); |
| if (numWritten == (targetLen - 1)) |
| { |
| str[targetLen - 1] = 0; |
| result = numWritten; |
| } |
| else |
| { |
| result = -1; |
| } |
| } |
| } |
| |
| return result; |
| |
| |
| } // ComSafePrinter::vsnPrintf() |
| |
| Int32 ComSafePrinter::snPrintf(char *str, size_t n, const char *format, ...) |
| { |
| va_list args; |
| va_start(args, format); |
| Int32 result = vsnPrintf(str, n, format, args); |
| va_end(args); |
| return result; |
| } |
| |