Skip to content

Commit 27f4a6c

Browse files
earlephilhowerigrr
authored andcommitted
Save ~3KB by moving strings out of RODATA
Constant text strings actually take up SRAM space on the ESP8266 because the .RODATA segment must be copied to RAM at startup since FLASH isn't byte-accessible. Move the constant format strings in a printf completely into FLASH and add a wrapper to copy it into a local stack-allocated space when needed, freeing up about 3100 bytes of RAM for use. This doesn't make FLASH usage any higher, either, since those strings were already being stored there (but never used after the power-on startup code). Minor edits required in some of the output/debug/tracing functions, but no logic changed.
1 parent 1b2c299 commit 27f4a6c

File tree

4 files changed

+68
-37
lines changed

4 files changed

+68
-37
lines changed

ssl/crypto_misc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
127127
#endif
128128
#ifdef CONFIG_SSL_FULL_MODE
129129
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx);
130-
const char * x509_display_error(int error);
130+
const char * x509_display_error(int error, char *buff);
131131
#endif
132132

133133
/**************************************************************************

ssl/os_port.h

+21-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ extern "C" {
6868
#undef putc
6969
#endif
7070
#define putc(x, f) ets_putc(x)
71-
#ifdef printf
72-
#undef printf
73-
#endif
74-
#define printf(...) ets_printf(__VA_ARGS__)
7571

7672
#define SOCKET_READ(A,B,C) ax_port_read(A,B,C)
7773
#define SOCKET_WRITE(A,B,C) ax_port_write(A,B,C)
@@ -123,6 +119,27 @@ static inline uint8_t pgm_read_byte(const void* addr) {
123119
#define ax_array_read_u8(x, y) pgm_read_byte((x)+(y))
124120
#endif //WITH_PGM_READ_HELPER
125121

122+
#ifdef printf
123+
#undef printf
124+
#endif
125+
//#define printf(...) ets_printf(__VA_ARGS__)
126+
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
127+
#define PGM_VOID_P const void *
128+
static inline void* memcpy_P(void* dest, PGM_VOID_P src, size_t count) {
129+
const uint8_t* read = (const uint8_t*)(src);
130+
uint8_t* write = (uint8_t*)(dest);
131+
132+
while (count)
133+
{
134+
*write++ = pgm_read_byte(read++);
135+
count--;
136+
}
137+
138+
return dest;
139+
}
140+
#define printf(fmt, ...) do { static const char fstr[] PROGMEM = fmt; char rstr[sizeof(fmt)]; memcpy_P(rstr, fstr, sizeof(rstr)); ets_printf(rstr, ##__VA_ARGS__); } while (0)
141+
#define strcpy_P(dst, src) do { static const char fstr[] PROGMEM = src; memcpy_P(dst, fstr, sizeof(src)); } while (0)
142+
126143
#elif defined(WIN32)
127144

128145
/* Windows CE stuff */

ssl/tls1.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -2287,58 +2287,57 @@ void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok)
22872287
if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
22882288
return;
22892289

2290-
printf(not_ok ? "Error - invalid State:\t" : "State:\t");
2291-
printf(is_send ? "sending " : "receiving ");
2290+
if (not_ok) printf("Error - invalid State:\t");
2291+
else printf("State:\t");
2292+
if (is_send) printf("sending ");
2293+
else printf("receiving ");
22922294

22932295
switch (state)
22942296
{
22952297
case HS_HELLO_REQUEST:
2296-
str = "Hello Request (0)";
2298+
printf("Hello Request (0)\n");
22972299
break;
22982300

22992301
case HS_CLIENT_HELLO:
2300-
str = "Client Hello (1)";
2302+
printf("Client Hello (1)\n");
23012303
break;
23022304

23032305
case HS_SERVER_HELLO:
2304-
str = "Server Hello (2)";
2306+
printf("Server Hello (2)\n");
23052307
break;
23062308

23072309
case HS_CERTIFICATE:
2308-
str = "Certificate (11)";
2310+
printf("Certificate (11)\n");
23092311
break;
23102312

23112313
case HS_SERVER_KEY_XCHG:
2312-
str = "Certificate Request (12)";
2314+
printf("Certificate Request (12)\n");
23132315
break;
23142316

23152317
case HS_CERT_REQ:
2316-
str = "Certificate Request (13)";
2318+
printf("Certificate Request (13)\n");
23172319
break;
23182320

23192321
case HS_SERVER_HELLO_DONE:
2320-
str = "Server Hello Done (14)";
2322+
printf("Server Hello Done (14)\n");
23212323
break;
23222324

23232325
case HS_CERT_VERIFY:
2324-
str = "Certificate Verify (15)";
2326+
printf("Certificate Verify (15)\n");
23252327
break;
23262328

23272329
case HS_CLIENT_KEY_XCHG:
2328-
str = "Client Key Exchange (16)";
2330+
printf("Client Key Exchange (16)\n");
23292331
break;
23302332

23312333
case HS_FINISHED:
2332-
str = "Finished (16)";
2334+
printf("Finished (16)\n");
23332335
break;
23342336

23352337
default:
2336-
str = "Error (Unknown)";
2337-
2338+
printf("Error (Unknown)\n");
23382339
break;
23392340
}
2340-
2341-
printf("%s\n", str);
23422341
}
23432342

