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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
|
/**
* @file lscript_byteconvert.h
* @brief Shared code for compiler and assembler for LSL
*
* $LicenseInfo:firstyear=2002&license=viewergpl$
*
* Copyright (c) 2002-2007, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlife.com/developers/opensource/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at http://secondlife.com/developers/opensource/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
// data shared between compiler/assembler
// used to convert data between byte stream and outside data types
#ifndef LL_LSCRIPT_BYTECONVERT_H
#define LL_LSCRIPT_BYTECONVERT_H
#include "stdtypes.h"
#include "v3math.h"
#include "llquaternion.h"
#include "lscript_byteformat.h"
#include "lluuid.h"
void reset_hp_to_safe_spot(const U8 *buffer);
// remember that LScript byte stream is BigEndian
void set_fault(const U8 *stream, LSCRIPTRunTimeFaults fault);
inline S32 bytestream2integer(const U8 *stream, S32 &offset)
{
stream += offset;
offset += 4;
return (*stream<<24) | (*(stream + 1)<<16) | (*(stream + 2)<<8) | *(stream + 3);
}
inline U32 bytestream2unsigned_integer(const U8 *stream, S32 &offset)
{
stream += offset;
offset += 4;
return (*stream<<24) | (*(stream + 1)<<16) | (*(stream + 2)<<8) | *(stream + 3);
}
inline U64 bytestream2u64(const U8 *stream, S32 &offset)
{
stream += offset;
offset += 8;
return ((U64)(*stream)<<56)| ((U64)(*(stream + 1))<<48) | ((U64)(*(stream + 2))<<40) | ((U64)(*(stream + 3))<<32) |
((U64)(*(stream + 4))<<24) | ((U64)(*(stream + 5))<<16) | ((U64)(*(stream + 6))<<8) | (U64)(*(stream + 7));
}
inline void integer2bytestream(U8 *stream, S32 &offset, S32 integer)
{
stream += offset;
offset += 4;
*(stream) = (integer >> 24);
*(stream + 1) = (integer >> 16) & 0xff;
*(stream + 2) = (integer >> 8) & 0xff;
*(stream + 3) = (integer) & 0xff;
}
inline void unsigned_integer2bytestream(U8 *stream, S32 &offset, U32 integer)
{
stream += offset;
offset += 4;
*(stream) = (integer >> 24);
*(stream + 1) = (integer >> 16) & 0xff;
*(stream + 2) = (integer >> 8) & 0xff;
*(stream + 3) = (integer) & 0xff;
}
inline void u642bytestream(U8 *stream, S32 &offset, U64 integer)
{
stream += offset;
offset += 8;
*(stream) = (U8)(integer >> 56);
*(stream + 1) = (U8)((integer >> 48) & 0xff);
*(stream + 2) = (U8)((integer >> 40) & 0xff);
*(stream + 3) = (U8)((integer >> 32) & 0xff);
*(stream + 4) = (U8)((integer >> 24) & 0xff);
*(stream + 5) = (U8)((integer >> 16) & 0xff);
*(stream + 6) = (U8)((integer >> 8) & 0xff);
*(stream + 7) = (U8)((integer) & 0xff);
}
inline S16 bytestream2s16(const U8 *stream, S32 &offset)
{
stream += offset;
offset += 2;
return (*stream<<8) | *(stream + 1);
}
inline void s162bytestream(U8 *stream, S32 &offset, S16 integer)
{
stream += offset;
offset += 2;
*(stream) = (integer >> 8);
*(stream + 1) = (integer) & 0xff;
}
inline U16 bytestream2u16(const U8 *stream, S32 &offset)
{
stream += offset;
offset += 2;
return (*stream<<8) | *(stream + 1);
}
inline void u162bytestream(U8 *stream, S32 &offset, U16 integer)
{
stream += offset;
offset += 2;
*(stream) = (integer >> 8);
*(stream + 1) = (integer) & 0xff;
}
inline F32 bytestream2float(const U8 *stream, S32 &offset)
{
S32 value = bytestream2integer(stream, offset);
F32 fpvalue = *(F32 *)&value;
if (!llfinite(fpvalue))
{
fpvalue = 0;
set_fault(stream, LSRF_MATH);
}
return fpvalue;
}
inline void float2bytestream(U8 *stream, S32 &offset, F32 floatingpoint)
{
S32 value = *(S32 *)&floatingpoint;
integer2bytestream(stream, offset, value);
}
inline void bytestream_int2float(U8 *stream, S32 &offset)
{
S32 value = bytestream2integer(stream, offset);
offset -= 4;
F32 fpvalue = (F32)value;
if (!llfinite(fpvalue))
{
fpvalue = 0;
set_fault(stream, LSRF_MATH);
}
float2bytestream(stream, offset, fpvalue);
}
// Returns true on success, return false and clip copy on buffer overflow
inline bool bytestream2char(char *buffer, const U8 *stream, S32 &offset, S32 buffsize)
{
S32 source_len = strlen( (const char *)stream+offset );
S32 copy_len = buffsize - 1;
if( copy_len > source_len )
{
copy_len = source_len;
}
// strncpy without \0 padding overhead
memcpy( buffer, stream+offset, copy_len );
buffer[copy_len] = 0;
offset += source_len + 1; // advance past source string, include terminating '\0'
return source_len < buffsize;
}
inline void char2bytestream(U8 *stream, S32 &offset, const char *buffer)
{
while ((*(stream + offset++) = *buffer++))
;
}
inline U8 bytestream2byte(const U8 *stream, S32 &offset)
{
return *(stream + offset++);
}
inline void byte2bytestream(U8 *stream, S32 &offset, U8 byte)
{
*(stream + offset++) = byte;
}
inline void bytestream2bytestream(U8 *dest, S32 &dest_offset, const U8 *src, S32 &src_offset, S32 count)
{
while (count)
{
(*(dest + dest_offset++)) = (*(src + src_offset++));
count--;
}
}
inline void uuid2bytestream(U8 *stream, S32 &offset, const LLUUID &uuid)
{
S32 i;
for (i = 0; i < UUID_BYTES; i++)
{
*(stream + offset++) = uuid.mData[i];
}
}
inline void bytestream2uuid(U8 *stream, S32 &offset, LLUUID &uuid)
{
S32 i;
for (i = 0; i < UUID_BYTES; i++)
{
uuid.mData[i] = *(stream + offset++);
}
}
// vectors and quaternions and encoded in backwards order to match the way in which they are stored on the stack
inline void bytestream2vector(LLVector3 &vector, const U8 *stream, S32 &offset)
{
S32 value = bytestream2integer(stream, offset);
vector.mV[VZ] = *(F32 *)&value;
if (!llfinite(vector.mV[VZ]))
{
vector.mV[VZ] = 0;
set_fault(stream, LSRF_MATH);
}
value = bytestream2integer(stream, offset);
vector.mV[VY] = *(F32 *)&value;
if (!llfinite(vector.mV[VY]))
{
vector.mV[VY] = 0;
set_fault(stream, LSRF_MATH);
}
value = bytestream2integer(stream, offset);
vector.mV[VX] = *(F32 *)&value;
if (!llfinite(vector.mV[VX]))
{
vector.mV[VX] = 0;
set_fault(stream, LSRF_MATH);
}
}
inline void vector2bytestream(U8 *stream, S32 &offset, LLVector3 &vector)
{
S32 value = *(S32 *)&vector.mV[VZ];
integer2bytestream(stream, offset, value);
value = *(S32 *)&vector.mV[VY];
integer2bytestream(stream, offset, value);
value = *(S32 *)&vector.mV[VX];
integer2bytestream(stream, offset, value);
}
inline void bytestream2quaternion(LLQuaternion &quat, const U8 *stream, S32 &offset)
{
S32 value = bytestream2integer(stream, offset);
quat.mQ[VS] = *(F32 *)&value;
if (!llfinite(quat.mQ[VS]))
{
quat.mQ[VS] = 0;
set_fault(stream, LSRF_MATH);
}
value = bytestream2integer(stream, offset);
quat.mQ[VZ] = *(F32 *)&value;
if (!llfinite(quat.mQ[VZ]))
{
quat.mQ[VZ] = 0;
set_fault(stream, LSRF_MATH);
}
value = bytestream2integer(stream, offset);
quat.mQ[VY] = *(F32 *)&value;
if (!llfinite(quat.mQ[VY]))
{
quat.mQ[VY] = 0;
set_fault(stream, LSRF_MATH);
}
value = bytestream2integer(stream, offset);
quat.mQ[VX] = *(F32 *)&value;
if (!llfinite(quat.mQ[VX]))
{
quat.mQ[VX] = 0;
set_fault(stream, LSRF_MATH);
}
}
inline void quaternion2bytestream(U8 *stream, S32 &offset, LLQuaternion &quat)
{
S32 value = *(S32 *)&quat.mQ[VS];
integer2bytestream(stream, offset, value);
value = *(S32 *)&quat.mQ[VZ];
integer2bytestream(stream, offset, value);
value = *(S32 *)&quat.mQ[VY];
integer2bytestream(stream, offset, value);
value = *(S32 *)&quat.mQ[VX];
integer2bytestream(stream, offset, value);
}
inline S32 get_register(const U8 *stream, LSCRIPTRegisters reg)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
return bytestream2integer(stream, offset);
}
inline F32 get_register_fp(U8 *stream, LSCRIPTRegisters reg)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
F32 value = bytestream2float(stream, offset);
if (!llfinite(value))
{
value = 0;
set_fault(stream, LSRF_MATH);
}
return value;
}
inline U64 get_register_u64(U8 *stream, LSCRIPTRegisters reg)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
return bytestream2u64(stream, offset);
}
inline U64 get_event_register(U8 *stream, LSCRIPTRegisters reg, S32 major_version)
{
if (major_version == 1)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
return (U64)bytestream2integer(stream, offset);
}
else if (major_version == 2)
{
S32 offset = gLSCRIPTRegisterAddresses[reg + (LREG_NCE - LREG_CE)];
return bytestream2u64(stream, offset);
}
else
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
return (U64)bytestream2integer(stream, offset);
}
}
inline void set_register(U8 *stream, LSCRIPTRegisters reg, S32 value)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
integer2bytestream(stream, offset, value);
}
inline void set_register_fp(U8 *stream, LSCRIPTRegisters reg, F32 value)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
float2bytestream(stream, offset, value);
}
inline void set_register_u64(U8 *stream, LSCRIPTRegisters reg, U64 value)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
u642bytestream(stream, offset, value);
}
inline void set_event_register(U8 *stream, LSCRIPTRegisters reg, U64 value, S32 major_version)
{
if (major_version == 1)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
integer2bytestream(stream, offset, (S32)value);
}
else if (major_version == 2)
{
S32 offset = gLSCRIPTRegisterAddresses[reg + (LREG_NCE - LREG_CE)];
u642bytestream(stream, offset, value);
}
else
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
integer2bytestream(stream, offset, (S32)value);
}
}
inline F32 add_register_fp(U8 *stream, LSCRIPTRegisters reg, F32 value)
{
S32 offset = gLSCRIPTRegisterAddresses[reg];
F32 newvalue = bytestream2float(stream, offset);
newvalue += value;
if (!llfinite(newvalue))
{
newvalue = 0;
set_fault(stream, LSRF_MATH);
}
offset = gLSCRIPTRegisterAddresses[reg];
float2bytestream(stream, offset, newvalue);
return newvalue;
}
void lsa_print_heap(U8 *buffer);
inline void set_fault(const U8 *stream, LSCRIPTRunTimeFaults fault)
{
S32 fr = get_register(stream, LREG_FR);
// record the first error
if (!fr)
{
if ( (fault == LSRF_HEAP_ERROR)
||(fault == LSRF_STACK_HEAP_COLLISION)
||(fault == LSRF_BOUND_CHECK_ERROR))
{
reset_hp_to_safe_spot(stream);
// lsa_print_heap((U8 *)stream);
}
fr = LSCRIPTRunTimeFaultBits[fault];
set_register((U8 *)stream, LREG_FR, fr);
}
}
inline BOOL set_ip(U8 *stream, S32 ip)
{
// Verify that the Instruction Pointer is in a valid
// code area (between the Global Function Register
// and Heap Register).
S32 gfr = get_register(stream, LREG_GFR);
if (ip == 0)
{
set_register(stream, LREG_IP, ip);
return TRUE;
}
if (ip < gfr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
S32 hr = get_register(stream, LREG_HR);
if (ip >= hr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
set_register(stream, LREG_IP, ip);
return TRUE;
}
inline BOOL set_bp(U8 *stream, S32 bp)
{
// Verify that the Base Pointer is in a valid
// data area (between the Heap Pointer and
// the Top of Memory, and below the
// Stack Pointer).
S32 hp = get_register(stream, LREG_HP);
if (bp <= hp)
{
set_fault(stream, LSRF_STACK_HEAP_COLLISION);
return FALSE;
}
S32 tm = get_register(stream, LREG_TM);
if (bp >= tm)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
S32 sp = get_register(stream, LREG_SP);
if (bp < sp)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
set_register(stream, LREG_BP, bp);
return TRUE;
}
inline BOOL set_sp(U8 *stream, S32 sp)
{
// Verify that the Stack Pointer is in a valid
// data area (between the Heap Pointer and
// the Top of Memory).
S32 hp = get_register(stream, LREG_HP);
if (sp <= hp)
{
set_fault(stream, LSRF_STACK_HEAP_COLLISION);
return FALSE;
}
S32 tm = get_register(stream, LREG_TM);
if (sp >= tm)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
set_register(stream, LREG_SP, sp);
return TRUE;
}
inline void lscript_push(U8 *stream, U8 value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= 1;
if (set_sp(stream, sp))
{
*(stream + sp) = value;
}
}
inline void lscript_push(U8 *stream, S32 value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= LSCRIPTDataSize[LST_INTEGER];
if (set_sp(stream, sp))
{
integer2bytestream(stream, sp, value);
}
}
inline void lscript_push(U8 *stream, F32 value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= LSCRIPTDataSize[LST_FLOATINGPOINT];
if (set_sp(stream, sp))
{
float2bytestream(stream, sp, value);
}
}
inline void lscript_push(U8 *stream, LLVector3 &value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= LSCRIPTDataSize[LST_VECTOR];
if (set_sp(stream, sp))
{
vector2bytestream(stream, sp, value);
}
}
inline void lscript_push(U8 *stream, LLQuaternion &value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= LSCRIPTDataSize[LST_QUATERNION];
if (set_sp(stream, sp))
{
quaternion2bytestream(stream, sp, value);
}
}
inline void lscript_pusharg(U8 *stream, S32 arg)
{
S32 sp = get_register(stream, LREG_SP);
sp -= arg;
set_sp(stream, sp);
}
inline void lscript_poparg(U8 *stream, S32 arg)
{
S32 sp = get_register(stream, LREG_SP);
sp += arg;
set_sp(stream, sp);
}
inline U8 lscript_pop_char(U8 *stream)
{
S32 sp = get_register(stream, LREG_SP);
U8 value = *(stream + sp++);
set_sp(stream, sp);
return value;
}
inline S32 lscript_pop_int(U8 *stream)
{
S32 sp = get_register(stream, LREG_SP);
S32 value = bytestream2integer(stream, sp);
set_sp(stream, sp);
return value;
}
inline F32 lscript_pop_float(U8 *stream)
{
S32 sp = get_register(stream, LREG_SP);
F32 value = bytestream2float(stream, sp);
if (!llfinite(value))
{
value = 0;
set_fault(stream, LSRF_MATH);
}
set_sp(stream, sp);
return value;
}
inline void lscript_pop_vector(U8 *stream, LLVector3 &value)
{
S32 sp = get_register(stream, LREG_SP);
bytestream2vector(value, stream, sp);
set_sp(stream, sp);
}
inline void lscript_pop_quaternion(U8 *stream, LLQuaternion &value)
{
S32 sp = get_register(stream, LREG_SP);
bytestream2quaternion(value, stream, sp);
set_sp(stream, sp);
}
inline void lscript_pusharge(U8 *stream, S32 value)
{
S32 sp = get_register(stream, LREG_SP);
sp -= value;
if (set_sp(stream, sp))
{
S32 i;
for (i = 0; i < value; i++)
{
*(stream + sp++) = 0;
}
}
}
inline BOOL lscript_check_local(U8 *stream, S32 &address, S32 size)
{
S32 sp = get_register(stream, LREG_SP);
S32 bp = get_register(stream, LREG_BP);
address += size;
address = bp - address;
if (address < sp - size)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
S32 tm = get_register(stream, LREG_TM);
if (address + size > tm)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
return TRUE;
}
inline BOOL lscript_check_global(U8 *stream, S32 &address, S32 size)
{
S32 gvr = get_register(stream, LREG_GVR);
// Possibility of overwriting registers? -- DK 09/07/04
if (address < 0)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
address += gvr;
S32 gfr = get_register(stream, LREG_GFR);
if (address + size > gfr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
return TRUE;
}
inline void lscript_local_store(U8 *stream, S32 address, S32 value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_INTEGER]))
integer2bytestream(stream, address, value);
}
inline void lscript_local_store(U8 *stream, S32 address, F32 value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_FLOATINGPOINT]))
float2bytestream(stream, address, value);
}
inline void lscript_local_store(U8 *stream, S32 address, LLVector3 value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_VECTOR]))
vector2bytestream(stream, address, value);
}
inline void lscript_local_store(U8 *stream, S32 address, LLQuaternion value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_QUATERNION]))
quaternion2bytestream(stream, address, value);
}
inline void lscript_global_store(U8 *stream, S32 address, S32 value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_INTEGER]))
integer2bytestream(stream, address, value);
}
inline void lscript_global_store(U8 *stream, S32 address, F32 value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_FLOATINGPOINT]))
float2bytestream(stream, address, value);
}
inline void lscript_global_store(U8 *stream, S32 address, LLVector3 value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_VECTOR]))
vector2bytestream(stream, address, value);
}
inline void lscript_global_store(U8 *stream, S32 address, LLQuaternion value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_QUATERNION]))
quaternion2bytestream(stream, address, value);
}
inline S32 lscript_local_get(U8 *stream, S32 address)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_INTEGER]))
return bytestream2integer(stream, address);
return 0;
}
inline void lscript_local_get(U8 *stream, S32 address, F32 &value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_FLOATINGPOINT]))
value = bytestream2float(stream, address);
if (!llfinite(value))
{
value = 0;
set_fault(stream, LSRF_MATH);
}
}
inline void lscript_local_get(U8 *stream, S32 address, LLVector3 &value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_VECTOR]))
bytestream2vector(value, stream, address);
}
inline void lscript_local_get(U8 *stream, S32 address, LLQuaternion &value)
{
if (lscript_check_local(stream, address, LSCRIPTDataSize[LST_QUATERNION]))
bytestream2quaternion(value, stream, address);
}
inline S32 lscript_global_get(U8 *stream, S32 address)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_INTEGER]))
return bytestream2integer(stream, address);
return 0;
}
inline void lscript_global_get(U8 *stream, S32 address, F32 &value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_FLOATINGPOINT]))
value = bytestream2float(stream, address);
if (!llfinite(value))
{
value = 0;
set_fault(stream, LSRF_MATH);
}
}
inline void lscript_global_get(U8 *stream, S32 address, LLVector3 &value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_VECTOR]))
bytestream2vector(value, stream, address);
}
inline void lscript_global_get(U8 *stream, S32 address, LLQuaternion &value)
{
if (lscript_check_global(stream, address, LSCRIPTDataSize[LST_QUATERNION]))
bytestream2quaternion(value, stream, address);
}
inline S32 get_state_event_opcoode_start(U8 *stream, S32 state, LSCRIPTStateEventType event)
{
// get the start of the state table
S32 sr = get_register(stream, LREG_SR);
// get the position of the jump to the desired state
S32 value = get_register(stream, LREG_VN);
S32 state_offset_offset = 0;
S32 major_version = 0;
if (value == LSL2_VERSION1_END_NUMBER)
{
major_version = LSL2_MAJOR_VERSION_ONE;
state_offset_offset = sr + LSCRIPTDataSize[LST_INTEGER] + LSCRIPTDataSize[LST_INTEGER]*2*state;
}
else if (value == LSL2_VERSION_NUMBER)
{
major_version = LSL2_MAJOR_VERSION_TWO;
state_offset_offset = sr + LSCRIPTDataSize[LST_INTEGER] + LSCRIPTDataSize[LST_INTEGER]*3*state;
}
if ( state_offset_offset < 0 || state_offset_offset > TOP_OF_MEMORY )
{
return -1;
}
// get the actual position in memory of the desired state
S32 state_offset = sr + bytestream2integer(stream, state_offset_offset);
if ( state_offset < 0 || state_offset > TOP_OF_MEMORY )
{
return -1;
}
// save that value
S32 state_offset_base = state_offset;
// jump past the state name
S32 event_jump_offset = state_offset_base + bytestream2integer(stream, state_offset);
// get the location of the event offset
S32 event_offset = event_jump_offset + LSCRIPTDataSize[LST_INTEGER]*2*get_event_handler_jump_position(get_event_register(stream, LREG_ER, major_version), event);
if ( event_offset < 0 || event_offset > TOP_OF_MEMORY )
{
return -1;
}
// now, jump to the event
S32 event_start = bytestream2integer(stream, event_offset);
if ( event_start < 0 || event_start > TOP_OF_MEMORY )
{
return -1;
}
event_start += event_jump_offset;
S32 event_start_original = event_start;
// now skip past the parameters
S32 opcode_offset = bytestream2integer(stream, event_start);
if ( opcode_offset < 0 || opcode_offset > TOP_OF_MEMORY )
{
return -1;
}
return opcode_offset + event_start_original;
}
inline U64 get_handled_events(U8 *stream, S32 state)
{
U64 retvalue = 0;
// get the start of the state table
S32 sr = get_register(stream, LREG_SR);
// get the position of the jump to the desired state
S32 value = get_register(stream, LREG_VN);
S32 state_handled_offset = 0;
if (value == LSL2_VERSION1_END_NUMBER)
{
state_handled_offset = sr + LSCRIPTDataSize[LST_INTEGER]*2*state + 2*LSCRIPTDataSize[LST_INTEGER];
retvalue = bytestream2integer(stream, state_handled_offset);
}
else if (value == LSL2_VERSION_NUMBER)
{
state_handled_offset = sr + LSCRIPTDataSize[LST_INTEGER]*3*state + 2*LSCRIPTDataSize[LST_INTEGER];
retvalue = bytestream2u64(stream, state_handled_offset);
}
// get the handled events
return retvalue;
}
// Returns -1 on error
inline S32 get_event_stack_size(U8 *stream, S32 state, LSCRIPTStateEventType event)
{
// get the start of the state table
S32 sr = get_register(stream, LREG_SR);
// get state offset
S32 value = get_register(stream, LREG_VN);
S32 state_offset_offset = 0;
S32 major_version = 0;
if (value == LSL2_VERSION1_END_NUMBER)
{
major_version = LSL2_MAJOR_VERSION_ONE;
state_offset_offset = sr + LSCRIPTDataSize[LST_INTEGER] + LSCRIPTDataSize[LST_INTEGER]*2*state;
}
else if (value == LSL2_VERSION_NUMBER)
{
major_version = LSL2_MAJOR_VERSION_TWO;
state_offset_offset = sr + LSCRIPTDataSize[LST_INTEGER] + LSCRIPTDataSize[LST_INTEGER]*3*state;
}
if ( state_offset_offset < 0 || state_offset_offset > TOP_OF_MEMORY )
{
return -1;
}
S32 state_offset = bytestream2integer(stream, state_offset_offset);
state_offset += sr;
state_offset_offset = state_offset;
if ( state_offset_offset < 0 || state_offset_offset > TOP_OF_MEMORY )
{
return -1;
}
// skip to jump table
S32 jump_table = bytestream2integer(stream, state_offset_offset);
jump_table += state_offset;
if ( jump_table < 0 || jump_table > TOP_OF_MEMORY )
{
return -1;
}
// get the position of the jump to the desired state
S32 stack_size_offset = jump_table + LSCRIPTDataSize[LST_INTEGER]*2*get_event_handler_jump_position(get_event_register(stream, LREG_ER, major_version), event) + LSCRIPTDataSize[LST_INTEGER];
// get the handled events
S32 stack_size = bytestream2integer(stream, stack_size_offset);
if ( stack_size < 0 || stack_size > TOP_OF_MEMORY )
{
return -1;
}
return stack_size;
}
inline LSCRIPTStateEventType return_first_event(S32 event)
{
S32 count = 1;
while (count < LSTT_EOF)
{
if (event & 0x1)
{
return (LSCRIPTStateEventType) count;
}
else
{
event >>= 1;
count++;
}
}
return LSTT_NULL;
}
// the safe instruction versions of these commands will only work if offset is between
// GFR and HR, meaning that it is an instruction (more or less) in global functions or event handlers
inline BOOL safe_instruction_check_address(U8 *stream, S32 offset, S32 size)
{
S32 gfr = get_register(stream, LREG_GFR);
if (offset < gfr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
else
{
S32 hr = get_register(stream, LREG_HR);
if (offset + size > hr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
else
{
return TRUE;
}
}
}
inline BOOL safe_heap_check_address(U8 *stream, S32 offset, S32 size)
{
S32 hr = get_register(stream, LREG_HR);
if (offset < hr)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
else
{
S32 hp = get_register(stream, LREG_HP);
if (offset + size > hp)
{
set_fault(stream, LSRF_BOUND_CHECK_ERROR);
return FALSE;
}
else
{
return TRUE;
}
}
}
inline U8 safe_instruction_bytestream2byte(U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, 1))
{
return *(stream + offset++);
}
else
{
return 0;
}
}
inline void safe_instruction_byte2bytestream(U8 *stream, S32 &offset, U8 byte)
{
if (safe_instruction_check_address(stream, offset, 1))
{
*(stream + offset++) = byte;
}
}
inline S32 safe_instruction_bytestream2integer(U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_INTEGER]))
{
return (bytestream2integer(stream, offset));
}
else
{
return 0;
}
}
inline void safe_instruction_integer2bytestream(U8 *stream, S32 &offset, S32 value)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_INTEGER]))
{
integer2bytestream(stream, offset, value);
}
}
inline U16 safe_instruction_bytestream2u16(U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, 2))
{
return (bytestream2u16(stream, offset));
}
else
{
return 0;
}
}
inline void safe_instruction_u162bytestream(U8 *stream, S32 &offset, U16 value)
{
if (safe_instruction_check_address(stream, offset, 2))
{
u162bytestream(stream, offset, value);
}
}
inline F32 safe_instruction_bytestream2float(U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_INTEGER]))
{
F32 value = bytestream2float(stream, offset);
if (!llfinite(value))
{
value = 0;
set_fault(stream, LSRF_MATH);
}
return value;
}
else
{
return 0;
}
}
inline void safe_instruction_float2bytestream(U8 *stream, S32 &offset, F32 value)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_FLOATINGPOINT]))
{
float2bytestream(stream, offset, value);
}
}
inline void safe_instruction_bytestream2char(char *buffer, U8 *stream, S32 &offset, S32 buffsize)
{
// This varies from the old method. Previously, we would copy up until we got an error,
// then halt the script via safe_isntruction_check_address. Now we don't bother
// copying a thing if there's an error.
if( safe_instruction_check_address(stream, offset, strlen( (const char *)stream + offset ) + 1 ) )
{
// Takes the same parms as this function. Won't overread, per above check.
bytestream2char( buffer, stream, offset, buffsize );
}
else
{
// Truncate - no point in copying
*buffer = 0;
}
}
inline void safe_instruction_bytestream_count_char(U8 *stream, S32 &offset)
{
while ( (safe_instruction_check_address(stream, offset, 1))
&&(*(stream + offset++)))
;
}
inline void safe_heap_bytestream_count_char(U8 *stream, S32 &offset)
{
while ( (safe_heap_check_address(stream, offset, 1))
&&(*(stream + offset++)))
;
}
inline void safe_instruction_char2bytestream(U8 *stream, S32 &offset, const char* buffer)
{
while ( (safe_instruction_check_address(stream, offset, 1))
&&(*(stream + offset++) = *buffer++))
;
}
inline void safe_instruction_bytestream2vector(LLVector3 &value, U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_VECTOR]))
{
bytestream2vector(value, stream, offset);
}
}
inline void safe_instruction_vector2bytestream(U8 *stream, S32 &offset, LLVector3 &value)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_VECTOR]))
{
vector2bytestream(stream, offset, value);
}
}
inline void safe_instruction_bytestream2quaternion(LLQuaternion &value, U8 *stream, S32 &offset)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_QUATERNION]))
{
bytestream2quaternion(value, stream, offset);
}
}
inline void safe_instruction_quaternion2bytestream(U8 *stream, S32 &offset, LLQuaternion &value)
{
if (safe_instruction_check_address(stream, offset, LSCRIPTDataSize[LST_QUATERNION]))
{
quaternion2bytestream(stream, offset, value);
}
}
static inline LSCRIPTType char2type(char type)
{
switch(type)
{
case 'i':
return LST_INTEGER;
case 'f':
return LST_FLOATINGPOINT;
case 's':
return LST_STRING;
case 'k':
return LST_KEY;
case 'v':
return LST_VECTOR;
case 'q':
return LST_QUATERNION;
case 'l':
return LST_LIST;
default:
return LST_NULL;
}
}
#endif
|