@@ -1757,31 +1757,41 @@ int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell
17571757 return err ;
17581758}
17591759
1760+ int AMXAPI amx_PushStringLen (AMX * amx , cell * amx_addr , cell * * phys_addr , const char * string , int length , int pack , int use_wchar )
1761+ {
1762+ cell * paddr , xaddr ;
1763+ int numcells , err ;
1764+
1765+ assert (amx != NULL );
1766+
1767+ numcells = length + 1 ;
1768+
1769+ if (pack )
1770+ numcells = (numcells + sizeof (cell ) - 1 ) / sizeof (cell );
1771+ err = amx_Allot (amx , numcells , & xaddr , & paddr );
1772+ if (err == AMX_ERR_NONE ) {
1773+ if (amx_addr != NULL )
1774+ * amx_addr = xaddr ;
1775+ if (phys_addr != NULL )
1776+ * phys_addr = paddr ;
1777+ amx_SetStringLen (paddr , string , length , pack , use_wchar , UNLIMITED );
1778+ err = amx_Push (amx , xaddr );
1779+ } /* if */
1780+ return err ;
1781+ }
1782+
17601783int AMXAPI amx_PushString (AMX * amx , cell * amx_addr , cell * * phys_addr , const char * string , int pack , int use_wchar )
17611784{
1762- cell * paddr , xaddr ;
1763- int numcells ,err ;
1785+ int length ;
17641786
1765- assert (amx != NULL );
17661787 assert (string != NULL );
17671788
17681789 #if defined AMX_ANSIONLY
1769- numcells = strlen (string ) + 1 ;
1790+ length = strlen (string );
17701791 #else
1771- numcells = (use_wchar ? wcslen ((const wchar_t * )string ) : strlen (string )) + 1 ;
1792+ length = (use_wchar ? wcslen ((const wchar_t * )string ) : strlen (string ));
17721793 #endif
1773- if (pack )
1774- numcells = (numcells + sizeof (cell )- 1 )/sizeof (cell );
1775- err = amx_Allot (amx ,numcells ,& xaddr ,& paddr );
1776- if (err == AMX_ERR_NONE ) {
1777- if (amx_addr != NULL )
1778- * amx_addr = xaddr ;
1779- if (phys_addr != NULL )
1780- * phys_addr = paddr ;
1781- amx_SetString (paddr ,string ,pack ,use_wchar ,UNLIMITED );
1782- err = amx_Push (amx ,xaddr );
1783- } /* if */
1784- return err ;
1794+ return amx_PushStringLen (amx , amx_addr , phys_addr , string , length , pack , use_wchar );
17851795}
17861796#endif /* AMX_PUSHXXX */
17871797
@@ -1937,7 +1947,7 @@ static const void * const amx_opcodelist[] = {
19371947 if (index == AMX_EXEC_MAIN ) {
19381948 if (hdr -> cip < 0 )
19391949 return AMX_ERR_INDEX ;
1940- cip = (cell * )(code + (int )hdr -> cip );
1950+ cip = (cell * )(code + (int )hdr -> cip );
19411951 } else if (index == AMX_EXEC_CONT ) {
19421952 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
19431953 frm = amx -> frm ;
@@ -1947,14 +1957,14 @@ static const void * const amx_opcodelist[] = {
19471957 alt = amx -> alt ;
19481958 reset_stk = amx -> reset_stk ;
19491959 reset_hea = amx -> reset_hea ;
1950- cip = (cell * )(code + (int )amx -> cip );
1960+ cip = (cell * )(code + (int )amx -> cip );
19511961 } else if (index < 0 ) {
19521962 return AMX_ERR_INDEX ;
19531963 } else {
19541964 if (index >=(int )NUMENTRIES (hdr ,publics ,natives ))
19551965 return AMX_ERR_INDEX ;
19561966 func = GETENTRY (hdr ,publics ,index );
1957- cip = (cell * )(code + (int )func -> address );
1967+ cip = (cell * )(code + (int )func -> address );
19581968 } /* if */
19591969 /* check values just copied */
19601970 CHKSTACK ();
@@ -2206,7 +2216,7 @@ static const void * const amx_opcodelist[] = {
22062216 frm = pri ;
22072217 break ;
22082218 case 6 :
2209- cip = (cell * )(code + (int )pri );
2219+ cip = (cell * )(code + (int )pri );
22102220 break ;
22112221 } /* switch */
22122222 NEXT (cip );
@@ -2301,7 +2311,7 @@ static const void * const amx_opcodelist[] = {
23012311 NEXT (cip );
23022312 op_jrel :
23032313 offs = * cip ;
2304- cip = (cell * )((unsigned char * )cip + (int )offs + sizeof (cell ));
2314+ cip = (cell * )((unsigned char * )cip + (int )offs + sizeof (cell ));
23052315 NEXT (cip );
23062316 op_jzer :
23072317 if (pri == 0 )
@@ -2749,7 +2759,7 @@ static const void * const amx_opcodelist[] = {
27492759 NEXT (cip );
27502760 op_symbol :
27512761 GETPARAM (offs );
2752- cip = (cell * )((unsigned char * )cip + (int )offs );
2762+ cip = (cell * )((unsigned char * )cip + (int )offs );
27532763 NEXT (cip );
27542764 op_srange :
27552765 SKIPPARAM (2 );
@@ -3093,7 +3103,7 @@ int AMXAPI amx_Exec(AMX *amx, cell *retval, int index)
30933103 if (index == AMX_EXEC_MAIN ) {
30943104 if (hdr -> cip < 0 )
30953105 return AMX_ERR_INDEX ;
3096- cip = (cell * )(code + (int )hdr -> cip );
3106+ cip = (cell * )(code + (int )hdr -> cip );
30973107 } else if (index == AMX_EXEC_CONT ) {
30983108 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
30993109 frm = amx -> frm ;
@@ -3103,14 +3113,14 @@ int AMXAPI amx_Exec(AMX *amx, cell *retval, int index)
31033113 alt = amx -> alt ;
31043114 reset_stk = amx -> reset_stk ;
31053115 reset_hea = amx -> reset_hea ;
3106- cip = (cell * )(code + (int )amx -> cip );
3116+ cip = (cell * )(code + (int )amx -> cip );
31073117 } else if (index < 0 ) {
31083118 return AMX_ERR_INDEX ;
31093119 } else {
31103120 if (index >=(cell )NUMENTRIES (hdr ,publics ,natives ))
31113121 return AMX_ERR_INDEX ;
31123122 func = GETENTRY (hdr ,publics ,index );
3113- cip = (cell * )(code + (int )func -> address );
3123+ cip = (cell * )(code + (int )func -> address );
31143124 } /* if */
31153125 /* check values just copied */
31163126 CHKSTACK ();
@@ -3405,7 +3415,7 @@ int AMXAPI amx_Exec(AMX *amx, cell *retval, int index)
34053415 frm = pri ;
34063416 break ;
34073417 case 6 :
3408- cip = (cell * )(code + (int )pri );
3418+ cip = (cell * )(code + (int )pri );
34093419 break ;
34103420 } /* switch */
34113421 break ;
@@ -3501,7 +3511,7 @@ int AMXAPI amx_Exec(AMX *amx, cell *retval, int index)
35013511 break ;
35023512 case OP_JREL :
35033513 offs = * cip ;
3504- cip = (cell * )((unsigned char * )cip + (int )offs + sizeof (cell ));
3514+ cip = (cell * )((unsigned char * )cip + (int )offs + sizeof (cell ));
35053515 break ;
35063516 case OP_JZER :
35073517 if (pri == 0 )
@@ -3946,7 +3956,7 @@ int AMXAPI amx_Exec(AMX *amx, cell *retval, int index)
39463956 break ;
39473957 case OP_SYMBOL :
39483958 GETPARAM (offs );
3949- cip = (cell * )((unsigned char * )cip + (int )offs );
3959+ cip = (cell * )((unsigned char * )cip + (int )offs );
39503960 break ;
39513961 case OP_SRANGE :
39523962 SKIPPARAM (2 );
@@ -4306,61 +4316,72 @@ int AMXAPI amx_StrLen(const cell *cstr, int *length)
43064316#endif
43074317
43084318#if defined AMX_XXXSTRING || defined AMX_EXEC
4319+ int AMXAPI amx_SetStringLen (cell * dest , const char * source , int length , int pack , int use_wchar , size_t size )
4320+ { /* the memory blocks should not overlap */
4321+ int len , i ;
4322+
4323+ assert_static (UNLIMITED > 0 );
4324+ len = length ;
4325+ if (pack ) {
4326+ /* create a packed string */
4327+ if (size < UNLIMITED / sizeof (cell ) && (size_t )len >= size * sizeof (cell ))
4328+ len = size * sizeof (cell ) - 1 ;
4329+ dest [len / sizeof (cell )] = 0 ; /* clear last bytes of last (semi-filled) cell*/
4330+ #if defined AMX_ANSIONLY
4331+ memcpy (dest , source , len );
4332+ #else
4333+ if (use_wchar ) {
4334+ for (i = 0 ; i < len ; i ++ )
4335+ ((char * )dest )[i ] = (char )(((wchar_t * )source )[i ]);
4336+ } else {
4337+ memcpy (dest , source , len );
4338+ } /* if */
4339+ #endif
4340+ /* On Big Endian machines, the characters are well aligned in the
4341+ * cells; on Little Endian machines, we must swap all cells.
4342+ */
4343+ assert (check_endian ());
4344+ #if BYTE_ORDER == LITTLE_ENDIAN
4345+ len /= sizeof (cell );
4346+ while (len > 0 )
4347+ swapcell ((ucell * )& dest [len -- ]);
4348+ swapcell ((ucell * )& dest [0 ]);
4349+ #endif
4350+
4351+ } else {
4352+ /* create an unpacked string */
4353+ if (size < UNLIMITED && (size_t )len >= size )
4354+ len = size - 1 ;
4355+ #if defined AMX_ANSIONLY
4356+ for (i = 0 ; i < len ; i ++ )
4357+ dest [i ] = (cell )source [i ];
4358+ #else
4359+ if (use_wchar ) {
4360+ for (i = 0 ; i < len ; i ++ )
4361+ dest [i ] = (cell )(((wchar_t * )source )[i ]);
4362+ } else {
4363+ for (i = 0 ; i < len ; i ++ )
4364+ dest [i ] = (cell )(unsigned char )source [i ];
4365+ } /* if */
4366+ #endif
4367+ dest [len ] = 0 ;
4368+ } /* if */
4369+ return AMX_ERR_NONE ;
4370+ }
4371+
43094372int AMXAPI amx_SetString (cell * dest ,const char * source ,int pack ,int use_wchar ,size_t size )
43104373{ /* the memory blocks should not overlap */
4311- int len , i ;
4374+ int length ;
4375+
4376+ assert (source != NULL );
43124377
4313- assert_static (UNLIMITED > 0 );
43144378 #if defined AMX_ANSIONLY
43154379 (void )use_wchar ;
4316- len = strlen (source );
4380+ length = strlen (source );
43174381 #else
4318- len = use_wchar ? wcslen ((const wchar_t * )source ) : strlen (source );
4382+ length = use_wchar ? wcslen ((const wchar_t * )source ) : strlen (source );
43194383 #endif
4320- if (pack ) {
4321- /* create a packed string */
4322- if (size < UNLIMITED /sizeof (cell ) && (size_t )len >=size * sizeof (cell ))
4323- len = size * sizeof (cell )- 1 ;
4324- dest [len /sizeof (cell )]= 0 ; /* clear last bytes of last (semi-filled) cell*/
4325- #if defined AMX_ANSIONLY
4326- memcpy (dest ,source ,len );
4327- #else
4328- if (use_wchar ) {
4329- for (i = 0 ; i < len ; i ++ )
4330- ((char * )dest )[i ]= (char )(((wchar_t * )source )[i ]);
4331- } else {
4332- memcpy (dest ,source ,len );
4333- } /* if */
4334- #endif
4335- /* On Big Endian machines, the characters are well aligned in the
4336- * cells; on Little Endian machines, we must swap all cells.
4337- */
4338- assert (check_endian ());
4339- #if BYTE_ORDER == LITTLE_ENDIAN
4340- len /= sizeof (cell );
4341- while (len >=0 )
4342- swapcell ((ucell * )& dest [len -- ]);
4343- #endif
4344-
4345- } else {
4346- /* create an unpacked string */
4347- if (size < UNLIMITED && (size_t )len >=size )
4348- len = size - 1 ;
4349- #if defined AMX_ANSIONLY
4350- for (i = 0 ; i < len ; i ++ )
4351- dest [i ]= (cell )source [i ];
4352- #else
4353- if (use_wchar ) {
4354- for (i = 0 ; i < len ; i ++ )
4355- dest [i ]= (cell )(((wchar_t * )source )[i ]);
4356- } else {
4357- for (i = 0 ; i < len ; i ++ )
4358- dest [i ]= (cell )source [i ];
4359- } /* if */
4360- #endif
4361- dest [len ]= 0 ;
4362- } /* if */
4363- return AMX_ERR_NONE ;
4384+ return amx_SetStringLen (dest , source , length , pack , use_wchar , size );
43644385}
43654386#endif
43664387
0 commit comments