]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Bitstream encoder. | |
3 | */ | |
4 | ||
5 | #include "duk_internal.h" | |
6 | ||
7 | DUK_INTERNAL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits) { | |
8 | duk_uint8_t tmp; | |
9 | ||
10 | DUK_ASSERT(ctx != NULL); | |
11 | DUK_ASSERT(ctx->currbits < 8); | |
12 | ||
13 | /* This limitation would be fixable but adds unnecessary complexity. */ | |
14 | DUK_ASSERT(bits >= 1 && bits <= 24); | |
15 | ||
16 | ctx->currval = (ctx->currval << bits) | data; | |
17 | ctx->currbits += bits; | |
18 | ||
19 | while (ctx->currbits >= 8) { | |
20 | if (ctx->offset < ctx->length) { | |
21 | tmp = (duk_uint8_t) ((ctx->currval >> (ctx->currbits - 8)) & 0xff); | |
22 | ctx->data[ctx->offset++] = tmp; | |
23 | } else { | |
24 | /* If buffer has been exhausted, truncate bitstream */ | |
25 | ctx->truncated = 1; | |
26 | } | |
27 | ||
28 | ctx->currbits -= 8; | |
29 | } | |
30 | } | |
31 | ||
32 | DUK_INTERNAL void duk_be_finish(duk_bitencoder_ctx *ctx) { | |
33 | duk_small_int_t npad; | |
34 | ||
35 | DUK_ASSERT(ctx != NULL); | |
36 | DUK_ASSERT(ctx->currbits < 8); | |
37 | ||
38 | npad = (duk_small_int_t) (8 - ctx->currbits); | |
39 | if (npad > 0) { | |
40 | duk_be_encode(ctx, 0, npad); | |
41 | } | |
42 | DUK_ASSERT(ctx->currbits == 0); | |
43 | } |