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 support@ccsinfo.com

Having trouble with ANSI C code in CCS

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



Joined: 08 Sep 2014
Posts: 20

View user's profile Send private message

Having trouble with ANSI C code in CCS
PostPosted: Sun May 10, 2015 3:55 am     Reply with quote

Hello,
I am having trouble with an ansi c code to encrypt/decrypt aes. The code can be found in https://github.com/kokke/tiny-AES128-C . I got it compiled in computer using GCC, but I am having lots of problems in CCS C and when I try to fix I get in the aes test failure. Here are the problems:

In file aes.h, I get error here:
Code:
void AES128_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);

The error is in the last two variables with const. My patch is to remove the const modifier:
Code:
void AES128_CBC_encrypt_buffer (uint8_t* output, uint8_t* input, uint32_t length, uint8_t* key, uint8_t* iv);


Then in aes. c I get errors in:

Function ShiftRows
When rotating rows to the left in all the code like this:

Code:
(*state)[0][1] = (*state)[1][1];


I get error in this part
Code:
(*state)[0][1]
, getting LVALUE error.

I fix it with the next code:

Code:
 uint8_t second_row_value = 1;

  // Rotate first row 1 columns to right 
  temp=(*state)[3][1];
  (*state)[3][second_row_value]=(*state)[2][1];
  (*state)[2][second_row_value]=(*state)[1][1];
  (*state)[1][second_row_value]=(*state)[0][1];
  (*state)[0][second_row_value]=temp;


Also I am having with the type intptr_t which ccs doesn't recognize. What type could I replace it?

I am using ccs 5.025

Thanks for helping
temtronic



Joined: 01 Jul 2010
Posts: 9196
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 10, 2015 5:09 am     Reply with quote

You need to understand that CCS C is NOT ANSI C. You can't just grab an ANSI C program or function and assume it will work in CCS C thinking that C is C. Since V2.534 I've understood this especially since I've never coded anything in 'ANSI C' and have been programming since '74.

Your first error deals with the fact that in CCS C 'const' is a reserved word, hence it must be used in a specific way. Just consult the manual and you'll see how it's to be used. Every language has 'reserved' words,syntax, limits, etc. That's why is important to read the manual,look at the CCS supplied examples as well, There is a LOT of knowledge i them !!

jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 10, 2015 10:18 am     Reply with quote

Have you tried adding #device ANSI, as shown below ? That will fix some
of the errors:
Code:

#include <18F4620.h>
#device ANSI   // *** Add this line
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)


Quote:
The error is in the last two variables with const. My patch is to remove the const modifier:

#device ANSI will fix that one. It will make 'const' mean read-only
instead of "put it in ROM".


Quote:
Then in aes. c I get errors in:
Function ShiftRows
When rotating rows to the left in all the code like this:
(*state)[0][1] = (*state)[1][1];

#device ANSI won't fixt that one.
Ttelmah



Joined: 11 Mar 2010
Posts: 19430

View user's profile Send private message

PostPosted: Sun May 10, 2015 12:19 pm     Reply with quote

typedef int16 *intptr_t;

CCS with the ANSI declaration, is 99% compatible with ANSI-C90. However the intptr types, were only added with ANSI-99. A lot later....

'ANSI', covers (now) several versions, with their own oddities, and extras.

CCS have made the compiler get close to matching the original version (as described by K&R in the second edition of the 'C programming language', with some later features, but you still need to tidy up such 'later' features.
ercarlitosg



Joined: 08 Sep 2014
Posts: 20

View user's profile Send private message

PostPosted: Mon May 11, 2015 1:45 am     Reply with quote

Thanks for your help

Do you know how to fix this?

Code:

(*state)[0][1] = (*state)[1][1];


This works well on gcc compiler.
Ttelmah



Joined: 11 Mar 2010
Posts: 19430

View user's profile Send private message

PostPosted: Mon May 11, 2015 1:45 am     Reply with quote

I just compiled the code. Built a function to call it all with the CCS setups, and ANSI mode selected. Added the typedef for intptr_t, and made just one change to the code. Wherever *state is used, I replaced this with an explicit cast, so *(state_t *)state. I've noticed before that CCS sometimes seems to 'forget' what a pointer actually points 'to', when it to something like an array.
Obviously changed the main so it never exits, and changed it's declaration to void.

Two lines give warnings:
tempa[0] = tempa[0] ^ Rcon[i/Nk];

Which a simple bracket fixes:
tempa[0] = tempa[0] ^ (Rcon[i/Nk]);

and one logic test:
else if (Nk > 6 && i % Nk == 4)

Which gives a warning 'always FALSE'. Nk, is #defined as '4', so the compiler is right, this _will_ always return FALSE.

Whether it runs is another matter....

I'd suspect the main problem will be the functions to rotate the arrays. Honestly better to rewrite these perhaps using memcpy, indirect pointer accesses like this are very inefficient in the PIC.....
ercarlitosg



Joined: 08 Sep 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu May 14, 2015 5:45 am     Reply with quote

Ttelmah wrote:
I just compiled the code. Built a function to call it all with the CCS setups, and ANSI mode selected. Added the typedef for intptr_t, and made just one change to the code. Wherever *state is used, I replaced this with an explicit cast, so *(state_t *)state. I've noticed before that CCS sometimes seems to 'forget' what a pointer actually points 'to', when it to something like an array.
Obviously changed the main so it never exits, and changed it's declaration to void.

Two lines give warnings:
tempa[0] = tempa[0] ^ Rcon[i/Nk];

Which a simple bracket fixes:
tempa[0] = tempa[0] ^ (Rcon[i/Nk]);

and one logic test:
else if (Nk > 6 && i % Nk == 4)

Which gives a warning 'always FALSE'. Nk, is #defined as '4', so the compiler is right, this _will_ always return FALSE.

Whether it runs is another matter....

I'd suspect the main problem will be the functions to rotate the arrays. Honestly better to rewrite these perhaps using memcpy, indirect pointer accesses like this are very inefficient in the PIC.....


Thanks, but I moved to other implementation and this works quite well.
Many Thanks
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