CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Large array size calculations

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Large array size calculations
PostPosted: Mon Mar 14, 2016 5:31 pm     Reply with quote

Hi All,

How do i get the size of a large array?
I'm trying to get the length of a
Code:
Const Char Array[]


I'm trying to Store a CA Certificate (for SSL/TSL) in a Modem which by manual count is 1243 characters long.

STRLEN is not handling it well.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Mar 14, 2016 5:39 pm     Reply with quote

I assume we are not talking "sizeof" here -
SO what you mean is : how many active bytes actually stored in it ??
Why are you bothering with STRLEN?

what confuses me a lot is that its a
CONST -
SO how do you not know how many chars were in it at compile time??

its not like it's going to change in runtime is it ??? Very Happy Very Happy Very Happy

i don't get the question.........
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Mar 14, 2016 5:43 pm     Reply with quote

Trying to figure the size of this:


Code:
const char CA_Certificate[]="-----BEGIN CERTIFICATE-----\r\
MIIDWzCCAsSgAwIBAgIKFeQVggADAAA7NjANBgkqhkiG9w0BAQUFADBGMQswCQYD\r\
VQQGEwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzEiMCAGA1UEAxMZR29vZ2xlIElu\r\
dGVybmV0IEF1dGhvcml0eTAeFw0xMTExMTgwMTU3MTdaFw0xMjExMTgwMjA3MTda\r\
MGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1N\r\
b3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRcwFQYDVQQDEw5zbXRw\r\
LmdtYWlsLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAuK+t5ZRq6c3K\r\
kWPwLuIcPa6DgiBURaQK9akP4OBoXKJ6bqYIQWsS4C3RgnOaGaDENadxHSNZ5Qpl\r\
Vqg2S54N54SM5OXwOq0NtrqdlbhgigB53TZouiJvnLDxxIexSOn2Gx1qyZF2z8Ii\r\
MoUhHuStWgW5YoOHje8z6K9xQdYkQp0CAwEAAaOCASwwggEoMB0GA1UdDgQWBBTs\r\
OL4jbtJ5l8B6/eoEvv30KEiTrjAfBgNVHSMEGDAWgBS/wDDr9UMRPme6npH7/Gra\r\
42sSJDBbBgNVHR8EVDBSMFCgTqBMhkpodHRwOi8vd3d3LmdzdGF0aWMuY29tL0dv\r\
b2dsZUludGVybmV0QXV0aG9yaXR5L0dvb2dsZUludGVybmV0QXV0aG9yaXR5LmNy\r\
bDBmBggrBgEFBQcBAQRaMFgwVgYIKwYBBQUHMAKGSmh0dHA6Ly93d3cuZ3N0YXRp\r\
Yy5jb20vR29vZ2xlSW50ZXJuZXRBdXRob3JpdHkvR29vZ2xlSW50ZXJuZXRBdXRo\r\
b3JpdHkuY3J0MCEGCSsGAQQBgjcUAgQUHhIAVwBlAGIAUwBlAHIAdgBlAHIwDQYJ\r\
KoZIhvcNAQEFBQADgYEAQiMlHuQLRFqR10UsSg5WTNe3vagbdnBLAkdhvAf90B5a\r\
9beBxJH2/ylTSIGfD2uceAqzcsQe6Ouy4C9r3rz86qA1dhdtIcPg6uoZb+E2qhE5\r\
UaOJOPO4rHInX9kscBxh+baHbpBMh+ch6v5L8plss8hd0id8C4g10YKzwcgPYlQ=\r\
-----END CERTIFICATE-----";

_________________
CCS PCM 5.078 & CCS PCH 5.093
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Mar 14, 2016 6:28 pm     Reply with quote

1 row of 30
18 rows of 66
1 row of 24

1134 bytes is what i get

i use MultiEdit as my main programming editor and it uses
constant character cells on screen so its awfully easy to
size up a bunch of text like this....
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Mon Mar 14, 2016 8:53 pm     Reply with quote

If there's a chance that the certificate could grow in size in future, set aside 2k for the array (or perhaps even 4k).

