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-10-03 20:03:20 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        2793 : mal_unquote(char *msg)
      22             : {
      23        2793 :         char *p = msg, *s;
      24             : 
      25        2793 :         s = p;
      26      144231 :         while (*p) {
      27      141438 :                 if (*p == '\\') {
      28       22253 :                         p++;
      29       22253 :                         switch (*p) {
      30        1317 :                         case 'n':
      31        1317 :                                 *s = '\n';
      32        1317 :                                 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       20936 :                                 *s = *p;
      60       20936 :                                 break;
      61             :                         }
      62       22253 :                         p++;
      63             :                 } else {
      64      119185 :                         *s = *p++;
      65             :                 }
      66      141438 :                 s++;
      67             :         }
      68        2793 :         *s = 0;                                         /* close string */
      69        2793 : }
      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