Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
EncodingConvertor.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 EncodingConvertor.h
4
5 Qore Programming Language
6
7 Copyright (C) 2023 Qore Technologies, sro
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_ENCODINGCONVERTOR_H
33#define _QORE_ENCODINGCONVERTOR_H
34
35#include "qore/Transform.h"
37
38class EncodingConvertor : public Transform {
39
40public:
41 EncodingConvertor(const QoreEncoding *srcEncoding, const QoreEncoding *dstEncoding, ExceptionSink *xsink)
42 : conv(dstEncoding, srcEncoding, xsink), inCount(0), outCount(0) {
43 }
44
45 DLLLOCAL std::pair<int64, int64> apply(const void *src, int64 srcLen, void *dst, int64 dstLen, ExceptionSink *xsink) override {
46
47 const char *srcPtr = static_cast<const char *>(src);
48 char *dstPtr = static_cast<char *>(dst);
49 while (true) {
50 size_t r = QORE_MIN(static_cast<size_t>(srcLen), BUFSIZE - inCount);
51 if (r) {
52 memcpy(inBuf + inCount, srcPtr, r);
53 srcLen -= r;
54 srcPtr += r;
55 inCount += r;
56 }
57
58 char *inbuf = inBuf;
59 size_t inavail = inCount;
60 char *outbuf = outBuf + outCount;
61 size_t outavail = BUFSIZE - outCount;
62 if (conv.iconv(&inbuf, &inavail, &outbuf, &outavail) == (size_t) -1) {
63 switch (errno) {
64 case EINVAL:
65 if (src == nullptr) { //flushing - there will be no more input
66 conv.reportIllegalSequence(inbuf - inBuf, xsink);
67 return std::make_pair(0, 0);
68 }
69 break;
70 case E2BIG:
71 break;
72 case EILSEQ:
73 conv.reportIllegalSequence(inbuf - inBuf, xsink);
74 return std::make_pair(0, 0);
75 default:
76 conv.reportUnknownError(xsink);
77 return std::make_pair(0, 0);
78 }
79 }
80 size_t rc = inCount - inavail;
81 if (rc) {
82 inCount -= rc;
83 memmove(inBuf, inBuf + rc, inCount);
84 }
85
86 size_t wc = BUFSIZE - outCount - outavail;
87 outCount += wc;
88
89 size_t w = QORE_MIN(static_cast<size_t>(dstLen), outCount);
90 if (w) {
91 memcpy(dstPtr, outBuf, w);
92 dstPtr += w;
93 dstLen -= w;
94 outCount -= w;
95 memmove(outBuf, outBuf + w, outCount);
96 }
97
98 if (r == 0 && w == 0) {
99 return std::make_pair(srcPtr - static_cast<const char *>(src), dstPtr - static_cast<char *>(dst));
100 }
101 }
102 }
103
104private:
105 static constexpr size_t BUFSIZE = 4096;
106 IconvHelper conv;
107 char inBuf[BUFSIZE];
108 size_t inCount;
109 char outBuf[BUFSIZE];
110 size_t outCount;
111};
112
113#endif // _QORE_ENCODINGCONVERTOR_H
Defines a helper class wrapping libiconv.
#define QORE_MIN(a, b)
macro to return the minimum of 2 numbers
Definition: QoreLib.h:619
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
Interface for private data of transformations.
Definition: Transform.h:40
virtual std::pair< int64, int64 > apply(const void *src, int64 srcLen, void *dst, int64 dstLen, ExceptionSink *xsink)=0
Applies the transformation.
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260