asn: refactoring SetName to reduce stack usage: 1152 bytes - pointers size moved to the heap.

--- variable names moved to the heap (1152 bytes saved)
This commit is contained in:
Moisés Guimarães
2014-06-27 15:55:45 -03:00
parent 1cd81c1bb6
commit 8dd4589650

View File

@@ -5255,7 +5255,18 @@ static int SetCa(byte* output)
static int SetName(byte* output, CertName* name) static int SetName(byte* output, CertName* name)
{ {
int totalBytes = 0, i, idx; int totalBytes = 0, i, idx;
#ifdef CYASSL_SMALL_STACK
EncodedName* names = NULL;
#else
EncodedName names[NAME_ENTRIES]; EncodedName names[NAME_ENTRIES];
#endif
#ifdef CYASSL_SMALL_STACK
names = (EncodedName*)XMALLOC(sizeof(EncodedName) * NAME_ENTRIES, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (names == NULL)
return MEMORY_E;
#endif
for (i = 0; i < NAME_ENTRIES; i++) { for (i = 0; i < NAME_ENTRIES; i++) {
const char* nameStr = GetOneName(name, i); const char* nameStr = GetOneName(name, i);
@@ -5297,8 +5308,12 @@ static int SetName(byte* output, CertName* name)
setSz = SetSet(thisLen, set); setSz = SetSet(thisLen, set);
thisLen += setSz; thisLen += setSz;
if (thisLen > (int)sizeof(names[i].encoded)) if (thisLen > (int)sizeof(names[i].encoded)) {
#ifdef CYASSL_SMALL_STACK
XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return BUFFER_E; return BUFFER_E;
}
/* store it */ /* store it */
idx = 0; idx = 0;
@@ -5348,8 +5363,12 @@ static int SetName(byte* output, CertName* name)
/* header */ /* header */
idx = SetSequence(totalBytes, output); idx = SetSequence(totalBytes, output);
totalBytes += idx; totalBytes += idx;
if (totalBytes > ASN_NAME_MAX) if (totalBytes > ASN_NAME_MAX) {
#ifdef CYASSL_SMALL_STACK
XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return BUFFER_E; return BUFFER_E;
}
for (i = 0; i < NAME_ENTRIES; i++) { for (i = 0; i < NAME_ENTRIES; i++) {
if (names[i].used) { if (names[i].used) {
@@ -5357,6 +5376,11 @@ static int SetName(byte* output, CertName* name)
idx += names[i].totalLen; idx += names[i].totalLen;
} }
} }
#ifdef CYASSL_SMALL_STACK
XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return totalBytes; return totalBytes;
} }