LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 62 27.4 %
Date: 2024-12-19 23:10:26 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * (c) M. Kersten
      15             :  * Passing strings between front-end and kernel often require marshalling.
      16             :  */
      17             : #include "monetdb_config.h"
      18             : #include "mal_utils.h"
      19             : 
      20             : void
      21        2783 : mal_unquote(char *msg)
      22             : {
      23        2783 :         char *p = msg, *s;
      24             : 
      25        2783 :         s = p;
      26      143846 :         while (*p) {
      27      141063 :                 if (*p == '\\') {
      28       22223 :                         p++;
      29       22223 :                         switch (*p) {
      30        1313 :                         case 'n':
      31        1313 :                                 *s = '\n';
      32        1313 :                                 break;
      33           0 :                         case 't':
      34           0 :                                 *s = '\t';
      35           0 :                                 break;
      36           0 :                         case 'r':
      37           0 :                                 *s = '\r';
      38           0 :                                 break;
      39           0 :                         case 'f':
      40           0 :                                 *s = '\f';
      41           0 :                                 break;
      42           0 :                         case '0':
      43             :                         case '1':
      44             :                         case '2':
      45             :                         case '3':
      46             :                                 /* this could be the start of
      47             :                                    an octal sequence, check it
      48             :                                    out */
      49           0 :                                 if (p[1] && p[2] && p[1] >= '0' && p[1] <= '7' && p[2] >= '0'
      50           0 :                                         && p[2] <= '7') {
      51           0 :                                         *s = (char) (((p[0] - '0') << 6) |
      52             :                                                                  ((p[1] - '0') << 3) |
      53           0 :                                                                  (p[2] - '0'));
      54           0 :                                         p += 2;
      55           0 :                                         break;
      56             :                                 }
      57             :                                 /* fall through */
      58             :                         default:
      59       20910 :                                 *s = *p;
      60       20910 :                                 break;
      61             :                         }
      62       22223 :                         p++;
      63             :                 } else {
      64      118840 :                         *s = *p++;
      65             :                 }
      66      141063 :                 s++;
      67             :         }
      68        2783 :         *s = 0;                                         /* close string */
      69        2783 : }
      70             : 
      71             : char *
      72           0 : mal_quote(const char *msg, size_t size)
      73             : {
      74           0 :         char *s = GDKmalloc(size * 2 + 1);      /* we absolutely don't need more than this (until we start producing octal escapes */
      75           0 :         char *t = s;
      76             : 
      77           0 :         if (s == NULL)
      78             :                 return NULL;
      79           0 :         while (size > 0) {
      80           0 :                 size--;
      81           0 :                 switch (*msg) {
      82           0 :                 case '"':
      83           0 :                         *t++ = '\\';
      84           0 :                         *t++ = '"';
      85           0 :                         break;
      86           0 :                 case '\n':
      87           0 :                         *t++ = '\\';
      88           0 :                         *t++ = 'n';
      89           0 :                         break;
      90           0 :                 case '\t':
      91           0 :                         *t++ = '\\';
      92           0 :                         *t++ = 't';
      93           0 :                         break;
      94           0 :                 case '\\':
      95           0 :                         *t++ = '\\';
      96           0 :                         *t++ = '\\';
      97           0 :                         break;
      98           0 :                 default:
      99           0 :                         *t++ = *msg;
     100           0 :                         break;
     101             :                 }
     102           0 :                 msg++;
     103             :                 /* also deal with binaries */
     104             :         }
     105           0 :         *t = 0;
     106           0 :         return s;
     107             : }

Generated by: LCOV version 1.14