LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/geojson/libjson - printbuf.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 48 71 67.6 %
Date: 2024-11-21 22:18:42 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
       3             :  *
       4             :  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
       5             :  * Michael Clark <michael@metaparadigm.com>
       6             :  *
       7             :  * This library is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the MIT license. See COPYING for details.
       9             :  *
      10             :  *
      11             :  * Copyright (c) 2008-2009 Yahoo! Inc.  All rights reserved.
      12             :  * The copyrights to the contents of this file are licensed under the MIT License
      13             :  * (http://www.opensource.org/licenses/mit-license.php)
      14             :  */
      15             : 
      16             : #include "config.h"
      17             : 
      18             : #include <limits.h>
      19             : #include <stdio.h>
      20             : #include <stdlib.h>
      21             : #include <string.h>
      22             : 
      23             : #include "cpl_string.h"
      24             : 
      25             : #ifdef HAVE_STDARG_H
      26             : #include <stdarg.h>
      27             : #else /* !HAVE_STDARG_H */
      28             : #error Not enough var arg support!
      29             : #endif /* HAVE_STDARG_H */
      30             : 
      31             : #include "debug.h"
      32             : #include "printbuf.h"
      33             : 
      34             : static int printbuf_extend(struct printbuf *p, int min_size);
      35             : 
      36       43336 : struct printbuf *printbuf_new(void)
      37             : {
      38             :   struct printbuf *p;
      39             : 
      40       43336 :   p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
      41       43336 :   if (!p)
      42           0 :     return NULL;
      43       43336 :   p->size = 32;
      44       43336 :   p->bpos = 0;
      45       43336 :   if (!(p->buf = (char *)malloc(p->size)))
      46             :   {
      47           0 :     free(p);
      48           0 :     return NULL;
      49             :   }
      50       43336 :   p->buf[0] = '\0';
      51       43336 :   return p;
      52             : }
      53             : 
      54             : /**
      55             :  * Extend the buffer p so it has a size of at least min_size.
      56             :  *
      57             :  * If the current size is large enough, nothing is changed.
      58             :  *
      59             :  * Note: this does not check the available space!  The caller
      60             :  *  is responsible for performing those calculations.
      61             :  */
      62       32097 : static int printbuf_extend(struct printbuf *p, int min_size)
      63             : {
      64             :   char *t;
      65             :   int new_size;
      66             : 
      67       32097 :   if (p->size >= min_size)
      68        7833 :     return 0;
      69             :   /* Prevent signed integer overflows with large buffers. */
      70       24264 :   if (min_size > INT_MAX - 8)
      71           0 :     return -1;
      72       24264 :   if (p->size > INT_MAX / 2)
      73           0 :     new_size = min_size + 8;
      74             :   else {
      75       24264 :     new_size = p->size * 2;
      76       24264 :     if (new_size < min_size + 8)
      77         817 :       new_size = min_size + 8;
      78             :   }
      79             : #ifdef PRINTBUF_DEBUG
      80             :   MC_DEBUG("printbuf_memappend: realloc "
      81             :            "bpos=%d min_size=%d old_size=%d new_size=%d\n",
      82             :            p->bpos, min_size, p->size, new_size);
      83             : #endif /* PRINTBUF_DEBUG */
      84       24264 :   if (!(t = (char *)realloc(p->buf, new_size)))
      85           0 :     return -1;
      86       24264 :   p->size = new_size;
      87       24264 :   p->buf = t;
      88       24264 :   return 0;
      89             : }
      90             : 
      91     1513050 : int printbuf_memappend(struct printbuf *p, const char *buf, int size)
      92             : {
      93             :   /* Prevent signed integer overflows with large buffers. */
      94     1513050 :   if (size > INT_MAX - p->bpos - 1)
      95           0 :     return -1;
      96     1513050 :   if (p->size <= p->bpos + size + 1)
      97             :   {
      98       31087 :     if (printbuf_extend(p, p->bpos + size + 1) < 0)
      99           0 :       return -1;
     100             :   }
     101     1513050 :   memcpy(p->buf + p->bpos, buf, size);
     102     1513050 :   p->bpos += size;
     103     1513050 :   p->buf[p->bpos] = '\0';
     104     1513050 :   return size;
     105             : }
     106             : 
     107      262213 : int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
     108             : {
     109             :   int size_needed;
     110             : 
     111      262213 :   if (offset == -1)
     112      262213 :     offset = pb->bpos;
     113             :   /* Prevent signed integer overflows with large buffers. */
     114      262213 :   if (len > INT_MAX - offset)
     115           0 :     return -1;
     116      262213 :   size_needed = offset + len;
     117      262213 :   if (pb->size < size_needed)
     118             :   {
     119        1010 :     if (printbuf_extend(pb, size_needed) < 0)
     120           0 :       return -1;
     121             :   }
     122             : 
     123      262213 :   memset(pb->buf + offset, charvalue, len);
     124      262213 :   if (pb->bpos < size_needed)
     125      259445 :     pb->bpos = size_needed;
     126             : 
     127      262213 :   return 0;
     128             : }
     129             : 
     130             : /* Use CPLVASPrintf for portability issues */
     131           0 : int sprintbuf(struct printbuf *p, const char *msg, ...)
     132             : {
     133             :   va_list ap;
     134             :   char *t;
     135             :   int size, ret;
     136             : 
     137             :   /* user stack buffer first */
     138           0 :   va_start(ap, msg);
     139           0 :   size = CPLVASPrintf(&t, msg, ap);
     140           0 :   va_end(ap);
     141           0 :   if( size == -1 )
     142           0 :       return -1;
     143             : 
     144           0 :   if (strcmp(msg, "%f") == 0)
     145             :   {
     146           0 :       char* pszComma = strchr(t, ',');
     147           0 :       if (pszComma)
     148           0 :           *pszComma = '.';
     149             :   }
     150             : 
     151           0 :   ret = printbuf_memappend(p, t, size);
     152           0 :   CPLFree(t);
     153           0 :   return ret;
     154             : }
     155             : 
     156     3039220 : void printbuf_reset(struct printbuf *p)
     157             : {
     158     3039220 :   p->buf[0] = '\0';
     159     3039220 :   p->bpos = 0;
     160     3039220 : }
     161             : 
     162     3169160 : void printbuf_free(struct printbuf *p)
     163             : {
     164     3169160 :   if (p)
     165             :   {
     166       43336 :     free(p->buf);
     167       43336 :     free(p);
     168             :   }
     169     3169160 : }

Generated by: LCOV version 1.14