@@ -34582,8 +34582,6 @@ typedef enum BCTagEnum {
34582
34582
BC_TAG_OBJECT,
34583
34583
BC_TAG_ARRAY,
34584
34584
BC_TAG_BIG_INT,
34585
- BC_TAG_BIG_FLOAT,
34586
- BC_TAG_BIG_DECIMAL,
34587
34585
BC_TAG_TEMPLATE_OBJECT,
34588
34586
BC_TAG_FUNCTION_BYTECODE,
34589
34587
BC_TAG_MODULE,
@@ -34593,24 +34591,21 @@ typedef enum BCTagEnum {
34593
34591
BC_TAG_DATE,
34594
34592
BC_TAG_OBJECT_VALUE,
34595
34593
BC_TAG_OBJECT_REFERENCE,
34594
+ #ifdef CONFIG_BIGNUM
34595
+ BC_TAG_BIG_FLOAT,
34596
+ BC_TAG_BIG_DECIMAL,
34597
+ #endif
34596
34598
} BCTagEnum;
34597
34599
34598
34600
#ifdef CONFIG_BIGNUM
34599
- #define BC_BASE_VERSION 2
34601
+ #define BC_VERSION 0x43
34600
34602
#else
34601
- #define BC_BASE_VERSION 1
34602
- #endif
34603
- #define BC_BE_VERSION 0x40
34604
- #ifdef WORDS_BIGENDIAN
34605
- #define BC_VERSION (BC_BASE_VERSION | BC_BE_VERSION)
34606
- #else
34607
- #define BC_VERSION BC_BASE_VERSION
34603
+ #define BC_VERSION 3
34608
34604
#endif
34609
34605
34610
34606
typedef struct BCWriterState {
34611
34607
JSContext *ctx;
34612
34608
DynBuf dbuf;
34613
- BOOL byte_swap : 8;
34614
34609
BOOL allow_bytecode : 8;
34615
34610
BOOL allow_sab : 8;
34616
34611
BOOL allow_reference : 8;
@@ -34640,8 +34635,6 @@ static const char * const bc_tag_str[] = {
34640
34635
"object",
34641
34636
"array",
34642
34637
"bigint",
34643
- "bigfloat",
34644
- "bigdecimal",
34645
34638
"template",
34646
34639
"function",
34647
34640
"module",
@@ -34651,31 +34644,44 @@ static const char * const bc_tag_str[] = {
34651
34644
"Date",
34652
34645
"ObjectValue",
34653
34646
"ObjectReference",
34647
+ #ifdef CONFIG_BIGNUM
34648
+ "bigfloat",
34649
+ "bigdecimal",
34650
+ #endif
34654
34651
};
34655
34652
#endif
34656
34653
34654
+ static inline BOOL is_be(void)
34655
+ {
34656
+ union {
34657
+ uint16_t a;
34658
+ uint8_t b;
34659
+ } u = {0x100};
34660
+ return u.b;
34661
+ }
34662
+
34657
34663
static void bc_put_u8(BCWriterState *s, uint8_t v)
34658
34664
{
34659
34665
dbuf_putc(&s->dbuf, v);
34660
34666
}
34661
34667
34662
34668
static void bc_put_u16(BCWriterState *s, uint16_t v)
34663
34669
{
34664
- if (s->byte_swap )
34670
+ if (is_be() )
34665
34671
v = bswap16(v);
34666
34672
dbuf_put_u16(&s->dbuf, v);
34667
34673
}
34668
34674
34669
34675
static __maybe_unused void bc_put_u32(BCWriterState *s, uint32_t v)
34670
34676
{
34671
- if (s->byte_swap )
34677
+ if (is_be() )
34672
34678
v = bswap32(v);
34673
34679
dbuf_put_u32(&s->dbuf, v);
34674
34680
}
34675
34681
34676
34682
static void bc_put_u64(BCWriterState *s, uint64_t v)
34677
34683
{
34678
- if (s->byte_swap )
34684
+ if (is_be() )
34679
34685
v = bswap64(v);
34680
34686
dbuf_put(&s->dbuf, (uint8_t *)&v, sizeof(v));
34681
34687
}
@@ -34845,7 +34851,7 @@ static int JS_WriteFunctionBytecode(BCWriterState *s,
34845
34851
pos += len;
34846
34852
}
34847
34853
34848
- if (s->byte_swap )
34854
+ if (is_be() )
34849
34855
bc_byte_swap(bc_buf, bc_len);
34850
34856
34851
34857
dbuf_put(&s->dbuf, bc_buf, bc_len);
@@ -34936,20 +34942,14 @@ static int JS_WriteBigNum(BCWriterState *s, JSValueConst obj)
34936
34942
bc_put_leb128(s, len);
34937
34943
/* always saved in byte based little endian representation */
34938
34944
for(j = 0; j < n1; j++) {
34939
- dbuf_putc(&s->dbuf , v >> (j * 8));
34945
+ bc_put_u8(s , v >> (j * 8));
34940
34946
}
34941
34947
for(; i < a->len; i++) {
34942
34948
limb_t v = a->tab[i];
34943
34949
#if LIMB_BITS == 32
34944
- #ifdef WORDS_BIGENDIAN
34945
- v = bswap32(v);
34946
- #endif
34947
- dbuf_put_u32(&s->dbuf, v);
34950
+ bc_put_u32(s, v);
34948
34951
#else
34949
- #ifdef WORDS_BIGENDIAN
34950
- v = bswap64(v);
34951
- #endif
34952
- dbuf_put_u64(&s->dbuf, v);
34952
+ bc_put_u64(s, v);
34953
34953
#endif
34954
34954
}
34955
34955
} else {
@@ -34992,14 +34992,14 @@ static int JS_WriteBigNum(BCWriterState *s, JSValueConst obj)
34992
34992
v8 = d;
34993
34993
bpos = 1;
34994
34994
} else {
34995
- dbuf_putc(&s->dbuf , v8 | (d << 4));
34995
+ bc_put_u8(s , v8 | (d << 4));
34996
34996
bpos = 0;
34997
34997
}
34998
34998
}
34999
34999
}
35000
35000
/* flush the last digit */
35001
35001
if (bpos) {
35002
- dbuf_putc(&s->dbuf , v8);
35002
+ bc_put_u8(s , v8);
35003
35003
}
35004
35004
}
35005
35005
}
@@ -35412,15 +35412,10 @@ static int JS_WriteObjectAtoms(BCWriterState *s)
35412
35412
JSRuntime *rt = s->ctx->rt;
35413
35413
DynBuf dbuf1;
35414
35414
int i, atoms_size;
35415
- uint8_t version;
35416
35415
35417
35416
dbuf1 = s->dbuf;
35418
35417
js_dbuf_init(s->ctx, &s->dbuf);
35419
-
35420
- version = BC_VERSION;
35421
- if (s->byte_swap)
35422
- version ^= BC_BE_VERSION;
35423
- bc_put_u8(s, version);
35418
+ bc_put_u8(s, BC_VERSION);
35424
35419
35425
35420
bc_put_leb128(s, s->idx_to_atom_count);
35426
35421
for(i = 0; i < s->idx_to_atom_count; i++) {
@@ -35453,8 +35448,6 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
35453
35448
35454
35449
memset(s, 0, sizeof(*s));
35455
35450
s->ctx = ctx;
35456
- /* XXX: byte swapped output is untested */
35457
- s->byte_swap = ((flags & JS_WRITE_OBJ_BSWAP) != 0);
35458
35451
s->allow_bytecode = ((flags & JS_WRITE_OBJ_BYTECODE) != 0);
35459
35452
s->allow_sab = ((flags & JS_WRITE_OBJ_SAB) != 0);
35460
35453
s->allow_reference = ((flags & JS_WRITE_OBJ_REFERENCE) != 0);
@@ -35575,33 +35568,45 @@ static int bc_get_u8(BCReaderState *s, uint8_t *pval)
35575
35568
35576
35569
static int bc_get_u16(BCReaderState *s, uint16_t *pval)
35577
35570
{
35571
+ uint16_t v;
35578
35572
if (unlikely(s->buf_end - s->ptr < 2)) {
35579
35573
*pval = 0; /* avoid warning */
35580
35574
return bc_read_error_end(s);
35581
35575
}
35582
- *pval = get_u16(s->ptr);
35576
+ v = get_u16(s->ptr);
35577
+ if (is_be())
35578
+ v = bswap16(v);
35579
+ *pval = v;
35583
35580
s->ptr += 2;
35584
35581
return 0;
35585
35582
}
35586
35583
35587
35584
static __maybe_unused int bc_get_u32(BCReaderState *s, uint32_t *pval)
35588
35585
{
35586
+ uint32_t v;
35589
35587
if (unlikely(s->buf_end - s->ptr < 4)) {
35590
35588
*pval = 0; /* avoid warning */
35591
35589
return bc_read_error_end(s);
35592
35590
}
35593
- *pval = get_u32(s->ptr);
35591
+ v = get_u32(s->ptr);
35592
+ if (is_be())
35593
+ v = bswap32(v);
35594
+ *pval = v;
35594
35595
s->ptr += 4;
35595
35596
return 0;
35596
35597
}
35597
35598
35598
35599
static int bc_get_u64(BCReaderState *s, uint64_t *pval)
35599
35600
{
35601
+ uint64_t v;
35600
35602
if (unlikely(s->buf_end - s->ptr < 8)) {
35601
35603
*pval = 0; /* avoid warning */
35602
35604
return bc_read_error_end(s);
35603
35605
}
35604
- *pval = get_u64(s->ptr);
35606
+ v = get_u64(s->ptr);
35607
+ if (is_be())
35608
+ v = bswap64(v);
35609
+ *pval = v;
35605
35610
s->ptr += 8;
35606
35611
return 0;
35607
35612
}
@@ -35711,10 +35716,15 @@ static JSString *JS_ReadString(BCReaderState *s)
35711
35716
js_free_string(s->ctx->rt, p);
35712
35717
return NULL;
35713
35718
}
35714
- // XXX: potential endianness issue
35715
35719
memcpy(p->u.str8, s->ptr, size);
35716
35720
s->ptr += size;
35717
- if (!is_wide_char) {
35721
+ if (is_wide_char) {
35722
+ if (is_be()) {
35723
+ uint32_t i;
35724
+ for (i = 0; i < len; i++)
35725
+ p->u.str16[i] = bswap16(p->u.str16[i]);
35726
+ }
35727
+ } else {
35718
35728
p->u.str8[size] = '\0'; /* add the trailing zero for 8 bit strings */
35719
35729
}
35720
35730
#ifdef DUMP_READ_OBJECT
@@ -35753,6 +35763,9 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
35753
35763
}
35754
35764
b->byte_code_buf = bc_buf;
35755
35765
35766
+ if (is_be())
35767
+ bc_byte_swap(bc_buf, bc_len);
35768
+
35756
35769
pos = 0;
35757
35770
while (pos < bc_len) {
35758
35771
op = bc_buf[pos];
@@ -35873,15 +35886,9 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
35873
35886
#if LIMB_BITS == 32
35874
35887
if (bc_get_u32(s, &v))
35875
35888
goto fail;
35876
- #ifdef WORDS_BIGENDIAN
35877
- v = bswap32(v);
35878
- #endif
35879
35889
#else
35880
35890
if (bc_get_u64(s, &v))
35881
35891
goto fail;
35882
- #ifdef WORDS_BIGENDIAN
35883
- v = bswap64(v);
35884
- #endif
35885
35892
#endif
35886
35893
a->tab[i] = v;
35887
35894
}
@@ -36589,7 +36596,6 @@ static int JS_ReadObjectAtoms(BCReaderState *s)
36589
36596
36590
36597
if (bc_get_u8(s, &v8))
36591
36598
return -1;
36592
- /* XXX: could support byte swapped input */
36593
36599
if (v8 != BC_VERSION) {
36594
36600
JS_ThrowSyntaxError(s->ctx, "invalid version (%d expected=%d)",
36595
36601
v8, BC_VERSION);
@@ -54857,12 +54863,9 @@ static JSValue js_dataview_getValue(JSContext *ctx,
54857
54863
size = 1 << typed_array_size_log2(class_id);
54858
54864
if (JS_ToIndex(ctx, &pos, argv[0]))
54859
54865
return JS_EXCEPTION;
54860
- is_swap = FALSE ;
54866
+ is_swap = TRUE ;
54861
54867
if (argc > 1)
54862
- is_swap = JS_ToBool(ctx, argv[1]);
54863
- #ifndef WORDS_BIGENDIAN
54864
- is_swap ^= 1;
54865
- #endif
54868
+ is_swap = !JS_ToBool(ctx, argv[1]);
54866
54869
abuf = ta->buffer->u.array_buffer;
54867
54870
if (abuf->detached)
54868
54871
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
@@ -54986,12 +54989,9 @@ static JSValue js_dataview_setValue(JSContext *ctx,
54986
54989
v64 = u.u64;
54987
54990
}
54988
54991
}
54989
- is_swap = FALSE ;
54992
+ is_swap = TRUE ;
54990
54993
if (argc > 2)
54991
- is_swap = JS_ToBool(ctx, argv[2]);
54992
- #ifndef WORDS_BIGENDIAN
54993
- is_swap ^= 1;
54994
- #endif
54994
+ is_swap = !JS_ToBool(ctx, argv[2]);
54995
54995
abuf = ta->buffer->u.array_buffer;
54996
54996
if (abuf->detached)
54997
54997
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
0 commit comments