aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/charset/iconv.cpp
blob: dc604c849205cd5ec301da5dfcf640d5a6edebfc (plain) (blame)
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
#include "iconv.h"

#include <iconv.h>

using namespace NICONVPrivate;

TDescriptor::TDescriptor(const char* from, const char* to)
    : Descriptor_(libiconv_open(to, from))
    , From_(from)
    , To_(to)
{
    if (!Invalid()) {
        int temp = 1;

        libiconvctl(Descriptor_, ICONV_SET_DISCARD_ILSEQ, &temp);
    }
}

TDescriptor::~TDescriptor() {
    if (!Invalid()) {
        libiconv_close(Descriptor_);
    }
}

size_t NICONVPrivate::RecodeImpl(const TDescriptor& descriptor, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
    Y_ASSERT(!descriptor.Invalid());
    Y_ASSERT(in);
    Y_ASSERT(out);

    char* inPtr = const_cast<char*>(in);
    char* outPtr = out;
    size_t inSizeMod = inSize;
    size_t outSizeMod = outSize;
    size_t res = libiconv(descriptor.Get(), &inPtr, &inSizeMod, &outPtr, &outSizeMod);

    read = inSize - inSizeMod;
    written = outSize - outSizeMod;

    return res;
}

void NICONVPrivate::DoRecode(const TDescriptor& descriptor, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
    if (descriptor.Invalid()) {
        ythrow yexception() << "Can not convert from " << descriptor.From() << " to " << descriptor.To();
    }

    size_t res = RecodeImpl(descriptor, in, out, inSize, outSize, read, written);

    if (res == static_cast<size_t>(-1)) {
        switch (errno) {
            case EILSEQ:
                read = inSize;
                break;

            case EINVAL:
                read = inSize;
                break;

            case E2BIG:
                ythrow yexception() << "Iconv error: output buffer is too small";

            default:
                ythrow yexception() << "Unknown iconv error";
        }
    }
}

RECODE_RESULT NICONVPrivate::DoRecodeNoThrow(const TDescriptor& descriptor, const char* in, char* out, size_t inSize, size_t outSize, size_t& read, size_t& written) {
    if (descriptor.Invalid()) {
        return RECODE_ERROR;
    }

    size_t res = RecodeImpl(descriptor, in, out, inSize, outSize, read, written);

    if (res == static_cast<size_t>(-1)) {
        switch (errno) {
            case EILSEQ:
                read = inSize;
                break;

            case EINVAL:
                read = inSize;
                break;

            case E2BIG:
                return RECODE_EOOUTPUT;

            default:
                return RECODE_ERROR;
        }
    }

    return RECODE_OK;
}