1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
* Copyright (C) Patrick Monnerat <patrick@monnerat.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the copyright holder nor the names
* of any other contributors may be used to endorse or
* promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/* Character encoding wrappers. */
#include "libssh2_priv.h"
#include "libssh2_ccsid.h"
#include <qtqiconv.h>
#include <iconv.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define CCSID_UTF8 1208
#define CCSID_UTF16BE 13488
#define STRING_GRANULE 256
#define MAX_CHAR_SIZE 4
#define OFFSET_OF(t, f) ((size_t) ((char *) &((t *) 0)->f - (char *) 0))
#define ALLOC(s, sz) ((s)? LIBSSH2_ALLOC((s), (sz)): malloc(sz))
#define REALLOC(s, p, sz) ((s)? LIBSSH2_REALLOC((s), (p), (sz)): \
realloc((p), (sz)))
#define FREE(s, p) ((s)? LIBSSH2_FREE((s), (p)): free(p))
struct _libssh2_string_cache {
libssh2_string_cache * next;
char string[1];
};
static const QtqCode_T utf8code = { CCSID_UTF8 };
static ssize_t
terminator_size(unsigned short ccsid)
{
QtqCode_T outcode;
iconv_t cd;
char *inp;
char *outp;
size_t ilen;
size_t olen;
char buf[MAX_CHAR_SIZE];
/* Return the null-terminator size for the given CCSID. */
/* Fast check usual CCSIDs. */
switch(ccsid) {
case CCSID_UTF8:
case 0: /* Job CCSID is SBCS EBCDIC. */
return 1;
case CCSID_UTF16BE:
return 2;
}
/* Convert an UTF-8 NUL to the target CCSID: use the converted size as
result. */
memset((void *) &outcode, 0, sizeof(outcode));
outcode.CCSID = ccsid;
cd = QtqIconvOpen(&outcode, (QtqCode_T *) &utf8code);
if(cd.return_value == -1)
return -1;
inp = "";
ilen = 1;
outp = buf;
olen = sizeof(buf);
iconv(cd, &inp, &ilen, &outp, &olen);
iconv_close(cd);
olen = sizeof(buf - olen);
return olen ? olen : -1;
}
static char *
convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
unsigned short outccsid, unsigned short inccsid,
const char *instring, ssize_t inlen, size_t *outlen)
{
char *inp;
char *outp;
size_t olen;
size_t ilen;
size_t buflen;
size_t curlen;
ssize_t termsize;
int i;
char *dst;
libssh2_string_cache *outstring;
QtqCode_T incode;
QtqCode_T outcode;
iconv_t cd;
if(!instring) {
if(outlen)
*outlen = 0;
return NULL;
}
if(outlen)
*outlen = -1;
if(!cache)
return NULL;
/* Get terminator size. */
termsize = terminator_size(outccsid);
if(termsize < 0)
return NULL;
/* Prepare conversion parameters. */
memset((void *) &incode, 0, sizeof(incode));
memset((void *) &outcode, 0, sizeof(outcode));
incode.CCSID = inccsid;
outcode.CCSID = outccsid;
curlen = OFFSET_OF(libssh2_string_cache, string);
inp = (char *) instring;
ilen = inlen;
buflen = inlen + curlen;
if(inlen < 0) {
incode.length_option = 1;
buflen = STRING_GRANULE;
ilen = 0;
}
/* Allocate output string buffer and open conversion descriptor. */
dst = ALLOC(session, buflen + termsize);
if(!dst)
return NULL;
cd = QtqIconvOpen(&outcode, &incode);
if(cd.return_value == -1) {
FREE(session, dst);
return NULL;
}
/* Convert string. */
for(;;) {
outp = dst + curlen;
olen = buflen - curlen;
i = iconv(cd, &inp, &ilen, &outp, &olen);
if(inlen < 0 && olen == buflen - curlen) {
/* Special case: converted 0-length (sub)strings do not store the
terminator. */
if(termsize) {
memset(outp, 0, termsize);
olen -= termsize;
}
}
curlen = buflen - olen;
if(i >= 0 || errno != E2BIG)
break;
/* Must expand buffer. */
buflen += STRING_GRANULE;
outp = REALLOC(session, dst, buflen + termsize);
if(!outp)
break;
dst = outp;
}
iconv_close(cd);
/* Check for error. */
if(i < 0 || !outp) {
FREE(session, dst);
return NULL;
}
/* Process terminator. */
if(inlen < 0)
curlen -= termsize;
else if(termsize)
memset(dst + curlen, 0, termsize);
/* Shorten buffer if possible. */
if(curlen < buflen)
dst = REALLOC(session, dst, curlen + termsize);
/* Link to cache. */
outstring = (libssh2_string_cache *) dst;
outstring->next = *cache;
*cache = outstring;
/* Return length if required. */
if(outlen)
*outlen = curlen - OFFSET_OF(libssh2_string_cache, string);
return outstring->string;
}
LIBSSH2_API char *
libssh2_from_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
unsigned short ccsid, const char *string, ssize_t inlen,
size_t *outlen)
{
return convert_ccsid(session, cache,
CCSID_UTF8, ccsid, string, inlen, outlen);
}
LIBSSH2_API char *
libssh2_to_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
unsigned short ccsid, const char *string, ssize_t inlen,
size_t *outlen)
{
return convert_ccsid(session, cache,
ccsid, CCSID_UTF8, string, inlen, outlen);
}
LIBSSH2_API void
libssh2_release_string_cache(LIBSSH2_SESSION *session,
libssh2_string_cache **cache)
{
libssh2_string_cache *p;
if(cache)
while((p = *cache)) {
*cache = p->next;
FREE(session, (char *) p);
}
}
/* vim: set expandtab ts=4 sw=4: */
|