As far as getting the size, when you populate the array ensure that the end is null terminated (you could pre-zero the entire array before it's populated). Write your own function to return the size - just count up through the array until you hit a null, and that's your size.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 15, 2016 4:50 am     Reply with quote

Hi all thanks for the help...
I'll write my own length counter, with black jack and hookers.

YES i know the size at compile time but i do not know if this is the actual/final certificate i need.... this is supposed to be Gmail's.
Id like to write code that in the future i can just drop the certificate in and not have to first count chars on a text editor. I'm sure this is not the last time SSL/TSL will be needed.

I'm trying to do SMTP emails from a telit modem via gmail but gmail requires encryption.

But there is CERO information on the web on how to do SSL/TSL SMTP emails via GPRS.... and ive been trying to load this damn certificate for days and the modem rejects it..... the length function will guarantee (i hope) that at least I'm getting that part right.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 15, 2016 5:01 am     Reply with quote

Ive found that most internet related stuff is very picky when it comes to sizes of strings sent... ESP8266, GPRS modems, HTTP requests.... and some modems need the "size" with or without the GET part of the command others dont....a lot of trial and error... 100s of compile test cycles.

Point is, having the right SIZE Is important.

I have the SMTP mail code done...(pending this certificate issue) i can talk to googles and hotmails servers.... as many other people on the web but NO ONE on the net has posted any info on this.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19492

View user's profile Send private message

PostPosted: Tue Mar 15, 2016 10:31 am     Reply with quote

Both sizeof, and strlen can solve this 'out of the box'.
Both give 0x4EE for the posted array. 1262 bytes. Remember the '\' at the end of each line is not part of the array.
A string is automatically null terminated for you.

Why go more complex?. Remember this is one more than the actual number of characters (since both include the null terminator). The advantage of sizeof, if that since it is done at compile time, 0 code cost/time cost.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Mar 15, 2016 10:53 am     Reply with quote

Gabriel wrote:
Trying to figure the size of this:
Code:
const char CA_Certificate[]="-----BEGIN CERTIFICATE-----\r\
MIIDWzCCAsSgAwIBAgIKFeQVggADAAA7NjANBgkqhkiG9w0BAQUFADBGMQswCQYD\r\


Hmm, I'd be wary of the carriage returns. On their own they are not a commonly used line separator, and I'd be wary wherever,whenever they were used. This could be messing up what ever you're sending this to.

For single character separators, it would be more common to use newline, \n, which was the Unix, and its many relatives, standard. DOS derived systems, which includes all Windows, tend to use carriage return-newline combos, \r\n instead. In either case receiving systems can ignore the \r, i.e. treat it as whitespace, and look for the newlines. No newlines and, oh dear, no lines. And not the other way round as we sometimes see, \n\r, that generally ends the line and puts whitespace at the beginning of the next: again generally NOT what is required.
Maybe... or not. Worth thinking about anyway.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 15, 2016 12:19 pm     Reply with quote

Quote:
not a commonly used line separator,


i think that varies more than you think.

The default in MY general area of endeavor is to use naked \r, ASC13/0xD as the 'bang' at a line end w/o a pesky ASC10/0xA LF. I have two life science clients for whom my VCOM connected gadgets ONLY use the naked \r for termination....... On the receiving end of a command stream -
my parsers "swallow" ASC10 as if it was never sent and ONLY send it if talking to a brain-dead device that NEEDS it. Nearly ALL early terminals also treat ASC10 exclusively as a cursor position character -used ONLY when feeding a physical printer or putting a cursor back to the beginning of a line.

In the device to device command area,
IBM format spec files from the dawn of "terminal time" for things like P1 and P2 simple serial command protocol, say things like this:

Quote:
In ASCII, carriage return/line feed is X'0D'/X'0A'. X'0D is the command terminator. The X'0A character function is optional, and user defined.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 15, 2016 9:06 pm     Reply with quote

STRLEN was giving me 218.... and some how i concluded it must be limited to 8 bits? (Im using 18F)
Maybe i was doing something wrong.... ill try again.

Ive tried with \n, \r\n, \r.... just a \, and nothing.... neither termination has worked...(accepted by the modem)

As for the termination debate i am a '\r' and only use \n for pretty screens....atleast lately.

I have noticed a blank space before all the lines on the modems echo.... ill see if RF's comments shed any light on this.

Thabks again
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19492

View user's profile Send private message

PostPosted: Wed Mar 16, 2016 2:53 am     Reply with quote

Honestly sizeof is much better. This will automatically recalculate if you change the size of the array, and needs zero code.
How are you 'looking' at strlen's answer?.
If using a debugger, probably it thinks the value is int8. Or the printout may be wrongly typed and pulling the int8 part of the result.
Also, what compiler version?. Could quite believe an old compiler (just after CCS started allowing >255 byte constants for example), might get this wrong.
Merrily gives int16 result for me.

UPDATED:

I see you actually say 5.012. This was one of the first reasonably working V5 versions, so might well get this wrong.

Simple 'replacement' that should work:
Code:

int16 ctr;

#define LENGTH(array) ctr=0; while (array[ctr++] != '\0')


used as "LENGTH(CA_Certificate);"

Will give the length in ctr.

Key thing is this will work like strlen, but with a constant array, while strlen will require you to be using 'PASS_STRINGS=IN_RAM', or ANSI mode to work. It should however complain with 'attempt to create a pointer to a constant', if you use it without these options, rather than giving a wrong result though. I'd suspect your version is faulty here and is not detecting that the string is a constant....

This will give the 4EE length (including the NULL at the end), but saves an enormous amount of RAM over using strlen.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group