XLinkStringUtils.c 4.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
* Copyright 2017-2019 Intel Corporation.
* The source code, information and material ("Material") contained herein is
* owned by Intel Corporation or its suppliers or licensors, and title to such
* Material remains with Intel Corporation or its suppliers or licensors.
* The Material contains proprietary information of Intel or its suppliers and
* licensors. The Material is protected by worldwide copyright laws and treaty
* provisions.
* No part of the Material may be used, copied, reproduced, modified, published,
* uploaded, posted, transmitted, distributed or disclosed in any way without
* Intel's prior express written permission. No license under any patent,
* copyright or other intellectual property rights in the Material is granted to
* or conferred upon you, either expressly, by implication, inducement, estoppel
* or otherwise.
* Any license under such intellectual property rights must be express and
* approved by Intel in writing.
*/
18

19
#include "XLinkStringUtils.h"
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

int mv_strcpy(char *dest, size_t destsz, const char *src) {
   const char *overlap_bumper;

   if (dest == NULL) {
       return ESNULLP;
   }
   if (destsz == 0) {
       return ESZEROL;
   }
   if (destsz > RSIZE_MAX_STR) {
       return ESLEMAX;
   }
   if (src == NULL) {
       while (destsz) {
           *dest = '\0';
           destsz--;
           dest++;
       }
       return ESNULLP;
   }
   // It’s ok if src and dest are in the same place. No action is needed.
   if (dest == src) {
       return EOK;
   }

   if (dest < src) {
       overlap_bumper = src;

       while (destsz > 0) {
           if (dest == overlap_bumper) {
               return ESOVRLP;
           }

           *dest = *src;
           if (*dest == '\0') {
               while (destsz) {
                   *dest = '\0';
                   destsz--;
                   dest++;
               }
               return EOK;
           }
           destsz--;
           dest++;
           src++;
       }
   } else {
       overlap_bumper = dest;

       while (destsz > 0) {
           if (src == overlap_bumper) {
               return ESOVRLP;
           }

           *dest = *src;
           if (*dest == '\0') {
               while (destsz) {
                   *dest = '\0';
                   destsz--;
                   dest++;
               }
               return EOK;
           }
           destsz--;
           dest++;
           src++;
       }
   }

   // Ran out of space in dest, and did not find the null terminator in src.
   return ESNOSPC;
}

int mv_strncpy(char *dest, size_t destsz, const char *src, size_t count) {
    if (dest == NULL) {
        return ESNULLP;
    }
    if (src == NULL) {
        while (destsz) {
            *dest = '\0';
            destsz--;
            dest++;
        }
        return ESNULLP;
    }
    if (destsz == 0) {
        return ESZEROL;
    }
    if (destsz > RSIZE_MAX_STR || count > RSIZE_MAX_STR) {
        return ESLEMAX;
    }
    if (destsz < (count + 1)) {
        dest[0] = '\0';
        return ESNOSPC;
    }
    if (((src  < dest) && ((src + destsz) >= dest)) ||
            ((dest < src)  && ((dest + destsz) >= src))) {
        dest[0] = '\0';
        return ESOVRLP;
    }

    if (dest == src) {
        while (destsz > 0) {
            if (*dest == '\0') {
                // Add nulls to complete dest.
                while (destsz) {
                    *dest = '\0';
                    destsz--;
                    dest++;
                }
                return EOK;
            }
            destsz--;
            dest++;
            count--;
            if (count == 0) {
                // We have copied count characters, add null terminator.
                *dest = '\0';
            }
        }
        return ESNOSPC;
    }

    // Normal copying.
    while (destsz > 0) {
        // Copy data from src to dest.
        *dest = *src;

        if (count == 0) {
            // We have copied count characters, add null terminator.
            *dest = '\0';
        }

        // Check for end of copying.
        if (*dest == '\0') {
            // Add nulls to complete dest.
            while (destsz) {
                *dest = '\0';
                destsz--;
                dest++;
            }
            return EOK;
        }
        destsz--;
        count--;
        dest++;
        src++;
    }

    // Ran out of space in dest, and did not find the null terminator in src.
    return ESNOSPC;
}