Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:13+00:00 2026-05-28T05:22:13+00:00

In C, I have a function that implements both the encryption and decryption routines

  • 0

In C, I have a function that implements both the encryption and decryption routines of a block cipher. In order to both maintain a common naming and use convention, and to leave open the possibility of separating the routines into two different functions later, I’ve done the following:

void cipher(char *out, const char *in);
#define encrypt    cipher
#define decrypt    cipher

That works fine, except that I’d really like to hide the actual function (cipher) so people have to use encrypt or decrypt. Right now, cipher is part of the public interface, so if I decide to separate it into two different functions later and delete cipher, strictly speaking, I’m breaking the interface. But if I can hide cipher so only encrypt and decrypt are part of the interface, I’ll be fine.

The only option I’ve come up with so far is make cipher static, and implement actual functions for encrypt an decrypt to call cipher, but I’m not sure that the added overhead is actually worth it (I’m trying to keep the code size as tight as possible, and I have multiple occurrences of this same problem).

Is there something I can do with function pointers? Any other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:22:14+00:00Added an answer on May 28, 2026 at 5:22 am

    You could use function pointers:

    static void cipher(...);
    
    void (*encrypt)(...) = cipher;
    void (*decrypt)(...) = cipher;
    

    At least in typical use (the user just uses encrypt(whatever);) this wouldn’t normally be visible. The only obvious problem would be that as defined above, the pointers remain writable, so you might want to make them const so the user can’t accidentally overwrite them with the address of some other function.

    Another possibility would be to live with the name cipher being public (or rename it to something like private_cipher_ to avoid accidental name collisions) and then just use a couple of macros:

    #define encrypt(x, y) private_cipher_((x), (y))
    #define decrypt(x, y) private_cipher_((x), (y))
    

    This should ensure against any overhead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I use called sqlf(), it emulates prepared statements. For
I have the following code that implements 2 jQuery sliders : <script type=text/javascript> $(document).ready(function
I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward
If I have a subclass that has yet to implement a function provided by
If I have a function foo() that windows has implemented in kernel32.dll and it
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.