LCOV - code coverage report
Current view: top level - lib/util/charset - pull_push.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 24 24 100.0 %
Date: 2021-09-23 10:06:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Character set conversion Extensions
       4             :    Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
       5             :    Copyright (C) Andrew Tridgell 2001
       6             :    Copyright (C) Simo Sorce 2001
       7             :    Copyright (C) Martin Pool 2003
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : 
      22             : */
      23             : 
      24             : #include "replace.h"
      25             : #include "system/locale.h"
      26             : #include "charset.h"
      27             : 
      28             : /**
      29             :  * Copy a string from a unix char* src to a UCS2 destination,
      30             :  * allocating a buffer using talloc().
      31             :  *
      32             :  * @param dest always set at least to NULL
      33             :  * @parm converted_size set to the number of bytes occupied by the string in
      34             :  * the destination on success.
      35             :  *
      36             :  * @return true if new buffer was correctly allocated, and string was
      37             :  * converted.
      38             :  **/
      39      981803 : bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src,
      40             :                       size_t *converted_size)
      41             : {
      42      981803 :         size_t src_len = strlen(src)+1;
      43             : 
      44      981803 :         *dest = NULL;
      45      981803 :         return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len,
      46             :                                      (void **)dest, converted_size);
      47             : }
      48             : 
      49             : /**
      50             :  * @brief Create a UTF-8 string from a unix charset string.
      51             :  *
      52             :  * The resulting UTF-8 string is talloc'ed.
      53             :  *
      54             :  * @param[in]  ctx      The talloc memory context.
      55             :  *
      56             :  * @param[in]  dest     A pointer to store the pointer to the talloc'ed UTF-8
      57             :  *                      string.
      58             :  *
      59             :  * @param[in]  src      The unix charset string to convert.
      60             :  *
      61             :  * @param[in]  converted_size A pointer to store the length of the talloc'ed
      62             :  *                            UTF-8 string including the nul-termination bytes.
      63             :  *
      64             :  * The destination string should be free'd using talloc_free() if no longer
      65             :  * needed.
      66             :  *
      67             :  * @return True on succcess, false otherwise.
      68             :  */
      69       18371 : bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
      70             :                       size_t *converted_size)
      71             : {
      72       18371 :         size_t src_len = strlen(src)+1;
      73             : 
      74       18371 :         *dest = NULL;
      75       18371 :         return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len,
      76             :                                      (void**)dest, converted_size);
      77             : }
      78             : 
      79             : /**
      80             :  * Copy a string from a unix char* src to an ASCII destination,
      81             :  * allocating a buffer using talloc().
      82             :  *
      83             :  * @param dest always set at least to NULL
      84             :  *
      85             :  * @param converted_size The number of bytes occupied by the string in the destination
      86             :  * @returns boolean indicating if the conversion was successful
      87             :  **/
      88       60300 : bool push_ascii_talloc(TALLOC_CTX *mem_ctx, char **dest, const char *src, size_t *converted_size)
      89             : {
      90       60300 :         size_t src_len = strlen(src)+1;
      91             : 
      92       60300 :         *dest = NULL;
      93       60300 :         return convert_string_talloc(mem_ctx, CH_UNIX, CH_DOS, src, src_len,
      94             :                                      (void **)dest, converted_size);
      95             : }
      96             : 
      97             : /**
      98             :  * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
      99             :  *
     100             :  * @param dest always set at least to NULL
     101             :  * @parm converted_size set to the number of bytes occupied by the string in
     102             :  * the destination on success.
     103             :  *
     104             :  * @return true if new buffer was correctly allocated, and string was
     105             :  * converted.
     106             :  **/
     107             : 
     108      598650 : bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src,
     109             :                       size_t *converted_size)
     110             : {
     111      598650 :         size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
     112             : 
     113      598650 :         *dest = NULL;
     114      598650 :         return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len,
     115             :                                      (void **)dest, converted_size);
     116             : }
     117             : 
     118             : 
     119             : /**
     120             :  * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
     121             :  *
     122             :  * @param dest always set at least to NULL
     123             :  * @parm converted_size set to the number of bytes occupied by the string in
     124             :  * the destination on success.
     125             :  *
     126             :  * @return true if new buffer was correctly allocated, and string was
     127             :  * converted.
     128             :  **/
     129             : 
     130       22242 : bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
     131             :                       size_t *converted_size)
     132             : {
     133       22242 :         size_t src_len = strlen(src)+1;
     134             : 
     135       22242 :         *dest = NULL;
     136       22242 :         return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len,
     137             :                                      (void **)dest, converted_size);
     138             : }
     139             : 
     140             : 
     141             : /**
     142             :  * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
     143             :  *
     144             :  * @param dest always set at least to NULL
     145             :  * @parm converted_size set to the number of bytes occupied by the string in
     146             :  * the destination on success.
     147             :  *
     148             :  * @return true if new buffer was correctly allocated, and string was
     149             :  * converted.
     150             :  **/
     151             : 
     152      183291 : bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
     153             :                        size_t *converted_size)
     154             : {
     155      183291 :         size_t src_len = strlen(src)+1;
     156             : 
     157      183291 :         *dest = NULL;
     158      183291 :         return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len,
     159             :                                      (void **)dest, converted_size);
     160             : }

Generated by: LCOV version 1.13