/** char* x2crypt(char* pass, char* bsalt) pass を bsalt で2回暗号化(ハッシュ値化)する.@n bsaltには改行コード(@\nまたは @\r@\n)で区切られた2個のsaltが入っている必要がある. @param pass ハッシュ値化するパスワード @param bsalt 改行コード(@\nまたは @\r@\n)で区切られた2個の salt. @return 2度ハッシュ値化された文字列.要 free @bug bsaltに 2個のslatが入っていなかったときの動作は? -> 不明 */ char* x2crypt(char* pass, char* bsalt) { char* cpass; char* dpass = NULL; char* csalt; if (pass==NULL || bsalt==NULL) return NULL; csalt = get_line(bsalt, 1); cpass = crypt(pass, csalt); freeNull(csalt); csalt = get_line(bsalt, 2); if (csalt==NULL) return NULL; if (strlen(cpass)==LEN_MD5PASS) { dpass = cut_str(cpass, LEN_MD5SALT, LEN_MD5PASS-1); cpass = crypt(dpass, csalt); freeNull(dpass); dpass = cut_str(cpass, LEN_MD5SALT, LEN_MD5PASS-1); } else if (strlen(cpass)==LEN_DESPASS) { dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1); cpass = crypt(dpass, csalt); freeNull(dpass); dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1); } freeNull(csalt); return dpass; }
/** void free_JPEGImage(JPEGImage* jp) */ void free_JPEGImage(JPEGImage* jp) { if (jp==NULL) return; freeNull(jp->gp); freeNull(jp->img); jp->xs = jp->ys = jp->col = 0; return; }
int msgHandler(int sock) { // Header struct serbeep_header header; if (readHeader(sock, &header) != 0) return 1; if (header.magic != MAGIC) { fprintf(stderr, "Invalid magic\n"); return 1; } // clientHello if ((header.cmd & 0x2) == 0x2) { // serverHello header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) return 1; return 0; } // musicScore if ((header.cmd & 0x4) == 0x4) { if (pthread_mutex_trylock(&global_score_mutex) != 0) return 1; if (readStruct(sock, &global_score_header, sizeof(global_score_header)) != 1) { pthread_mutex_unlock(&global_score_mutex); return 1; } global_score_header.length = ntohs(global_score_header.length); size_t size = sizeof(struct serbeep_score_note) * global_score_header.length; global_score_notes = (struct serbeep_score_note *)malloc(size); if (global_score_notes == NULL) { fprintf(stderr, "fail...malloc(%zd bytes)\n", size); pthread_mutex_unlock(&global_score_mutex); return 1; } if (readStruct(sock, global_score_notes, size) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } // musicAck header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } pthread_mutex_unlock(&global_score_mutex); return 0; } return 0; // Success }
/** JPEGImage make_JPEGImage(int xs, int ys, int col) */ JPEGImage make_JPEGImage(int xs, int ys, int col) { int j; JPEGImage jp; memset(&jp, 0, sizeof(JPEGImage)); if (xs==0 || ys==0) { jp.state = ERROR_GRAPH_IVDARG; return jp; } if (col<1) col = 3; jp.img = (JSAMPARRAY)malloc(sizeof(JSAMPROW)*ys); if (jp.img==NULL) { jp.state = ERROR_GRAPH_MEMORY; return jp; } jp.gp = (JSAMPLE*)malloc(sizeof(JSAMPLE)*col*xs*ys); if (jp.gp==NULL) { freeNull(jp.img); jp.state = ERROR_GRAPH_MEMORY; return jp; } for (j=0; j<ys; j++) { jp.img[j] = (JSAMPROW)&jp.gp[j*col*xs]; } jp.xs = xs; jp.ys = ys; jp.col = col; return jp; }
void setup_jp2k(JP2KImage* jp) { if (jp==NULL || jp->image==NULL || jp->image->comps==NULL) return; // jp->xs = jp->image->x1 - jp->image->x0; jp->ys = jp->image->y1 - jp->image->y0; int fac = (int)jp->image->comps->factor; jp->ws = (jp->xs + (1<<fac) - 1)>>fac; jp->hs = (jp->ys + (1<<fac) - 1)>>fac; jp->col = (int)jp->image->numcomps; if (jp->image->color_space==CLRSPC_SRGB) { jp->col = 3; } else if (jp->image->color_space==CLRSPC_GRAY) { jp->col = 1; } /* #if OPENJPEG_VER > JP2K_VER_12 else if (jp->image->color_space==CLRSPC_UNSPECIFIED) { jp->col = (int)jp->image->numcomps; } #endif */ // 設定されないものについては,未対応 jp->cmode = GRAPH_COLOR_UNKNOWN; int depth = (int)jp->image->comps->bpp; if (depth==0) { if (jp->col==3) jp->cmode = GRAPH_COLOR_RGB; else if (jp->col==4) jp->cmode = GRAPH_COLOR_RGBA; } else if (depth==32) { if (jp->col==3) jp->cmode = GRAPH_COLOR_RGB; else if (jp->col==4) jp->cmode = GRAPH_COLOR_RGBA; } else if (depth==24) { if (jp->col==3) jp->cmode = GRAPH_COLOR_RGB; } else if (depth==16) { if (jp->col==1) jp->cmode = GRAPH_COLOR_MONO16; else if (jp->col==3) jp->cmode = GRAPH_COLOR_RGB16; else if (jp->col==4) jp->cmode = GRAPH_COLOR_RGBA16; } else if (depth==8) { if (jp->col==1) jp->cmode = GRAPH_COLOR_MONO; } #if OPENJPEG_VER > JP2K_VER_12 freeNull(jp->image->icc_profile_buf); jp->image->icc_profile_len = 0; #endif }
void playNotes(void *args) { struct timespec *end = (struct timespec *)args; if (pthread_mutex_trylock(&global_score_mutex) != 0) return; if (global_score_notes == NULL) { pthread_mutex_unlock(&global_score_mutex); return; } // Start if (pthread_mutex_lock(&global_play_mutex) != 0) return; global_play_state = 1; pthread_mutex_unlock(&global_play_mutex); // Play int i, fd = open(DEVICE_CONSOLE, O_WRONLY); int length = (int)global_score_header.length; struct serbeep_score_note *notes = global_score_notes; for (i=0; i<length; i++) { uint16_t number = ntohs(notes[i].number); uint16_t beep_time = ntohs(notes[i].length); uint16_t sleep_time = ntohs(notes[i].duration); // Beep if (beep_time != 0) { double freq = 440 * pow(2, (double)(number - 69) / (double)12); int val = (int)(CLOCK_TICK_RATE / freq); ioctl(fd, KIOCSOUND, val); if (goodSleep(end, beep_time) == 1) break; ioctl(fd, KIOCSOUND, 0); } // Sleep if (sleep_time != 0) { if (goodSleep(end, sleep_time) == 1) break; } } // End ioctl(fd, KIOCSOUND, 0); pthread_mutex_lock(&global_play_mutex); global_play_state = 0; pthread_mutex_unlock(&global_play_mutex); free(end); freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); close(fd); }
/** int write_jpeg_mem(unsigned char** buf, unsigned long* len, JPEGImage jp, int qulty) jp の画像データを *bufに書き出す.*bufは要 free @param[out] buf 画像データが格納される.要 free @param[out] len buf の長さ(Byte)が格納される. @param jp 保存する JPEGデータ @param qulty 保存のクオリティ 0〜100 100が最高画質 @retval ERROR_GRAPH_OPFILE ファイルオープンエラー @retval ERROR_GRAPH_HEADER 不正ファイル(JPEGファイルでない?) @retval ERROR_GRAPH_MEMORY メモリエラー @retval ERROR_GRAPH_NODATA jp にデータが無い @retval ERROR_GRAPH_IVDARH buf が NULL, or サポート外のチャンネル数(現在の所チャンネル数は 1か 3のみをサポート) */ int write_jpeg_mem(unsigned char** buf, unsigned long* len, JPEGImage jp, int qulty) { struct jpeg_compress_struct jdat; struct jpeg_error_mgr jerr; if (buf==NULL || len==NULL) return ERROR_GRAPH_IVDARG; if (jp.col!=1 && jp.col!=3) return ERROR_GRAPH_IVDARG; if (jp.gp==NULL || jp.img==NULL) return ERROR_GRAPH_NODATA; *len = jp.xs*jp.ys*jp.col; if (*len<=0) return ERROR_GRAPH_IVDARG; if (qulty>100) qulty = 100; else if (qulty<0) qulty = 0; *buf = (unsigned char*)malloc(*len); if (*buf==NULL) { return ERROR_GRAPH_MEMORY; } jdat.err = jpeg_std_error(&jerr); jpeg_create_compress(&jdat); jpeg_mem_dest(&jdat, buf, len); jdat.image_width = jp.xs; jdat.image_height = jp.ys; jdat.input_components = jp.col; if (jp.col==1) jdat.in_color_space = JCS_GRAYSCALE; else jdat.in_color_space = JCS_RGB; jpeg_set_quality (&jdat, qulty, TRUE); jpeg_set_defaults(&jdat); jpeg_start_compress (&jdat, TRUE); jpeg_write_scanlines(&jdat, jp.img, jp.ys); jpeg_finish_compress(&jdat); jpeg_destroy_compress(&jdat); if (*len<=0) { freeNull(*buf); return ERROR_GRAPH; } return 0; }
/** void free_pw(struct passwd* pw) パスワード構造体 struct pw* を free する.getnisnam()の返す構造体にのみ適用すること.@n 一般のUNIXライブラリの返すパスワード構造体に対して適用してはいけない(セグメンテーションフォルトを起こす). @param pw 開放する struct pw型変数へのポインタ. @see struct pw */ void free_pw(struct passwd* pw) { if (pw==NULL) return; freeNull(pw->pw_name); freeNull(pw->pw_passwd); freeNull(pw->pw_gecos); freeNull(pw->pw_dir); freeNull(pw->pw_shell); freeNull(pw); return; }
void free_TIFF_ifd(TIFF_ifd* ifd) { if (ifd==NULL) return; freeNull(ifd->ex_value); free(ifd); }