damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

cdump.h (814B)


      1 #ifndef CDUMP_H
      2 #define CDUMP_H
      3 
      4 #ifdef __cplusplus
      5 extern "C" {
      6 #endif
      7 
      8 #include <stdio.h>
      9 
     10 /* Generates a constant a C byte array. */
     11 static void cdump(const char *name, void *addr, size_t len, FILE *fp) {
     12     unsigned int i;
     13     unsigned char *pc = (unsigned char*)addr;
     14 
     15     // Output description if given.
     16     name = name ? name : "dump";
     17     fprintf(fp, "const unsigned char %s[] = {", name);
     18 
     19     // Process every byte in the data.
     20     for (i = 0; i < (unsigned int)len; i++) {
     21         // Multiple of 16 means new line (with line offset).
     22 
     23         if ((i % 16) == 0) {
     24             fprintf(fp, "\n   ");
     25         } else if ((i % 8) == 0) {
     26             fprintf(fp, "   ");
     27         }
     28 
     29         fprintf(fp, " 0x%02x,", pc[i]);
     30     }
     31     fprintf(fp, "\n};\n");
     32 }
     33 
     34 #ifdef __cplusplus
     35 }
     36 #endif
     37 
     38 #endif /* CDUMP_H */