23442343
/**
@@ -2383,7 +2382,8 @@ EXP_FUNC void STDCALL ssl_display_error(int error_code)
23832382
/* X509 error? */
23842383
if (error_code < SSL_X509_OFFSET)
23852384
{
2386-
printf("%s\n", x509_display_error(error_code - SSL_X509_OFFSET));
2385+
char buff[64];
2386+
printf("%s\n", x509_display_error(error_code - SSL_X509_OFFSET, buff));
23872387
return;
23882388
}
23892389

ssl/x509.c

+29-15
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
225225
if (ret)
226226
{
227227
#ifdef CONFIG_SSL_FULL_MODE
228+
char buff[64];
228229
printf("Error: Invalid X509 ASN.1 file (%s)\n",
229-
x509_display_error(ret));
230+
x509_display_error(ret, buff));
230231
#endif
231232
x509_free(x509_ctx);
232233
*ctx = NULL;
@@ -821,9 +822,10 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
821822
if (ca_cert_ctx)
822823
{
823824
int pathLenConstraint = 0;
825+
char buff[64];
824826
printf("Verify:\t\t\t\t%s\n",
825827
x509_display_error(x509_verify(ca_cert_ctx, cert,
826-
&pathLenConstraint)));
828+
&pathLenConstraint), buff));
827829
}
828830

829831
#if 0
@@ -840,45 +842,57 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
840842
TTY_FLUSH();
841843
}
842844

843-
const char * x509_display_error(int error)
845+
const char * x509_display_error(int error, char *buff)
844846
{
845847
switch (error)
846848
{
847849
case X509_OK:
848-
return "Certificate verify successful";
850+
strcpy_P(buff, "Certificate verify successful");
851+
return buff;
849852

850853
case X509_NOT_OK:
851-
return "X509 not ok";
854+
strcpy_P(buff, "X509 not ok");
855+
return buff;
852856

853857
case X509_VFY_ERROR_NO_TRUSTED_CERT:
854-
return "No trusted cert is available";
858+
strcpy_P(buff, "No trusted cert is available");
859+
return buff;
855860

856861
case X509_VFY_ERROR_BAD_SIGNATURE:
857-
return "Bad signature";
862+
strcpy_P(buff, "Bad signature");
863+
return buff;
858864

859865
case X509_VFY_ERROR_NOT_YET_VALID:
860-
return "Cert is not yet valid";
866+
strcpy_P(buff, "Cert is not yet valid");
867+
return buff;
861868

862869
case X509_VFY_ERROR_EXPIRED:
863-
return "Cert has expired";
870+
strcpy_P(buff, "Cert has expired");
871+
return buff;
864872

865873
case X509_VFY_ERROR_SELF_SIGNED:
866-
return "Cert is self-signed";
874+
strcpy_P(buff, "Cert is self-signed");
875+
return buff;
867876

868877
case X509_VFY_ERROR_INVALID_CHAIN:
869-
return "Chain is invalid (check order of certs)";
878+
strcpy_P(buff, "Chain is invalid (check order of certs)");
879+
return buff;
870880

871881
case X509_VFY_ERROR_UNSUPPORTED_DIGEST:
872-
return "Unsupported digest";
882+
strcpy_P(buff, "Unsupported digest");
883+
return buff;
873884

874885
case X509_INVALID_PRIV_KEY:
875-
return "Invalid private key";
886+
strcpy_P(buff, "Invalid private key");
887+
return buff;
876888

877889
case X509_VFY_ERROR_BASIC_CONSTRAINT:
878-
return "Basic constraint invalid";
890+
strcpy_P(buff, "Basic constraint invalid");
891+
return buff;
879892

880893
default:
881-
return "Unknown";
894+
strcpy_P(buff, "Unknown");
895+
return buff;
882896
}
883897
}
884898
#endif /* CONFIG_SSL_FULL_MODE */

0 commit comments

Comments
 (0)