Method: Zstd::Encoder#initialize

Defined in:
ext/extzstd_stream.c

#initialize(outport, compression_parameters = nil, predict = nil) ⇒ Object



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
# File 'ext/extzstd_stream.c', line 95

static VALUE
enc_init(int argc, VALUE argv[], VALUE self)
{
    /*
     * ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
     * ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
     *                                              ZSTD_parameters params, unsigned long long pledgedSrcSize);
     */

    VALUE outport, params, predict;
    switch (argc) {
    case 1:
        outport = argv[0];
        params = predict = Qnil;
        break;
    case 2:
        outport = argv[0];
        params = argv[1];
        predict = Qnil;
        break;
    case 3:
        outport = argv[0];
        params = argv[1];
        predict = argv[2];
        break;
    default:
        rb_error_arity(argc, 1, 3);
    }

    struct encoder *p = getencoder(self);
    if (p->context) {
        rb_raise(rb_eTypeError,
                "initialized already - #<%s:%p>",
                rb_obj_classname(self), (void *)self);
    }

    const void *predictp;
    size_t predictsize;
    if (NIL_P(predict)) {
        predictp = NULL;
        predictsize = 0;
    } else {
        rb_check_type(predict, RUBY_T_STRING);
        predict = rb_str_new_frozen(predict);
        RSTRING_GETMEM(predict, predictp, predictsize);
    }

    AUX_TRY_WITH_GC(
            p->context = ZSTD_createCStream(),
            "failed ZSTD_createCStream()");

    if (extzstd_params_p(params)) {
        ZSTD_parameters *paramsp = extzstd_getparams(params);
        ZSTD_CCtx *zstd = p->context;
        p->context = NULL; // 一時的に無効化する

        aux_ZSTD_CCtx_reset(zstd, ZSTD_reset_session_and_parameters);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_windowLog, paramsp->cParams.windowLog);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_chainLog, paramsp->cParams.chainLog);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_hashLog, paramsp->cParams.hashLog);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_searchLog, paramsp->cParams.searchLog);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_minMatch, paramsp->cParams.minMatch);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_targetLength, paramsp->cParams.targetLength);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_strategy, paramsp->cParams.strategy);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_contentSizeFlag, paramsp->fParams.contentSizeFlag);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_checksumFlag, paramsp->fParams.checksumFlag);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_dictIDFlag, !paramsp->fParams.noDictIDFlag);
        aux_ZSTD_CCtx_loadDictionary(zstd, predictp, predictsize);

        p->context = zstd;
    } else {
        int clevel = aux_num2int(params, ZSTD_CLEVEL_DEFAULT);
        ZSTD_CCtx *zstd = p->context;
        p->context = NULL; // 一時的に無効化する

        aux_ZSTD_CCtx_reset(zstd, ZSTD_reset_session_and_parameters);
        aux_ZSTD_CCtx_setParameter(zstd, ZSTD_c_compressionLevel, clevel);
        aux_ZSTD_CCtx_loadDictionary(zstd, predictp, predictsize);

        p->context = zstd;
    }

    p->predict = predict;
    p->outport = outport;

    return self;
}