LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 57 61.4 %
Date: 2021-09-14 22:17:06 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.14