Ignore:
Timestamp:
Feb 7, 2019, 8:36:33 AM (5 years ago)
Author:
coas-nagasima
Message:

wolfsslを3.15.7にバージョンアップ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/wc_port.c

    r337 r372  
    4343#endif
    4444
    45 #if defined(FREESCALE_LTC_TFM)
     45#ifdef FREESCALE_LTC_TFM
    4646    #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
    4747#endif
    4848
    49 #ifdef WOLFSSL_ATMEL
     49#if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
    5050    #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
     51#endif
     52
     53#if defined(WOLFSSL_STSAFEA100)
     54    #include <wolfssl/wolfcrypt/port/st/stsafe.h>
    5155#endif
    5256
     
    5862    #include <wolfssl/wolfcrypt/memory.h>
    5963    #include <wolfssl/wolfcrypt/mem_track.h>
     64#endif
     65
     66#if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
     67    defined(WOLFSSL_IMX6_CAAM_BLOB)
     68    #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
     69#endif
     70
     71#ifdef WOLF_CRYPTO_DEV
     72    #include <wolfssl/wolfcrypt/cryptodev.h>
    6073#endif
    6174
     
    7790    if (initRefCount == 0) {
    7891        WOLFSSL_ENTER("wolfCrypt_Init");
     92
     93    #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
     94        {
     95            word32 rngMallocFail;
     96            time_t seed = time(NULL);
     97            srand((word32)seed);
     98            rngMallocFail = rand() % 2000; /* max 2000 */
     99            printf("\n--- RNG MALLOC FAIL AT %d---\n", rngMallocFail);
     100            wolfSSL_SetMemFailCount(rngMallocFail);
     101        }
     102    #endif
     103
     104    #ifdef WOLF_CRYPTO_DEV
     105        wc_CryptoDev_Init();
     106    #endif
    79107
    80108    #ifdef WOLFSSL_ASYNC_CRYPT
     
    125153    #endif
    126154
    127     #ifdef WOLFSSL_ATMEL
    128         atmel_init();
     155    #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
     156        ret = atmel_init();
     157        if (ret != 0) {
     158            WOLFSSL_MSG("CryptoAuthLib init failed");
     159            return ret;
     160        }
     161    #endif
     162
     163    #if defined(WOLFSSL_STSAFEA100)
     164        stsafe_interface_init();
    129165    #endif
    130166
    131167    #ifdef WOLFSSL_ARMASM
    132168        WOLFSSL_MSG("Using ARM hardware acceleration");
     169    #endif
     170
     171    #ifdef WOLFSSL_AFALG
     172        WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
    133173    #endif
    134174
     
    154194#endif
    155195
     196#if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
     197    defined(WOLFSSL_IMX6_CAAM_BLOB)
     198        if ((ret = wc_caamInit()) != 0) {
     199            return ret;
     200        }
     201#endif
     202
    156203        initRefCount = 1;
    157204    }
     
    190237    #endif
    191238
     239    #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
     240        defined(WOLFSSL_IMX6_CAAM_BLOB)
     241        wc_caamFree();
     242    #endif
     243
    192244        initRefCount = 0; /* allow re-init */
    193245    }
     
    196248}
    197249
    198 #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
     250#if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
     251        !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
    199252
    200253/* File Handling Helpers */
    201 /* returns 0 if file found, -1 if no files or negative error */
     254/* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
    202255int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
    203256{
    204     int ret = -1; /* default to no files found */
     257    int ret = WC_READDIR_NOFILE; /* default to no files found */
     258    int pathLen = 0;
     259    int dnameLen = 0;
    205260
    206261    if (name)
     
    212267
    213268    XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
     269    pathLen = (int)XSTRLEN(path);
    214270
    215271#ifdef USE_WINDOWS_API
    216     XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4);
    217     XSTRNCAT(ctx->name, "\\*", 3);
     272    if (pathLen > MAX_FILENAME_SZ - 3)
     273        return BAD_PATH_ERROR;
     274
     275    XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
     276    XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
    218277
    219278    ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
     
    225284    do {
    226285        if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
    227             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
    228             XSTRNCAT(ctx->name, "\\", 2);
    229             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
     286            dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
     287
     288            if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
     289                return BAD_PATH_ERROR;
     290            }
     291            XSTRNCPY(ctx->name, path, pathLen + 1);
     292            ctx->name[pathLen] = '\\';
     293            XSTRNCPY(ctx->name + pathLen + 1,
     294                     ctx->FindFileData.cFileName,
     295                     MAX_FILENAME_SZ - pathLen - 1);
    230296            if (name)
    231297                *name = ctx->name;
     
    241307
    242308    while ((ctx->entry = readdir(ctx->dir)) != NULL) {
    243         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
    244         XSTRNCAT(ctx->name, "/", 1);
    245         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
     309        dnameLen = (int)XSTRLEN(ctx->entry->d_name);
     310
     311        if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
     312            ret = BAD_PATH_ERROR;
     313            break;
     314        }
     315        XSTRNCPY(ctx->name, path, pathLen + 1);
     316        ctx->name[pathLen] = '/';
     317        XSTRNCPY(ctx->name + pathLen + 1,
     318                 ctx->entry->d_name, MAX_FILENAME_SZ - pathLen - 1);
    246319
    247320        if (stat(ctx->name, &ctx->s) != 0) {
     
    249322            ret = BAD_PATH_ERROR;
    250323            break;
    251         } else if (ctx->s.st_mode & S_IFREG) {
     324        } else if (S_ISREG(ctx->s.st_mode)) {
    252325            if (name)
    253326                *name = ctx->name;
     
    261334}
    262335
    263 /* returns 0 if file found, -1 if no more files */
     336/* returns 0 if file found, WC_READDIR_NOFILE if no more files */
    264337int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
    265338{
    266     int ret = -1; /* default to no file found */
     339    int ret = WC_READDIR_NOFILE; /* default to no file found */
     340    int pathLen = 0;
     341    int dnameLen = 0;
    267342
    268343    if (name)
     
    274349
    275350    XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
     351    pathLen = (int)XSTRLEN(path);
    276352
    277353#ifdef USE_WINDOWS_API
    278354    while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
    279355        if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
    280             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
    281             XSTRNCAT(ctx->name, "\\", 2);
    282             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
     356            dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
     357
     358            if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
     359                return BAD_PATH_ERROR;
     360            }
     361            XSTRNCPY(ctx->name, path, pathLen + 1);
     362            ctx->name[pathLen] = '\\';
     363            XSTRNCPY(ctx->name + pathLen + 1,
     364                     ctx->FindFileData.cFileName,
     365                     MAX_FILENAME_SZ - pathLen - 1);
    283366            if (name)
    284367                *name = ctx->name;
     
    288371#else
    289372    while ((ctx->entry = readdir(ctx->dir)) != NULL) {
    290         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
    291         XSTRNCAT(ctx->name, "/", 1);
    292         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
     373        dnameLen = (int)XSTRLEN(ctx->entry->d_name);
     374
     375        if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
     376            ret = BAD_PATH_ERROR;
     377            break;
     378        }
     379        XSTRNCPY(ctx->name, path, pathLen + 1);
     380        ctx->name[pathLen] = '/';
     381        XSTRNCPY(ctx->name + pathLen + 1,
     382                 ctx->entry->d_name, MAX_FILENAME_SZ - pathLen - 1);
    293383
    294384        if (stat(ctx->name, &ctx->s) != 0) {
     
    296386            ret = BAD_PATH_ERROR;
    297387            break;
    298         } else if (ctx->s.st_mode & S_IFREG) {
     388        } else if (S_ISREG(ctx->s.st_mode)) {
    299389            if (name)
    300390                *name = ctx->name;
     
    403493#endif /* USE_WOLF_STRTOK */
    404494
     495#ifdef USE_WOLF_STRSEP
     496char* wc_strsep(char **stringp, const char *delim)
     497{
     498    char *s, *tok;
     499    const char *spanp;
     500
     501    /* null check */
     502    if (stringp == NULL || *stringp == NULL)
     503        return NULL;
     504
     505    s = *stringp;
     506    for (tok = s; *tok; ++tok) {
     507        for (spanp = delim; *spanp; ++spanp) {
     508            /* found delimiter */
     509            if (*tok == *spanp) {
     510                *tok = '\0'; /* replace delim with null term */
     511                *stringp = tok + 1; /* return past delim */
     512                return s;
     513            }
     514        }
     515    }
     516
     517    *stringp = NULL;
     518    return s;
     519}
     520#endif /* USE_WOLF_STRSEP */
     521
    405522#if WOLFSSL_CRYPT_HW_MUTEX
    406523/* Mutex for protection of cryptography hardware */
     
    445562/* Mutex Ports */
    446563/* ---------------------------------------------------------------------------*/
     564#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
     565    static mutex_cb*     compat_mutex_cb = NULL;
     566
     567    /* Function that locks or unlocks a mutex based on the flag passed in.
     568     *
     569     * flag lock or unlock i.e. CRYPTO_LOCK
     570     * type the type of lock to unlock or lock
     571     * file name of the file calling
     572     * line the line number from file calling
     573     */
     574    int wc_LockMutex_ex(int flag, int type, const char* file, int line)
     575    {
     576        if (compat_mutex_cb != NULL) {
     577            compat_mutex_cb(flag, type, file, line);
     578            return 0;
     579        }
     580        else {
     581            WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
     582            return BAD_STATE_E;
     583        }
     584    }
     585
     586
     587    /* Set the callback function to use for locking/unlocking mutex
     588     *
     589     * cb callback function to use
     590     */
     591    int wc_SetMutexCb(mutex_cb* cb)
     592    {
     593        compat_mutex_cb = cb;
     594        return 0;
     595    }
     596#endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
    447597#ifdef SINGLE_THREADED
    448598
     
    602752        else
    603753            return BAD_MUTEX_E;
     754    }
     755
     756#elif defined(WOLFSSL_VXWORKS)
     757
     758    int wc_InitMutex(wolfSSL_Mutex* m)
     759    {
     760        if (m) {
     761            if ((*m = semMCreate(0)) != SEM_ID_NULL)
     762                return 0;
     763        }
     764        return BAD_MUTEX_E;
     765    }
     766
     767
     768    int wc_FreeMutex(wolfSSL_Mutex* m)
     769    {
     770        if (m) {
     771            if (semDelete(*m) == OK)
     772                return 0;
     773        }
     774        return BAD_MUTEX_E;
     775    }
     776
     777
     778    int wc_LockMutex(wolfSSL_Mutex* m)
     779    {
     780        if (m) {
     781            if (semTake(*m, WAIT_FOREVER) == OK)
     782                return 0;
     783        }
     784        return BAD_MUTEX_E;
     785    }
     786
     787
     788    int wc_UnLockMutex(wolfSSL_Mutex* m)
     789    {
     790        if (m) {
     791            if (semGive(*m) == OK)
     792                return 0;
     793        }
     794        return BAD_MUTEX_E;
    604795    }
    605796
     
    11701361    }
    11711362
     1363#elif defined(WOLFSSL_NUCLEUS_1_2)
     1364
     1365    int wc_InitMutex(wolfSSL_Mutex* m)
     1366    {
     1367        /* Call the Nucleus function to create the semaphore */
     1368        if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
     1369                                NU_PRIORITY) == NU_SUCCESS) {
     1370            return 0;
     1371        }
     1372
     1373        return BAD_MUTEX_E;
     1374    }
     1375
     1376    int wc_FreeMutex(wolfSSL_Mutex* m)
     1377    {
     1378        if (NU_Delete_Semaphore(m) == NU_SUCCESS)
     1379            return 0;
     1380
     1381        return BAD_MUTEX_E;
     1382    }
     1383
     1384    int wc_LockMutex(wolfSSL_Mutex* m)
     1385    {
     1386        /* passing suspend task option */
     1387        if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
     1388            return 0;
     1389
     1390        return BAD_MUTEX_E;
     1391    }
     1392
     1393    int wc_UnLockMutex(wolfSSL_Mutex* m)
     1394    {
     1395        if (NU_Release_Semaphore(m) == NU_SUCCESS)
     1396            return 0;
     1397
     1398        return BAD_MUTEX_E;
     1399    }
     1400
    11721401#else
    11731402    #warning No mutex handling defined
     
    11751404#endif
    11761405
     1406#ifndef NO_ASN_TIME
     1407#if defined(_WIN32_WCE)
     1408time_t windows_time(time_t* timer)
     1409{
     1410    SYSTEMTIME     sysTime;
     1411    FILETIME       fTime;
     1412    ULARGE_INTEGER intTime;
     1413    time_t         localTime;
     1414
     1415    if (timer == NULL)
     1416        timer = &localTime;
     1417
     1418    GetSystemTime(&sysTime);
     1419    SystemTimeToFileTime(&sysTime, &fTime);
     1420
     1421    XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
     1422    /* subtract EPOCH */
     1423    intTime.QuadPart -= 0x19db1ded53e8000;
     1424    /* to secs */
     1425    intTime.QuadPart /= 10000000;
     1426    *timer = (time_t)intTime.QuadPart;
     1427
     1428    return *timer;
     1429}
     1430#endif /*  _WIN32_WCE */
     1431
     1432#if defined(WOLFSSL_APACHE_MYNEWT)
     1433#include "os/os_time.h"
     1434
     1435time_t mynewt_time(time_t* timer)
     1436{
     1437    time_t now;
     1438    struct os_timeval tv;
     1439    os_gettimeofday(&tv, NULL);
     1440    now = (time_t)tv.tv_sec;
     1441    if(timer != NULL) {
     1442        *timer = now;
     1443    }
     1444    return now;
     1445}
     1446#endif /* WOLFSSL_APACHE_MYNEWT */
     1447
     1448#if defined(WOLFSSL_GMTIME)
     1449struct tm* gmtime(const time_t* timer)
     1450{
     1451    #define YEAR0          1900
     1452    #define EPOCH_YEAR     1970
     1453    #define SECS_DAY       (24L * 60L * 60L)
     1454    #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
     1455    #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
     1456
     1457    static const int _ytab[2][12] =
     1458    {
     1459        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
     1460        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
     1461    };
     1462
     1463    static struct tm st_time;
     1464    struct tm* ret = &st_time;
     1465    time_t secs = *timer;
     1466    unsigned long dayclock, dayno;
     1467    int year = EPOCH_YEAR;
     1468
     1469    dayclock = (unsigned long)secs % SECS_DAY;
     1470    dayno    = (unsigned long)secs / SECS_DAY;
     1471
     1472    ret->tm_sec  = (int) dayclock % 60;
     1473    ret->tm_min  = (int)(dayclock % 3600) / 60;
     1474    ret->tm_hour = (int) dayclock / 3600;
     1475    ret->tm_wday = (int) (dayno + 4) % 7;        /* day 0 a Thursday */
     1476
     1477    while(dayno >= (unsigned long)YEARSIZE(year)) {
     1478        dayno -= YEARSIZE(year);
     1479        year++;
     1480    }
     1481
     1482    ret->tm_year = year - YEAR0;
     1483    ret->tm_yday = (int)dayno;
     1484    ret->tm_mon  = 0;
     1485
     1486    while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
     1487        dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
     1488        ret->tm_mon++;
     1489    }
     1490
     1491    ret->tm_mday  = (int)++dayno;
     1492    ret->tm_isdst = 0;
     1493
     1494    return ret;
     1495}
     1496#endif /* WOLFSSL_GMTIME */
     1497
     1498
     1499#if defined(HAVE_RTP_SYS)
     1500#define YEAR0          1900
     1501
     1502struct tm* rtpsys_gmtime(const time_t* timer)       /* has a gmtime() but hangs */
     1503{
     1504    static struct tm st_time;
     1505    struct tm* ret = &st_time;
     1506
     1507    DC_RTC_CALENDAR cal;
     1508    dc_rtc_time_get(&cal, TRUE);
     1509
     1510    ret->tm_year  = cal.year - YEAR0;       /* gm starts at 1900 */
     1511    ret->tm_mon   = cal.month - 1;          /* gm starts at 0 */
     1512    ret->tm_mday  = cal.day;
     1513    ret->tm_hour  = cal.hour;
     1514    ret->tm_min   = cal.minute;
     1515    ret->tm_sec   = cal.second;
     1516
     1517    return ret;
     1518}
     1519
     1520#endif /* HAVE_RTP_SYS */
     1521
     1522
     1523#if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
     1524
     1525/*
     1526 * time() is just a stub in Microchip libraries. We need our own
     1527 * implementation. Use SNTP client to get seconds since epoch.
     1528 */
     1529time_t pic32_time(time_t* timer)
     1530{
     1531#ifdef MICROCHIP_TCPIP_V5
     1532    DWORD sec = 0;
     1533#else
     1534    uint32_t sec = 0;
     1535#endif
     1536    time_t localTime;
     1537
     1538    if (timer == NULL)
     1539        timer = &localTime;
     1540
     1541#ifdef MICROCHIP_MPLAB_HARMONY
     1542    sec = TCPIP_SNTP_UTCSecondsGet();
     1543#else
     1544    sec = SNTPGetUTCSeconds();
     1545#endif
     1546    *timer = (time_t) sec;
     1547
     1548    return *timer;
     1549}
     1550
     1551#endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
     1552
     1553#if defined(MICRIUM)
     1554
     1555time_t micrium_time(time_t* timer)
     1556{
     1557    CLK_TS_SEC sec;
     1558
     1559    Clk_GetTS_Unix(&sec);
     1560
     1561    if (timer != NULL)
     1562        *timer = sec;
     1563
     1564    return (time_t) sec;
     1565}
     1566
     1567#endif /* MICRIUM */
     1568
     1569#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
     1570
     1571time_t mqx_time(time_t* timer)
     1572{
     1573    time_t localTime;
     1574    TIME_STRUCT time_s;
     1575
     1576    if (timer == NULL)
     1577        timer = &localTime;
     1578
     1579    _time_get(&time_s);
     1580    *timer = (time_t) time_s.SECONDS;
     1581
     1582    return *timer;
     1583}
     1584
     1585#endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
     1586
     1587
     1588#if defined(WOLFSSL_TIRTOS)
     1589
     1590time_t XTIME(time_t * timer)
     1591{
     1592    time_t sec = 0;
     1593
     1594    sec = (time_t) Seconds_get();
     1595
     1596    if (timer != NULL)
     1597        *timer = sec;
     1598
     1599    return sec;
     1600}
     1601
     1602#endif /* WOLFSSL_TIRTOS */
     1603
     1604#if defined(WOLFSSL_XILINX)
     1605#include "xrtcpsu.h"
     1606
     1607time_t XTIME(time_t * timer)
     1608{
     1609    time_t sec = 0;
     1610    XRtcPsu_Config* con;
     1611    XRtcPsu         rtc;
     1612
     1613    con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
     1614    if (con != NULL) {
     1615        if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
     1616            sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
     1617        }
     1618        else {
     1619            WOLFSSL_MSG("Unable to initialize RTC");
     1620        }
     1621    }
     1622
     1623    if (timer != NULL)
     1624        *timer = sec;
     1625
     1626    return sec;
     1627}
     1628
     1629#endif /* WOLFSSL_XILINX */
     1630#endif /* !NO_ASN_TIME */
     1631
     1632#ifndef WOLFSSL_LEANPSK
     1633char* mystrnstr(const char* s1, const char* s2, unsigned int n)
     1634{
     1635    unsigned int s2_len = (unsigned int)XSTRLEN(s2);
     1636
     1637    if (s2_len == 0)
     1638        return (char*)s1;
     1639
     1640    while (n >= s2_len && s1[0]) {
     1641        if (s1[0] == s2[0])
     1642            if (XMEMCMP(s1, s2, s2_len) == 0)
     1643                return (char*)s1;
     1644        s1++;
     1645        n--;
     1646    }
     1647
     1648    return NULL;
     1649}
     1650#endif
     1651
     1652/* custom memory wrappers */
     1653#ifdef WOLFSSL_NUCLEUS_1_2
     1654
     1655    /* system memory pool */
     1656    extern NU_MEMORY_POOL System_Memory;
     1657
     1658    void* nucleus_malloc(unsigned long size, void* heap, int type)
     1659    {
     1660        STATUS status;
     1661        void*  stack_ptr;
     1662
     1663        status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
     1664                                    NU_NO_SUSPEND);
     1665        if (status == NU_SUCCESS) {
     1666            return 0;
     1667        } else {
     1668            return stack_ptr;
     1669        }
     1670    }
     1671
     1672    void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
     1673    {
     1674        STATUS     status;
     1675        DM_HEADER* old_header;
     1676        word32     old_size, copy_size;
     1677        void*      new_mem;
     1678
     1679        /* if ptr is NULL, behave like malloc */
     1680        new_mem = nucleus_malloc(size, NULL, 0);
     1681        if (new_mem == 0 || ptr == 0) {
     1682            return new_mem;
     1683        }
     1684
     1685        /* calculate old memory block size */
     1686        /* mem pointers stored in block headers (ref dm_defs.h) */
     1687        old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
     1688        old_size   = (byte*)old_header->dm_next_memory - (byte*)ptr;
     1689
     1690        /* copy old to new */
     1691        if (old_size < size) {
     1692            copy_size = old_size;
     1693        } else {
     1694            copy_size = size;
     1695        }
     1696        XMEMCPY(new_mem, ptr, copy_size);
     1697
     1698        /* free old */
     1699        nucleus_free(ptr, NULL, 0);
     1700
     1701        return new_mem;
     1702    }
     1703
     1704    void nucleus_free(void* ptr, void* heap, int type)
     1705    {
     1706        if (ptr != NULL)
     1707            NU_Deallocate_Memory(ptr);
     1708    }
     1709
     1710#endif /* WOLFSSL_NUCLEUS_1_2 */
    11771711
    11781712#if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
Note: See TracChangeset for help on using the changeset viewer.